Original Post
My Instructor has given this program : I am unable to understand the flow of execution in this program.I am a newbie to openGL PLZ help. [Edited by - shankhs on January 28, 2008 12:50:09 PM]
#include<windows.h>
#include<gl/gl.h>
#include<gl/glu.h>
#include<gl/glut.h>
#include<cmath>
#define GL_PI 3.1415
GLfloat xRot =0.0f;
GLfloat yRot =0.0f;
BOOL bCull=false;
BOOL bDepth=false;
BOOL bOutline=false;
void ProcessMenu(int value)
{
switch (value)
{
case 1:
bDepth=!bDepth;
break;
case 2:
bCull=!bCull;
break;
case 3:
bOutline=!bOutline;
break;
default:
break;
}
glutPostRedisplay();
}
void initScene()
{
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(1.0f,0.0f,0.0f);
else
glColor3f(0.0f,1.0f,0.0f);
glVertex2f(x,y);
iPivot++;
}
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.0*sin(angle);
y=50.0f*cos(angle);
if(iPivot%2==0)
glColor3f(1.0f,0.0f,0.0f);
else
glColor3f(0.0f,1.0f,0.0f);
iPivot++;
glVertex2f(x,y);
}
glEnd();
glPopMatrix();
glutSwapBuffers();
}
void setupRC()
{
glClearColor(0.0f,0.0f,0.0f,1.0f);
glShadeModel(GL_FLAT);
glFrontFace(GL_CW);
}
void SpecialKeys(int key, int x, int y)
{
if(key == GLUT_KEY_UP)
xRot=xRot- 5.0f;
if(key == GLUT_KEY_DOWN)
xRot= xRot+ 5.0f;
if(key == GLUT_KEY_LEFT)
yRot =yRot- 5.0f;
if(key == GLUT_KEY_RIGHT)
yRot =yRot+ 5.0f;
if(key > 356.0f)
xRot = 0.0f;
if(key < -1.0f)
xRot = 355.0f;
if(key > 356.0f)
yRot = 0.0f;
if(key < -1.0f)
yRot = 355.0f;
// Refresh the Window
glutPostRedisplay();
}
void ChangeSize(int w, int h)
{
GLfloat nRange = 100.0f;
// Prevent a divide by zero
if(h == 0)
h = 1;
// Set Viewport to window dimensions
glViewport(0, 0, w, h);
// Reset projection matrix stack
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
// Establish clipping volume (left, right, bottom, top, near, far)
if (w <= h)
glOrtho (-nRange, nRange, -nRange*h/w, nRange*h/w, -nRange, nRange);
else
glOrtho (-nRange*w/h, nRange*w/h, -nRange, nRange, -nRange, nRange);
// Reset Model view matrix stack
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void handleKeyPress(unsigned char key,int x,int y)
{
switch(key)
{
case 27:
exit(0);
case 32:
exit(0);
}
}
int main(int argc,char **argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB|GLUT_DEPTH);
glutCreateWindow("I love GLUT");
glutCreateMenu(ProcessMenu);
glutAddMenuEntry("Toggle depth test",1);
glutAddMenuEntry("Toggle cull backface",2);
glutAddMenuEntry("Toggle outline back",3);
glutAttachMenu(GLUT_RIGHT_BUTTON);
glutSpecialFunc(SpecialKeys);
glutReshapeFunc(ChangeSize);
glutDisplayFunc(initScene);
setupRC();
glutKeyboardFunc(handleKeyPress);
glutMainLoop();
return 0;
}