Viewport是用来设置当前渲染上下文的视口:Viewport(int x, int y, int width, int height)。
其中(x,y)是以像素为单位,指定窗口的左下角位置,width、height是视口矩形的宽度和高度,根据窗口的实时变化重绘窗口。
在OpenGL中,窗口坐标都是以左下角为(0,0),向右为X正方向,向上为Y正方向,如下图所示:
做一个简单的范例,先绘制一个立方体,立方体的六个面颜色不一样:
private void Draw(SharpGL.OpenGL gl)
{
gl.Begin(OpenGL.GL_QUADS); // Start Drawing The Cube
gl.Color(0.0f, 1.0f, 0.0f); // Set The Color To Green
gl.Vertex(1.0f, 1.0f, -1.0f); // Top Right Of The Quad (Top)
gl.Vertex(-1.0f, 1.0f, -1.0f); // Top Left Of The Quad (Top)
gl.Vertex(-1.0f, 1.0f, 1.0f); // Bottom Left Of The Quad (Top)
gl.Vertex(1.0f, 1.0f, 1.0f); // Bottom Right Of The Quad (Top)
gl.Color(1.0f, 0.5f, 0.0f); // Set The Color To Orange
gl.Vertex(1.0f, -1.0f, 1.0f); // Top Right Of The Quad (Bottom)
gl.Vertex(-1.0f, -1.0f, 1.0f); // Top Left Of The Quad (Bottom)
gl.Vertex(-1.0f, -1.0f, -1.0f); // Bottom Left Of The Quad (Bottom)
gl.Vertex(1.0f, -1.0f, -1.0f); // Bottom Right Of The Quad (Bottom)
gl.Color(1.0f, 0.0f, 0.0f); // Set The Color To Red
gl.Vertex(1.0f, 1.0f, 1.0f); // Top Right Of The Quad (Front)
gl.Vertex(-1.0f, 1.0f, 1.0f); // Top Left Of The Quad (Front)
gl.Vertex(-1.0f, -1.0f, 1.0f); // Bottom Left Of The Quad (Front)
gl.Vertex(1.0f, -1.0f, 1.0f); // Bottom Right Of The Quad (Front)
gl.Color(1.0f, 1.0f, 0.0f); // Set The Color To Yellow
gl.Vertex(1.0f, -1.0f, -1.0f); // Bottom Left Of The Quad (Back)
gl.Vertex(-1.0f, -1.0f, -1.0f); // Bottom Right Of The Quad (Back)
gl.Vertex(-1.0f, 1.0f, -1.0f); // Top Right Of The Quad (Back)
gl.Vertex(1.0f, 1.0f, -1.0f); // Top Left Of The Quad (Back)
gl.Color(0.0f, 0.0f, 1.0f); // Set The Color To Blue
gl.Vertex(-1.0f, 1.0f, 1.0f); // Top Right Of The Quad (Left)
gl.Vertex(-1.0f, 1.0f, -1.0f); // Top Left Of The Quad (Left)
gl.Vertex(-1.0f, -1.0f, -1.0f); // Bottom Left Of The Quad (Left)
gl.Vertex(-1.0f, -1.0f, 1.0f); // Bottom Right Of The Quad (Left)
gl.Color(1.0f, 0.0f, 1.0f); // Set The Color To Violet
gl.Vertex(1.0f, 1.0f, -1.0f); // Top Right Of The Quad (Right)
gl.Vertex(1.0f, 1.0f, 1.0f); // Top Left Of The Quad (Right)
gl.Vertex(1.0f, -1.0f, 1.0f); // Bottom Left Of The Quad (Right)
gl.Vertex(1.0f, -1.0f, -1.0f); // Bottom Right Of The Quad (Right)
gl.End(); // Done Drawing The Q
}
效果如下:
然后利用ViewPort,将窗口分成四块区域窗口,分别:左上窗口显示立方体上视图,左下窗口显示立方体下视图,右上窗口显示立方体左视图,右下窗口显示立方体右视图:
private void openGLControl1_OpenGLDraw(object sender, RenderEventArgs args)
{
SharpGL.OpenGL gl = this.openGLControl1.OpenGL;
gl.Clear(OpenGL.GL_COLOR_BUFFER_BIT | OpenGL.GL_DEPTH_BUFFER_BIT);
gl.MatrixMode(OpenGL.GL_PROJECTION);
gl.LoadIdentity();
gl.Perspective(60, (double)(this.Width / this.Height), 0.01, 100.0);
gl.Viewport(0, 0, this.Width, this.Height);
gl.LookAt(eye_x, eye_y, eye_z, 0, 0, 0, 0, 1, 0);
gl.MatrixMode(OpenGL.GL_MODELVIEW);
Draw(gl);//绘制立方体
if (Top_View)//按键w触发显示
Top_View_Show(gl);//绘制上视图
if (Bottom_View)//按键s触发显示
Bottom_View_Show(gl);//绘制下视图
if (Left_view)//按键a触发显示
Left_View_Show(gl);//绘制左视图
if (Right_View)//按键d触发显示
Right_View_Show(gl);//绘制右视图
gl.Flush();
}
不同视图显示程序:
private void Top_View_Show(SharpGL.OpenGL gl)//上视图
{
gl.MatrixMode(OpenGL.GL_PROJECTION);
gl.LoadIdentity();
gl.Perspective(60, (double)(this.Width / this.Height), 0.01, 100.0);
gl.LookAt(0, 0, 5, 0, 0, 0, 0, 1, 0);//设置上视角
gl.MatrixMode(OpenGL.GL_MODELVIEW);
gl.Viewport(0, this.Height / 2, this.Width / 2, this.Height / 2);//设置窗口位置与大小
Draw(gl);//绘制立方体
}
private void Bottom_View_Show(SharpGL.OpenGL gl)//下视图
{
gl.MatrixMode(OpenGL.GL_PROJECTION);
gl.LoadIdentity();
gl.Perspective(60, (double)(this.Width / this.Height), 0.01, 100.0);
gl.LookAt(0, 0, -5, 0, 0, 0, 0, 1, 0);//设置下视角
gl.MatrixMode(OpenGL.GL_MODELVIEW);
gl.Viewport(0, 0, this.Width / 2, this.Height / 2);//设置窗口位置与大小
Draw(gl);//绘制立方体
}
private void Left_View_Show(SharpGL.OpenGL gl)//左视图
{
gl.MatrixMode(OpenGL.GL_PROJECTION);
gl.LoadIdentity();
gl.Perspective(60, (double)(this.Width / this.Height), 0.01, 100.0);
gl.LookAt(-5, 0, 0, 0, 0, 0, 0, 1, 0);//设置左视角
gl.MatrixMode(OpenGL.GL_MODELVIEW);
gl.Viewport(this.Width/2, this.Height/2, this.Width / 2, this.Height / 2);//设置窗口位置与大小
Draw(gl);//绘制立方体
}
private void Right_View_Show(SharpGL.OpenGL gl)//右视图
{
gl.MatrixMode(OpenGL.GL_PROJECTION);
gl.LoadIdentity();
gl.Perspective(60, (double)(this.Width / this.Height), 0.01, 100.0);
gl.LookAt(5, 0, 0, 0, 0, 0, 0, 1, 0);//设置右视角
gl.MatrixMode(OpenGL.GL_MODELVIEW);
gl.Viewport(this.Width / 2, 0, this.Width / 2, this.Height / 2);//设置窗口位置与大小
Draw(gl);//绘制立方体
}
加上几个按键触发,触发各视图显示与隐藏,切换立方体的视角等操作:
private void Key_Press(object sender, KeyPressEventArgs e)
{
if (e.KeyChar =='w')
Top_View = Top_View ? false : true;
else if (e.KeyChar == 's')
Bottom_View = Bottom_View ? false : true;
else if (e.KeyChar == 'a')
Left_view = Left_view ? false : true;
else if (e.KeyChar == 'd')
Right_View = Right_View ? false : true;
if (e.KeyChar == 'i')
eye_y = eye_y + 0.1;
else if (e.KeyChar=='k')
eye_y = eye_y - 0.1;
else if (e.KeyChar=='j')
eye_x = eye_x - 0.1;
else if (e.KeyChar == 'l')
eye_x = eye_x + 0.1;
}
最终效果如下:
测试程序在这里:SharpGL测试范例