Hey everyone, I'm basically slowly losing my mind
I have the following code:
glBindBuffer(vertices.type(), vertices.id());
glBindBuffer(indices.type(), indices.id());
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 0, nullptr);
glDrawElements(GL_TRIANGLES, indices.size() * 3, GL_UNSIGNED_INT, 0);
glBindBuffer(indices.type(), 0);
glBindBuffer(vertices.type(), 0);
The above code outputs the below image. Vertices are a number of xyzw format vertices, and indices are three-tuples of indices. In the concrete case below, indices.size() is 1, so the call to glDrawElements is
glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_INT, 0);
This is confirmed by glslDevil which shows the above call. How can this possibly yield more than 1 triangle?
This is the cube model I am using (I take the 1-indexation used by obj into consideration when deserializing):
#
# object Box001
#
v -0.2500 -0.2500 -0.7500
v -0.2500 0.2500 -0.7500
v 0.2500 0.2500 -0.7500
v 0.2500 -0.2500 -0.7500
v -0.2500 -0.2500 0.7500
v 0.2500 -0.2500 0.7500
v 0.2500 0.2500 0.7500
v -0.2500 0.2500 0.7500
# 8 vertices
g Box001
#f 1 2 3
#f 3 4 1
#f 5 6 7
#f 7 8 5
#f 1 4 6
#f 6 5 1
f 4 3 7
#f 7 6 4
#f 3 2 8
#f 8 7 3
#f 2 1 5
#f 5 8 2
# 12 faces
I have a vertex shader doing perspective correction that I've cobbled together without really understanding the math, here's the shader used:
#version 430
layout(location = 0) in vec4 position;
uniform mat4 perspective;
void main()
{
vec4 offsetPos = position + vec4(0.5f, 0.5f, 0, 0);
gl_Position = perspective * offsetPos;
}
the uniform perspective above has the following value (when bound):
matrix<4, 4> perspective(float frustumScale, float z_near, float z_far)
{
matrix<4, 4> mat = { 0 };
mat[0][0] = frustumScale;
mat[1][1] = frustumScale;
mat[2][2] = (z_far + z_near) / (z_near - z_far);
mat[3][2] = (2 * z_far * z_near) / (z_near - z_far);
mat[2][3] = -1;
return mat;
}
As far as I know, it should be impossible for the perspective shader to make 3 vertices appear as 4? If I just render all 12 faces for the below cube, it looks alright, but the individual faces (those subject to perspective correction, that wouldn't be visible in an orthogonal projection) all look skewed. What am I doing wrong? Do you need more information? Thanks for any help, I'm really a newbie to graphics programming in general.