0
\$\begingroup\$

TL;DR I render a scene with M,V,P matrices as always. Than I render something (in this case - text) without any matrices and using separate shaders pair on coordinates with Z = -1. Somehow it's rendered closer to the camera than something on coordinates with Z = 0.9. I can't understand why.

Here is something I can't figure out.

Consider this vertex shader which is used for text rendering:

#version 330 core

layout(location = 0) in vec2 vertexPos;
layout(location = 1) in vec2 vertexUV;

out vec2 fragmentUV;

void main() {
    fragmentUV = vertexUV;

    gl_Position.x = vertexPos.x*2 - 1;
    gl_Position.y = vertexPos.y*2 - 1;
    gl_Position.z = -1.0;
    gl_Position.w =  1.0;
}

When text is rendered with Z = -1 everything looks fine:

http://i.imgur.com/ShCYqqU.png

And if I change Z to 0.9 scene looks like this:

http://i.imgur.com/RhPtHG9.png

According to this tutorial perspective matrix modifies the scene so that all visible objects are in cube where -1 <= {X,Y,Z} <= 1. Also by default camera looks "down" towards Z axis.

So shouldn't it be conversely: larger Z - closer to the camera?

Full source code is on GitHub. Here is a code which renders text. And this code fills corresponding VBO.

\$\endgroup\$
1
\$\begingroup\$

Mystery solved. I was not aware of the fact that OpenGL expects vertex shader output to be left-handed. And that it is usually left-handed after multiplying coordinates to projection matrix (see this article). So smaller Z in vertex shader - closer to the camera.

And here is a great cheat sheet about left-handed and right-handed coordinates:

http://image.slidesharecdn.com/06gfxmath-120207173111-phpapp01/95/cs-354-graphics-math-27-728.jpg

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.