My VAO is simply not showing up and i'm not sure if my matrices are wrong or if the shader is wrong… Please have a look a see what is wrong… My code is big so i'll post the essential.
Matrix4 Matrix4::LookAt(Vector3 eye, Vector3 target, Vector3 up)
{
Vector3 z = Vector3::Normalize(eye - target);
Vector3 x = Vector3::Normalize(Vector3::Cross(up, z));
Vector3 y = Vector3::Normalize(Vector3::Cross(z, x));
Matrix4 result;
result.row0.x = x.x;
result.row0.y = y.x;
result.row0.z = z.x;
result.row0.w = 0;
result.row1.x = x.y;
result.row1.y = y.y;
result.row1.z = z.y;
result.row1.w = 0;
result.row2.x = x.z;
result.row2.y = y.z;
result.row2.z = z.z;
result.row2.w = 0;
result.row3.x = -((x.x * eye.x) + (x.y * eye.y) + (x.z * eye.z));
result.row3.y = -((y.x * eye.x) + (y.y * eye.y) + (y.z * eye.z));
result.row3.z = -((z.x * eye.x) + (z.y * eye.y) + (z.z * eye.z));
result.row3.w = 1;
return result;
}
Matrix4 Matrix4::CreatePerspectiveOffCenter(
float left, float right, float bottom, float top,
float near, float far)
{
float x = 2.0f * near / (right - left);
float y = 2.0f * near / (top - bottom);
float a = (right + left) / (right - left);
float b = (top + bottom) / (top - bottom);
float c = -(far + near) / (far - near);
float d = -(2.0f * far * near) / (far - near);
Matrix4 result;
result.row0.x = x;
result.row0.y = 0;
result.row0.z = 0;
result.row0.w = 0;
result.row1.x = 0;
result.row1.y = y;
result.row1.z = 0;
result.row1.w = 0;
result.row2.x = a;
result.row2.y = b;
result.row2.z = c;
result.row2.w = -1;
result.row3.x = 0;
result.row3.y = 0;
result.row3.z = d;
result.row3.w = 0;
return result;
}
Matrix4 Matrix4::CreatePerspectiveFieldOfView(
float fovy, float aspect,
float near, float far)
{
float maxY = near * tanf(0.5f * fovy);
float minY = -maxY;
float minX = minY * aspect;
float maxX = maxY * aspect;
return CreatePerspectiveOffCenter(
minX, maxX, minY, maxY, near, far);
}
Matrix4 Matrix4::CreateTranslation(Vector3 position)
{
Matrix4 result = Matrix4::Identity();
result.row3.x = position.x;
result.row3.y = position.y;
result.row3.z = position.z;
return result;
}
GLfloat* Matrix4::ToGlArray()
{
GLfloat result[16] =
{
row0.x, row0.y, row0.z, row0.w,
row1.x, row1.y, row1.z, row1.w,
row2.x, row2.y, row2.z, row2.w,
row3.x, row3.y, row3.z, row3.w,
};
return result;
}
This is How I'm setting up the shader matrices
void MeshRenderer::Render(Shader* shader)
{
glProgramUniformMatrix4fv(
shader->GetID(),
shader->GetUniformLocation("model"),
1, GL_TRUE,
GetGameObject()->GetTransform()->
GetTransformation().ToGlArray());
mesh->Render(shader);
}
void Camera::Render(Shader *shader)
{
glClearColor(
backgroundColor.GetR(),
backgroundColor.GetG(),
backgroundColor.GetB(),
backgroundColor.GetA());
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
float aspect = (float)Screen::GetWidth() / (float)Screen::GetHeight();
projection = Matrix4::CreatePerspectiveFieldOfView(
60 * Mathf::DegreesToRadians(),
aspect,
near, far);
view = Matrix4::LookAt(
GetGameObject()->GetTransform()->GetPosition(),
GetGameObject()->GetTransform()->GetPosition() +
GetGameObject()->GetTransform()->GetForward(),
GetGameObject()->GetTransform()->GetUp());
glProgramUniformMatrix4fv(
shader->GetID(),
shader->GetUniformLocation("view"),
1, GL_TRUE, view.ToGlArray());
glProgramUniformMatrix4fv(
shader->GetID(),
shader->GetUniformLocation("projection"),
1, GL_TRUE, projection.ToGlArray());
}
Here is my vertex shader
#version 330 core
layout (location = 0) in vec3 position;
layout (location = 1) in vec2 texCoords;
layout (location = 2) in vec3 normals;
out vec2 TexCoords;
uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;
void main()
{
gl_Position = model * view * projection * vec4(position, 1.0);
TexCoords = texCoords;
}
This is how I'm setting the vao etc...
void Mesh::Entry::SetupEntry()
{
glGenVertexArrays(1, &VAO);
glBindVertexArray(VAO);
glGenBuffers(1, &VBO1);
glBindBuffer(GL_ARRAY_BUFFER, VBO1);
glBufferData(
GL_ARRAY_BUFFER,
sizeof(float) * vertices.size(),
&this->vertices[0],
GL_STATIC_DRAW);
glVertexAttribPointer(
0, 3,
GL_FLOAT, GL_FALSE,
sizeof(float) * 3, NULL);
glEnableVertexAttribArray(0);
glGenBuffers(1, &VBO2);
glBindBuffer(GL_ARRAY_BUFFER, VBO2);
glBufferData(
GL_ARRAY_BUFFER,
sizeof(float) * uvs.size(),
&this->uvs[0], GL_STATIC_DRAW);
glVertexAttribPointer(
1, 2, GL_FLOAT,
GL_FALSE, sizeof(float) * 2,
NULL);
glEnableVertexAttribArray(1);
glGenBuffers(1, &VBO3);
glBindBuffer(GL_ARRAY_BUFFER, VBO3);
glBufferData(
GL_ARRAY_BUFFER,
sizeof(float) * normals.size(),
&this->normals[0], GL_STATIC_DRAW);
glVertexAttribPointer(
2, 3,
GL_FLOAT, GL_FALSE,
sizeof(float) * 3,
NULL);
glEnableVertexAttribArray(2);
glGenBuffers(1, &VBO4);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, VBO4);
glBufferData(
GL_ELEMENT_ARRAY_BUFFER,
sizeof(unsigned int) * indices.size(),
&this->indices[0],
GL_STATIC_DRAW);
glBindVertexArray(0);
}
EIDT :
void Mesh::Entry::Render(Shader *shader)
{
glBindVertexArray(VAO);
glDrawElements(
GL_TRIANGLES,
indices.size(),
GL_UNSIGNED_INT,
(void*)0);
glBindVertexArray(0);
}
model * view * projection
is correct. They are applied from right to left, it makes more sense to me to apply the model transformation first then view then projection. \$\endgroup\$