# OpenGL 3D Projection to 2D screen space

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

## 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 on other sites
I think the z coordinate is ignored with orthographic projection, or assumed to be 1.

##### 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 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 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 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 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

1. 1
2. 2
3. 3
4. 4
Rutin
13
5. 5

• 26
• 11
• 9
• 9
• 11
• ### Forum Statistics

• Total Topics
633701
• Total Posts
3013435
×