我在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