Jump to content
  • Advertisement
Sign in to follow this  
shankhs

OpenGL Flow ofExecution

This topic is 3917 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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]

Share this post


Link to post
Share on other sites
Advertisement
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

Share this post


Link to post
Share on other sites
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.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;
}

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!