0

I'm trying to create a modelview projection matrix for my shader like this:

I'm getting the matrix for the camera translation:

public Matrix4 GetMatrix()
{
    return Matrix4.LookAt(Position, LookingAt, Vector3.UnitY);
}

Postition is the current position of the camera, LookingAt is the target of the camera.

Now I'm assmbling the final matrix for the shader and passing it:

Matrix4 cameraMatrix = camera.GetMatrix();
Matrix4 mvprojectionMatrix = Matrix4.CreatePerspectiveFieldOfView((float)Math.PI / 4, 16f / 9f, 1f, 30f) * cameraMatrix;

GL.UniformMatrix4(mvprojectionMatrixAttribute, false, ref mvprojectionMatrix);

This is the vertex shader (pass through)

[Shader vertex]

#version 150 core
in vec3 pos;
in vec3 color;
uniform float time;
uniform vec3 offset;
uniform mat4 cameraView;
uniform mat4 projection;


out float vTime;
out vec3 vColor;
out mat4 vProjection;
void main() {
    gl_Position = vec4(pos, 1.0);
    vColor = color;
    vTime = time;
    vProjection = projection;
}

I'm passing this to the geometry shader where I'm using them to position my vertices:

void makeVertex(vec3 shift, mat4 rotation)
{
    gl_Position = (gl_in[0].gl_Position + vec4(shift, 0.0) * rotation) * vProjection[0];
    EmitVertex();   
}

the rotation matrix is simply a local rotation of the vertex. vProjection[0] comes from the vertex shader...

Somehow if the camera moves, stuff will quickly leave the view frustum, but the actual view does not move. So I assume the modelview matrix is wrong ( does not translate from camera to world space ). But I'm not sure how to create the correct matrix.

Any tips ? Thanks.

pixartist
  • 1,137
  • 2
  • 18
  • 40
  • This doesn't seem to fit together. In your C# code, you build a model-view-projection matrix, and use it for setting a uniform value. But then your vertex shader declares separate `cameraView` and `projection` matrices. Where `cameraView` is never used. Are you sure that this is a consistent set of code? – Reto Koradi Jul 02 '14 at 02:58
  • Yes, I used both before, now I simply multiply them in C#. So yes, one of them is unused. – pixartist Jul 02 '14 at 16:37

1 Answers1

1

I multiplied the matrices in the wrong order.

pixartist
  • 1,137
  • 2
  • 18
  • 40