PDA

View Full Version : Render position



tai
01-28-2007, 01:12 PM
I want to render my scene at the lower left corner of windows, the following codes work fine if the Camera is located at(0,0,0), but my scene
doe not stay at the viewport(0,0,w/4,h/4)if the Camera position change, what should I do in order to have my scene stay at the lower left corner when Camera move.

MyRenderFunction(Camera* cam)
{
int w=cam->m_Width;
int h=cam->m_Height;
glPushMatrix();
glViewport(0,0,w/4,h/4);

::glMatrixMode(GL_PROJECTION);
::glPushMatrix();
::glLoadIdentity();

double Aspect = (double)w/(double)h;
::glOrtho(-2*Aspect,2*Aspect,-2,2,-200,200);

...
MySceneDisplayFunction();
...

glMatrixMode(GL_MODELVIEW);
glPopMatrix();

glViewport(0,0,w,h);
glPopMatrix();
}

jacky_romano
01-29-2007, 12:41 PM
Tai,
I'm not sure I fully understand your code. Note that your glPop/glPush are not balanced.

To your question, sounds like what you are missing is a call to:
glTranslatef(-xcam, -ycam, -zcam) just before
MySceneDisplayFunction();

jtipton
01-29-2007, 05:59 PM
glViewport does not require a call to glPushMatrix. I would write it like this:


MyRenderFunction(Camera* cam)
{
double w = cam->m_Width;
double h = cam->m_Height;

glPushAttrib(GL_VIEWPORT_BIT | GL_TRANSFORM_BIT);
{
glViewport(0,0,w/4,h/4);

glMatrixMode(GL_PROJECTION);
glPushMatrix();
{
glLoadIdentity();

double aspect = w / h;
glOrtho(-2 * aspect, 2 * aspect, -2, 2, -200, 200);

glMatrixMode(GL_MODELVIEW);
glPushMatrix();
{
glLoadIdentity();

...
MySceneDisplayFunction();
...
}
glMatrixMode(GL_MODELVIEW);
glPopMatrix();
}
glMatrixMode(GL_PROJECTION);
glPopMatrix();
}
glPopAttrib();
} Note the braces near the push/pop calls. I know it is not typical, but I have added them to my GL coding standard and swear by it. It really helps keep things in order, and easily find matching push/pop pairs. Try fixing the push/pop pairings and I bet your code starts behaving as expected.

tai
01-29-2007, 07:48 PM
Thanks for your kind reply, two questions to ask:
1) when I add a simple axis drawing in your modified code, it always show the 2D drawing, not 3D drawing in 3D view.
2) If I comment out the second glLoadIdentity()(the line wit Question to ask) , it shows the 3D drawing in 3D view, but if I modify the camera poition, the drawing position change or disappear.

Thanks
P.S. Sorry, somehow I can not get your orginal format.

MyRenderFunction(Camera* cam){
double w = cam->m_Width;
double h = cam->m_Height;
glPushAttrib(GL_VIEWPORT_BIT | GL_TRANSFORM_BIT);
{
glViewport(0,0,w/4,h/4);
glMatrixMode(GL_PROJECTION);
glPushMatrix();
{
glLoadIdentity();
double aspect = w / h;
glOrtho(-2*aspect,2*aspect,-2,2,-200, 200);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
{
glLoadIdentity(); //Question to ask
double axislen = 1.0;
glBegin(GL_LINES);
glColor3f( 0.0, 0.0, 1.0 );
glVertex3d(0.0, 0.0, 0.0);
glVertex3d(axislen, 0.0, 0.0);

glColor3f( 0.0, 1.0, 0.0 );
glVertex3d(0.0, 0.0, 0.0);
glVertex3d(0.0, axislen, 0.0);

glColor3f( 1.0, 0.0, 0.0 );
glVertex3d(0.0, 0.0, 0.0);
glVertex3d(0.0, 0.0, axislen);
glEnd();
}
glMatrixMode(GL_MODELVIEW);
glPopMatrix();
}
glMatrixMode(GL_PROJECTION);
glPopMatrix();
}
glPopAttrib();
}

jtipton
01-29-2007, 09:04 PM
The call to glLoadIdentity means that your drawing will not be transformed. It sets the modelview transform to the idenity matrix, essentially cancelling out any previous model transformation you may have had. This leaves only the projection transform to affect your vertices. Only the X and Y axes will be drawn. This is to be expected. The camera position is not related to the viewport. The only relation between the camera and the viewport is the viewing frustum you create. This frustum (ortho/perspective) is mapped to the viewport bounds that you specify. All of the code above works as expected. I would verify the width and height being returned from your camera object.

tai
01-29-2007, 09:35 PM
jtipton, thanks for your reply. So if I want to draw XYZ axes, I need to comment out the second
glLoadIdentity() line with questions to ask?

Also, to have XYZ axes drawn in the specified viewport, only the parameters
glOrtho(-2*aspect,2*aspect,-2,2,-200, 200) are related.

Thanks