3

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

为什么这段代码不画三角形?

4

1 回答 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);将其放在后面。

于 2011-05-18T18:12:36.930 回答