public static void PrepareForUse(VertexDeclaration vd) { GLStateManager.VertexArray(true); bool normal = false; bool color = false; bool texcoord = false; foreach (var ve in vd.GetVertexElements()) { switch (ve.VertexElementUsage) { case VertexElementUsage.Position: GL.VertexPointer( ve.VertexElementFormat.OpenGLNumberOfElements(), ve.VertexElementFormat.OpenGLVertexPointerType(), vd.VertexStride, (IntPtr)ve.Offset ); break; case VertexElementUsage.Color: GL.ColorPointer( ve.VertexElementFormat.OpenGLNumberOfElements(), ve.VertexElementFormat.OpenGLColorPointerType(), vd.VertexStride, (IntPtr)ve.Offset ); color = true; break; case VertexElementUsage.Normal: GL.NormalPointer( ve.VertexElementFormat.OpenGLNormalPointerType(), vd.VertexStride, (IntPtr)ve.Offset ); normal = true; break; case VertexElementUsage.TextureCoordinate: GL.TexCoordPointer( ve.VertexElementFormat.OpenGLNumberOfElements(), ve.VertexElementFormat.OpenGLTexCoordPointerType(), vd.VertexStride, (IntPtr)ve.Offset ); texcoord = true; break; default: throw new NotImplementedException(); } } GLStateManager.TextureCoordArray(texcoord); GLStateManager.ColorArray(color); GLStateManager.NormalArray(normal); }
internal override void Apply() { GLStateManager.Projection(Projection); GLStateManager.World(World); GLStateManager.View(View); base.Apply(); // set camera Matrix _matrix = Matrix.Identity; GL.MatrixMode(MatrixMode.Projection); GL.LoadIdentity(); GL.Ortho(0, 320, 480, 0, -1, 1); GL.MatrixMode(MatrixMode.Modelview); GL.LoadMatrix(ref _matrix.M11); GL.Viewport(0, 0, 320, 480); // 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); GL.Disable(EnableCap.CullFace); }
public static void PrepareForUse(VertexDeclaration vd, IntPtr arrayStart) { GLStateManager.VertexArray(true); bool normal = false; bool color = false; bool texcoord = false; foreach (var ve in vd.GetVertexElements()) { switch (ve.VertexElementUsage) { case VertexElementUsage.Position: /* TODO GL.VertexPointer( * ve.VertexElementFormat.OpenGLNumberOfElements(), * ve.VertexElementFormat.OpenGLValueType(), * vd.VertexStride, * new IntPtr(arrayStart.ToInt32() + ve.Offset) * );*/ break; case VertexElementUsage.Color: /* TODO GL.ColorPointer( * ve.VertexElementFormat.OpenGLNumberOfElements(), * ve.VertexElementFormat.OpenGLValueType(), * vd.VertexStride, * new IntPtr(arrayStart.ToInt32() + ve.Offset) * );*/ color = true; break; case VertexElementUsage.Normal: /* TODO GL.NormalPointer( * ve.VertexElementFormat.OpenGLValueType(), * vd.VertexStride, * new IntPtr(arrayStart.ToInt32() + ve.Offset) * );*/ normal = true; break; case VertexElementUsage.TextureCoordinate: /* TODO GL.TexCoordPointer( * ve.VertexElementFormat.OpenGLNumberOfElements(), * ve.VertexElementFormat.OpenGLValueType(), * vd.VertexStride, * new IntPtr(arrayStart.ToInt32() + ve.Offset) * );*/ texcoord = true; break; default: throw new NotImplementedException(); } } GLStateManager.TextureCoordArray(texcoord); GLStateManager.ColorArray(color); GLStateManager.NormalArray(normal); }
internal override void Apply() { // May need to be moved elsewhere within this method OnApply(); GLStateManager.Projection(Projection); GLStateManager.World(World); GLStateManager.View(View); base.Apply(); GLStateManager.Textures2D(Texture != null); GLStateManager.ColorArray(VertexColorEnabled); }
internal override void Apply() { // May need to be moved elsewhere within this method OnApply(); GLStateManager.Viewport(Game.Instance.Window.ClientBounds); GLStateManager.Projection(Projection); GLStateManager.WorldView(World, View); base.Apply(); GLStateManager.Textures2D(Texture != null); GLStateManager.ColorArray(VertexColorEnabled); }
internal void UnsetGraphicsStates() { // Make sure we are not user any shaders GL.UseProgram(0); // if primitives were used then we need to reset them if (resetVertexStates) { GLStateManager.VertexArray(false); GLStateManager.ColorArray(false); GLStateManager.NormalArray(false); GLStateManager.TextureCoordArray(false); resetVertexStates = false; } GL.PopMatrix(); }
internal override void Apply() { // May need to be moved elsewhere within this method OnApply(); GLStateManager.Projection(Projection); GLStateManager.WorldView(World, View); // this should probably not be here. // this should probably be set immediately on assignment. GLStateManager.Cull(this.graphicsDevice.RasterizerState.CullMode); base.Apply(); GLStateManager.Textures2D(Texture != null); GLStateManager.ColorArray(VertexColorEnabled); }
internal virtual void Apply() { GLStateManager.Cull(graphicsDevice.RasterizerState.CullMode.OpenGL11()); // TODO: This is prolly not right (DepthBuffer, etc) GLStateManager.DepthTest(graphicsDevice.DepthStencilState.DepthBufferEnable); }
public void End() { // apply the custom effect if there is one if (_effect != null) { _effect.CurrentTechnique.Passes [0].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); // } graphicsDevice.BlendState = _blendState; graphicsDevice.SetGraphicsStates(); // 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) GLStateManager.SetDepthStencilState(_depthStencilState); //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; } spriteEffect.CurrentTechnique.Passes [0].Apply(); }
public void EndGL11() { // Disable Blending by default = BlendState.Opaque GL11.Disable(ALL11.Blend); // set the blend mode if (_blendState == BlendState.NonPremultiplied) { GL11.BlendFunc(ALL11.SrcAlpha, ALL11.OneMinusSrcAlpha); GL11.Enable(ALL11.Blend); } if (_blendState == BlendState.AlphaBlend) { GL11.BlendFunc(ALL11.One, ALL11.OneMinusSrcAlpha); GL11.Enable(ALL11.Blend); } if (_blendState == BlendState.Additive) { GL11.BlendFunc(ALL11.SrcAlpha, ALL11.One); GL11.Enable(ALL11.Blend); } if (_blendState == BlendState.Multiply) { GL11.BlendFunc(ALL11.DstColor, ALL11.Zero); GL11.Enable(ALL11.Blend); } if (_blendState == BlendState.Multiplyx2) { GL11.BlendFunc(ALL11.DstColor, ALL11.SrcColor); GL11.Enable(ALL11.Blend); } // set camera GL11.MatrixMode(ALL11.Projection); GL11.LoadIdentity(); GL11.Ortho(0, this.graphicsDevice.Viewport.Width, this.graphicsDevice.Viewport.Height, 0, -1, 1); // Enable Scissor Tests if necessary if (this.graphicsDevice.RasterizerState.ScissorTestEnable) { GL11.Enable(ALL11.ScissorTest); } GL11.MatrixMode(ALL11.Modelview); GL11.Viewport(0, 0, this.graphicsDevice.Viewport.Width, this.graphicsDevice.Viewport.Height); // Enable Scissor Tests if necessary if (this.graphicsDevice.RasterizerState.ScissorTestEnable) { GL11.Scissor(this.graphicsDevice.ScissorRectangle.X, this.graphicsDevice.ScissorRectangle.Y, this.graphicsDevice.ScissorRectangle.Width, this.graphicsDevice.ScissorRectangle.Height); } GL11.LoadMatrix(ref _matrix.M11); // Initialize OpenGL states (ideally move this to initialize somewhere else) GL11.Disable(ALL11.DepthTest); GL11.TexEnv(ALL11.TextureEnv, ALL11.TextureEnvMode, (int)ALL11.BlendSrc); GL11.Enable(ALL11.Texture2D); GL11.EnableClientState(ALL11.VertexArray); GL11.EnableClientState(ALL11.ColorArray); GL11.EnableClientState(ALL11.TextureCoordArray); // No need to cull sprites. they will all be same-facing by construction. // Plus, setting frontface to Clockwise is a troll move. GLStateManager.Cull(CullMode.None); GL11.Color4(1.0f, 1.0f, 1.0f, 1.0f); _batcher.DrawBatchGL11(_sortMode, _samplerState); if (this.graphicsDevice.RasterizerState.ScissorTestEnable) { GL11.Disable(ALL11.ScissorTest); } }
/// <summary> /// Lazily computes derived parameter values immediately before applying the effect. /// </summary> protected internal override void OnApply() { // Recompute the shader index? // if ((dirtyFlags & EffectDirtyFlags.ShaderIndex) != 0) { int shaderIndex = 0; if (!fogEnabled) { shaderIndex += 1; } if (vertexColorEnabled) { shaderIndex += 2; } if (textureEnabled) { shaderIndex += 4; } if (lightingEnabled) { if (preferPerPixelLighting) { shaderIndex += 24; } else if (oneLight) { shaderIndex += 16; } else { shaderIndex += 8; } } // // //shaderIndexParam.SetValue (shaderIndex); // // dirtyFlags &= ~EffectDirtyFlags.ShaderIndex; if (oldIndex != shaderIndex) { int vertexShader = VSArray[VSIndices[shaderIndex]]; int fragmentShader = PSArray[PSIndices[shaderIndex]]; UpdateTechnique("BasicEffect", "", vertexShader, fragmentShader); oldIndex = shaderIndex; // Update here } // } // These are the states that work GLStateManager.Projection(Projection); GLStateManager.WorldView(world, view); // Override this for now for testing purposes dirtyFlags |= EffectDirtyFlags.World | EffectDirtyFlags.WorldViewProj; dirtyFlags |= EffectDirtyFlags.WorldViewProj | EffectDirtyFlags.EyePosition; dirtyFlags &= ~EffectDirtyFlags.FogEnable; // turn off fog for now dirtyFlags |= EffectDirtyFlags.MaterialColor; GLStateManager.Textures2D(TextureEnabled); GLStateManager.ColorArray(VertexColorEnabled); // Recompute the world+view+projection matrix or fog vector? dirtyFlags = EffectHelpers.SetWorldViewProjAndFog(dirtyFlags, ref world, ref view, ref projection, ref worldView, fogEnabled, fogStart, fogEnd, worldViewProjParam, fogVectorParam); // Recompute the diffuse/emissive/alpha material color parameters? if ((dirtyFlags & EffectDirtyFlags.MaterialColor) != 0) { EffectHelpers.SetMaterialColor(lightingEnabled, alpha, ref diffuseColor, ref emissiveColor, ref ambientLightColor, diffuseColorParam, emissiveColorParam); dirtyFlags &= ~EffectDirtyFlags.MaterialColor; } if (TextureEnabled) { _texture.Apply(); textureParam.SetValue(_texture); //System.Console.WriteLine("Texture set"); } // // if (lightingEnabled) { // // Recompute the world inverse transpose and eye position? // dirtyFlags = EffectHelpers.SetLightingMatrices (dirtyFlags, ref world, ref view, worldParam, worldInverseTransposeParam, eyePositionParam); // // // Check if we can use the only-bother-with-the-first-light shader optimization. // bool newOneLight = !light1.Enabled && !light2.Enabled; // // if (oneLight != newOneLight) { // oneLight = newOneLight; // dirtyFlags |= EffectDirtyFlags.ShaderIndex; // } // } }
public void End() { // set up camera switch (this.graphicsDevice.PresentationParameters.DisplayOrientation) { case DisplayOrientation.LandscapeLeft: case DisplayOrientation.LandscapeRight: case DisplayOrientation.PortraitUpsideDown: throw new NotImplementedException(); default: if (this.graphicsDevice.RenderTarget != null) { Matrix4.CreateOrthographicOffCenter(0, this.graphicsDevice.RenderTarget.Width, this.graphicsDevice.RenderTarget.Height, 0, -1, 1, out GLStateManager.Projection); break; } else { Matrix4.CreateOrthographicOffCenter(0, this.graphicsDevice.Viewport.Width, this.graphicsDevice.Viewport.Height, 0, -1, 1, out GLStateManager.Projection); break; } } GL.Viewport(this.graphicsDevice.Viewport.X, this.graphicsDevice.Viewport.Y, this.graphicsDevice.Viewport.Width, this.graphicsDevice.Viewport.Height); // Enable Scissor Tests if necessary if (this.graphicsDevice.RenderState.ScissorTestEnable) { GL.Enable(EnableCap.ScissorTest); GL.Scissor(this.graphicsDevice.ScissorRectangle.X, this.graphicsDevice.ScissorRectangle.Y, this.graphicsDevice.ScissorRectangle.Width, this.graphicsDevice.ScissorRectangle.Height); } GLStateManager.ModelView = _matrix.ToMatrix4(); // Initialize OpenGL states (ideally move this to initialize somewhere else) GLStateManager.DepthTest(false); GLStateManager.Textures2D(true); // Enable Culling for better performance GLStateManager.Cull(FrontFaceDirection.Cw); switch (_sortMode) { case SpriteSortMode.Immediate: break; default: this.graphicsDevice.RenderState.SourceBlend = _blendState.ColorSourceBlend; this.graphicsDevice.RenderState.DestinationBlend = _blendState.ColorDestinationBlend; break; } GLStateManager.BlendFunc((BlendingFactorSrc)this.graphicsDevice.RenderState.SourceBlend, (BlendingFactorDest)this.graphicsDevice.RenderState.DestinationBlend); _batcher.DrawBatch(_sortMode); // GG EDIT always disable scissor test after drawing a batch if (this.graphicsDevice.RenderState.ScissorTestEnable) { GL.Disable(EnableCap.ScissorTest); } }