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?
user25477109 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.