예제 #1
0
        public void End()
        {
            // apply the custom effect if there is one
            if (_effect != null)
            {
                _effect.Apply();

                if (graphicsDevice.Textures._textures.Count > 0)
                {
                    foreach (EffectParameter ep in _effect._textureMappings)
                    {
                        // if user didn't inform the texture index, we can't bind it
                        if (ep.UserInedx == -1)
                        {
                            continue;
                        }

                        Texture tex = graphicsDevice.Textures[ep.UserInedx];

                        // Need to support multiple passes as well
                        GL.ActiveTexture((TextureUnit)((int)TextureUnit.Texture0 + ep.UserInedx));
                        GL.BindTexture(TextureTarget.Texture2D, tex._textureId);
                        GL.Uniform1(ep.UniformLocation, ep.UserInedx);
                    }
                }
            }

            // Disable Blending by default = BlendState.Opaque
            GL.Disable(EnableCap.Blend);

            // set the blend mode
            if (_blendState == BlendState.NonPremultiplied)
            {
                GL.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.OneMinusSrcAlpha);
                GL.Enable(EnableCap.Blend);
            }

            if (_blendState == BlendState.AlphaBlend)
            {
                GL.BlendFunc(BlendingFactorSrc.One, BlendingFactorDest.OneMinusSrcAlpha);
                GL.Enable(EnableCap.Blend);
            }

            if (_blendState == BlendState.Additive)
            {
                GL.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.One);
                GL.Enable(EnableCap.Blend);
            }

            // set camera
            GL.MatrixMode(MatrixMode.Projection);
            GL.LoadIdentity();

            Viewport vp = this.graphicsDevice.Viewport;

            // Switch on the flags.
            switch (this.graphicsDevice.PresentationParameters.DisplayOrientation)
            {
            case DisplayOrientation.LandscapeLeft:
            {
                GL.Rotate(-90, 0, 0, 1);
                break;
            }

            case DisplayOrientation.LandscapeRight:
            {
                GL.Rotate(90, 0, 0, 1);
                break;
            }

            case DisplayOrientation.PortraitUpsideDown:
            {
                GL.Rotate(180, 0, 0, 1);
                break;
            }

            default:
            {
                break;
            }
            }

            GL.Ortho(0, vp.Width, vp.Height, 0, -1, 1);

            // Enable Scissor Tests if necessary
            if (this.graphicsDevice.RasterizerState.ScissorTestEnable)
            {
                GL.Enable(EnableCap.ScissorTest);
            }

            GL.MatrixMode(MatrixMode.Modelview);

            GL.Viewport(0, 0, vp.Width, vp.Height);
            //GL.Viewport (vp.X, vp.Y, vp.Width, vp.Height);

            // Enable Scissor Tests if necessary
            if (this.graphicsDevice.RasterizerState.ScissorTestEnable)
            {
                GL.Scissor(this.graphicsDevice.ScissorRectangle.X, this.graphicsDevice.ScissorRectangle.Y, this.graphicsDevice.ScissorRectangle.Width, this.graphicsDevice.ScissorRectangle.Height);
            }

            GL.LoadMatrix(ref _matrix.M11);

            // Initialize OpenGL states (ideally move this to initialize somewhere else)
            GL.Disable(EnableCap.DepthTest);
            GL.TexEnv(TextureEnvTarget.TextureEnv, TextureEnvParameter.TextureEnvMode, (int)All.BlendSrc);
            GL.Enable(EnableCap.Texture2D);
            GL.EnableClientState(ArrayCap.VertexArray);
            GL.EnableClientState(ArrayCap.ColorArray);
            GL.EnableClientState(ArrayCap.TextureCoordArray);

            // Enable Culling for better performance
            GL.Enable(EnableCap.CullFace);
            GL.FrontFace(FrontFaceDirection.Cw);
            GL.Color4(1.0f, 1.0f, 1.0f, 1.0f);

            _batcher.DrawBatch(_sortMode, _samplerState);

            // Disable Scissor Tests if necessary
            if (this.graphicsDevice.RasterizerState.ScissorTestEnable)
            {
                GL.Disable(EnableCap.ScissorTest);
            }

            // clear out the textures
            graphicsDevice.Textures._textures.Clear();

            // unbinds shader
            if (_effect != null)
            {
                GL.UseProgram(0);
                _effect = null;
            }
        }
예제 #2
0
        public void End()
        {
            // apply the custom effect if there is one
            if (_effect != null)
            {
                if (graphicsDevice.Textures._textures.Count > 0)
                {
                    foreach (var texture in graphicsDevice.Textures._textures)
                    {
                        int index = texture.Key;
                        if (index > 0)
                        {  // zero is the active texture
                            if (index < _effect._textureMappings.Count)
                            {
                                var tex       = _effect._textureMappings[index];
                                int texOffset = (int)TextureUnit.Texture0;
                                var tex2      = texture.Value;
                                if (tex != null)
                                {
                                    // Need to support multiple passes as well
                                    GL.UseProgram(_effect.CurrentTechnique.Passes[0].shaderProgram);
                                    GL.ActiveTexture((TextureUnit)texOffset + index);
                                    GL.BindTexture(TextureTarget.Texture2D, tex2._textureId);
                                    GL.Uniform1(tex.internalIndex, tex2._textureId);
                                    GL.UseProgram(0);
                                }
                            }
                        }
                    }
                }
                _effect.Apply();
            }

            // Disable Blending by default = BlendState.Opaque
            GL.Disable(EnableCap.Blend);

            // set the blend mode
            if (_blendState == BlendState.NonPremultiplied)
            {
                GL.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.OneMinusSrcAlpha);
                GL.Enable(EnableCap.Blend);
            }

            if (_blendState == BlendState.AlphaBlend)
            {
                GL.BlendFunc(BlendingFactorSrc.One, BlendingFactorDest.OneMinusSrcAlpha);
                GL.Enable(EnableCap.Blend);
            }

            if (_blendState == BlendState.Additive)
            {
                GL.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.One);
                GL.Enable(EnableCap.Blend);
            }

            // set camera
            GL.MatrixMode(MatrixMode.Projection);
            GL.LoadIdentity();

            // Switch on the flags.
            switch (this.graphicsDevice.PresentationParameters.DisplayOrientation)
            {
            case DisplayOrientation.LandscapeLeft:
            {
                GL.Rotate(-90, 0, 0, 1);
                GL.Ortho(0, this.graphicsDevice.Viewport.Height, this.graphicsDevice.Viewport.Width, 0, -1, 1);
                break;
            }

            case DisplayOrientation.LandscapeRight:
            {
                GL.Rotate(90, 0, 0, 1);
                GL.Ortho(0, this.graphicsDevice.Viewport.Height, this.graphicsDevice.Viewport.Width, 0, -1, 1);
                break;
            }

            case DisplayOrientation.PortraitUpsideDown:
            {
                GL.Rotate(180, 0, 0, 1);
                GL.Ortho(0, this.graphicsDevice.Viewport.Width, this.graphicsDevice.Viewport.Height, 0, -1, 1);
                break;
            }

            default:
            {
                GL.Ortho(0, this.graphicsDevice.Viewport.Width, this.graphicsDevice.Viewport.Height, 0, -1, 1);
                break;
            }
            }

            // Enable Scissor Tests if necessary
            if (this.graphicsDevice.RasterizerState.ScissorTestEnable)
            {
                GL.Enable(EnableCap.ScissorTest);
            }

            GL.MatrixMode(MatrixMode.Modelview);

            GL.Viewport(0, 0, this.graphicsDevice.Viewport.Width, this.graphicsDevice.Viewport.Height);

            // Enable Scissor Tests if necessary
            if (this.graphicsDevice.RasterizerState.ScissorTestEnable)
            {
                GL.Scissor(this.graphicsDevice.ScissorRectangle.X, this.graphicsDevice.ScissorRectangle.Y, this.graphicsDevice.ScissorRectangle.Width, this.graphicsDevice.ScissorRectangle.Height);
            }

            GL.LoadMatrix(ref _matrix.M11);

            // Initialize OpenGL states (ideally move this to initialize somewhere else)
            GL.Disable(EnableCap.DepthTest);
            GL.TexEnv(TextureEnvTarget.TextureEnv, TextureEnvParameter.TextureEnvMode, (int)All.BlendSrc);
            GL.Enable(EnableCap.Texture2D);
            GL.EnableClientState(ArrayCap.VertexArray);
            GL.EnableClientState(ArrayCap.ColorArray);
            GL.EnableClientState(ArrayCap.TextureCoordArray);

            // Enable Culling for better performance
            GL.Enable(EnableCap.CullFace);
            GL.FrontFace(FrontFaceDirection.Cw);
            GL.Color4(1.0f, 1.0f, 1.0f, 1.0f);
            //GL.Color4(1.0f, 1.0f, 1.0f, 0.5f);

            _batcher.DrawBatch(_sortMode, _samplerState);
            //GL.UseProgram(0);
            // clear out the textures
            graphicsDevice.Textures._textures.Clear();

            spriteEffect.CurrentTechnique.Passes[0].Apply();

            if (this.graphicsDevice.RasterizerState.ScissorTestEnable)
            {
                GL.Disable(EnableCap.ScissorTest);
            }
        }