2
votes

Opengl has right-hand coordinate system. It means z values increase towards me.

right-hand coordinate system
right-hand coordinate system

I draw two triangles:

float vertices[] =
{
     //position                     //color
     //triangle 1
     0.0f,  1.0f, -1.0f,            1.0f, 0.0f, 0.0f,//0
    -1.0f, -1.0f, -1.0f,            1.0f, 0.0f, 0.0f,//1
     1.0f, -1.0f, -1.0f,            1.0f, 0.0f, 0.0f,//2
     //triangle 2
     0.0f, -1.0f, 0.0f,             0.0f, 0.0f, 1.0f,//3
     1.0f,  1.0f, 0.0f,             0.0f, 0.0f, 1.0f,//4
    -1.0f,  1.0f, 0.0f,             0.0f, 0.0f, 1.0f//5
};

Why triangle 1 is in front? Triangle 2 should be in front, because of 0.0f > -1.0f.

I have only gl_Position = vec4(aPos, 1.0); in vertex shader.

After it, if I translate vertices by z=-3 in vertex shader, this translation behaves as it should be. Object becomes further.

1
How do you set up the VAO?Yakov Galka
@ybungalobill I use GLFW_OPENGL_COMPAT_PROFILE mode and I don't use any VAO.MrVIAV
@Rabbid76 I dont use projection matrix and the view matrix when I don't translate. When I translate vertices by z=-3, I use only model and projection matrix. projection matrix: glm::mat4 projectionMatrix = glm::perspective<float>(glm::radians(60.0f), (float)SCR_WIDTH / (float)SCR_HEIGHT, 0.1f, 10.0f); model matrix: glm::mat4 modelProjectionMatrix = glm::translate(projectionMatrix, glm::vec3(0.0f, 0.0f, -6.0f));MrVIAV

1 Answers

2
votes

Why triangle 1 is in front? Triangle 2 should be in front, because of 0.0f > -1.0f.
I have only gl_Position = vec4(aPos, 1.0); in vertex shader.

Of course the red triangle is in front of the blue one, because you don't use any projection matrix. You forgot to transform the input vertex by the projection matrix before you assign the vertex coordinate to gl_Position.

This causes that the vertices are equal to the normalized device space coordinates. In normalized device space the z-axis points into the viewport and the "projection" is orthographic and not perspective.

You have to do something like this:

in vec3 aPos;
mat4 modelProjectionMatrix;

void main()
{
    gl_Position = modelProjectionMatrix * vec4(aPos, 1.0);
}