Multiple Viewports: Need Help

Started by
5 comments, last by KumGame07 16 years, 8 months ago
I am trying to get three viewports in a game I am making in OpenGL. This is a first-person-shooter game where I need two black bars at the top and the bottom to display some status messages. Here is what I'm doing: void glResize(int width, int height) { if(height==0) height=1; glViewport(0,0,width,height); } void glControl() // glControl is my *glutDisplayFunc* { if(NeedUpdate) { StatusUpdate(); NeedUpdate=FALSE; // This is so that I update only once } switch(SCENE) { case 1: Level1(); break; } } void Level1() { glViewport(0,150,WINWIDTH,WINHEIGHT-300); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(45.0f,2.0f,1.0f,400.0f); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glRotatef(90.0f-theta,0.0f,1.0f,0.0f); glTranslatef(-x,-y,-z); // x,y,z are the co-ordinates of the person assuming he started at 0,0,0 // theta is the angle at which he is facing w.r.t y-axis . . . } void StatusUpdate() { glViewport(0,0,WINWIDTH,150); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(0,WINWIDTH,0,150); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glColor3ub(255,255,255); glBegin(GL_TRIANGLES); glVertex2i(0,0); glVertex2i(100,0); glVertex2i(100,50); glEnd(); . . . } But this code above draws only the middle viewport i.e. Level1 and I am not able to see the triangle at all. I am pretty confused now, and I somehow want to see all three viewports showing *something* for a start. Please help!
Advertisement
First, use source tags to post long snippets of code (you can read about it in the FAQ).

Second, I think the problem is in the glResize() function. In it, you are only setting one large viewport, and you're not adjusting the projection matrix, which you should. Even if your window isn't resized, I think this function is called when the program starts (to confirm this, try resizing the window and see what happens) so it needs to work properly.

You seem to have left out some relevant code eg, probably WINWIDTH and WINHEIGHT should be getting set in glResize and NeedUpdate should be getting set to true somewhere (or gotten rid of, since the app doesn't know when it needs to redraw, that is why there are paint msgs/glutDisplayFunc/etc).

I have tried setting a gluOrtho2D matrix for GL_PROJECTION in my glResize function.

The WINWIDTH and WINHEIGHT are defined globally and are constants. Initially, I had set NeedUpdate to TRUE globally as well. I assumed that this will cause the StatusUpdate() function to be executed once.

Now, I have set NeedUpdate back to true in my Level1() function, so that Level1() and StatusUpdate() get called alternatingly inside glControl().

But this too, does not work. I am not able to see anything in the bottom viewport, no matter what I draw.
Hi, is it possible that you are leaving GL in a un-expected state at the end of you Leve1 drawing. ie: if you remark out your drawing code in the Level1 procedure and still call the StatusUpdate procedure - do you see anything?
Hello vgrama,

As john_day_gamedev has already said, try either commenting the call to Level1() or drawing some simple object (a triangle/quad) in Level1().
Best Regards,KumGame07
Hello vgrama,

As john_day_gamedev has already said, try either commenting the call to Level1() or drawing some simple object (a triangle/quad) in Level1().
Best Regards,KumGame07

This topic is closed to new replies.

Advertisement