我不确定为什么这段代码不是简单地在屏幕上绘制一个三角形(正交)。我正在使用与 OpenGL 1.1 相同的 OpenTK 1.1。
List<Vector3> simpleVertices = new List<Vector3>();
simpleVertices.Add(new Vector3(0,0,0));
simpleVertices.Add(new Vector3(100,0,0));
simpleVertices.Add(new Vector3(100,100,0));
GL.MatrixMode(All.Projection);
GL.LoadIdentity();
GL.MatrixMode(All.Projection);
GL.Ortho(0, 480, 320, 0,0,1000);
GL.MatrixMode(All.Modelview);
GL.LoadIdentity();
GL.Translate(0,0,10);
unsafe
{
Vector3* data = (Vector3*)Marshal.AllocHGlobal(
Marshal.SizeOf(typeof(Vector3)) * simpleVertices.Count);
for(int i = 0; i < simpleVertices.Count; i++)
{
((Vector3*)data)[i] = simpleVertices[i];
}
GL.VertexPointer(3, All.Float, sizeof(Vector3), new IntPtr(data));
GL.DrawArrays(All.Triangles, 0, simpleVertices.Count);
}
代码在绘制函数中的每个更新周期执行一次。我认为我正在做的(但显然不是)是创建一组位置顶点以形成一个三角形并将其绘制在相机前面 10 个单位处。
为什么这段代码没有画三角形?
最佳答案
在OpenGL中,Z轴指向屏幕外,所以当你写的时候
GL.Translate(0,0,10);
它实际上是在屏幕“前面”翻译它。
现在 GL.Ortho
的最后两个参数是 0,1000。这意味着将显示 MINUS Z 方向(= 在相机前面)0 到 1000 之间的所有内容。
换句话说,GL.Translate(0,0,-10);
会将你的对象放在相机前面,而 GL.Translate(0,0,10) ;
会把它放在后面。
关于c# - 如何使用 OpenTK 绘制三角形?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6028565/