copenglsdl

Why does my quad not render as I expect it to?


I'm learning about how OpenGL and 3D rendering works and I've been following this tutorial. I'm doing some things slightly differently (using C instead of C++ and using my own matrix calculation implementation instead of GLM). My program is supposed to render a quad in 3D space. My problem is that when I run the program, it shows a blank screen if I use the same coordinates as him. I can change the coordinates to put the quad in view, but it looks distorted.

I've played around and I can get the quad in view, but it looks distorted, especially when I move around. This is what it looks like in the tutorial: Expected result. These are the coordinates he uses:

For the projection matrix, he uses these parameters:

The quad vertex coordinates are:

When I use the exact same values, the window is blank. If I change the following values:

It will show me something, but it looks like this: My result.

If I change the eye to be (0.0, -0.3, 2.0) with the z for the vertex coordinates to still be -1.0 it looks like this.

This is my main.c file:

GLint uniView = glGetUniformLocation(shaderProgram, "uniViewTransf");
GLint uniProjection = glGetUniformLocation(shaderProgram, "uniProjTransf");

struct Matrix4 viewTransform = ViewTransformation(Vector3(0.0f, -1.0f, 1.0f), Vector3(0.0f, 0.0f, 0.0f), Vector3(0.0f, 0.0f, 1.0f));
struct Matrix4 projTransform = ProjectionTransformation(M_PI/4.0f, 640.0f/480.0f, 1.0f, 10.0f);

glUniformMatrix4fv(uniView, 1, GL_FALSE, (float*)viewTransform.v);
glUniformMatrix4fv(uniProjection, 1, GL_FALSE, (float*)projTransform.v);

while (quit == false)
{
    ...

    viewTransform = ViewTransformation(Vector3(x, y, z), Vector3(0.0f, 0.0f, 0.0f), Vector3(0.0f, 0.0f, 1.0f));
    glUniformMatrix4fv(uniView, 1, GL_FALSE, (float*)viewTransform.v);

    glClear(GL_COLOR_BUFFER_BIT);
    RenderWindow();

    SDL_GL_SwapWindow(window);
}
...

Here are the relevant functions of my matrices.c file:

struct Matrix4
{
    float v[4][4];
};

struct Vector3
{
    float v[3];
};

...

struct Vector3 NormaliseVector3(struct Vector3 v)
{
    float magnitude = sqrt(v.v[0] * v.v[0] + v.v[1] * v.v[1] + v.v[2] * v.v[2]);
    return Vector3(v.v[0] / magnitude, v.v[1] / magnitude, v.v[2] / magnitude);
}

struct Vector3 CrossProduct(struct Vector3 a, struct Vector3 b)
{
    return Vector3(
        a.v[1] * b.v[2] - a.v[2] * b.v[1],
        a.v[2] * b.v[0] - a.v[0] * b.v[2],
        a.v[0] * b.v[1] - a.v[1] * b.v[0]
    );
}

struct Matrix4 ViewTransformation(struct Vector3 eye, struct Vector3 centre, struct Vector3 up)
{
    struct Vector3 f = Vector3(centre.v[0] - eye.v[0], centre.v[1] - eye.v[1], centre.v[2] - eye.v[2]);
    f = NormaliseVector3(f);

    struct Vector3 s = CrossProduct(f, NormaliseVector3(up));

    struct Vector3 u = CrossProduct(NormaliseVector3(s), f);

    return (struct Matrix4)
    {
        .v = {
            { s.v[0],  s.v[1],  s.v[2], 0.0f},
            { u.v[0],  u.v[1],  u.v[2], 0.0f},
            {-f.v[0], -f.v[1], -f.v[2], 0.0f},
            { 0.0f,    0.0f,    0.0f,   1.0f}
        }
    };
}

struct Matrix4 ProjectionTransformation(float verticalFOV, float aspectRatio, float nearPlane, float farPlane)
{
    float f = 1.0f / tan(verticalFOV / 2.0f);
    
    struct Matrix4 m = IdentityMatrix();

    m.v[0][0] = f / aspectRatio;
    m.v[1][1] = f;
    m.v[2][2] = (farPlane + nearPlane) / (nearPlane - farPlane);
    m.v[2][3] = (2.0f * farPlane * nearPlane) / (nearPlane - farPlane);
    m.v[3][2] = -1.0f;
    m.v[3][3] = 0.0f;

    return m;
}

This is my vertex shader code:

...
in vec2 vertPosition;

uniform mat4 uniModelTransf;
uniform mat4 uniViewTransf;
uniform mat4 uniProjTransf;

void main()
{
    gl_Position = uniProjTransf * (uniViewTransf * vec4(vertPosition, -1.0, 1.0));
}

I checked glGetError() and it always returns 0.

For the view and projection matrix math, I used these as references:

Also I'm on MacOS (M1) and using SDL2 for window creation if that's relevant.

Why is my program not rendering the quad as it does in the tutorial?

Edit: For anyone else who comes across this, I've finally worked it out. I've changed the parameter in glUniformMatrixfv() from GL_FALSE to GL_TRUE as per rafix07's answer.

After that, it seemed better, but still wasn't working properly. I found another problem which was that I wasn't constructing my view matrix properly. In the tutorial, they used glm::lookAt() for the view matrix. When I implemented it myself, I based it on gluLookAt() according to this page by khronos. Firstly, the s vector in the formula needed to be normalised even though it didn't say that on the page. Also, this formula seems to only be the rotation part of the view transformation according to this gamedev stackexchange post, and so I needed to add a translation part to my view matrix.

The solution was to multiply my rotation view matrix by a translation view matrix like so:

             ROTATION            TRANSLATION
         x.x  x.y  x.z  0       1  0  0  -c.x
VIEW  =  y.x  y.y  y.z  0   •   0  1  0  -c.y
         z.x  z.y  z.z  0       0  0  1  -c.z
         0.0  0.0  0.0  1       0  0  0     1

where c is the camera or eye position.

It's now working as it does in the tutorial.


Solution

  • Multi-dimensional arrays in C are stored in row major order (as your Matrix4 is defined). If we have [2][2] in memory it looks like:

    [0][0], [0][1], [1][0], [1][1]
    

    OpenGL expects matrix defined in column major order. With this approach the order of indices is:

    0 4 8  12
    1 5 9  13
    2 6 10 14
    3 7 11 15
    

    Let's look how you build projection matrix. Coefficient

    (2.0f * farPlane * nearPlane) / (nearPlane - farPlane);
    

    in expected matrix layout should have index 14 (based 0), but in your approach its index is 11.

    As possible solutions you can:

    1. make your matrix as one dimensional array [16], then reassign matrix' coefficients when building view, projection matrix (with one dimenstion it will be easier to assign values)
    2. transpose passed matrix to shaders when calling glUniformMatrix4fv by settings third its parameter to GL_TRUE