Saturday, 24 August 2013

Can you help me solve the paradox that has confused me about the glViewport and glOrtho functions?

Can you help me solve the paradox that has confused me about the
glViewport and glOrtho functions?

suppose that I have an image that I want to apply as a texture on a
geometry (at time we don't consider texture mapping but this image has a
certain width and height here it is a 512x512 image)
I want the image be drawn at the center of the window so I have set the
window coordinates as follows:
glOrtho(-oglWindowWidth/2, oglWindowWidth/2,
oglWindowHeight/2,-oglWindowHeight/2, -1, 1);
and then drew the geometry:
float x0 = -ImageWidth/2; // top left corner of image
float y0 = -ImageHeight/2;
float x1 = x0 + ImageWidth; // bottom right corner of image
float y1 = y0 + ImageHeight;
glBegin(GL_TRIANGLE_STRIP);
{
glVertex2f(x0, y1);
glVertex2f(x0, y0);
glVertex2f(x1, y1);
glVertex2f(x1, y0);
}
glEnd();
It's true and what I reach as the result is:

Meaning that the coordinates of the center of both the image and window is
(0,0) and the image will be drawn at the center of the window
But since I need the viewport, I used this code after drawing the geometry:
GLint iViewport[4];
glGetIntegerv(GL_VIEWPORT, iViewport);
and reached the result: (0,0,573,543)
now I think that the line of code: glOrtho(-oglWindowWidth/2,
oglWindowWidth/2, oglWindowHeight/2,-oglWindowHeight/2, -1, 1); says that:
left of window : -573/2 = -286.5
right of window : 286.5
top of window : -543/2 = -271.5
bottom of window : 271.5
But the Viewport tells:
left of window : 0
right of window : 573
bottom of window : 0
top of window : 543
Note that if I set the Viewport by something like the following code:
glViewport(-oglWindowWidth/2, oglWindowHeight/2, oglWindowWidth,
oglWindowHeight);
I won't get the true result. I mean the geometry will not be drawn at the
center of window as shown in the photo?
How can I solve the paradox having two different coordinate values for the
bottom left of the image by two different codes?

No comments:

Post a Comment