Archived

This topic is now archived and is closed to further replies.

Big problem with glViewport

This topic is 4959 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

Ok, the problem is, I decided to make the scene window smaller, so I can add a HUD right and down. hud_x=64,hud_y=49 (proportions preserved) This code works flawlesly, except that the HUD is up, not down. So, I changed it to glViewport(0, hud_y, window_width-hud_x, window_height); Well, surprise surprise, now the scene is CUT on Y, instead of beign rescaled! I tried all kind of things, but I can't just make it just rescale properly, instead of cuting off the upper part... Please, this is extremly important for me, I spent 4 hours doing various things, trying to fix it, and my head is almost blowing. Any ideas?
//this code works
void resize_window()
{
	float window_ratio;

	if (window_height==0)window_height=1;			// Prevent A Divide By Zero

	glViewport(0, 0, window_width-hud_x, window_height-hud_y);	// Reset The Current Viewport

	glMatrixMode(GL_PROJECTION);					// Select The Projection Matrix
	glLoadIdentity();
        window_ratio=(GLfloat)window_width/(GLfloat)window_height;							// Reset The Projection Matrix
	glOrtho( -1.0*zoom_level*window_ratio, 1.0*zoom_level*window_ratio, -1.0*zoom_level, 1.0+*zoom_level, -40.0, 40.0 );

	glMatrixMode(GL_MODELVIEW);					// Select The Modelview Matrix
	glLoadIdentity();							// Reset The Modelview Matrix
	last_texture=-1;	//no active texture
}
  
[edit] forgot a line, in the copy and paste. [edited by - Raduprv on May 18, 2004 12:52:50 AM]

Share this post


Link to post
Share on other sites
OpenGL uses the bottom left corner as (0, 0) by default.
If you want to change this behavior to make it like windows, reverse the top and bottom params for glOrtho.

Also, I don''t know everything about your program, but the calls to both glViewport and glOrtho look kinda wrong to me.
shouldn''t it be something like:

glViewport(hud_x, screen_height - hud_y, hud_width, hud_height);
glOrtho(0, hud_width, hud_height, 0, -1, 1); // reversed

Hope that helps.

Share this post


Link to post
Share on other sites
Hi!

Just a question...why do you resize your viewport?? To add a HUD?
man...keep the viewport as it was and render all your HUD objects as overlays!

oh and btw...you should get rid of all those glFunctionCalls(..) all you need is glLoadMatrix/glMultMatrix. ;-)
Makes life much easier!!

have a good one!

Share this post


Link to post
Share on other sites
Melekor, the routine I posted is for the SCENE, not for the GUI. So the scene is supposed to start in the upper left corner, and the GUI will be bottom and right. So I am doing the things correctly, but my scene is cut at the upper part. The GUI displays flawlesly, tho (I am changing the viewport as the full window, when drawing the GUI).

Acid-Chris, try to use this code, and the code I posted above.

void Enter2DMode()
{
glPushAttrib(GL_LIGHTING_BIT|GL_DEPTH_BUFFER_BIT);
glDisable(GL_LIGHTING);
glDisable(GL_DEPTH_TEST);

glViewport(0, 0, window_width, window_height);

glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
glOrtho(0.0, (GLdouble)window_width, (GLdouble)window_height, 0.0, -250.0, 250.0);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
}

void Leave2DMode()
{
glMatrixMode(GL_MODELVIEW);
glPopMatrix();
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
glPopAttrib();
glViewport(0, hud_y, window_width-hud_x, window_height);
//glViewport(0, 0, window_width-hud_x, window_height-hud_y); // Reset The Current Viewport
}

Share this post


Link to post
Share on other sites
Fixed it!!!!!
Changed it to: glViewport(0, hud_y, window_width-hud_x, window_height-hud_y);, now it works perfectly... But I have now idea why.

Share this post


Link to post
Share on other sites
Without subrtacting hud_y from window_height, the viewport will end hud_y pixels outside the window since it started hud_y pixels above the bottom. The last parameter is the height of the viewport, not the end cooridnate.

Share this post


Link to post
Share on other sites