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:

  • Eye coordinates (camera): (1.2, 1.2, 1.2)
  • Looking at (centre): (0.0, 0.0, 0.0)
  • “Up” axis: (0.0, 0.0, 1.0)

For the projection matrix, he uses these parameters:

  • Vertical FOV: 45 degrees, converted to radians
  • Aspect ratio: 800 / 600
  • Near clipping plane: 1.0
  • Far clipping plane: 10.0

The quad vertex coordinates are:

  • (-0.5, 0.5, 0.0)
  • (0.5, -0.5, 0.0)
  • (0.5, -0.5, 0.0)
  • (-0.5, 0.5, 0.0)

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

  • Eye coordinates (camera): (0.0, -1.0, 1.0)
  • Change the z of the vertex coordinates to -1.0

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:

  • https://registry.khronos.org/OpenGL-Refpages/gl2.1/xhtml/gluLookAt.xml
  • https://registry.khronos.org/OpenGL-Refpages/gl2.1/xhtml/gluPerspective.xml

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?

New contributor

user25477109 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật