第一次开发opengl,本文介绍在windowsform下通过使用glcontrol进行开发的例子,最终结果是显示一个蓝底的窗体和一个黄色的三角形。
1.绘制蓝底的窗体
GL.ClearColor(Color.SkyBlue);
GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
glControl1.SwapBuffers();//交换缓冲区以在前台显示
2.设置绘图窗口投影
private void SetupViewport()
{
int w = glControl1.Width;
int h = glControl1.Height;
GL.MatrixMode(MatrixMode.Projection);
GL.LoadIdentity();
GL.Ortho(0, w, 0, h, -1, 1); // Bottom-left corner pixel has coordinate (0, 0)
GL.Viewport(0, 0, w, h); // Use all of the glControl painting area
}
3.绘制三角形
GL.MatrixMode(MatrixMode.Modelview);
GL.LoadIdentity();
GL.Color3(Color.Yellow);
GL.Begin(BeginMode.Polygon);
GL.Vertex2(10, 20);
GL.Vertex2(100, 20);
GL.Vertex2(100, 50);
GL.Vertex2(20, 100);
GL.End();