如何使用OpenTK绘制三角形?

3

我不确定为什么这段代码不能简单地在屏幕上绘制一个三角形(正交)。我正在使用OpenTK 1.1,它与OpenGL 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个单位处。

为什么这段代码没有绘制出三角形?


如果您将调用Ortho的顶部和底部值颠倒,会发生什么情况:GL.Ortho(0, 480, 0, 320, 0, 1000); - Jackson Pope
在应用翻译之前,您应该将模型视图重置为标识。否则,在每个渲染迭代中,您都会进一步翻译它。 - datenwolf
你的意思是在GL.MatrixMode(All.ModelView)之后加入'GL.LoadIdentity()'吗?我现在添加了这个... - meds
1个回答

1
在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);会将其放在相机后面。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接