Thursday, June 2, 2011

GL_TRIANGLE_FAN example c c++ java objc

GL_TRIANGLE_FAN is a parameter name passed to glBegin() function. When this mode is on, first two vertices are shared with following triangles. See following figure for example. Here, verttices are down v0, v1, v2, then v0, v2, v3, then v0, v3, v4. [GL_TRIANGLE_FAN example]



Example of GL_TRIANGLE_FAN

void RenderScene(void) 
{
        GLfloat x,y,angle;

        int iPivot = 1;
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

        if(bCull) 
                glEnable(GL_CULL_FACE);
        else
                glDisable(GL_CULL_FACE);


        if(bDepth)
                glEnable(GL_DEPTH_TEST);
        else
                glDisable(GL_DEPTH_TEST);


        if(bOutline) 
                glPolygonMode(GL_BACK,GL_LINE);
        else
                glPolygonMode(GL_BACK,GL_FILL);


        glPushMatrix(); 
        glRotatef(xRot,1.0f,0.0f,0.0f);
        glRotatef(yRot,0.0f,1.0f,0.0f);

        glBegin(GL_TRIANGLE_FAN);
        glVertex3f(0.0f,0.0f,75.0f);
        for(angle=0.0f; angle < (2.0f*GL_PI); angle +=(GL_PI/8.0f))
        {
                x = 50.0f*sin(angle);

                y = 50.0f*cos(angle);

                if((iPivot %2) == 0)
                        glColor3f(0.0f,1.0f,0.0f);
                else
                        glColor3f(1.0f,0.0f,0.0f);


                iPivot++;

                glVertex2f(x,y);

        }

        glEnd();
        glBegin(GL_TRIANGLE_FAN);
        glVertex2f(0.0f,0.0f);
        for(angle = 0.0f; angle < (2.0f*GL_PI); angle += (GL_PI/8.0f))
        {
                x=50.0f*sin(angle);
                y=50.0f*cos(angle);

                if((iPivot %2) == 0)
                        glColor3f(0.0f,1.0f,0.0f);
                else
                        glColor3f(1.0f,0.0f,0.0f);
                iPivot++;
                glVertex2f(x,y);
        }

        glEnd();
        glPopMatrix();
        glFlush(); 
}


Example of GL_TRIANGLE_FAN end!