Skip to main content
gamedev.net
🔒 Locked

Flow ofExecution

Started by shankhs Jan 28, 2008 at 12:15 PM 4 replies 2.4k views
Original Post
shankhs
shankhs
My Instructor has given this program :

#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;
}

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]
FlyingIsFun1217
FlyingIsFun1217
Please use source tags:

Quote:

[s ource]Code Here[/s ource]


Obviously without the space in the word 'source'.

Doing this will get your topic a lot more attention.
FlyingIsFun1217
shankhs
shankhs
Sorry I had no idea:
#include<windows.h>#include<gl/gl.h>#include<gl/glu.h>#include<gl/glut.h>#include<cmath>#define GL_PI 3.1415GLfloat 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);elseglDisable(GL_CULL_FACE);if(bDepth)glEnable(GL_DEPTH_TEST);elseglDisable(GL_DEPTH_TEST);if(bOutline)glPolygonMode(GL_BACK,GL_LINE);elseglPolygonMode(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);elseglColor3f(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);elseglColor3f(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 WindowglutPostRedisplay();}void ChangeSize(int w, int h){GLfloat nRange = 100.0f;// Prevent a divide by zeroif(h == 0)h = 1;// Set Viewport to window dimensionsglViewport(0, 0, w, h);// Reset projection matrix stackglMatrixMode(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);elseglOrtho (-nRange*w/h, nRange*w/h, -nRange, nRange, -nRange, nRange);// Reset Model view matrix stackglMatrixMode(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;}
stonemetal
stonemetal
This is a typical if not very involved gui program. what happens is a window is created by the create window function. functions are registered as callbacks by glut*func calls. glutmainloop is what is known as a message pump. it watches for keyboard and mouse events then calls the function that was registered to handle that type of event. The call to glut post redisplay causes the screen to redraw.
shankhs
shankhs
Wat is the use of defining setupRC as a distinct function?Cant I put it in some glut*func calls?
ne0_kamen
ne0_kamen
It's just for initialization and no you cannot put it in these callbacks because they are called each frame and not only at the start of the program.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.