Hello

I wish to display a rectangle in OpenGL by passing vertex coordinates and the MVP matrix into the vertex shader (then fragment shader).

My rectangle vertices are the usual (-0.5, 0.5, 0), (0.5, 0.5, 0), (-0.5, -0.5, 0) and (0.5, -0.5, 0)

My model matrix is 4x4 identity.

My camera sits at (0,0,1) and has the rotation of (0,0,0) (pitch, yaw, roll) so looks towards the negative z-axis.

Hence my view matrix is

(1, 0, 0, 0,

0, 1, 0, 0,

0, 0, 1, -1,

0, 0, 0, 1)

Setting the MVP matrix to view * model produces a coloured rectangle in correct NDC coordinates.

However, when I introduce the projection matrix - generated by .setPerspective(3.14159 / 2, width / height, 0.1, 100) - and set MVP = projection * view * model, the rectangle disappears entirely.

Passing data is not the issue since if my projection matrix is also an identity, the rectangle reappears.

I have no idea what I am doing wrong. I have tried numerous implementations of the projection matrix which always resulted in producing no graphical output (which raises the suspicion of the fault not lying in the projection matrix...)

Here is my vertex shader:

`#version 330 core`

in vec3 in_Position;

uniform mat4 u_MVP;

out vec3 colour;

void main(void){

gl_Position = u_MVP * vec4(in_Position, 1.0);

colour = vec3((in_Position.x+1) / 2, 1, (in_Position.y+1) / 2);

}

And my fragment shader:

`#version 330 core`

in vec3 colour;

out vec4 out_Colour;

void main(void){

out_Colour = vec4(colour, 1.0);

}

My render function:

` //enable shaders & bind to rectangle data`

shader.start();

glBindVertexArray(vaos.get(0));

//MVP

Matrix4f model = shape.getModelTransform(); //assume identity

Matrix4f view = camera.getViewTransform(); //assume translation -1 in z

Matrix4f projection = new Matrix4f();

projection.setPerspective((float) Math.PI / 2, (float) width / (float) height, 0.1f, 100f);

Matrix4f transform = projection.mul(view.mul(model));

//pass MVP & render indexed vertices

glUniformMatrix4fv(u_MVP, false, transform.get(BufferUtils.createFloatBuffer(16)));

glDrawElements(GL_TRIANGLES, shape.getPoints().length * 3, GL_UNSIGNED_INT, 0);

//close renderer

glBindVertexArray(0);

shader.stop();

If someone could please clarify how to implement the correct projection matrix or point out other mistakes I have made, I'd be grateful.

P.S.

When P is identity, a camera translation actually results in the rectangle being distorted but I believe that's because there is no correct P matrix to rectify it. Correct me if I'm wrong