Viewport coordinates are upside-down

Started by
9 comments, last by Mekanikles 16 years, 8 months ago
As the subject suggest, the coordinates when specifying the viewport is inverted along the X-axis. This means (0,0) is in the lower left corner of the window. Is there a simple way to flip the coordinates and get (0,0) in the upper left corner instead? thnx /Mekanikles
Advertisement
The first thing I'd try would be to call glRotatef(180,1.0,0,0)

I'm not sure if that will work, give it a try? You might need to translate around a little bit after that, dunno.

Good luck! :)
Does it actually matter? Most of the time nobody does a whole lot in viewport-space, except for mouse picking, so shouldn't be too hard to flip the y-coordinate on the fly.

Also, is this glViewport() itself we are talking about here?

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

I assume you are doing 2d graphics of some kind using glOrtho or gluOrtho2D. If that is the case then simply flip the two y coordinates of the ortho call.

Old:
glOrtho(0,width,0,height,-1,1)

New:
glOrtho(0,width,height,0,-1,1)

That simply makes the top left corner 0,0 and has y increasing going down instead of increasing up.
Sorry if I was unclear but it's not the actual viewport that is flipped, its the position coordinates of it, sort of :)

I'm using it for splitscreens, say that I want my viewport to be positioned at top left (0,0) and stretch one quarter of the screen to (160, 120) (on a 320x240 screen). If I call glViewport(0, 0, 160, 120) I will get a square in the bottom left corner instead.

It's not that much of problem since I can adjust the coordinates with the width and height of the window but it bothers me that the positioning differs from everything else.

I'm gonna guess that its not possible since its probably Windows' way of doing things, but thnx anyway.

/Mekanikles
Everything in OpenGL is defined to be consistent with a system with the origin in the lower left corner. Window coordinates, world space coordinates (look at the default matrix setup and parameters to glOrtho and glFrustum for example), texture coordinates... everything is based on a lower left coordinate system.

You can change some of the coordinate systems, but you're then stepping away from OpenGL's default setup and care must be taken to convert between different coordinate systems at the right places. My advice is to use a lower left coordinate system and everything in OpenGL will come out nice and consistent at all places.
This really isn't a big problem, you just need to flip the coordinates back again. Just create a transform matrix and invert the y-coordinate. Then multiply this with your orthographic transform matrix and reverse your winding order. Bingo.
Hello,

just to clarify, this issue hasn't got anything to do with Windows. OpenGL uses a right-handed coordinate system for all of it's calculations, but when the projection transform and perspective division is applied to go from view-space coordinates to normalized device coordinates, the coordinate system is switched to left-handed. NCDS is a left-handed coordinate system.

Jeroen
huh?

Confusion seems to reign supreme (sob: well at least in my head);

And there I was thinking this was all about
number 12 of the good ol' "16 Common Opengl Pitfalls"

[here: http://www.opengl.org/resources/features/KilgardTechniques/oglpitfall]

Enter sweeping statement: Anyone that's created a viewport in a windows app knows that you need to map the 2D y axis "upside down" to position it.

But on a more positive note: That's all Mekanikles
needs to do ( ...and somehow my psychic powers tell me that Mekanikles has
worked that out already :)).
Oh...and to [edit:"hammer home my point" is to harsh] re-inforce my point
if I'm right:

All Mekanikles really needed was a link
to the best opengl ref material on the net (controversial of late?):
Specifically Nehe lesson 42 (about 3/4 down the page down)

and the (hardcoded) lines like:

// Set The Viewport To The Top Left.
//It Will Take Up Half The Screen Width And Height
glViewport (0, window_height/2, window_width/2, window_height/2);
...
...
...
// Set The Viewport To The Top Right.
//It Will Take Up Half The Screen Width And Height
glViewport (window_width/2, window_height/2, window_width/2, window_height/2);
...
...
etc, etc

cheers

p.s. if anyone has a problem using opengl (and you have no need for the interaction the forum provides), Nehe is still a good first stop: for better or worse, a version of the answer u seek may just be there.

[Edited by - steven katic on August 17, 2007 11:34:59 PM]

This topic is closed to new replies.

Advertisement