[英]How do I draw a triangle using OpenTK?
我不確定為什么這個代碼不是簡單地繪制三角形到屏幕(正交)。 我正在使用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個單位。
為什么這段代碼沒有繪制三角形?
在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);
會把它放在后面。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.