Jump to content
  • Advertisement
Sign in to follow this  
howie_007

OpenGL 3D Projection to 2D screen space

This topic is 1082 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Just for kicks, I'm wring a software renderer. No particular reason, just for kicks.

 

In my DirectX engine or my OpenGL engine, doing perspective or orthographic rendering is simply the matter of generating the corresponding matrix, perspective or orthographic. Both rendering API's handle this without any additional information.

 

Here's my code to convert perspective projection to screen space.

((trans.vert.x / trans.vert.z) * m_screenHalf.w) + m_screenHalfS.w + 0.5f;
((trans.vert.y / trans.vert.z) * m_screenHalf.h) + m_screenHalfS.h + 0.5f;

The problem is, this same code can't be used for orthographic projection. For orthographic projection, you don't need to divide by z so I was assuming z would translate to 1. The orthographic matrix is described as below.

yScale = cot(fovY/2)
xScale = yScale / aspect ratio
xScale     0          0              0
0        yScale       0              0
0        0        zf/(zn-zf)        -1
0        0        zn*zf/(zn-zf)      0

So to correct this problem, I would simple do the below.

(trans.vert.x * m_screenHalf.w) + m_screenHalfS.w + 0.5f;
(trans.vert.y * m_screenHalf.h) + m_screenHalfS.h + 0.5f;

So my question is, why do I need to differentiate in my code the projection of perspective vs orthographic when DirectX or OpenGL works seamlessly? For DirectX or OpenGL, you can give your geometry any z value and it seems to ignore it. I'm getting z values I can't ues for orthographic projection.

Share this post


Link to post
Share on other sites
Advertisement
I think the z coordinate is ignored with orthographic projection, or assumed to be 1.

Share this post


Link to post
Share on other sites

those are two different projections and the reason OpenGL and D3D don't need to change their code is that they use matrices for doing the projection so a change in matrix, changes the projection, the Z component is still used and is not ignored nor is set 1.

Share this post


Link to post
Share on other sites

those are two different projections and the reason OpenGL and D3D don't need to change their code is that they use matrices for doing the projection so a change in matrix, changes the projection, the Z component is still used and is not ignored nor is set 1.


This is an area that I am totally rusty on. If the z coordinate is used, wouldn't it have an effect on the final projection?

Share this post


Link to post
Share on other sites

I setup some test DirectX code, generated an orthographic matrix and translated some points. The z is being set to some value that otherwise shouldn't work but yet it's passed down the pipeline and all is well. Since this is done in the shader, DirectX or OpenGL only recieves the translated verts so I don't know how or why this works.

Share this post


Link to post
Share on other sites

the basics of 2 projection methods are completely different. In orthogonal projection the z does not effect the outcome's  x, y values.

 

the point is first translated and then the x, y, and z are only scaled depending on the input of the projection matrix, and this translation and scaling is the same for all the points, independent to their z values.

 

https://msdn.microsoft.com/en-us/library/windows/desktop/bb205347(v=vs.85).aspx

 

as you can see the matrix is a simple combination of translation matrix and a scaling matrix.

 

so the z is not ignored nor is set to a constant value. ignoring the z wouldn't make the z test possible.

Edited by IYP

Share this post


Link to post
Share on other sites

Looking at the homogeneous vectors and projection matrices, thing is that the step in question does not divide by z but by w. Now w depends on z (within view space) in case of perspective projection, but it does not in case of orthogonal projection. So also in orthogonal projection there is still a varying z (used for depth test) although x and y are not affected by the value of z.

Edited by haegarr

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!