13, thirteen examples: Examples of the light source: light surround quadric sphere (II)

namespace sharpGLTest13
{
    public partial class Form1 : Form
    {
        private float rotation = 0.0f;
        float m_bReadX, m_bReadY;
        float m_bGreenX, m_bGreenY;
        float m_bBlueX, m_bBlueY;

        //3个光源位置
        float[] lightPosR = new float[] { 0f, 0f, 2f, 1f };
        float[] lightPosG = new float[] { 0f, 0f, 2f, 1f };
        float[] lightPosB = new float[] { 0f, 0f, 2f, 1f };

        //3 source diffused light 
        a float [] diffLightR = {1F, 0F, 0F, 1F};
         a float [] diffLightG = {0F, 1F, 0F, 1F};
         a float [] diffLightB = {0F, 0F, 1F, 1F}; 

        // definition of three specular light source I 
        a float [] specLightR = {1F, 0F, 0F, 1F};
         a float [] specLightG = {0F, 1F, 0F, 1F};
         a float [] specLightB = {0F, 0F, 1F , 1F}; 

        // default source, light gray, default lighting for 
        a float [] = defDiffLight new new  a float [] { 0.8 F , 0.8 F , 0.8 F , 1F};
         a float[] defSpecLight = new float[] { 1f, 1f, 1f, 1f };
        float[] defLightPos = new float[] { 0f, 0f, 10f, 1f };

        public Form1()
        {
            InitializeComponent();
        }

        private void openGLControl_OpenGLInitialized(object sender, EventArgs e)
        {
            OpenGL gl = openGLControl.OpenGL;
            setLight(gl);
            //gl.Enable(OpenGL.GL_NORMALIZE);
            gl.ClearColor(0, 0, 0, 0);
        }
        private void setLight(OpenGL gl)
        {
            //0号灯光,默认灯光
            gl.Light(OpenGL.GL_LIGHT0, OpenGL.GL_DIFFUSE, defDiffLight);
            gl.Light(OpenGL.GL_LIGHT0, OpenGL.GL_SPECULAR, defSpecLight);
            gl.Light(OpenGL.GL_LIGHT0, OpenGL.GL_POSITION, defLightPos);

            //1号灯光
            gl.Light(OpenGL.GL_LIGHT1, OpenGL.GL_DIFFUSE, diffLightR);
            gl.Light(OpenGL.GL_LIGHT1, OpenGL.GL_SPECULAR, specLightR);
            gl.Light(OpenGL.GL_LIGHT1, OpenGL.GL_POSITION, lightPosR);

            //2号灯光
            gl.Light(OpenGL.GL_LIGHT2, OpenGL.GL_DIFFUSE, diffLightG);
            gl.Light(OpenGL.GL_LIGHT2, OpenGL.GL_SPECULAR, specLightG);
            gl.Light(OpenGL.GL_LIGHT2, OpenGL.GL_POSITION, lightPosG);

            //3号灯光
            gl.Light(OpenGL.GL_LIGHT3, OpenGL.GL_DIFFUSE, diffLightB);
            gl.Light(OpenGL.GL_LIGHT3, OpenGL.GL_SPECULAR, specLightB);
            gl.Light(OpenGL.GL_LIGHT3, OpenGL.GL_POSITION, lightPosB);

            gl.Enable(OpenGL.GL_LIGHTING);
            gl.Enable(OpenGL.GL_LIGHT0);  //启用默认光源

        }

        private void openGLControl_Resized(object sender, EventArgs e)
        {
            OpenGL gl = openGLControl.OpenGL;
            gl.MatrixMode(OpenGL.GL_PROJECTION);
            gl.LoadIdentity();
            gl.Perspective(70.0f, (double)Width / (double)Height, 0.01, 100.0);
            gl.LookAt(-5, 5, -5, 0, 0, 0, 0, 1, 0);
            gl.MatrixMode(OpenGL.GL_MODELVIEW);
        }

        private void openGLControl_OpenGLDraw(object sender, PaintEventArgs e)
        {
            OpenGL gl = openGLControl.OpenGL;
            gl.Clear(OpenGL.GL_COLOR_BUFFER_BIT | OpenGL.GL_DEPTH_BUFFER_BIT);
            gl.LoadIdentity();
            gl.Rotate(rotation, 0.0f, 1.0f, 0.0f);

            draw(gl);
            rotation += 3.0f;

            update(gl);
        }

        void update(OpenGL gl)
        {
            gl.Enable(OpenGL.GL_LIGHT1);
            m_bReadX += 16;
            m_bReadY += 12;
            gl.Enable (OpenGL.GL_LIGHT2); 
            m_bGreenX += 10 ; 
            m_bGreenY + = . 6 ; 
            gl.Enable (OpenGL.GL_LIGHT3); 
            m_bBlueX + = 2 ; 
            m_bBlueY + = . 4 ; 
        } 

        void Draw (the OpenGL GL) 
        { 
            gl.PushMatrix (); 
            // rotation red 
            gl.Rotate (m_bReadX, 1F, 0F, 0F); 
            gl.Rotate (m_bReadY, 0F, 1F, 0F); 
            // set the position of the red 
            gl.Light (OpenGL.GL_LIGHT1, OpenGL.GL_POSITION, lightPosR);
             // draw photosphere 
            gl. Translate (lightPosR [ 0 ], lightPosR [. 1 ], lightPosR [ 2 ]); 
            gl.Color (1F, 0F, 0F); 
            gl.PushAttrib (OpenGL.GL_LIGHTING_BIT); 
            gl.Disable (OpenGL.GL_LIGHTING); 
            drawSphere (GL, lightPosR [ 0 ], lightPosR [ . 1 ], lightPosR [ 2 ], 0.2f , 10 , 10 , to false ); 
            gl.Enable (OpenGL.GL_LIGHTING); 
            gl.PopAttrib (); 
            gl.PopMatrix (); 


            gl.PushMatrix (); 
            // rotation green 
            gl.Rotate(m_bGreenX, 1f, 0f, 0f);
            gl .Rotate (m_bGreenY, 0F, 1F, 0F); 
            // set the position of the green
             gl.Light (OpenGL.GL_LIGHT2, OpenGL.GL_POSITION, lightPosG);
             // draw photosphere 
            gl.Translate (lightPosG [ 0 ], lightPosG [ . 1 ], lightPosG [ 2 ]); 
            gl.Color (0F, 1F, 0F); 
            gl.PushAttrib (OpenGL.GL_LIGHTING_BIT); 
            gl.Disable (OpenGL.GL_LIGHTING); 
            drawSphere (GL, lightPosG [ 0 ], lightPosG [ . 1 ], lightPosG [ 2 ], 0.2f , 10 , 10 , to false );
            gl.Enable (OpenGL.GL_LIGHTING); 
            gl.PopAttrib();
            gl.PopMatrix (); 


            gl.PushMatrix (); 
            // rotation blue 
            gl.Rotate (m_bBlueX, 1F, 0F, 0F); 
            gl.Rotate (m_bBlueY, 0F, 1F, 0F) ; 
            // set the position of the blue light 
            gl.Light (OpenGL.GL_LIGHT3, OpenGL.GL_POSITION, lightPosB);
             // draw photosphere 
            gl.Translate (lightPosB [ 0 ], lightPosB [ . 1 ], lightPosB [ 2 ]); 
            gl.Color (0F, 0F, 1F); 
            gl.PushAttrib (OpenGL.GL_LIGHTING_BIT); 
            gl.Disable (OpenGL.GL_LIGHTING); 
            drawSphere (GL, lightPosB [ 0 ], lightPosB [ . 1 ], lightPosB [2], 0.2f, 10, 10, false);
            gl.Enable(OpenGL.GL_LIGHTING);
            gl.PopAttrib();
            gl.PopMatrix();


            //绘制球体
            gl.PushMatrix();
            gl.Rotate(rotation, 1f, 0f, 0f);
            gl.Rotate(rotation, 0f, 1f, 0f);
            gl.Rotate(rotation, 0f, 0f, 1f);
            drawSphere(gl, 0, 0, 0, 3, 40, 40, false);

            gl.PopMatrix (); 

            gl.Flush (); 

        } 
        // quadric spherical 
        void drawSphere (the OpenGL GL, a float X, a float Y, a float Z, Double RADIUS, int SEGx, int SEGY, BOOL isLines) 
        { 
            gl.PushMatrix (); 
            gl.Translate (X, Y, Z); 
            var Sphere = gl.NewQuadric (); 

            / * 
             * QuadricDrawStyle (IntPtr quadObject, uint DrawStyle); 
             * the first parameter is a pointer to the object state quadratic 
             * of enumeration value of two parameters: GLU_FILL (quadratic objects drawn entity), GLU_LINE (drawn as wireframe objects quadratic) 
             * GLU_POINT (quadratic objects Videos set into a set of vertices), GLU_SILHOUETTE (similar to the frame, adjacent sides of the polygon but not drawn) 
             ** / 
            IF (isLines) 
                gl.QuadricDrawStyle (Sphere, OpenGL.GL_LINES); 
            the else 
                gl.QuadricDrawStyle (Sphere, OpenGL.GL_QUADS); 

            / * 
             * QuadricNormals (IntPtr quadricObject, uint Normals); 
             * This function specifies how objects quadratic generate normals. 
             * The second parameter may be: GLU_NONE not generate normals, GLU_FLAT flat normals, GLU_SMOOTH normal smoothing. 
             * 
             * * / 
            Gl.QuadricNormals (Sphere, OpenGL.GLU_NONE);    // GLU_NONE, GLU_FLAT, GLU_SMOOTH 

            / * 
             * QuadricOrientation (IntPtr quadricObject, int Orientation); 
             * This function can be specified normals are facing, inside or just outside the point . 
             * Orientation may be GLU_OUTSIDE or GLU_INSIDE these two values. 
             * OpenGL default is GL_CCW counterclockwise direction is positive 
             * 
             * * / 
            gl.QuadricOrientation (Sphere, ( int ) OpenGL.GLU_OUTSIDE);   // GLU_OUTSIDE, GLU_INSIDE 

            / * 
             * QuadricTexture (IntPtr quadricObject, int textureCoords) 
             * This function can quadratic surface texture coordinates designated 
             * textureCoords this parameter can be GL_TRUE or GL_FALSE,. 
             * as for the sphere and cylinder generating texture coordinates, texture symmetrically around the surface of the sphere and cylinder 
             on if applied to the disc * , then the texture of the center is the center of the disk, and then expanded to linear interpolation manner to the boundary of the disc 
             * 
             ** / 
            Gl.QuadricTexture (Sphere, ( int) OpenGL.GLU_FALSE);   // GL_TRUE, GLU_FALSE 
            gl.Sphere (Sphere, RADIUS, SEGx, SEGY); 
            gl.DeleteQuadric (Sphere); 
            gl.PopMatrix (); 
        } 

        / * 
         * a point Circle 
         * 
         * * / 
        // sphere center coordinates (x, y, z), for the radius of the sphere radius, M, N respectively longitudinal cross-sphere is divided into many parts 
        void drawSphere1 (the OpenGL GL, a float XX, a float YY, a float ZZ, a float rADIUS, a float M, a float N, BOOL isLines) 
        { 
            const  a float the PI = 3.1415926f;
            float step_z = (float)Math.PI / M;
            float step_xy = 2 * PI / N;
            float[] x = new float[4] { 0, 0, 0, 0 };
            float[] y = new float[4] { 0, 0, 0, 0 };
            float[] z = new float[4] { 0, 0, 0, 0 };

            float angle_z = 0.0f;
            float angle_xy = 0.0f;
            int i = 0, j = 0;
            gl.Begin(OpenGL.GL_QUADS);
            for (i = 0; i < M; i++)
            {
                angle_z = i * step_z;
                for (j = 0; j < N; j++)
                {
                    angle_xy = j * step_xy;

                    x[0] = (float)(radius * Math.Sin(angle_z) * Math.Cos(angle_xy));
                    y[0] = (float)(radius * Math.Sin(angle_z) * Math.Sin(angle_xy));
                    z[0] = (float)(radius * Math.Cos(angle_z));

                    x[1] = (float)(radius * Math.Sin(angle_z + step_z) * Math.Cos(angle_xy));
                    y[1] = (float)(radius * Math.Sin(angle_z + step_z) * Math.Sin(angle_xy));
                    z[1] = (float)(radius * Math.Cos(angle_z + step_z));

                    x[2] = (float)(radius * Math.Sin(angle_z + step_z) * Math.Cos(angle_xy + step_xy));
                    y[2] = (float)(radius * Math.Sin(angle_z + step_z) * Math.Sin(angle_xy + step_xy));
                    z[2] = (float)(radius * Math.Cos(angle_z + step_z));

                    x[3] = (float)(radius * Math.Sin(angle_z) * Math.Cos(angle_xy + step_xy));
                    y[3] = (float)(radius * Math.Sin(angle_z) * Math.Sin(angle_xy + step_xy));
                    z[3] = (float)(radius * Math.Cos(angle_z));

                    for (int k = 0; k < 4; k++)
                    {
                        gl.Vertex(xx + x[k], yy + y[k], zz + z[k]);
                    }
                }
            }
            gl.End();
        }
    }
}

 

Guess you like

Origin www.cnblogs.com/lotuses/p/11360292.html