2011-05-17 152 views
3

我不确定为什么这段代码不是简单地画一个三角形来屏幕(正确地)。我使用的OpenTK 1.1与OpenGL 1.1是一样的。如何使用OpenTK绘制三角形?

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); 
} 

该代码在draw函数的每个更新周期中执行一次。我认为自己正在做的事情(但事实上并非如此)正在创建一组位置顶点以形成一个三角形,并在相机前绘制10个单位。

为什么此代码不绘制三角形?

+0

如果将顶部和底部值颠倒为调用Ortho,会发生什么情况:GL.Ortho(0,480,0,320,0,1000);'? – 2011-05-17 09:54:58

+0

什么都没发生...... – tweetypi 2011-05-17 09:58:09

+0

在应用该翻译之前,您应该将模型视图重置为标识。 Otherweise与每个渲染迭代你进一步翻译它。 – datenwolf 2011-05-17 10:26:27

回答

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);会把它放在后面。