/** * Called once when OpenGL is ready. * */ public void Init() { // Stencil test GL.Enable(EnableCap.StencilTest); // Depth test GL.Enable(EnableCap.DepthTest); // Blending GL.Enable(EnableCap.Blend); GL.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.OneMinusSrcAlpha); // Culling GL.Enable(EnableCap.CullFace); GL.CullFace(CullFaceMode.Back); GL.FrontFace(FrontFaceDirection.Ccw); Shader.CheckError(); // OpenGL-Information string version_string = GL.GetString(StringName.Version); Console.WriteLine("OpenGL-Version: " + version_string); Shader.CheckError(); Console.WriteLine("Stencil buffer bits: " + GL.GetInteger(GetPName.StencilBits)); Shader.CheckError(); Console.WriteLine("Depth buffer bits: " + GL.GetInteger(GetPName.DepthBits)); Shader.CheckError(); // For OpenGL 2.1 and lower Dictionary <string, bool> extensions = new Dictionary <string, bool>(); string extension_string = GL.GetString(StringName.Extensions); foreach (string extension in extension_string.Split(' ')) { extensions.Add(extension, true); } GL.ClearColor(rootNode.BackgroundColor.X, rootNode.BackgroundColor.Y, rootNode.BackgroundColor.Z, 1); // Setup shader shader.CompileAndLink(); shader.Use(); ShaderAttributes.GetInstance().GetAttributes(shader.ProgramId); // Start timer timer.Start(); InitContent(); }
/** * Draw the scene graph content - regular mode. * */ private void DrawRegular() { ShaderAttributes.GetInstance().SetCameraEyeParameter(camera.Eye); ShaderAttributes.GetInstance().SetShaderModeParameter(GetRoot().Shader.Mode); ShaderAttributes.GetInstance().SetLightPositionParameter(rootNode.LightPosition); // No change in stencil buffer GL.StencilOp(StencilOp.Keep, StencilOp.Keep, StencilOp.Keep); // Draw if stencil value is > 0 GL.StencilFunc(StencilFunction.Always, 0, 255); rootNode.Traverse(RenderMode.REGULAR, Matrix4.Identity); }
/** * Draw using the VBO * */ public void Draw() { if (vertexBufferId < 0 || normalBufferId < 0 || colorBufferId < 0 || indexBufferId < 0 || texCoordBufferId < 0) { Init(); } GL.EnableVertexAttribArray( ShaderAttributes.GetInstance().LocationPosition); GL.EnableVertexAttribArray( ShaderAttributes.GetInstance().LocationNormal); GL.EnableVertexAttribArray( ShaderAttributes.GetInstance().LocationColor); GL.EnableVertexAttribArray( ShaderAttributes.GetInstance().LocationTexCoords); GL.BindBuffer(BufferTarget.ArrayBuffer, vertexBufferId); GL.VertexAttribPointer(ShaderAttributes.GetInstance().LocationPosition, 3, VertexAttribPointerType.Float, false, 0, IntPtr.Zero); GL.BindBuffer(BufferTarget.ArrayBuffer, normalBufferId); GL.VertexAttribPointer(ShaderAttributes.GetInstance().LocationNormal, 3, VertexAttribPointerType.Float, false, 0, IntPtr.Zero); GL.BindBuffer(BufferTarget.ArrayBuffer, colorBufferId); GL.VertexAttribPointer(ShaderAttributes.GetInstance().LocationColor, 4, VertexAttribPointerType.Float, false, 0, IntPtr.Zero); GL.BindBuffer(BufferTarget.ArrayBuffer, texCoordBufferId); GL.VertexAttribPointer(ShaderAttributes.GetInstance().LocationTexCoords, 2, VertexAttribPointerType.Float, false, 0, IntPtr.Zero); GL.DrawArrays(primitiveType, 0, renderVertices.Count); Shader.CheckError(); }
public override void Traverse(RenderMode mode, Matrix4 modelMatrix) { ShaderAttributes.GetInstance().SetModelMatrixParameter(modelMatrix); ShaderAttributes.GetInstance().SetViewMatrixParameter(GetRootNode().Camera.GetViewMatrix()); DrawGL(mode, modelMatrix); }
private void DrawShadowVolume() { GL.ClearStencil(0); GL.Clear(ClearBufferMask.StencilBufferBit); // ************* ORIGINAL SCENE - NO LIGHTING ************* // Stencil buffer never changed GL.StencilOp(StencilOp.Keep, StencilOp.Keep, StencilOp.Keep); // Stencil test always passed GL.StencilFunc(StencilFunction.Always, 0, 255); // Setup shader shader.Mode = Shader.ShaderMode.AMBIENT_ONLY; ShaderAttributes.GetInstance().SetCameraEyeParameter(camera.Eye); ShaderAttributes.GetInstance().SetShaderModeParameter(GetRoot().Shader.Mode); ShaderAttributes.GetInstance().SetLightPositionParameter(rootNode.LightPosition); // Render scene w/o lighting rootNode.Traverse(RenderMode.REGULAR, Matrix4.Identity); // ************* SHADOW VOLUMS BACK FACES ************* // Disable writes to the depth and color buffers. GL.DepthMask(false); GL.ColorMask(false, false, false, false); // Use back-face culling. GL.CullFace(CullFaceMode.Back); // Stencil buffer always inc GL.StencilOp(StencilOp.Keep, StencilOp.Keep, StencilOp.Incr); // Stencil test always passed GL.StencilFunc(StencilFunction.Always, 0, 255); // Render the shadow volumes (because of culling, only their front faces are rendered). rootNode.Traverse(RenderMode.SHADOW_VOLUME, Matrix4.Identity); // ************* SHADOW VOLUMS FRONT FACES ************* // GL.Begin (PrimitiveType.Quads); // GL.Vertex3 (-0.5f, 0.5, -0.5f); // GL.Vertex3 (-0.5f, 0.5, 0.5f); // GL.Vertex3 (0.5f, 0.5, 0.5f); // GL.Vertex3 (0.5f, 0.5, -0.5f); // GL.End (); // // Use front-face culling. GL.CullFace(CullFaceMode.Front); // Set the stencil operation to decrement on depth pass. GL.StencilOp(StencilOp.Keep, StencilOp.Keep, StencilOp.Decr); // Render the shadow volumes (only their back faces are rendered). rootNode.Traverse(RenderMode.SHADOW_VOLUME, Matrix4.Identity); // ************* ORIGINAL SCENE ************* // No change in stencil buffer GL.StencilOp(StencilOp.Keep, StencilOp.Keep, StencilOp.Keep); // Draw if stencil value is > 0 GL.StencilFunc(StencilFunction.Equal, 0, 255); GL.DepthMask(true); GL.ColorMask(true, true, true, true); GL.CullFace(CullFaceMode.Back); GL.Clear(ClearBufferMask.DepthBufferBit); shader.Mode = Shader.ShaderMode.PHONG; ShaderAttributes.GetInstance().SetShaderModeParameter(GetRoot().Shader.Mode); rootNode.Traverse(RenderMode.REGULAR, Matrix4.Identity); }