Beispiel #1
0
        //控件大小调整事件
        //改变大小时触发,在程序启动时将执行一次
        private void glControl1_Resize(object sender, EventArgs e)
        {
            if (!GLLoaded)//如果没有加载控件,则返回
            {
                return;
            }

            if (glControl1.ClientSize.Height == 0)
            {
                glControl1.ClientSize = new System.Drawing.Size(glControl1.ClientSize.Width, 1);
            }

            GL.Viewport(0, 0, glControl1.ClientSize.Width, glControl1.ClientSize.Height);
            //设置3D绘图画布
            float aspect_ratio = Width / (float)Height;//求得控件宽高比
            //三维透视矩阵
            Matrix4 perpective = Matrix4.CreatePerspectiveFieldOfView(PerpectAngle,
                                                                      aspect_ratio, DisNear, DisFar);

            GL.MatrixMode(MatrixMode.Projection);
            GL.LoadMatrix(ref perpective);//加载透视矩阵
            //创建轨迹球
            _ArcBall = new ArcBall(-Eye_distance, -Eye_distance,
                                   Eye_distance, 0, 0, 0, 0, 0, 1);
            _ArcBall.SetBounds(Width, Height);//设置高宽
        }
Beispiel #2
0
        private void openGLControl1_OpenGLInitialized(object sender, EventArgs e)
        {
            var gl = openGLControl1.OpenGL;

            gl.Viewport(0, 0, openGLControl1.Width, openGLControl1.Height);

            gl.MatrixMode(OpenGL.GL_PROJECTION);
            gl.LoadIdentity();
            gl.Perspective(90, (float)openGLControl1.Width / openGLControl1.Height, 1, 1000);


            gl.LookAt(-200, 0, 50, 0, 0, 0, 0, 0, 1);


            gl.ShadeModel(OpenGL.GL_SMOOTH);
            var mat_specular  = new[] { 1.0f, 1.0f, 1.0f, 1.0f };
            var mat_ambient   = new[] { 0.2f, 0.2f, 0.2f, 0.2f };
            var mat_diffuse   = new[] { 2.0f, 2.0f, 2.0f, 0.1f };
            var mat_shininess = new[] { 100.0f };

            gl.Material(OpenGL.GL_FRONT, OpenGL.GL_SPECULAR, mat_specular);
            gl.Material(OpenGL.GL_FRONT, OpenGL.GL_AMBIENT, mat_ambient);
            gl.Material(OpenGL.GL_FRONT, OpenGL.GL_DIFFUSE, mat_diffuse);
            gl.Material(OpenGL.GL_FRONT, OpenGL.GL_SHININESS, mat_shininess);

            var ambientLight = new[] { 0.2f, 0.2f, 0.2f, 0.2f };
            var diffuseLight = new[] { 1.0f, 1.0f, 1.0f, 1.0f };
            var posLight0    = new[] { -200.0f, 200.0f, 200f, 1.0f };

            gl.Light(OpenGL.GL_LIGHT0, OpenGL.GL_AMBIENT, ambientLight);
            gl.Light(OpenGL.GL_LIGHT0, OpenGL.GL_DIFFUSE, diffuseLight);
            gl.Light(OpenGL.GL_LIGHT0, OpenGL.GL_POSITION, posLight0);

            gl.Enable(OpenGL.GL_LIGHTING);
            gl.Enable(OpenGL.GL_LIGHT0);
            gl.Enable(OpenGL.GL_COLOR_MATERIAL);
            gl.Enable(OpenGL.GL_DEPTH_TEST);
            gl.Enable(OpenGL.GL_DOUBLEBUFFER);

            ArcBall.SetBounds(openGLControl1.Width, openGLControl1.Height);
        }