PDA

View Full Version : Viewport scaling problem



Özgür Özön
02-16-2012, 06:34 AM
If window’s size change, GLRecti rectlange’s size will scalings of viewport (Rectlange created with glRecti).


How to make constant size GLRecti reclangle in resizable window handle? (for Developing GUI).



And images :

Before resizing window:
http://imageshack.us/photo/my-images/812/001paf.png/

After resizing window:
http://imageshack.us/photo/my-images/14/002pie.png/

but I want this:
http://imageshack.us/photo/my-images/822/003pu.png/

And Opengl Draw Code:

//RECT r is window size variable.
// lbottom and lright is firs window size.

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
glOrtho(0, r.right, r.bottom, 0, -1, 1);
glMatrixMode(GL_MODELVIEW);

// glFrustum(-zoom * 1, zoom * 1, -zoom * 1, zoom * 1,1,-1);
gluPerspective(1, 1, 1, 1);

glEnable(GL_COLOR_LOGIC_OP);
glLogicOp(GL_XOR);
glColor4f(1.0f, 1.0f, 1.0f, 1.0f);


glRecti( 10,10,lright-200,lbottom-200);
glBegin (GL_LINES);
glVertex2i (30,30);
glVertex2i (70,30);
glEnd();
glDisable(GL_COLOR_LOGIC_OP);
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
SwapBuffers(DeviceContext);

Thanks.

carsten neumann
02-16-2012, 09:26 AM
It should work, but you are screwing up your modelview matrix with this:



glMatrixMode(GL_MODELVIEW);
// glFrustum(-zoom * 1, zoom * 1, -zoom * 1, zoom * 1,1,-1);
gluPerspective(1, 1, 1, 1);


The matrices generated by these calls (almost) always belong on the projection matrix stack.

Özgür Özön
02-16-2012, 12:16 PM
But how to fix this problem?
Deleting this lines?

Özgür Özön
02-17-2012, 03:37 AM
and additional codes from project:

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
glOrtho(0, r.right, r.bottom, 0, -1, 1);
glMatrixMode(GL_MODELVIEW);

// glFrustum(-zoom * 1, zoom * 1, -zoom * 1, zoom * 1,1,-1);
gluPerspective(1, 1, 1, 1);

//gluOrtho2D(0.0,3.0,0.0,3.0);
//glMatrixMode(GL_MODELVIEW);
// glMatrixMode(GL_MODELVIEW);
// glLoadIdentity();
...
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
SwapBuffers(DeviceContext);




how to fix this problem?

BionicBytes
02-17-2012, 07:42 AM
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
glOrtho(0, r.right, r.bottom, 0, -1, 1);
glMatrixMode(GL_MODELVIEW);

// glFrustum(-zoom * 1, zoom * 1, -zoom * 1, zoom * 1,1,-1);
gluPerspective(1, 1, 1, 1);

how to fix this problem?
You have been told how to already....


glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glPushMatrix(); <----- is there some reason why you need to preserve this matrix?
glLoadIdentity();
gluPerspective ( your parameters here );
glMatrixMode(GL_MODELVIEW);


A lot of beginners just push and pop the projection matrix at the start/end of the draw loop for no apparent reason. I did for years! Then I actually thought about what I was doing and explicitly set the matrix when I need it, rather than relying upon some saved state. Saves so many problems in the future...!

You need to add your prameters for gluPerspective based on your camera properties, eg FOV (usually between 45.0 -> 60.0 degrees), znear (0.1) and zfar (10,000 or what ever you need)