3.光滑着色和平面着色

发布时间:2019-09-02 07:43:15编辑:auto阅读(1501)

    下面是OpenGLDraw的事件响应函数,相对上节主要增加了对

    public void Color(double red, double green, double blue)
            SharpGL.OpenGL 的成员

    的函数的应用,该函数是个重载函数,目前只用到上面提到的这个形式,即以RGB为参数进行着色。

    下面是OpenGLDraw的事件响应函数:

                    private void openGLControl1_OpenGLDraw(object sender, PaintEventArgs e)
                    {

                            //    Get the OpenGL object, just to clean up the code.
                            SharpGL.OpenGL gl = this.openGLControl1.OpenGL;

                            gl.Clear(OpenGL.COLOR_BUFFER_BIT | OpenGL.DEPTH_BUFFER_BIT);  // Clear The Screen And The Depth Buffer
                            gl.LoadIdentity();          // Reset The View
                            gl.Translate(-1.5f, 0.0f, -6.0f);        // Move Left And Into The Screen

                            gl.Begin(OpenGL.TRIANGLES);          // Start Drawing The Pyramid

                            gl.Color(1.0f, 0.0f, 0.0f);      // Red
                            gl.Vertex(0.0f, 1.0f, 0.0f);      // Top Of Triangle (Front)
                            gl.Color(0.0f, 1.0f, 0.0f);      // Green
                            gl.Vertex(-1.0f, -1.0f, 0.0f);      // Left Of Triangle (Front)
                            gl.Color(0.0f, 0.0f, 1.0f);      // Blue
                            gl.Vertex(1.0f, -1.0f, 0.0f);      // Right Of Triangle (Front)

                            gl.End();            // Done Drawing The Pyramid

                            gl.Translate(3f, 0.0f, 0.0f);        // Move Right And Into The Screen


                            gl.Begin(OpenGL.QUADS);          // Start Drawing The Cube

                            gl.Color(0.5f, 0.5f, 1.0f);      // Set The Color To Green
                            gl.Vertex(-1.0f, 1.0f, 0.0f);      // Top Right Of The Quad (Top)
                            gl.Vertex(1.0f, 1.0f, 0.0f);      // Top Left Of The Quad (Top)
                            gl.Vertex(1.0f, -1.0f, 0.0f);      // Bottom Left Of The Quad (Top)
                            gl.Vertex(-1.0f, -1.0f, 0.0f);      // Bottom Right Of The Quad (Top)


                            gl.End();            // Done Drawing The Q

                            gl.Flush();  
                    }

    显示效果如下图:

     

    源代码

关键字