Exemplo n.º 1
0
        void IRenderable.Render(OpenGL gl, RenderMode renderMode)
        {
            if (positionBuffer != null && colorBuffer != null && radiusBuffer != null)
            {
                if (this.shaderProgram == null)
                {
                    this.shaderProgram = InitShader(gl, renderMode);
                }
                if (this.vertexArrayObject == null)
                {
                    CreateVertexArrayObject(gl, renderMode);
                }

                BeforeRendering(gl, renderMode);

                if (this.RenderGrid && this.vertexArrayObject != null)
                {
                    gl.Enable(OpenGL.GL_BLEND);
                    gl.BlendFunc(SharpGL.Enumerations.BlendingSourceFactor.SourceAlpha, SharpGL.Enumerations.BlendingDestinationFactor.OneMinusSourceAlpha);

                    gl.BindVertexArray(this.vertexArrayObject[0]);
                    gl.DrawArrays(OpenGL.GL_POINTS, 0, count);
                    gl.BindVertexArray(0);

                    gl.Disable(OpenGL.GL_BLEND);
                }

                AfterRendering(gl, renderMode);
            }
        }
Exemplo n.º 2
0
        private void InitializeOpenGL()
        {
            _openGL = new OpenGL();
            _openGL.Create(OpenGLVersion.OpenGL2_1, RenderContextType.FBO, Canvas.Width, Canvas.Height, 32, null);
            //_openGL.Viewport(0, 0, Canvas.Width, Canvas.Height);

            _openGL.ShadeModel(OpenGL.GL_SMOOTH);
            _openGL.ClearColor(1.0f, 0.0f, 0.0f, 1.0f);
            _openGL.ClearDepth(1.0f);
            _openGL.Enable(OpenGL.GL_DEPTH_TEST);
            _openGL.DepthFunc(OpenGL.GL_LEQUAL);
            _openGL.Hint(OpenGL.GL_PERSPECTIVE_CORRECTION_HINT, OpenGL.GL_NICEST);

            _openGL.Enable(OpenGL.GL_BLEND);
            _openGL.BlendFunc(OpenGL.GL_SRC_ALPHA, OpenGL.GL_ONE_MINUS_SRC_ALPHA);
            //_openGL.BlendFunc(BlendingSourceFactor.SourceAlpha, BlendingDestinationFactor.DestinationAlpha);
            _openGL.Enable(OpenGL.GL_LINE_SMOOTH);
            _openGL.Hint(OpenGL.GL_LINE_SMOOTH_HINT, OpenGL.GL_NICEST);

            _openGL.Clear(OpenGL.GL_DEPTH_BUFFER_BIT);
            //_openGL.Ortho2D(0, Canvas.Width, Canvas.Height, 0);
            _openGL.Viewport(0, 0, Canvas.Width, Canvas.Height);
            _openGL.MatrixMode(MatrixMode.Projection);
            _openGL.LoadIdentity();
            _openGL.Ortho(-1, 1, -1, 1, -1, 1);
        }
Exemplo n.º 3
0
        public static void DrawTriangle(OpenGL gl, Vector3 a, Vector3 b, Vector3 c, Color32 color, bool usePolygonOffset)
        {
            gl.PushAttrib(OpenGL.GL_ENABLE_BIT);
            gl.Disable(OpenGL.GL_LIGHTING);
            gl.Disable(OpenGL.GL_TEXTURE_2D);
            gl.Enable(OpenGL.GL_COLOR_MATERIAL);
            gl.ColorMaterial(OpenGL.GL_FRONT_AND_BACK, OpenGL.GL_AMBIENT_AND_DIFFUSE);
            gl.BlendFunc(OpenGL.GL_SRC_ALPHA, OpenGL.GL_ONE_MINUS_SRC_ALPHA);
            gl.Enable(OpenGL.GL_BLEND);

            if (usePolygonOffset)
            {
                gl.Enable(OpenGL.GL_POLYGON_OFFSET_FILL);
                gl.PolygonOffset(-1.0f, -1.0f);
            }

            gl.Begin(OpenGL.GL_TRIANGLES);

            gl.Color(color.R_Double, color.G_Double, color.B_Double, color.A_Double);

            gl.Vertex(a.x, a.y, a.z);
            gl.Vertex(b.x, b.y, b.z);
            gl.Vertex(c.x, c.y, c.z);

            gl.End();

            gl.PopAttrib();
        }
Exemplo n.º 4
0
        private void preGL2D(OpenGL gl, int width, int height)
        {
            gl.DrawBuffer(OpenGL.GL_FRONT);
            gl.Viewport(0, 0, width, height);

            gl.MatrixMode(OpenGL.GL_PROJECTION);
            gl.LoadIdentity();
            gl.Ortho(0, width, 0, height, -1, 1);

            gl.MatrixMode(OpenGL.GL_MODELVIEW);
            gl.LoadIdentity();

            // Передвижение полотна при зажатом колёсике
            gl.Translate(canvasPositionX, -canvasPositionY, 0);

            gl.ShadeModel(OpenGL.GL_SMOOTH);
            gl.PixelStore(OpenGL.GL_UNPACK_ALIGNMENT, 4);
            gl.Hint(OpenGL.GL_PERSPECTIVE_CORRECTION_HINT, OpenGL.GL_NICEST);

            gl.Enable(OpenGL.GL_DEPTH_TEST);
            gl.Disable(OpenGL.GL_CULL_FACE);
            gl.Enable(OpenGL.GL_BLEND);

            gl.BlendFunc(OpenGL.GL_SRC_ALPHA, OpenGL.GL_ONE_MINUS_SRC_ALPHA);
            gl.Enable(OpenGL.GL_LINE_SMOOTH);
            gl.Hint(OpenGL.GL_LINE_SMOOTH_HINT, OpenGL.GL_NICEST);

            gl.ClearColor(1, 1, 1, 1);
            gl.ClearStencil(0);
            gl.ClearDepth(1.0);

            gl.DepthFunc(OpenGL.GL_LEQUAL);

            gl.Clear(OpenGL.GL_DEPTH_BUFFER_BIT | OpenGL.GL_COLOR_BUFFER_BIT);
        }
Exemplo n.º 5
0
        // Fill rectangle to given coordinates, Z assumed 0. Expected to be used when in Ortho mode.
        // Ensure states option is to ensure all enable/disable states to be set before drawing, if false then assumed that correct states are set (optimization)
        public static void FillRectangle(OpenGL gl, Vector2 topLeft, Vector2 bottomRight, Color32 color, bool ensureStates)
        {
            if (ensureStates)
            {
                gl.PushAttrib(OpenGL.GL_ENABLE_BIT);
                gl.Disable(OpenGL.GL_LIGHTING);
                gl.Disable(OpenGL.GL_TEXTURE_2D);
                gl.Enable(OpenGL.GL_COLOR_MATERIAL);
                gl.Enable(OpenGL.GL_BLEND);
                gl.ColorMaterial(OpenGL.GL_FRONT_AND_BACK, OpenGL.GL_AMBIENT_AND_DIFFUSE);
                gl.BlendFunc(OpenGL.GL_SRC_ALPHA, OpenGL.GL_ONE_MINUS_SRC_ALPHA);
            }

            gl.Begin(OpenGL.GL_QUADS);

            gl.Color(color.R_Double, color.G_Double, color.B_Double, color.A_Double);

            gl.Vertex(topLeft.x, topLeft.y);
            gl.Vertex(bottomRight.x, topLeft.y);
            gl.Vertex(bottomRight.x, bottomRight.y);
            gl.Vertex(topLeft.x, bottomRight.y);
            gl.End();

            if (ensureStates)
            {
                gl.PopAttrib();
            }
        }
        /// <summary>
        /// Sets the attributes.
        /// </summary>
        /// <param name="gl">The OpenGL instance.</param>
        public override void SetAttributes(OpenGL gl)
        {
            if (enableAlphaTest.HasValue && alphaTestFunction.HasValue && alphaTestReferenceValue.HasValue)
            {
                gl.EnableIf(OpenGL.GL_ALPHA_TEST, enableAlphaTest.Value);
                gl.AlphaFunc(alphaTestFunction.Value, alphaTestReferenceValue.Value);
            }

            if (enableBlend.HasValue && blendingSourceFactor.HasValue && blendingDestinationFactor.HasValue)
            {
                gl.EnableIf(OpenGL.GL_BLEND, enableBlend.Value);
                gl.BlendFunc(blendingSourceFactor.Value, blendingDestinationFactor.Value);
            }

            if (enableDither.HasValue)
            {
                gl.EnableIf(OpenGL.GL_DITHER, enableDither.Value);
            }
            if (drawBufferMode.HasValue)
            {
                gl.DrawBuffer(drawBufferMode.Value);
            }

            if (enableLogicOp.HasValue && logicOp.HasValue)
            {
                gl.EnableIf(OpenGL.GL_COLOR_LOGIC_OP, enableLogicOp.Value);
                gl.LogicOp(logicOp.Value);
            }
            if (colorModeClearColor != null)
            {
                gl.ClearColor(colorModeClearColor.R, colorModeClearColor.G, colorModeClearColor.B, colorModeClearColor.A);
            }
            //todowritemask
        }
Exemplo n.º 7
0
        public static uint RegisterTexture(Bitmap textureImage)
        {
            uint[] textureID = new uint[1];

            // Initiate texture
            gl.Enable(OpenGL.GL_TEXTURE_2D);

            // Get texture ID and save it in the array
            gl.GenTextures(1, textureID);

            gl.BindTexture(OpenGL.GL_TEXTURE_2D, textureID[0]);

            // Enable transparency
            gl.Enable(OpenGL.GL_BLEND);
            gl.BlendFunc(OpenGL.GL_SRC_ALPHA, OpenGL.GL_ONE_MINUS_SRC_ALPHA);

            // Tells OpenGL where are the image pixels/data
            IntPtr pixels = textureImage.LockBits(new Rectangle(0, 0, textureImage.Width, textureImage.Height), ImageLockMode.ReadOnly, textureImage.PixelFormat).Scan0;

            gl.TexImage2D(OpenGL.GL_TEXTURE_2D, 0, (int)OpenGL.GL_RGBA, textureImage.Width, textureImage.Height, 0, OpenGL.GL_BGRA, OpenGL.GL_UNSIGNED_BYTE, pixels);

            gl.TexParameter(OpenGL.GL_TEXTURE_2D, OpenGL.GL_TEXTURE_MIN_FILTER, OpenGL.GL_LINEAR);
            gl.TexParameter(OpenGL.GL_TEXTURE_2D, OpenGL.GL_TEXTURE_MAG_FILTER, OpenGL.GL_LINEAR);

            return(textureID[0]);
        }
Exemplo n.º 8
0
        public override void drawFilled(OpenGL gl)
        {
            if (!initialized)
            {
                init(gl);
            }

            gl.Enable(OpenGL.GL_TEXTURE_2D);
            gl.BindTexture(OpenGL.GL_TEXTURE_2D, texture.TextureName);

            gl.Enable(OpenGL.GL_BLEND);
            gl.BlendFunc(OpenGL.GL_SRC_ALPHA, OpenGL.GL_ONE_MINUS_SRC_ALPHA);
            
            gl.Begin(OpenGL.GL_QUADS);
            gl.TexCoord(0.0f, 1.0f); gl.Vertex(Pos.X, Pos.Y, Pos.Z);                    // Bottom Left Of The Texture and Quad
            gl.TexCoord(1.0f, 1.0f); gl.Vertex(Pos.X + Size.X, Pos.Y, Pos.Z);           // Bottom Right Of The Texture and Quad
            gl.TexCoord(1.0f, 0.0f); gl.Vertex(Pos.X + Size.X, Pos.Y + Size.Y, Pos.Z);  // Top Right Of The Texture and Quad
            gl.TexCoord(0.0f, 0.0f); gl.Vertex(Pos.X, Pos.Y + Size.Y, Pos.Z);           // Top Left Of The Texture and Quad
            gl.End();

            gl.Disable(OpenGL.GL_BLEND);
            gl.Disable(OpenGL.GL_TEXTURE_2D);
            gl.BindTexture(OpenGL.GL_TEXTURE_2D, 0);

            //this.drawBorder(gl);
        }
Exemplo n.º 9
0
        /// <summary>
        /// Handles the OpenGLInitialized event of the openGLControl control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void openGLControl_OpenGLInitialized(object sender, EventArgs e)
        {
            //  Get the OpenGL object.
            OpenGL gl = openGLControl.OpenGL;

            //  Set the clear color.
            gl.ClearColor(0, 0, 0, 0);

            // light
            float[] light    = { 1.0f, 1.0f, 1.0f, 0.8f };
            float[] amblight = { 0.5f, 0.5f, 0.5f, 0.8f };
            gl.LightModel(LightModelParameter.Ambient, amblight);
            gl.Light(OpenGL.GL_LIGHT0, OpenGL.GL_POSITION, light);
            gl.Light(OpenGL.GL_LIGHT0, OpenGL.GL_DIFFUSE, light);
            gl.Light(OpenGL.GL_LIGHT0, OpenGL.GL_SPECULAR, light);
            gl.ShadeModel(OpenGL.GL_SMOOTH);

            gl.Enable(OpenGL.GL_LIGHTING);
            gl.Enable(OpenGL.GL_LIGHT0);
            gl.Enable(OpenGL.GL_DEPTH_TEST);
            gl.Enable(OpenGL.GL_LINE_SMOOTH);
            gl.Hint(HintTarget.LineSmooth, HintMode.Nicest);
            gl.Enable(OpenGL.GL_BLEND);
            gl.BlendFunc(BlendingSourceFactor.SourceAlpha, BlendingDestinationFactor.OneMinusSourceAlpha);
            //gl.Enable(OpenGL.GL_NORMALIZE);
        }
Exemplo n.º 10
0
        /// <summary>
        /// Sets the attributes.
        /// </summary>
        /// <param name="gl">The OpenGL instance.</param>
        public override void SetAttributes(OpenGL gl)
        {
            if(enableAlphaTest.HasValue && alphaTestFunction.HasValue && alphaTestReferenceValue.HasValue) 
            {
                gl.EnableIf(OpenGL.GL_ALPHA_TEST, enableAlphaTest.Value);
                gl.AlphaFunc(alphaTestFunction.Value, alphaTestReferenceValue.Value);
            }
            
            if(enableBlend.HasValue && blendingSourceFactor.HasValue && blendingDestinationFactor.HasValue) 
            {
                gl.EnableIf(OpenGL.GL_BLEND, enableBlend.Value);
                gl.BlendFunc(blendingSourceFactor.Value, blendingDestinationFactor.Value);
            }

            if(enableDither.HasValue) gl.EnableIf(OpenGL.GL_DITHER, enableDither.Value);
            if(drawBufferMode.HasValue) gl.DrawBuffer(drawBufferMode.Value);
            
            if(enableLogicOp.HasValue && logicOp.HasValue) 
            {
                gl.EnableIf(OpenGL.GL_COLOR_LOGIC_OP, enableLogicOp.Value);
                gl.LogicOp(logicOp.Value);
            }
            if(colorModeClearColor != null) gl.ClearColor(colorModeClearColor.R, colorModeClearColor.G, colorModeClearColor.B, colorModeClearColor.A);
            //todowritemask
        }
Exemplo n.º 11
0
        //private PostProcesser _postProcesser;


        public OpenGLStaticRenderer(int width, int height)
        {
            Width  = width;
            Height = height;

            Text = "";
            lock (gl)
            {
                //  Create OpenGL.
                gl.Create(OpenGLVersion.OpenGL2_1, RenderContextType.FBO, Width, Height, 32, null);
                gl.MakeCurrent();
                gl.Enable(OpenGL.GL_TEXTURE_2D);
                gl.Enable(OpenGL.GL_CULL_FACE);
                gl.Enable(OpenGL.GL_DEPTH_TEST);
                gl.Enable(OpenGL.GL_BLEND);
                gl.Enable(OpenGL.GL_VERTEX_ARRAY);
                gl.Hint(HintTarget.LineSmooth, HintMode.Nicest);
                gl.Enable(OpenGL.GL_LINE_SMOOTH);
                gl.Enable(OpenGL.GL_BLEND);
                gl.BlendFunc(BlendingSourceFactor.SourceAlpha, BlendingDestinationFactor.OneMinusSourceAlpha);
                gl.Enable(OpenGL.GL_MULTISAMPLE);
                gl.MinSampleShading(4.0f);
                gl.PolygonMode(FaceMode.FrontAndBack, PolygonMode.Filled);

                //_postProcesser = new PostProcesser(gl, width, height);


                //var t = new ShaderBindable(gl, "Shaders/Background.vert", "Shaders/Background.frag");
            }
        }
Exemplo n.º 12
0
        private void SharpGL_Render(object o, RenderEventArgs e)
        {
            TheOpenGL = this.ViewPort.OpenGL;
            // Clear The Screen And The Depth Buffer
            TheOpenGL.Clear(OpenGL.GL_COLOR_BUFFER_BIT | OpenGL.GL_DEPTH_BUFFER_BIT);
            // Reset The View
            TheOpenGL.LoadIdentity();
            // Re-Enable texture.
            TheOpenGL.Enable(OpenGL.GL_TEXTURE_2D);
            // Enable blend function.
            TheOpenGL.Enable(OpenGL.GL_BLEND);
            TheOpenGL.BlendFunc(OpenGL.GL_SRC_ALPHA, OpenGL.GL_ONE_MINUS_SRC_ALPHA);

            TheOpenGL.MatrixMode(SharpGL.Enumerations.MatrixMode.Modelview);
            TheOpenGL.Translate(Translation[0], Translation[1], Translation[2]);
            TheOpenGL.Rotate(Rotation[0], 1, 0, 0);
            TheOpenGL.Rotate(Rotation[1], 0, 1, 0);
            TheOpenGL.Rotate(Rotation[2], 0, 0, 1);
            // ########################################################################################
            // ########################################################################################

            DrawBackground();
            DrawActors();
            DrawForeground();

            // ########################################################################################
            // ########################################################################################
            TheOpenGL.Flush();
        }
Exemplo n.º 13
0
        private void RenderColor(OpenGL gl)
        {
            //Set Background Color
            gl.ClearColor(0.0f, 0.0f, 0.0f, 1.0f);

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

            float[] global_ambient = new float[] { 1.0f, 1.0f, 1.0f, 1.0f };
            float[] light0pos      = new float[] { 50.0f, 10.0f, 6.0f, 1.0f };
            float[] light0ambient  = new float[] { 1.0f, 1.0f, 1.0f, 0.5f };
            float[] light0diffuse  = new float[] { 1.0f, 1.0f, 1.0f, 1.0f };
            float[] light0specular = new float[] { 0.0f, 0.0f, 1.0f, 1.0f };
            float[] lmodel_ambient = new float[] { 1.2f, 1.2f, 1.2f, 1.0f };

            gl.LightModel(OpenGL.GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
            gl.LightModel(OpenGL.GL_LIGHT_MODEL_AMBIENT, global_ambient);
            gl.Light(OpenGL.GL_LIGHT0, OpenGL.GL_POSITION, light0pos);
            gl.Light(OpenGL.GL_LIGHT0, OpenGL.GL_AMBIENT, light0ambient);
            gl.Light(OpenGL.GL_LIGHT0, OpenGL.GL_DIFFUSE, light0diffuse);
            gl.Light(OpenGL.GL_LIGHT0, OpenGL.GL_SPECULAR, light0specular);

            gl.ColorMaterial(OpenGL.GL_FRONT_AND_BACK, OpenGL.GL_AMBIENT_AND_DIFFUSE);
            gl.Enable(OpenGL.GL_COLOR_MATERIAL);

            gl.ShadeModel(OpenGL.GL_SMOOTH);
            gl.Enable(OpenGL.GL_LINE_SMOOTH);

            gl.BlendFunc(OpenGL.GL_SRC_ALPHA, OpenGL.GL_ONE_MINUS_SRC_ALPHA);
            gl.Enable(OpenGL.GL_BLEND);
        }
Exemplo n.º 14
0
        public static void DrawAxis(OpenGL gl, Vector3 position, Vector3 forward, Vector3 up, double scale)
        {
            gl.PushAttrib(OpenGL.GL_ENABLE_BIT);
            gl.Disable(OpenGL.GL_LIGHTING);
            gl.Disable(OpenGL.GL_TEXTURE_2D);
            gl.Enable(OpenGL.GL_COLOR_MATERIAL);
            gl.Enable(OpenGL.GL_BLEND);
            gl.ColorMaterial(OpenGL.GL_FRONT_AND_BACK, OpenGL.GL_AMBIENT_AND_DIFFUSE);
            gl.BlendFunc(OpenGL.GL_SRC_ALPHA, OpenGL.GL_ONE_MINUS_SRC_ALPHA);

            Vector3 side = forward.Cross(up);

            gl.Begin(OpenGL.GL_LINES);

            gl.Color(1.0, 0.0, 0.0, 1.0);
            gl.Vertex(position.x, position.y, position.z);
            gl.Vertex(position.x + side.x * scale, position.y + side.y * scale, position.z + side.z * scale);

            gl.Color(0.0, 1.0, 0.0, 1.0);
            gl.Vertex(position.x, position.y, position.z);
            gl.Vertex(position.x + up.x * scale, position.y + up.y * scale, position.z + up.z * scale);

            gl.Color(0.0, 0.0, 1.0, 1.0);
            gl.Vertex(position.x, position.y, position.z);
            gl.Vertex(position.x + forward.x * scale, position.y + forward.y * scale, position.z + forward.z * scale);

            gl.End();

            gl.PopAttrib();
        }
Exemplo n.º 15
0
        public void Begin()
        {
            if (IsDisposed)
            {
                throw new ObjectDisposedException(nameof(SpriteBatch));
            }

            gl.Disable(GL_CULL_FACE);

            gl.Enable(GL_DEPTH_TEST);

            gl.Enable(GL_BLEND);

            gl.BlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);

            gl.BlendEquation(GL_FUNC_ADD_EXT);

            _shaderProgram.Bind(gl);

            gl.BindBuffer(GL_ARRAY_BUFFER, vertexBufferIds[0]);

            gl.BindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBufferIds[0]);

            gl.BindVertexArray(vaos[0]);

            currTexture = white1x1Tex;
        }
Exemplo n.º 16
0
        /// <summary>
        /// Handles the OpenGLInitialized event of the OpenGLControl control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="args">The <see cref="SharpGL.SceneGraph.OpenGLEventArgs"/> instance containing the event data.</param>
        private void OpenGLControl_OpenGLInitialized(object sender, EventArgs args)
        {
            gl = openGLControl.OpenGL;

            gl.Enable(OpenGL.GL_DEPTH_TEST);

            float[] global_ambient = new float[] { 0.15f, 0.15f, 0.15f, 1.0f };
            gl.LightModel(OpenGL.GL_LIGHT_MODEL_AMBIENT, global_ambient);

            float[] light0pos      = new float[] { 0.0f, 0.0f, 1.0f, 0.0f };
            float[] light0ambient  = new float[] { 0.0f, 0.0f, 0.0f, 1.0f };
            float[] light0diffuse  = new float[] { 1.0f, 1.0f, 1.0f, 1.0f };
            float[] light0specular = new float[] { 0.0f, 0.0f, 0.0f, 0.0f };

            gl.Light(OpenGL.GL_LIGHT0, OpenGL.GL_POSITION, light0pos);
            gl.Light(OpenGL.GL_LIGHT0, OpenGL.GL_AMBIENT, light0ambient);
            gl.Light(OpenGL.GL_LIGHT0, OpenGL.GL_DIFFUSE, light0diffuse);
            gl.Light(OpenGL.GL_LIGHT0, OpenGL.GL_SPECULAR, light0specular);
            gl.Enable(OpenGL.GL_LIGHTING);
            gl.Enable(OpenGL.GL_LIGHT0);

            gl.Enable(OpenGL.GL_NORMALIZE);

            gl.ShadeModel(OpenGL.GL_SMOOTH);

            gl.BlendFunc(OpenGL.GL_SRC_ALPHA, OpenGL.GL_ONE_MINUS_SRC_ALPHA);
            gl.Enable(OpenGL.GL_BLEND);
        }
Exemplo n.º 17
0
        private void InitializeOpenGL()
        {
            _openGL = new OpenGL();
            _openGL.Create(OpenGLVersion.OpenGL2_1, RenderContextType.FBO, Canvas.Width, Canvas.Height, 32, Canvas.Sketch.Container.WindowHandle);

            _openGL.ShadeModel(OpenGL.GL_SMOOTH);
            _openGL.ClearColor(1.0f, 0.0f, 0.0f, 1.0f);
            _openGL.ClearDepth(1.0f);
            _openGL.Enable(OpenGL.GL_DEPTH_TEST);
            _openGL.DepthFunc(OpenGL.GL_LEQUAL);
            _openGL.Hint(OpenGL.GL_PERSPECTIVE_CORRECTION_HINT, OpenGL.GL_NICEST);

            _openGL.Enable(OpenGL.GL_BLEND);
            _openGL.BlendFunc(OpenGL.GL_SRC_ALPHA, OpenGL.GL_ONE_MINUS_SRC_ALPHA);
            _openGL.Enable(OpenGL.GL_LINE_SMOOTH);
            _openGL.Hint(OpenGL.GL_LINE_SMOOTH_HINT, OpenGL.GL_NICEST);

            defCameraFOV    = 60f;
            defCameraX      = Canvas.Width / 2f;
            defCameraY      = Canvas.Height / 2f;
            defCameraZ      = defCameraY / ((float)Math.Tan((defCameraFOV / 360f * Constants.TWO_PI) / 2f));
            defCameraNear   = defCameraZ / 10f;
            defCameraFar    = defCameraZ * 10f;
            defCameraAspect = (float)Canvas.Width / (float)Canvas.Height;

            SetPerspective();
        }
        private void OpenGLControl_OpenGLInitialized(object sender, SharpGL.SceneGraph.OpenGLEventArgs args)
        {
            OpenGL gl = args.OpenGL;

            //gl.ClearColor(0.7f, 0.7f, 0.9f, 0.0f); //Set Background Color
            gl.Enable(OpenGL.GL_DEPTH_TEST);
            float[] global_ambient = new float[] { 0.5f, 0.5f, 0.5f, 1.0f };
            float[] light0pos      = new float[] { 1.0f, 1.0f, 1.0f, 0.0f };
            float[] light0ambient  = new float[] { 0.0f, 0.0f, 0.0f, 1.0f };
            float[] light0diffuse  = new float[] { 1.0f, 1.0f, 1.0f, 1.0f };
            float[] light0specular = new float[] { 1.0f, 1.0f, 1.0f, 1.0f };
            float[] lmodel_ambient = new float[] { 1.2f, 1.2f, 1.2f, 1.0f };
            gl.LightModel(OpenGL.GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
            gl.LightModel(OpenGL.GL_LIGHT_MODEL_AMBIENT, global_ambient);

            gl.Light(OpenGL.GL_LIGHT0, OpenGL.GL_POSITION, light0pos);
            gl.Light(OpenGL.GL_LIGHT0, OpenGL.GL_AMBIENT, light0ambient);
            gl.Light(OpenGL.GL_LIGHT0, OpenGL.GL_DIFFUSE, light0diffuse);
            gl.Light(OpenGL.GL_LIGHT0, OpenGL.GL_SPECULAR, light0specular);

            gl.Enable(OpenGL.GL_LIGHTING);
            gl.Enable(OpenGL.GL_LIGHT0);

            gl.ColorMaterial(OpenGL.GL_FRONT_AND_BACK, OpenGL.GL_AMBIENT_AND_DIFFUSE);
            gl.Enable(OpenGL.GL_COLOR_MATERIAL);

            gl.ShadeModel(OpenGL.GL_SMOOTH);
            gl.Enable(OpenGL.GL_LINE_SMOOTH);

            gl.BlendFunc(OpenGL.GL_SRC_ALPHA, OpenGL.GL_ONE_MINUS_SRC_ALPHA);
            gl.Enable(OpenGL.GL_BLEND);
        }
Exemplo n.º 19
0
        public void Draw(OpenGL gl, float originX, float originY, float originZ, vec3 leftColor, vec3 rightColor, float audioModifier)
        {
            float scaleModifier = audioModifier * 1.6f;

            if (scaleModifier < MinSize)
            {
                scaleModifier = MinSize;
            }

            for (int index = 0; index < _quad.VertexData.Length / _quad.DataStride; index++)
            {
                _quad.VertexData[index * _quad.DataStride + 3] = Constants.Colors[6, 0]; // r
                _quad.VertexData[index * _quad.DataStride + 4] = Constants.Colors[6, 1]; // g
                _quad.VertexData[index * _quad.DataStride + 5] = Constants.Colors[6, 2]; // b
                _quad.VertexData[index * _quad.DataStride + 6] = 1.0f;                   // a
            }

            // Begin Draw
            GlState.Instance.ModelMatrix = glm.translate(GlState.Instance.ModelMatrix, new vec3(originX, originY, originZ));
            GlState.Instance.ModelMatrix = glm.scale(GlState.Instance.ModelMatrix, new vec3(scaleModifier, scaleModifier, scaleModifier));

            // Update buffers
            gl.BindBuffer(OpenGL.GL_ARRAY_BUFFER, _vertexBufferObject[0]);
            GlBuffer.SetArrayData(gl, _quad.VertexData, _quad.SizeOfVertexDataBytes, OpenGL.GL_STATIC_DRAW);

            // Make model matrix available for drawing
            gl.UniformMatrix4(GlState.Instance.DefaultTexturedModelMatrixLocation, 1, false, GlState.Instance.ModelMatrix.to_array());

            // Set blending for particle system
            gl.BlendFunc(OpenGL.GL_ONE, OpenGL.GL_ONE);
            gl.DepthFunc(OpenGL.GL_ALWAYS);

            // Draw
            gl.ActiveTexture(OpenGL.GL_TEXTURE0);
            gl.BindTexture(OpenGL.GL_TEXTURE_2D, _texture);
            gl.BindVertexArray(_vertexArrayObject[0]);
            gl.DrawElements(OpenGL.GL_TRIANGLES, _quad.IndexData.Length, _quad.IndexData);
            gl.BindVertexArray(0);
            gl.BindTexture(OpenGL.GL_TEXTURE_2D, 0);

            // Reset depth and blend func
            gl.DepthFunc(OpenGL.GL_LESS);
            gl.BlendFunc(OpenGL.GL_SRC_ALPHA, OpenGL.GL_ONE_MINUS_SRC_ALPHA);

            GlState.Instance.ModelMatrix = mat4.identity();
            // End Draw
        }
Exemplo n.º 20
0
        public void OnInitialized()
        {
            gl.Enable(OpenGL.GL_BLEND);
            gl.BlendFunc(OpenGL.GL_SRC_ALPHA, OpenGL.GL_ONE_MINUS_SRC_ALPHA);

            //  Set the clear color.
            gl.ClearColor(0.9f, 0.9f, 0.9f, 0f);
        }
Exemplo n.º 21
0
        private void openGLControl1_OpenGLInitialized(object sender, EventArgs e)
        {
            OpenGL gl = this.openGLControl1.OpenGL;

            gl.Enable(OpenGL.GL_BLEND);
            gl.BlendFunc(OpenGL.GL_SRC_ALPHA, OpenGL.GL_ONE_MINUS_SRC_ALPHA);
            gl.ClearColor(0, 0, 0, 0);
        }
Exemplo n.º 22
0
        public void InitOpenGL(OpenGL openGL)
        {
            //  Set the clear color.
            _gl.ClearColor(0, 0, 0, 0);

            _gl.Enable(OpenGL.GL_BLEND);
            _gl.BlendFunc(OpenGL.GL_SRC_ALPHA, OpenGL.GL_ONE_MINUS_SRC_ALPHA);
        }
Exemplo n.º 23
0
        /// <summary>
        /// Affichage des flocons
        /// </summary>
        /// <param name="g"></param>
        /// <param name="maintenant"></param>
        /// <param name="tailleEcran"></param>
        /// <param name="couleur"></param>
        public override void AfficheOpenGL(OpenGL gl, Temps maintenant, Rectangle tailleEcran, Color couleur)
        {
#if TRACER
            RenderStart(CHRONO_TYPE.RENDER);
#endif
            float[]   col      = { couleur.R / 255.0f, couleur.G / 255.0f, couleur.B / 255.0f, ALPHA_CENTRE };
            GLfloat[] fogcolor = { couleur.R / 4096.0f, couleur.G / 4096.0f, couleur.B / 4096.0f, 0.5f };

            gl.ClearColor(fogcolor[0], fogcolor[1], fogcolor[2], fogcolor[3]);
            gl.Clear(OpenGL.GL_COLOR_BUFFER_BIT | OpenGL.GL_DEPTH_BUFFER_BIT);

            gl.Enable(OpenGL.GL_FOG);
            gl.Fog(OpenGL.GL_FOG_MODE, OpenGL.GL_LINEAR);
            gl.Fog(OpenGL.GL_FOG_COLOR, fogcolor);
            gl.Fog(OpenGL.GL_FOG_DENSITY, 0.05f);
            gl.Fog(OpenGL.GL_FOG_START, _tailleCubeZ * 3);
            gl.Fog(OpenGL.GL_FOG_END, _tailleCubeZ * 60);
            gl.TexParameter(OpenGL.GL_TEXTURE_2D, OpenGL.GL_TEXTURE_MAG_FILTER, OpenGL.GL_NEAREST);
            gl.TexParameter(OpenGL.GL_TEXTURE_2D, OpenGL.GL_TEXTURE_MIN_FILTER, OpenGL.GL_NEAREST);

            gl.LoadIdentity();
            gl.Translate(0, 0, -_zCamera);
            gl.Disable(OpenGL.GL_LIGHTING);
            gl.Disable(OpenGL.GL_DEPTH);

            gl.Enable(OpenGL.GL_BLEND);
            gl.BlendFunc(OpenGL.GL_SRC_ALPHA, OpenGL.GL_ONE_MINUS_SRC_ALPHA);
            gl.Enable(OpenGL.GL_TEXTURE_2D);
            gl.Color(col);

            int derniereTexture = -1;

            foreach (Flocon o in _flocons)
            {
                if (derniereTexture != o.type)
                {
                    texture[o.type].Bind(gl);
                    derniereTexture = o.type;
                }

                gl.PushMatrix();
                gl.Translate(o.x, o.y, o.z);
                gl.Rotate(o.ax, o.ay, o.az);
                gl.Begin(OpenGL.GL_QUADS);
                {
                    gl.TexCoord(0.0f, 0.0f); gl.Vertex(-TAILLE_FLOCON, -TAILLE_FLOCON, 0);
                    gl.TexCoord(0.0f, 1.0f); gl.Vertex(-TAILLE_FLOCON, TAILLE_FLOCON, 0);
                    gl.TexCoord(1.0f, 1.0f); gl.Vertex(TAILLE_FLOCON, TAILLE_FLOCON, 0);
                    gl.TexCoord(1.0f, 0.0f); gl.Vertex(TAILLE_FLOCON, -TAILLE_FLOCON, 0);
                }
                gl.End();
                gl.PopMatrix();
            }

#if TRACER
            RenderStop(CHRONO_TYPE.RENDER);
#endif
        }
Exemplo n.º 24
0
 private void OpenGLControl_OpenGLInitialized(object sender, OpenGLEventArgs args)
 {
     gl = args.OpenGL;
     gl.Clear(OpenGL.GL_COLOR_BUFFER_BIT | OpenGL.GL_DEPTH_BUFFER_BIT);
     gl.Enable(OpenGL.GL_POINT_SMOOTH);
     gl.Enable(OpenGL.GL_BLEND);
     gl.BlendFunc(OpenGL.GL_SRC_ALPHA, OpenGL.GL_ONE_MINUS_SRC_ALPHA);
     args.OpenGL.PointSize(6.0f);
     args.OpenGL.LineWidth(1.0f);
 }
Exemplo n.º 25
0
        private void Form1_Load(object sender, EventArgs e)
        {
            //  Get the OpenGL object.
            OpenGL gl = AnT.OpenGL;

            //  Clear the color and depth buffer.
            gl.Clear(OpenGL.GL_COLOR_BUFFER_BIT | OpenGL.GL_DEPTH_BUFFER_BIT);
            gl.Viewport(0, 0, AnT.Size.Width, AnT.Size.Height);
            gl.MatrixMode(OpenGL.GL_PROJECTION);
            //  Load the identity matrix.
            gl.LoadIdentity();
            //gl.Ortho(-AnT.Size.Width/2.0f, AnT.Size.Width / 2.0f, -AnT.Size.Height / 2.0f, AnT.Size.Height / 2.0f, 0.0f, 1.0f);

            //gl.Ortho(-10, 10, -10, 10, -10, 10);

            // размер проекции в зависимости от размеров AnT
            if ((float)AnT.Size.Width <= (float)AnT.Size.Height)
            {
                ScreenW = size_p;
                ScreenH = size_p * (float)AnT.Size.Height / (float)AnT.Size.Width;
                //gl.Ortho2D(0.0, ScreenW, 0.0, ScreenH);
            }
            else
            {
                ScreenW = size_p * (float)AnT.Size.Width / (float)AnT.Size.Width;
                ScreenH = size_p;
                //gl.Ortho2D(0.0, ScreenW, 0.0, ScreenH);
            }
            gl.Ortho2D(-ScreenW, ScreenW, -ScreenH, ScreenH);

            // коэффициенты для перевода координат оконной системы в систему OpenGL
            devX = (float)(ScreenW * 2.0f) / (float)AnT.Size.Width;
            devY = (float)(ScreenH * 2.0f) / (float)AnT.Size.Height;

            // установка объектно-видовой матрицы
            gl.MatrixMode(OpenGL.GL_MODELVIEW);

            gl.Enable(OpenGL.GL_BLEND);
            //gl.BlendEquationEXT(OpenGL.GL_FUNC_ADD_EXT);
            gl.BlendFunc(OpenGL.GL_SRC_ALPHA, OpenGL.GL_ONE_MINUS_SRC_ALPHA);
            //gl.BlendFunc(OpenGL.GL_DST_ALPHA, OpenGL.GL_ONE_MINUS_SRC_ALPHA);
            //gl.BlendFunc(OpenGL.GL_ONE, OpenGL.GL_ONE_MINUS_SRC_ALPHA);
            //gl.BlendFunc(OpenGL.GL_ZERO, OpenGL.GL_ONE_MINUS_CONSTANT_ALPHA_EXT);

            //gl.Enable(OpenGL.GL_POINT_SMOOTH);
            gl.Disable(OpenGL.GL_DEPTH_TEST);

            //gl.BlendColor(0.1f, 0.1f, 0.1f, 0.0f);

            // gl.BlendFunc(OpenGL.GL_SRC_ALPHA, OpenGL.GL_ONE);
            gl.LoadIdentity();

            // старт счетчика таймера
            PointInGrap.Start();
        }
Exemplo n.º 26
0
        private void openGLControl_SizeChanged(object sender, EventArgs e)
        {
            //opengl setting
            OpenGL gl = openGLControl.OpenGL;

            gl.Enable(OpenGL.GL_LIGHTING);
            gl.Enable(OpenGL.GL_LIGHT0);
            gl.ShadeModel(OpenGL.GL_SMOOTH);
            gl.Enable(OpenGL.GL_COLOR_MATERIAL);
            gl.MatrixMode(OpenGL.GL_PROJECTION);
            gl.LoadIdentity();
            gl.MatrixMode(OpenGL.GL_MODELVIEW);
            gl.Enable(OpenGL.GL_TEXTURE_2D);
            gl.Disable(OpenGL.GL_DEPTH_TEST);
            gl.Enable(OpenGL.GL_BLEND);
            gl.BlendFunc(OpenGL.GL_SRC_ALPHA, OpenGL.GL_ONE);

            double dHeight = (double)this.Size.Height;
            double dWidth  = (double)this.Size.Width;

            double dMainRatio = dHeight / dWidth;
            double dGLRatio   = (((m_dY02 - m_dY01)) / ((m_dX02 - m_dX01)));
            double dRatio     = 0.0;

            gl.Viewport(0, 0, (int)dWidth, (int)dHeight);
            gl.MatrixMode(OpenGL.GL_PROJECTION);
            gl.LoadIdentity();

            m_ResultdX01 = m_dX01;
            m_ResultdX02 = m_dX02;
            m_ResultdY01 = m_dY01;
            m_ResultdY02 = m_dY02;

            if (dGLRatio > dMainRatio)
            {
                dRatio = ((m_dY02 - m_dY01)) / dHeight;

                m_ResultdX01 = (((m_dX02 + m_dX01) - (dRatio * dWidth)) * 0.5);
                m_ResultdX02 = (((m_dX02 + m_dX01) + (dRatio * dWidth)) * 0.5);

                gl.Ortho(m_ResultdX01, m_ResultdX02, m_ResultdY01, m_ResultdY02, -1, 1);
            }
            else
            {
                dRatio = ((m_dX02 - m_dX01)) / dWidth;

                m_ResultdY01 = ((m_dY02 + m_dY01) + (dRatio * dHeight)) * 0.5;
                m_ResultdY02 = ((m_dY02 + m_dY01) - (dRatio * dHeight)) * 0.5;

                gl.Ortho(m_ResultdX01, m_ResultdX02, m_ResultdY01, m_ResultdY02, -1, 1);
            }

            gl.MatrixMode(OpenGL.GL_MODELVIEW);
            gl.LoadIdentity();
        }
Exemplo n.º 27
0
        /// <summary>
        /// Called before any glyphs have been rendered.
        /// </summary>
        /// <param name="bounds">The bounds the text will be rendered at and at whats size.</param>
        void IGlyphRenderer.BeginText(FontRectangle bounds)
        {
            // called before any thing else to provide access to the total required size to redner the text

            _gl.BlendFunc(OpenGL.GL_SRC_ALPHA, OpenGL.GL_ONE_MINUS_SRC_ALPHA);
            _gl.Enable(OpenGL.GL_BLEND);
            _gl.BindTexture(OpenGL.GL_TEXTURE_2D, 0);
            Vector4 colorV = Color;

            _gl.Color(colorV.X, colorV.Y, colorV.Z, colorV.W);
        }
Exemplo n.º 28
0
        protected override void DoRender(RenderEventArgs arg)
        {
            float deltaTime = (float)random.NextDouble() * 5;

            time += (float)random.NextDouble() * 5;

            OpenGL.BindBuffer(BufferTarget.UniformBuffer, attractor_buffer[0]);
            IntPtr attractors = OpenGL.MapBufferRange(BufferTarget.UniformBuffer,
                                                      0, 64 * Marshal.SizeOf(typeof(vec4)),
                                                      MapBufferRangeAccess.MapWriteBit | MapBufferRangeAccess.MapInvalidateBufferBit);

            unsafe
            {
                var array = (vec4 *)attractors.ToPointer();
                for (int i = 0; i < 64; i++)
                {
                    array[i] = new vec4(
                        (float)(Math.Sin(time)) * 50.0f,
                        (float)(Math.Cos(time)) * 50.0f,
                        (float)(Math.Cos(time)) * (float)(Math.Sin(time)) * 5.0f,
                        ParticleSimulatorCompute.attractor_masses[i]);
                }
            }

            OpenGL.UnmapBuffer(BufferTarget.UniformBuffer);
            OpenGL.BindBuffer(BufferTarget.UniformBuffer, 0);

            // Activate the compute program and bind the position and velocity buffers
            computeProgram.Bind();
            OpenGL.GetDelegateFor <OpenGL.glBindImageTexture>()(0, textureBufferVelocity[0], 0, false, 0, OpenGL.GL_READ_WRITE, OpenGL.GL_RGBA32F);
            OpenGL.GetDelegateFor <OpenGL.glBindImageTexture>()(1, textureBufferPosition[0], 0, false, 0, OpenGL.GL_READ_WRITE, OpenGL.GL_RGBA32F);
            // Set delta time
            computeProgram.SetUniform("dt", deltaTime);
            // Dispatch
            OpenGL.GetDelegateFor <OpenGL.glDispatchCompute>()(ParticleSimulatorCompute.particleGroupCount, 1, 1);

            OpenGL.GetDelegateFor <OpenGL.glMemoryBarrier>()(OpenGL.GL_SHADER_IMAGE_ACCESS_BARRIER_BIT);


            // Clear, select the rendering program and draw a full screen quad
            SwitchesOn();
            visualProgram.Bind();
            mat4 view       = arg.Camera.GetViewMat4();
            mat4 projection = arg.Camera.GetProjectionMat4();

            visualProgram.SetUniformMatrix4("mvp", (projection * view).to_array());
            OpenGL.GetDelegateFor <OpenGL.glBindVertexArray>()(render_vao[0]);
            OpenGL.Enable(OpenGL.GL_BLEND);
            OpenGL.BlendFunc(OpenGL.GL_ONE, OpenGL.GL_ONE);
            // glPointSize(2.0f);
            OpenGL.DrawArrays(DrawMode.Points, 0, ParticleSimulatorCompute.particleCount);
            OpenGL.Disable(OpenGL.GL_BLEND);
            SwitchesOff();
        }
Exemplo n.º 29
0
        public override void AfficheOpenGL(OpenGL gl, Temps maintenant, Rectangle tailleEcran, Color couleur)
        {
#if TRACER
            RenderStart(CHRONO_TYPE.RENDER);
#endif
            if (_bmp == null)
            {
                return;
            }

            gl.Clear(OpenGL.GL_COLOR_BUFFER_BIT | OpenGL.GL_DEPTH_BUFFER_BIT);

            gl.PushMatrix();
            gl.MatrixMode(OpenGL.GL_PROJECTION);
            gl.PushMatrix();
            gl.LoadIdentity();
            gl.Ortho2D(0.0, 1.0, 0.0, 1.0);
            gl.MatrixMode(OpenGL.GL_MODELVIEW);

            gl.Disable(OpenGL.GL_LIGHTING);
            gl.Disable(OpenGL.GL_DEPTH);
            gl.Enable(OpenGL.GL_TEXTURE_2D);
            gl.Enable(OpenGL.GL_BLEND);
            gl.BlendFunc(OpenGL.GL_SRC_ALPHA, OpenGL.GL_ONE_MINUS_SRC_ALPHA);

            updatePalette(couleur);


            float[] col = { couleur.R / 512.0f, couleur.G / 512.0f, couleur.B / 512.0f, 1 };
            gl.Color(col);

            Texture text = new Texture();
            text.Create(gl, _bmp);
            text.Bind(gl);

            gl.Begin(OpenGL.GL_QUADS);
            gl.TexCoord(0.0f, 0.0f); gl.Vertex(0, 1);
            gl.TexCoord(0.0f, 1.0f); gl.Vertex(0, 0);
            gl.TexCoord(1.0f, 1.0f); gl.Vertex(1, 0);
            gl.TexCoord(1.0f, 0.0f); gl.Vertex(1, 1);
            gl.End();

            uint[] textures = { text.TextureName };
            gl.DeleteTextures(1, textures);
            text.Destroy(gl);
            gl.MatrixMode(OpenGL.GL_PROJECTION);
            gl.PopMatrix();
            gl.MatrixMode(OpenGL.GL_MODELVIEW);
            gl.PopMatrix();

#if TRACER
            RenderStop(CHRONO_TYPE.RENDER);
#endif
        }
        private void initGL(OpenGL gl)
        {
            //  Clear the color and depth buffers.
            gl.Clear(OpenGL.GL_COLOR_BUFFER_BIT | OpenGL.GL_DEPTH_BUFFER_BIT);

            //  Reset the modelview.
            gl.LoadIdentity();
            gl.LookAt(-20, 20, 40, 0, 0, 0, 0, 1, 0);
            //  Move into a more central position.
            gl.Enable(OpenGL.GL_BLEND);
            gl.BlendFunc(OpenGL.GL_SRC_ALPHA, OpenGL.GL_ONE_MINUS_SRC_ALPHA);
        }
Exemplo n.º 31
0
 /// <summary>
 /// Initialises the supplied OpenGL instance for high quality rendering.
 /// </summary>
 /// <param name="gl">The OpenGL instance.</param>
 public static void InitialiseHighQuality(OpenGL gl)
 {
     //	Set parameters that give us some high quality settings.
     gl.Enable(OpenGL.GL_DEPTH_TEST);
     gl.Enable(OpenGL.GL_NORMALIZE);
     gl.Enable(OpenGL.GL_LIGHTING);
     gl.Enable(OpenGL.GL_TEXTURE_2D);
     gl.ShadeModel(OpenGL.GL_SMOOTH);
     gl.LightModel(OpenGL.GL_LIGHT_MODEL_TWO_SIDE, OpenGL.GL_TRUE);
     gl.Enable(OpenGL.GL_BLEND);
     gl.BlendFunc(OpenGL.GL_SRC_ALPHA, OpenGL.GL_ONE_MINUS_SRC_ALPHA);
 }
Exemplo n.º 32
0
 /// <summary>
 /// Initialises the supplied OpenGL instance for high quality rendering.
 /// </summary>
 /// <param name="gl">The OpenGL instance.</param>
 public static void InitialiseHighQuality(OpenGL gl)
 {
     //	Set parameters that give us some high quality settings.
     gl.Enable(OpenGL.GL_DEPTH_TEST);
     gl.Enable(OpenGL.GL_NORMALIZE);
     gl.Enable(OpenGL.GL_LIGHTING);
     gl.Enable(OpenGL.GL_TEXTURE_2D);
     gl.ShadeModel(OpenGL.GL_SMOOTH);
     gl.LightModel(OpenGL.GL_LIGHT_MODEL_TWO_SIDE, OpenGL.GL_TRUE);
     gl.Enable(OpenGL.GL_BLEND);
     gl.BlendFunc(OpenGL.GL_SRC_ALPHA, OpenGL.GL_ONE_MINUS_SRC_ALPHA);
 }
Exemplo n.º 33
0
        /// <summary>
        /// Casts a real time 3D shadow.
        /// </summary>
        /// <param name="gl">The OpenGL object.</param>
        /// <param name="light">The source light.</param>
        public void CastShadow(OpenGL gl, Collections.LightCollection lights)
        {
            //	Set the connectivity, (calculate the neighbours of each face).
            SetConnectivity();

            //	Go through every light in the scene.
            foreach(Lights.Light light in lights)
            {
                //	Skip null lights.
                if(light == null)
                    continue;

                //	Skip turned off lights and non shadow lights.
                if(light.On == false || light.CastShadow == false)
                    continue;

                //	Every face will have a visibility setting.
                bool[] facesVisible = new bool[faces.Count];

                //	Get the light position relative to the polygon.
                Vertex lightPos = light.Translate;
                lightPos = lightPos - Translate;

                //	Go through every face, finding out whether it's visible to the light.
                for(int nFace = 0; nFace < faces.Count; nFace++)
                {
                    //	Get a reference to the face.
                    Face face = faces[nFace];

                    //	Is this face facing the light?
                    float[] planeEquation = face.GetPlaneEquation(this);
                    float side = planeEquation[0] * lightPos.X +
                        planeEquation[1] * lightPos.Y +
                        planeEquation[2] * lightPos.Z + planeEquation[3];
                    facesVisible[nFace] = (side > 0) ? true : false;
                }

                //	Save all the attributes.
                gl.PushAttrib(OpenGL.ALL_ATTRIB_BITS);

                //	Turn off lighting.
                gl.Disable(OpenGL.LIGHTING);

                //	Turn off writing to the depth mask.
                gl.DepthMask(0);
                gl.DepthFunc(OpenGL.LEQUAL);

                //	Turn on stencil buffer testing.
                gl.Enable(OpenGL.STENCIL_TEST);

                //	Translate our shadow volumes.
                gl.PushMatrix();
                Transform(gl);

                //	Don't draw to the color buffer.
                gl.ColorMask(0, 0, 0, 0);
                gl.StencilFunc(OpenGL.ALWAYS, 1, 0xFFFFFFFF);

                gl.Enable(OpenGL.CULL_FACE);

                //	First Pass. Increase Stencil Value In The Shadow
                gl.FrontFace(OpenGL.CCW);
                gl.StencilOp(OpenGL.KEEP, OpenGL.KEEP, OpenGL.INCR);
                DoShadowPass(gl, lightPos, facesVisible);

                //	Second Pass. Decrease Stencil Value In The Shadow
                gl.FrontFace(OpenGL.CW);
                gl.StencilOp(OpenGL.KEEP, OpenGL.KEEP, OpenGL.DECR);
                DoShadowPass(gl, lightPos, facesVisible);

                gl.FrontFace(OpenGL.CCW);

                gl.PopMatrix();

                //	Enable writing to the color buffer.
                gl.ColorMask(1, 1, 1, 1);

                // Draw A Shadowing Rectangle Covering The Entire Screen
                gl.Color(light.ShadowColor);
                gl.Enable(OpenGL.BLEND);
                gl.BlendFunc(OpenGL.SRC_ALPHA, OpenGL.ONE_MINUS_SRC_ALPHA);
                gl.StencilFunc(OpenGL.NOTEQUAL, 0, 0xFFFFFFF);
                gl.StencilOp(OpenGL.KEEP, OpenGL.KEEP, OpenGL.KEEP);

                Quadrics.Sphere shadow = new Quadrics.Sphere();
                shadow.Scale.Set(shadowSize, shadowSize, shadowSize);
                shadow.Draw(gl);

                gl.PopAttrib();
            }
        }
Exemplo n.º 34
0
        public virtual void DrawGrid(OpenGL gl)
        {
            gl.PushAttrib(OpenGL.LINE_BIT | OpenGL.ENABLE_BIT | OpenGL.COLOR_BUFFER_BIT);

            //  Turn off lighting, set up some nice anti-aliasing for lines.
            gl.Disable(OpenGL.LIGHTING);
            gl.Hint(OpenGL.LINE_SMOOTH_HINT, OpenGL.NICEST);
            gl.Enable(OpenGL.LINE_SMOOTH);
            gl.Enable(OpenGL.BLEND);
            gl.BlendFunc(OpenGL.SRC_ALPHA, OpenGL.ONE_MINUS_SRC_ALPHA);

            //	Create the grid.
            gl.LineWidth(1.5f);

            gl.Begin(OpenGL.LINES);

                for (int i = -10; i <= 10; i++)
                {
                    gl.Color(0.2f, 0.2f, 0.2f, 1f);
                    gl.Vertex(i, 0, -10);
                    gl.Vertex(i, 0, 10);
                    gl.Vertex(-10, 0, i);
                    gl.Vertex(10, 0, i);
                }

            gl.End();

            //  Turn off the depth test for the axies.
            gl.Disable(OpenGL.DEPTH_TEST);
            gl.LineWidth(2.0f);

            //	Create the axies.
            gl.Begin(OpenGL.LINES);

                gl.Color(1f, 0f, 0f, 1f);
                gl.Vertex(0f, 0f, 0f);
                gl.Vertex(3f, 0f, 0f);
                gl.Color(0f, 1f, 0f, 1f);
                gl.Vertex(0f, 0f, 0f);
                gl.Vertex(0f, 3f, 0f);
                gl.Color(0f, 0f, 1f, 1f);
                gl.Vertex(0f, 0f, 0f);
                gl.Vertex(0f, 0f, 3f);

            gl.End();

            gl.PopAttrib();

               //     gl.Flush();
        }
Exemplo n.º 35
0
        /// <summary>
        /// Casts a real time 3D shadow.
        /// </summary>
        /// <param name="gl">The OpenGL object.</param>
        /// <param name="lights">The lights.</param>
		private void CastShadow(OpenGL gl)
		{
			//	Set the connectivity, (calculate the neighbours of each face).
			SetConnectivity();

            //  Get the lights in the scene.
            var lights = TraverseToRootElement().Traverse<Light>(l => l.IsEnabled && l.On && l.CastShadow);

            //  Get some useful references.
            var faces = ParentPolygon.Faces;

			//	Go through every light in the scene.
			foreach(var light in lights)
			{
				//	Every face will have a visibility setting.
				bool[] facesVisible = new bool[faces.Count];

				//	Get the light position relative to the polygon.
				Vertex lightPos = light.Position;
				lightPos = lightPos - ParentPolygon.Transformation.TranslationVertex;

				//	Go through every face, finding out whether it's visible to the light.
				for(int nFace = 0; nFace < faces.Count; nFace++)
				{
					//	Get a reference to the face.
					Face face = faces[nFace];

					//	Is this face facing the light?
					float[] planeEquation = face.GetPlaneEquation(ParentPolygon);
					float side = planeEquation[0] * lightPos.X + 
						planeEquation[1] * lightPos.Y + 
						planeEquation[2] * lightPos.Z + planeEquation[3];
					facesVisible[nFace] = (side > 0) ? true : false;
				}

				//	Save all the attributes.
                gl.PushAttrib(OpenGL.GL_ALL_ATTRIB_BITS);
			
				//	Turn off lighting.
                gl.Disable(OpenGL.GL_LIGHTING);

				//	Turn off writing to the depth mask.
				gl.DepthMask(0);
                gl.DepthFunc(OpenGL.GL_LEQUAL);

				//	Turn on stencil buffer testing.
                gl.Enable(OpenGL.GL_STENCIL_TEST);

				//	Translate our shadow volumes.
				ParentPolygon.PushObjectSpace(gl);

				//	Don't draw to the color buffer.
				gl.ColorMask(0, 0, 0, 0);
                gl.StencilFunc(OpenGL.GL_ALWAYS, 1, 0xFFFFFFFF);

                gl.Enable(OpenGL.GL_CULL_FACE);
			
				//	First Pass. Increase Stencil Value In The Shadow
                gl.FrontFace(OpenGL.GL_CCW);
                gl.StencilOp(OpenGL.GL_KEEP, OpenGL.GL_KEEP, OpenGL.GL_INCR);
				DoShadowPass(gl, lightPos, facesVisible);

				//	Second Pass. Decrease Stencil Value In The Shadow
                gl.FrontFace(OpenGL.GL_CW);
                gl.StencilOp(OpenGL.GL_KEEP, OpenGL.GL_KEEP, OpenGL.GL_DECR);
				DoShadowPass(gl, lightPos, facesVisible);

                gl.FrontFace(OpenGL.GL_CCW);

				ParentPolygon.PopObjectSpace(gl);
			
				//	Enable writing to the color buffer.
				gl.ColorMask(1, 1, 1, 1); 
            
				// Draw A Shadowing Rectangle Covering The Entire Screen
				gl.Color(light.ShadowColor);
                gl.Enable(OpenGL.GL_BLEND);
                gl.BlendFunc(OpenGL.GL_SRC_ALPHA, OpenGL.GL_ONE_MINUS_SRC_ALPHA);
                gl.StencilFunc(OpenGL.GL_NOTEQUAL, 0, 0xFFFFFFF);
                gl.StencilOp(OpenGL.GL_KEEP, OpenGL.GL_KEEP, OpenGL.GL_KEEP);
				
				Quadrics.Sphere shadow = new Quadrics.Sphere();
                shadow.Transformation.ScaleX = shadowSize;
                shadow.Transformation.ScaleY = shadowSize;
                shadow.Transformation.ScaleZ = shadowSize;
				shadow.Render(gl, RenderMode.Design);
	
				gl.PopAttrib();
			}
		}