Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 2 of 2

Thread: Ask for a favor about glColor3ub.

  1. #1
    Newbie Newbie
    Join Date
    Sep 2012
    Posts
    3

    Ask for a favor about glColor3ub.

    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!

  2. #2
    Advanced Member Frequent Contributor
    Join Date
    Jan 2007
    Posts
    964
    Please use code tags, like so:
    Code :
    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:
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •