Basic Program Structure

Hey guys.

I’m just starting to learn openGL using GLUT and I’m using “the RedBook” to help. With all the example progrms I’ve come accross so far they all have a similar layout but I dont quite understand how they link together and run. For example, each program has the functions:
void Reshape()
void Init()
void Display()

and then in the ‘main’ function there are the calls:
glutReshapeFunc(reshape)
glutDisplayFunc(display)
glutMainLoop()

So I think it goes something along the lines of: init called first and only once throughout to setup general parameters, then display is called, and reshape is only called when there has been a change (i.e. mouse click). And Mainloop() just runs constantly in a loop waiting for an event to happen. Is that sort of correct?

The reason I ask is because I’m trying to convert some sample code from this this tutorial into GLUT code but I’m strugling. Do you have to use this basic program structure or are there other ways to go about it?

Thanks

If you’re trying to use GLUT, you’re pretty much stuck with the glutMainLoop(). Some people have hacked in a glutExit() function, but even that is extra work.

You can think of the GLUT main loop as a continual series of checks to see which handler functions need to be called. The Reshape handler is called when the drawing window is resized; the Mouse handler function is called when the mouse is moved or clicked; the Keyboard handler is called when a key is pressed.

If nothing is happening, the Idle handler is called. Typically, people put stuff in here which needs to be continually updated----including, usually, glutPostRedisplay() which causes the Display handler to be called.

Of course, GLUT isn’t the only way to do OpenGL. It’s just a good starting point since it’s fairly easy and cross-platform.

OK thanks for clearing that up Lindley, yes I am using GLUT but I’m quite happy to stick with that form as long as I know what it does :slight_smile: .

I have a new completey unrelated question though. I’m experimenting drawing basic polygons using vertex arrays but I’m having some difficulty. It works perfectly with the glDrawArrays function but I cant get it to work with any of the ‘single’ functions, i.e. glDrawElements. Here’s the code I’m using

 static GLint vertices[] = {0, 0, 0,
				2, 0, 0,
				0, 2, 0,
				2, 2, 5,
				4, 2, 5,
				2, 4, 5};


static GLfloat colors[] = {1.0, 0.0, 0.0,
			1.0, 0.0, 0.0,
			1.0, 0.0, 0.0, 
			0.0, 1.0, 1.0,
			0.0, 1.0, 1.0,
			0.0, 1.0, 1.0};


void init(void) {

	glClearColor(0.0, 0.0, 0.0, 0.0);
	glEnable(GL_DEPTH_TEST);
	glShadeModel(GL_FLAT); 
}



void display (void){
	
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glPushMatrix();
	glColor3f(1.0,0.0,0.0);
	
	glEnableClientState(GL_COLOR_ARRAY);
	glEnableClientState(GL_VERTEX_ARRAY);

	glColorPointer(3, GL_FLOAT, 0, colors);
	glVertexPointer(3, GL_INT, 0, vertices);


        //***This works***
	//glDrawArrays(GL_TRIANGLES, 0, 6);  
	
        // <-***This Doesnt***
        glDrawElements(GL_TRIANGLES, 6, GL_INT,  vertices);



        //***Neither Does this***
        //glBegin(GL_TRIANGLES);
	//glArrayElement(0);
	//glArrayElement(1);
	//glArrayElement(2);
	//glEnd;


	glPopMatrix();
	glutSwapBuffers();
}


void reshape (int w, int h) {

	if(h == 0) // Prevent a divide by zero, when window is too short (you cant make a window of zero width).
		h = 1;

	ratio = 1.0f * w / h; // Reset the coordinate system before modifying
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();	
	glViewport(0, 0, w, h); // Set the viewport to be the entire window

	// Set the clipping volume
	gluPerspective(45,ratio,1,1000);
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
	gluLookAt(	x, y, z, 
		  		x + lx,y + ly,z + lz,
				0.0f,1.0f,0.0f);
}

int main (int argc, char** argv) {

	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
	glutInitWindowSize(350, 350);
	glutInitWindowPosition(100, 100);
	glutCreateWindow("Testing");
	init();
	glutSpecialFunc(inputKey);
	glutIdleFunc(display);
	glutDisplayFunc(display);
	glutReshapeFunc(reshape);
	glutMainLoop();
	return 0;
} 

There are some variables not explained to do with my camera system but I havent included the code for that. I have tried loads of different ways of using the glDrawElements function (different primitive shape types, different sized vertex arrays with different data, plotting single points) but nothng seems to work. What have I done wrong? :confused:

Thanks again.

glDrawElements() requires an index array in addition to a vertex array.

Your code may look like:

// index array
GLubyte indices = { 0,1,2, 3,4,5 };
...

// drawing
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_BYTE, indices);
...

Here is my note about Vertex Array .