Jump to content

  • Log In with Google      Sign In   
  • Create Account


Making glOrtho work (or writing an intuitive projection matrix)


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
10 replies to this topic

#1 SuperVGA   Members   -  Reputation: 1118

Like
0Likes
Like

Posted 14 July 2013 - 02:04 PM

Hi guys,

 

I've messed around with my fixed (only the width and heights) orthographic projection in relation to guis.

Now I'd like to put my neat, flexible Projection class to use and create a Camera that will render me an overview map.

 

The view vectors are potentially much different than the 2d stuff, as it doesn't need to picture my level from straight above, necessarily.

 

Unlike what many people say on the webs, it can be necessary to provide negative near and far clipping values to glOrtho, to get the wanted result. This is AFAIK because of some old ortho stuff from the early days of OpenGl.
(See http://mlucassmith.tumblr.com/post/10869898438/why-i-hate-glortho-and-all-its-offspring for more on glOrtho and why it doesn't behave like glFrustrum.)

 

What I'm getting if I just mess around with the values a bit (in case I want the ortho camera oriented directly downwards) is this:

 

argh.png

 

 

(The floor slabs are gray on the top colored on the underside)

I have depth buffering enabled, but it looks like (perhaps because of the inverted clipping plane values (near  -100, far: 100) the depth values are swapped) If it's not this, then it's the "up" vector not working as I intend it to, and it mirrors the projection somehow.

I get the correct projection by swapping near and far, and swapping the target and the eye vectors. (aside from being an orthographic projection, it's similar to me seeing the toy car in my kid's room when i see from the toy car, vieweing from me (near, 32.0f) to it (far, 0.1f)) in the direction of where i stand.

 

I read up on projection matrices (just how to set them up), and I messed with the values to initially copy the behavior of glOrtho:

    GLfloat tx = -((ortho_right+ortho_left) / (ortho_right-ortho_left));
    GLfloat ty = -((ortho_top+ortho_bottom) / (ortho_top-ortho_bottom));
    GLfloat tz = -((clip_far+clip_near) / (clip_far-clip_near));
    GLfloat cx =  (2.0f / (ortho_right - ortho_left));
    GLfloat cy =  (2.0f / (ortho_top   - ortho_bottom));
    GLfloat cz = -(2.0f / (clip_far    - clip_near));
    
    GLfloat mtx[16] ={  cx, 0.0f, 0.0f,   0.0f,
                      0.0f,   cy, 0.0f,   0.0f,
                      0.0f, 0.0f,   cz,   0.0f,
                      tx, ty, tz, 1.0f };
    glMultMatrixf(mtx);

I'd like to fix this so I end up with a more intuitive "myOrtho()" (meaning the clip_near resembles the direct linear distance ahead of the camera, in the view direction in which the near clipping plane exists, and the clip_far resembles the one of the far clipping plane (both being positive values)).

 

Unfortunately, my general projection matrix math is very rusty, and I barely managed to copy the khronos man page description for glOrtho.

Have you guys tried this before? The alternative would be to negate view directions and the near and far clipping values on every frame drawn into an orthographic projection. And I'm reluctant to do that as it seems a bit hackish. Shouldn't the projection just "work"?


Edited by SuperVGA, 15 July 2013 - 01:08 AM.


Sponsor:

#2 SuperVGA   Members   -  Reputation: 1118

Like
0Likes
Like

Posted 14 July 2013 - 11:53 PM

To clarify, if i want the same draw order and general view direction with glOrtho projection as I get with glFrustrum,

I need adapt my projection and modelview as such:

 

reverse proj vectors.png

 

(And I feel like I shouldn't have to mess with my ModelView matrix just to get the right contents on the screen.

-Can't we just have an orthographic projection set up so it can take the same eye and target vectors as a perspective projection?)


Edited by SuperVGA, 15 July 2013 - 01:11 AM.


#3 SuperVGA   Members   -  Reputation: 1118

Like
0Likes
Like

Posted 21 July 2013 - 02:17 AM

... Bump! Is there really no good way to set up an ortographic projection in opengl without hacking around with the model view matrix?



#4 Brother Bob   Moderators   -  Reputation: 7427

Like
0Likes
Like

Posted 21 July 2013 - 03:58 AM

In your code, you have swapped both the near and far planes, and the view direction. Those two negates each other. If you use an orthographic projection with the same eye and target position, and same near and far planes, you will get an equivalent view of the scene. Only the vertical and horizontal scaling will be different, but that is a natural result of the fundamental differences between the two types of projections.



#5 SuperVGA   Members   -  Reputation: 1118

Like
0Likes
Like

Posted 21 July 2013 - 06:12 AM

In your code, you have swapped both the near and far planes, and the view direction. Those two negates each other. If you use an orthographic projection with the same eye and target position, and same near and far planes, you will get an equivalent view of the scene. Only the vertical and horizontal scaling will be different, but that is a natural result of the fundamental differences between the two types of projections.

Hi, thanks for responding. The code for the projection is actually a copy of how glOrtho already works (see the glOrtho entry, paste the markup for the page into http://www.madogiwa.org/display-mathml/live.html to get the actual formulas (or use an mml client)). I'll guess Id only need to reverse the depth check to make it work,

but even then it doesn't seem to do the same thing as only using glOrtho without swapping the values. (Also, I read somewhere that only the far value should be negative)

 

(I'm still basically saying: I cannot replace glFrustrum with glOrtho and expect the same items showing (disregarding of the perspective vs parallel projection bit))

 

I have to change the eye position or the depth test.

 

At least, I haven't been able to make it just by changing the projection.


Edited by SuperVGA, 21 July 2013 - 06:16 AM.


#6 Brother Bob   Moderators   -  Reputation: 7427

Like
1Likes
Like

Posted 21 July 2013 - 06:26 AM

Perhaps I don't understand your problem. If you have (the equivalent of) the following setup:

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(..., 0.1, 32);
 
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glLookAt(eye.x, eye.y, eye.z, target.x, target.y, target.z, up.x, up.y, up.z);

then the equivalent orthographic view is:

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(l, r, t, b, 0.1, 32);
 
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glLookAt(eye.x, eye.y, eye.z, target.x, target.y, target.z, up.x, up.y, up.z);

where you have to adjust l, r, t and b to get the desired horizontal and vertical scaling. The orthographic view will be from the same location, towards the same target location and with the same depth range. Is this what you're trying to achieve?



#7 SuperVGA   Members   -  Reputation: 1118

Like
0Likes
Like

Posted 21 July 2013 - 07:26 AM

Perhaps I don't understand your problem. If you have (the equivalent of) the following setup:

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(..., 0.1, 32);
 
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glLookAt(eye.x, eye.y, eye.z, target.x, target.y, target.z, up.x, up.y, up.z);

then the equivalent orthographic view is:

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(l, r, t, b, 0.1, 32);
 
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glLookAt(eye.x, eye.y, eye.z, target.x, target.y, target.z, up.x, up.y, up.z);

where you have to adjust l, r, t and b to get the desired horizontal and vertical scaling. The orthographic view will be from the same location, towards the same target location and with the same depth range. Is this what you're trying to achieve?

Yes, it's exaclty that. smile.png Only, I cannot make it do that. I have a viewport 0,0 -> 800, 600 and set l=-1.0, r=1.0, t=1.0, b=1.0, -1.0

from a view looking from 0.5, -0.5, 0.5 towards 0.5, 0.5, 0.5 where up.z = {0, 0, 1}

 

I have a 90 deg perspective projection that i can swap with, near set to 0.1 and far to 2.0, still, i draw an object at 0,0,0 (with lengths of all bound sides == 1.0)

 

Still i end up getting a view as if the depth test was reversed (as if the ortho eye is being far away, looking back at where it should've been),

where the perspective projection shows what is expected off the given parameters.


Edited by SuperVGA, 21 July 2013 - 07:27 AM.


#8 Brother Bob   Moderators   -  Reputation: 7427

Like
1Likes
Like

Posted 21 July 2013 - 03:10 PM

Show a complete program demonstrating your behavior, and keep it as simple as you possibly can. If there is a fundamental problem with your setup, you should easily be able to reduce and show it in 50 lines of code with a decent window wrapper API like GLUT or SDL.


Edited by Brother Bob, 21 July 2013 - 03:30 PM.


#9 SuperVGA   Members   -  Reputation: 1118

Like
0Likes
Like

Posted 25 July 2013 - 11:00 AM

I just rewrote the setup as a small sample. Cannot reproduce the issue.... Thanks for your input man, I must've done something else wrong.smile.png



#10 Brother Bob   Moderators   -  Reputation: 7427

Like
0Likes
Like

Posted 25 July 2013 - 12:18 PM

If you want to identify the problem, you should reduce your existing code, not rewrite it. If there is an error, it will either remain in the reduced code or you will know which piece of code you just removed when it started working as expected.



#11 SuperVGA   Members   -  Reputation: 1118

Like
0Likes
Like

Posted 25 July 2013 - 01:34 PM

If you want to identify the problem, you should reduce your existing code, not rewrite it. If there is an error, it will either remain in the reduced code or you will know which piece of code you just removed when it started working as expected.

 

Alright let me rephrase my previous statement then,

I wrote the part that does just the basic proj and model setup, again (i copied my existing code), into a smaller program.

The problem disappeared. (It behaved as one would expect.)

 

I found that you swapped the b and t values of glOrtho and found that I had culling set the wrong way (which added to the mystery of obscured objects being drawn).

 

I've fixed the issue in the bigger project.






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS