I am trying to understand the model view projection matrix usage since a few days now, and still have some issues i just don't understand. I am using OpenTK (.NET OpenGL wrapper library).
Here is a "working" setup: (it displays the triangle)
// ### setup OpenGL ###
// not sure if this is important; this is basically just so that i can render only in part of a screen because the OpenGL thing is part of a larger project
// it's included just in case...
GL.Enable(EnableCap.ScissorTest);
GL.Viewport(0, 0, (int)windowPosition.Width, (int)windowPosition.Height);
GL.Scissor(x, y, width, height);
// maybe more important
GL.Enable(EnableCap.DepthTest);
GL.Disable(EnableCap.CullFace); // should be default value, but i am not sure if it is
// ### rendering ###
GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
var projection = Matrix4.CreateOrthographic(3, 3, 0.001f, 50);
var view = Matrix4.LookAt(
new Vector3(0.5f, 0.5f, -2),
new Vector3(0.5f, 0.5f, 0),
new Vector3(0, 1, 0));
var viewProjectionMatrix = projection * view;
// set uniforms...
var modelMatrix = ...
// values are: (scaling by factor 2)
// [2 0 0 0]
// [0 2 0 0]
// [0 0 2 0]
// [0 0 0 1]
// triangle vertices
var data = new float[]
{
0, 0, 0,
1, 0, 0,
0, 1, 0
};
// ### vertex shader ###
Vertex Shader:
#version 420
layout(location = 0) in vec3 position;
uniform mat4 viewProjection;
uniform mat4 model;
out vec3 outColor;
void main()
{
gl_Position = viewProjection * model * vec4(position, 1);
outColor = vec3(1,1,1);
}
As I said this setup works. As far as I understand this will create a "camera" placed further down the z axis (which in open gl normally means farther behind), since positive z values point to the viewer by default.
The problem:
To keep the usual OpenGL setup I would like the the camera eye to placed at new Vector3(0.5f, 0.5f, 2) instead of new Vector3(0.5f, 0.5f, -2), but for some reason I can't see the triangle anymore. I drew it on paper and don't get it. As you can see FaceCulling is explicitly disabled, so this can't be the reason.
Maybe I am misunderstanding something.
And just in case the problem is the the Matrix4.LookAt function, here is the matrix it creates (no idea if this makes sense)
with eye-z = -2
view: (eye-z = -2)
(-1, 0, 0, 0)
(0, 1, 0, 0)
(0, 0, -1, 0)
(0.5, -0.5, -2, 1)
projection:
(0.6666667, 0, 0, 0)
(0, 0.6666667, 0, 0)
(0, 0, -0.0400008, 0)
(0, 0, -1.00004, 1)
projection * view:
(-0.6666667, 0, 0, 0)
(0, 0.6666667, 0, 0)
(0, 0, 0.0400008, 0)
(0.5, -0.5, -0.9999601, 1)
view: (eye-z = 2)
(1, 0, 0, 0)
(0, 1, 0, 0)
(0, 0, 1, 0)
(-0.5, -0.5, -2, 1)
projection:
(0.6666667, 0, 0, 0)
(0, 0.6666667, 0, 0)
(0, 0, -0.0400008, 0)
(0, 0, -1.00004, 1)
projection * view:
(0.6666667, 0, 0, 0)
(0, 0.6666667, 0, 0)
(0, 0, -0.0400008, 0)
(-0.5, -0.5, -3.00004, 1)