4
\$\begingroup\$

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:

enter image description here
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?

\$\endgroup\$
5
\$\begingroup\$

Window coordinates - the coordinates in which the viewport is specified using glViewport - are not the same as the view space coordinates that you pass to glOrtho. Window coordinates are always measured in pixels from the lower-left corner of the window, while view space coordinates are entirely up to you and can be defined however you like.

You can think of glOrtho as a function that lets you define your view space by saying where the corners of the viewport should be in view space. On the other hand, glViewport lets you say where the corners of the viewport should be in actual pixels in the window (or in an offscreen render target).

When you submit a vertex (in view space, since you don't have a modelview matrix yet), it first gets transformed to what are called normalized device coordinates, using the projection matrix specified by glOrtho. Then it gets transformed from there to window coordinates (i.e. pixels) using the viewport specified by glViewport. Then it gets sent off to be rasterized.

Now, you happen to be setting up your glOrtho call in such a way that your view coordinates are also measured in pixels. But that's just a choice you're making. You're also setting it up so that the origin of view coordinates is at the center of the window. That's another choice you're making. That choice doesn't affect the definition of window coordinates, though; they're still measured in pixels from the lower-left corner of the window.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.