I'm try to render a scene with the orthographic projection matrix without any success.
this is my vertex shader:
#version 330 core
layout (location = 0) in vec3 position;
uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;
void main() {
gl_Position = projection * view * model * vec4(position, 1.0f);
}
and this is my fragment shader:
#version 150
uniform vec3 color;
out vec4 out_color;
void main() {
out_color = vec4(color, 1.0);
}
this is my render code:
shader.setUniform("projection", projection);
shader.setUniform("view", view);
shader.setUniform("model", model);
shader.setUniform("color", color);
glBindVertexArray(vao);
glEnableVertexAttribArray(0);
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_SHORT, nullptr);
now if I use as projection matrix:
glm::perspective(glm::radians(fov), width/(float)height, zNear, zFar);
everything works and I see the cube. But instead if I use this:
glm::ortho(0.0f, static_cast<float>(width), 0.0f, static_cast<float>(height));
the cube is gone.
What I'm missing?
Most likely the cube is clipped by either the far plane of the projection. If you set the projection by
glm::ortho(0.0f, static_cast<float>(width), 0.0f, static_cast<float>(height));
then the near plane is -1 and the far plane is 1. All the geometry, which is not in between the near and far plane is clipped.
Set the orthographic projection by glm::ortho
. Set the near and far planes explicitly and use the same near and far planes as when setting the perspective projection.
Furthermore the projection of the cube on the viewport is just a small point. Change the viewing volume:
float scale = 1.0f;
float aspect = static_cast<float>(width) / static_cast<float>(height);
glm::ortho(-aspect * scale, aspect * scale, -scale, scale, zNear, zFar);
If the view rectangle is too small, you can adjust it using the scale
.