Problem drawing simple quad using orthographic projection matrix

Started by Adro, January 19, 2022, 17:31:44

Previous topic - Next topic

Adro

Hi!

I'm trying to draw a simple quad into the screen with no perspective, only for setting an image on it, but it doesn't show up. It's not an issue about color or texture, because the quad is drawn with perspective if I use perspective matrix instead. In fact, the renderer says it's drawing the mesh... My code is very simple. Here I define the vertex positions, indices and texture coordinates:

posText = new float[]{
            0.0f, 0.0f,
            0.0f, 0.5f,
            0.5f, 0.5f,
            0.5f, 0.0f
        };
       
indicesText = new int[]{
            0, 1, 3, 1, 2, 3
        };

textCoords = new float[]{
            0.0f, 0.0f,
            0.0f, 1.0f,
            1.0f, 1.0f,
            1.0f, 0.0f,

        };

And when rendering it, the orthographic matrix is called and built as:

Matrix4f orthoProjMatrix = orthoProjectionMatrix(0, window.getWidth(), window.getHeight(), 0);
public Matrix4f orthoProjectionMatrix(float left, float right, float bottom, float top){
        orthoProjectionMatrix.identity();
        orthoProjectionMatrix.setOrtho2D(left, right, bottom, top);
        return orthoProjectionMatrix;
    }

The vertex shader is simple:

#version 330

layout (location=0) in vec3 position;
layout (location=1) in vec2 texCoord;


out vec2 outTexCoord;

uniform mat4 projectionMatrix;
uniform mat4 worldMatrix;

void main()
{
    gl_Position = projectionMatrix * worldMatrix * vec4(position.xy, 0.0, 1.0);
    outTexCoord = texCoord;
}


Where projectionMatrix takes the value of the othographic matrix. What am I doing wrong?

KaiHH

With the effective values:
orthoProjectionMatrix.setOrtho2D(0, window.getWidth(), window.getHeight(), 0);

you are saying that all coordinates between [0, window.getWidth()) and [0, window.getHeight()) get mapped to your viewport.
Therefore, when you render a model with model-space coordinates in [0, 0.5] that becomes smaller than one pixel and likely don't generate any fragments when rasterized.
You either need to modify the extents of the orthographic projection or increase the spacing of your model-space vertices.

Adro

Thanks for your answer, Kaihh!

So if for instance I put vertices in [0, 1.5] will generate fragments? Because I've seen that usually values are in the interval [0, 1] because the screen takes NDC system.

KaiHH

Quote
Because I've seen that usually values are in the interval [0, 1] because the screen takes NDC system.
Effectively, OpenGL only cares about the values that your vertex shader is storing into the pre-defined `gl_Position` variable.
This is in "clip space". Clip space is the same as normalized device coordinates (NDC) space when your vertices' `w` coordinate is 1.0 (which it is for orthographic projections).

HOWEVER you are _not_ providing OpenGL (i.e. its `gl_Position` variable) with 0.0 and 0.5 values, but with hugely scaled-down values more akin to 0.0002604 in the x coordinate (when your screen width is 1920 pixels), because you multiply it with an orthographic projection matrix that has said scaling factor in it, because - as I said before - you _want_ to project all values between [0..windowWidth] and [0..windowHeight] to [-1..1], because this is what you said when you created the orthographic projection matrix.

When you want to see your quad you can simply use an identity matrix as the projection matrix, or come to grips with what you actually _want_ (how your orthographic projection should look like).

Adro

Wow I completely get your point. Finally I could draw it! It turns out since my vertex shader is claiming for a vec3, I was missing Z coordinate on the vertex definition, which must be zero. Then I used coordinates in the range of a pixel, scaled the object and positioned it on the origin.