0
votes

I have been following the C++ OpenGL Matrices Tutorial and attempting to translate it into C# OpenTK, without using a MVP matrix I get a red triangle drawn on screen but whenever I try to use it I get nothing draw or at least a connot SEE anything being drawn, no matter where I try to place the camera.

There are code snippets below but you can view the whole source code here

This is the C# code to build the MVP Matrix

/Attempt to build our model view projection matrix

//We use an Identity matrix for the model
Matrix4 _model = Matrix4.Identity;

Matrix4 _projection = Matrix4.CreatePerspectiveFieldOfView(1.0f, 4.0f / 3.0f, 0.1f, 100.0f);

//Camera will be as 4,3,3 looking at origin, and set up direction to 1
Matrix4 _view = Matrix4.LookAt(new Vector3(4,3,3), new Vector3(0,0,0), new Vector3(0,1,0));

//combine into our model view projection
Matrix4 _model_view_projection = _projection * _view * _model;

Then we pass this matrix into the vertex shader, the vPosition element are vec3 vertices passed from the C# code

#version 330

uniform mat4 viewProjection;

in vec3 vPosition;

void main()
{
    //This line doesn't work
    gl_Position = viewProjection * vec4(vPosition, 1);

    //This line works
    //gl_Position = vec4(vPosition, 1);
}
1
How you pass the Matrix to the shader?Sometimes it is better to pass them one after one and combine them in your vertex shadergreedsin
And your view Matrix has an angle of 1 ,you maybe set it between 45 and 60greedsin
@lolio Will try that. Also, the fov parameter is in radians so 1.0f is roughly 60 degreesTom Gothorp

1 Answers

4
votes

Error in code:

Matrix4 _model_view_projection = _projection * _view * _model;

Must be:

Matrix4 _model_view_projection = _model * _view * _projection;
// For OpenTK, Pencil.Gaming ...

That's because OpenTK use row-major matrices (they are transposed). In C# you must multiply matrices in this way (not in the shaders).