我在3D空間中設置我的相機時遇到問題。設置OpenGL相機
這裏是我的代碼:
private void SetupViewPort()
{
GL.Viewport(0, 0, glControl1.Width, glControl1.Height);
GL.MatrixMode(MatrixMode.Projection);
GL.LoadIdentity();
GL.Ortho(0, 1000,0,1000, 0, 1);
GL.MatrixMode(MatrixMode.Modelview);
GL.LoadIdentity();
Vector3d eyePos = new Vector3d(0, 0, 1);
Vector3d point = new Vector3d(500, 500, 0.01);
Vector3d up = new Vector3d(0, 0 , 1);
Matrix4d mat = Matrix4d.LookAt(eyePos, point, up);
//mat.Invert();
GL.LoadMatrix(ref mat);
}
我期待看到我畫到2D平面形狀。但我每次都會看到一個空白屏幕。
這裏就是我的形狀繪製代碼:
private void glControl1_Paint(object sender, PaintEventArgs e)
{
if (!loaded)
return;
GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
GL.Enable(EnableCap.DepthTest);
GL.DepthMask(true);
GL.ClearDepth(1.0);
GL.Color3(Color.Yellow);
GL.Begin(PrimitiveType.Triangles);
GL.Vertex2(0, 0);
GL.Vertex2(0, 600);
GL.Vertex2(600, 600);
GL.Vertex2(100, 100);
GL.Vertex2(50, 70);
GL.Vertex2(200, 100);
GL.End();
glControl1.SwapBuffers();
}
供參考:您在'SetupViewPort'中擁有的所有東西都屬於繪圖代碼。 OpenGL的狀態機能讓你在其他地方設置視口和投影,但從長遠來看,這隻會讓人頭疼。 – datenwolf
一切?我想從第5行開始往下看是屬於繪圖代碼的。 –
一切!尤其是設置視口。在調整大小處理程序中設置視口是您想要使用幀緩衝區對象時最困難的部分。作爲一般規則,*總是*在您需要在繪圖代碼中設置完整視口和投影設置之前。沒有任何成本與其相關,因爲無論如何,必須爲每個繪圖批次發送相關的GPU命令。 – datenwolf