namespace sharpGLTest02 { public partial class Form1 : Form { private int mtype = 3; public Form1() { InitializeComponent(); } //初始化 private void openGLControl1_OpenGLInitialized(object sender, EventArgs e) { } //调整 private void openGLControl1_Resize(object sender, EventArgs e) { } //画图 private void openGLControl1_OpenGLDraw(object sender, PaintEventArgs e) { SharpGL.OpenGL gl = this.openGLControl1.OpenGL; gl.ClearColor(0, 0, 0, 1); //清除深度缓存 gl.Clear(OpenGL.GL_COLOR_BUFFER_BIT | OpenGL.GL_DEPTH_BUFFER_BIT); switch (mtype) { case 0: translateSample(gl); break; case 1: rotateSample(gl); break; case 2: scaleSample(gl); break; case 3: originSample(gl); break; } gl.Finish(); gl.Flush(); //强制刷新 } private void originSample(SharpGL.OpenGL gl) { gl.LoadIdentity(); gl.Translate(0f, 0f, -1f); drawPT(gl); //gl.LoadIdentity(); //gl.Translate(0f, 1f, -1f); //drawPT(gl); } private void scaleSample(SharpGL.OpenGL gl) { gl.LoadIdentity(); //缩放 //gl.Scale(1f, 1f, 1f); gl.Scale(1f, 2f, 1f); gl.Translate(0, 0, -9f); drawPT(gl); gl.LoadIdentity(); gl.Scale(5f, 1f, 1f); //gl.Translate(0, 1, -9f); gl.Translate(0, 2, -9f); drawPT(gl); } private void rotateSample(SharpGL.OpenGL gl) { gl.LoadIdentity(); //旋转 gl.Rotate(0,0, 45); gl.Translate(0, 0, -3f); drawPT(gl); //gl.Rotate(0, 0, 45); //gl.Translate(-1, 1,0f); //drawPT(gl); } private void translateSample(SharpGL.OpenGL gl) { //重置当前指定的矩阵为单位矩阵,将当前的用户坐标系的原点移到了屏幕中心 gl.LoadIdentity(); //因为gl.LoadIdentity();将变换点移到原点,坐标轴变换位置到(0, 0, -3),注意此时是相对上面(0,0,0)点定位 gl.Translate(0f, 0f, -3f); drawPT(gl); gl.LoadIdentity(); //gl.Translate(0f, 0.5f, -3f); gl.Translate(0f, 1f, -3f); //gl.Translate(0f, 1.5f, -3f); drawPT(gl); } private void drawPT(SharpGL.OpenGL gl) { gl.PointSize(5f); gl.Begin(OpenGL.GL_TRIANGLES); { gl.Vertex(0.0f, 0f, 0.0f); gl.Vertex(-1.0f, -1f, 0.0f); gl.Vertex(1.0f, -1f, 0.0f); } gl.End(); } //按钮点击事件 private void btnTranslate_Click(object sender, EventArgs e) { switch(((Button)sender).Name) { case "btnTranslate": mtype = 0; break; case "btnRotate": mtype = 1; break; case "btnScale": mtype = 2; break; case "btnOrigin": mtype = 3; break; } } } }