# OpenGL Multiple Viewports: Need Help

## Recommended Posts

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!

##### Share on other sites
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.

##### Share on other sites

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).

##### Share on other sites
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.

##### Share on other sites
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?

##### Share on other sites
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().

##### Share on other sites
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().

## Create an account

Register a new account

• ## Partner Spotlight

• ### Forum Statistics

• Total Topics
627651
• Total Posts
2978407
• ### Similar Content

• Both functions are available since 3.0, and I'm currently using glMapBuffer(), which works fine.
But, I was wondering if anyone has experienced advantage in using glMapBufferRange(), which allows to specify the range of the mapped buffer. Could this be only a safety measure or does it improve performance?
Note: I'm not asking about glBufferSubData()/glBufferData. Those two are irrelevant in this case.
• By xhcao
Before using void glBindImageTexture(    GLuint unit, GLuint texture, GLint level, GLboolean layered, GLint layer, GLenum access, GLenum format), does need to make sure that texture is completeness.
• By cebugdev
hi guys,
are there any books, link online or any other resources that discusses on how to build special effects such as magic, lightning, etc. in OpenGL? i mean, yeah most of them are using particles but im looking for resources specifically on how to manipulate the particles to look like an effect that can be use for games,. i did fire particle before, and I want to learn how to do the other 'magic' as well.
Like are there one book or link(cant find in google) that atleast featured how to make different particle effects in OpenGL (or DirectX)? If there is no one stop shop for it, maybe ill just look for some tips on how to make a particle engine that is flexible enough to enable me to design different effects/magic
let me know if you guys have recommendations.
• By dud3
How do we rotate the camera around x axis 360 degrees, without having the strange effect as in my video below?
Mine behaves exactly the same way spherical coordinates would, I'm using euler angles.
Tried googling, but couldn't find a proper answer, guessing I don't know what exactly to google for, googled 'rotate 360 around x axis', got no proper answers.

References:
Code: https://pastebin.com/Hcshj3FQ
The video shows the difference between blender and my rotation:

• By Defend
I've had a Google around for this but haven't yet found some solid advice. There is a lot of "it depends", but I'm not sure on what.
My question is what's a good rule of thumb to follow when it comes to creating/using VBOs & VAOs? As in, when should I use multiple or when should I not? My understanding so far is that if I need a new VBO, then I need a new VAO. So when it comes to rendering multiple objects I can either:
* make lots of VAO/VBO pairs and flip through them to render different objects, or
* make one big VBO and jump around its memory to render different objects.
I also understand that if I need to render objects with different vertex attributes, then a new VAO is necessary in this case.
If that "it depends" really is quite variable, what's best for a beginner with OpenGL, assuming that better approaches can be learnt later with better understanding?

• 10
• 12
• 22
• 13
• 33