Can someone explain glOrtho to me in plain english?

Started by
3 comments, last by MylesEvans 11 years, 5 months ago
I've been led to believe that in order to invert the y-axis for my 2d game I need to use glOrtho;but looking up information on it leads to definitions such as this.

void WINAPI glOrtho(
GLdouble left,
GLdouble right,
GLdouble bottom,
GLdouble top,
GLdouble zNear,
GLdouble zFar
);

Parameters
left
The coordinates for the left vertical clipping plane.
right
The coordinates for theright vertical clipping plane.
bottom
The coordinates for the bottom horizontal clipping plane.
top
The coordinates for the top horizontal clipping plans.
zNear
The distances to the nearer depth clipping plane. This distance is negative if the plane is to be behind the viewer.
zFar
The distances to the farther depth clipping plane. This distance is negative if the plane is to be behind the viewer.

I code in c++ mainly using sdl (but I use it with Opengl which I am not too familiar)

I just need to understand what it does so I can invert the y axis.

I have this problem where when I use SDL to get mouse coordinates (mouse.motion.x/y). When I attach an object Ive draw to the mouse (so it moves with the mouse) whenever I move the mouse up the object goes down and when I move it down it goes up. The x offset is fine though.

If I could just invert the y-axis it will all be fine. When I move the mouse up/down the object will follow.
Advertisement
It defines the rectangle in pixels, in which the orthographic projection takes place.
Swapping top and bottom might do the trick,
but it could lead to the rectangle having a negative height, and perhaps some issues follow...
Let's just focus on the meaning of one pair of parameters, the other two pairs work the same. Let's take the bottom and top parameters as an example since you're talking about the Y-axis.

The function in itself is used to specify the visible coordinate range for your screen (actually, for the viewport, but let's say the viewport covers the whole screen). That is, the value of the bottom parameter specifies the Y-coordinate corresponding to the bottom edge of the screen, and the top parameter specifies the Y-coordinate corresponding to the top of the screen. So if you specify bottom as 0 and top as 100, then everything that has a Y-coordinate between 0 and 100 will appear on the screen, where 0 is the bottom, 50 is in the middle and 100 is at the top.

So to invert the Y-axis, just swap the bottom and top parameter values. Thus, if you specify bottom as 100 and top as 0, then you have your inverted Y-axis since 0 now corresponds to the top of the screen and 100 to the bottom of the screen.

But that may or may not be the solution you're after. You're also taking about the problem being the mouse cursor, so it could very well be that a bottom-to-top coordinate is what you actually want, but that you just want to invert the actual mouse coordinate. In that case, just subtract the height from the mouse's Y-coordinate: y=height-1-y.
And then, glOrtho() may not be what you are looking for as it is Legacy OpenGL. Have a look at http://www.opengl.org/wiki/Legacy_OpenGL and make sure this is the road you want to go.

Still, understanding glOrtho() helps you even if you would decide not to use Legacy OpenGL.
[size=2]Current project: Ephenation.
[size=2]Sharing OpenGL experiences: http://ephenationopengl.blogspot.com/

Let's just focus on the meaning of one pair of parameters, the other two pairs work the same. Let's take the bottom and top parameters as an example since you're talking about the Y-axis.

The function in itself is used to specify the visible coordinate range for your screen (actually, for the viewport, but let's say the viewport covers the whole screen). That is, the value of the bottom parameter specifies the Y-coordinate corresponding to the bottom edge of the screen, and the top parameter specifies the Y-coordinate corresponding to the top of the screen. So if you specify bottom as 0 and top as 100, then everything that has a Y-coordinate between 0 and 100 will appear on the screen, where 0 is the bottom, 50 is in the middle and 100 is at the top.

So to invert the Y-axis, just swap the bottom and top parameter values. Thus, if you specify bottom as 100 and top as 0, then you have your inverted Y-axis since 0 now corresponds to the top of the screen and 100 to the bottom of the screen.

But that may or may not be the solution you're after. You're also taking about the problem being the mouse cursor, so it could very well be that a bottom-to-top coordinate is what you actually want, but that you just want to invert the actual mouse coordinate. In that case, just subtract the height from the mouse's Y-coordinate: y=height-1-y.


This makes sense. Thanks.

This topic is closed to new replies.

Advertisement