我一直关注C++ OpenGL Matrices Tutorial并尝试将其转换为C#OpenTK,而不使用MVP矩阵我在屏幕上绘制了一个红色三角形,但每当我尝试使用它时,我什么都没画或者至少没有无论在哪里我试图放置相机,都能看到任何被绘制的东西。
下面有代码段,但您可以查看整个source code here
这是构建MVP矩阵的C#代码
/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;
然后我们将这个矩阵传递给顶点着色器,vPosition元素是从C#代码传递的vec3顶点
#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);
}
答案 0 :(得分:2)
代码错误:
Matrix4 _model_view_projection = _projection * _view * _model;
必须:
Matrix4 _model_view_projection = _model * _view * _projection;
// For OpenTK, Pencil.Gaming ...
那是因为OpenTK使用行主矩阵(它们被转置)。在C#中,您必须以这种方式乘以矩阵(而不是在着色器中)。