PDA

View Full Version : Ask for a favor about glColor3ub.



coldwater
09-11-2012, 11:04 PM
I will appreciate it a lot if anyone could help me to understand the function build_object() below about glColor3ub, I have no idea how does glColor3ub work, especially about the bit operation, like a = i >> 16.

void build_object(Polygon3D *Object)
{
unsigned int i,j;
Face *face;
int *verts;
unsigned char a, b, c;
icVector3 up, ray;

theObject = glGenLists(1);
glNewList(theObject, GL_COMPILE);
for (i=0; i<Object->nfaces; i++) {
a = i >> 16;
b = (i & 0xFF00) >> 8;
c = i & 0xFF;
glColor3ub(255-a, 255-b, 255-c);
face = Object->flist[i];
verts = face->verts;
glBegin(GL_POLYGON);
for (j=0; j<face->nverts; j++) {
glVertex3d(Object->vlist[verts[j]]->x, Object->vlist[verts[j]]->y, Object->vlist[verts[j]]->z);
}
glEnd();
}
glEndList();
}

Thank you for your help!

mhagain
09-12-2012, 02:24 AM
Please use code tags, like so:
void build_object(Polygon3D *Object)
{
unsigned int i,j;
Face *face;
int *verts;
unsigned char a, b, c;
icVector3 up, ray;

theObject = glGenLists(1);
glNewList(theObject, GL_COMPILE);

for (i=0; i<Object->nfaces; i++) {
a = i >> 16;
b = (i & 0xFF00) >> 8;
c = i & 0xFF;
glColor3ub(255-a, 255-b, 255-c);
face = Object->flist[i];
verts = face->verts;

glBegin(GL_POLYGON);

for (j=0; j<face->nverts; j++) {
glVertex3d(Object->vlist[verts[j]]->x, Object->vlist[verts[j]]->y, Object->vlist[verts[j]]->z);
}

glEnd();
}

glEndList();
}


There, much easier to read!

What this snippet is obviously doing is drawing flat-shaded polygons from a list of faces. The colour used for the flat-shading is derived from the position of each face in the list using this part of the code:
a = i >> 16;
b = (i & 0xFF00) >> 8;
c = i & 0xFF;
This is basic C/C++ and nothing to do with OpenGL, so if you have trouble with this then I'd suggest a C/C++ tutorial as the best resource for help.