Example #1
0
        void RenderToScreen()
        {
            Mesh mesh = quadMesh;
            ProgramDeprecated program  = texturedProgram;
            MeshMode          meshMode = MeshMode.PolygonFill;

            GL.ClearColor(0.5f, 0.5f, 0.5f, 1.0f);
            GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit | ClearBufferMask.StencilBufferBit);

            UseViewport(windowViewport);
            program.Use();

            //  We animate one uniform every frame, so we also need to upload uniforms
            //  This covers changes to camera uniforms caused by resizes
            double time = (double)(System.Environment.TickCount);
            float  t    = (float)(System.Math.Sin(time * 0.01) * 0.5f + 0.5f);

            (parameters["t"] as Floats).Set(t);

            program.ApplyUniforms();

            var attributeBindings = mesh.AttributeBindings(program, meshMode);

            SetupAttributeBindings(attributeBindings);
            EnsureUploaded(mesh);

            BufferRange vertexBufferRange = mesh.VertexBufferRange;
            BufferRange indexBufferRange  = mesh.IndexBufferRange(MeshMode.PolygonFill);

            GL.DrawElementsBaseVertex(
                indexBufferRange.BeginMode,
                (int)indexBufferRange.Count,
                indexBufferRange.Buffer.DrawElementsType,
                (IntPtr)(indexBufferRange.OffsetBytes),
                vertexBufferRange.BaseVertex
                );

            DisableAttributeBindings(attributeBindings);
        }
Example #2
0
        protected override void OnLoad(System.EventArgs e)
        {
            //  Check GL version and feature capabilities
            RenderStack.Graphics.Configuration.Initialize();
            if (RenderStack.Graphics.Configuration.canUseFramebufferObject == false)
            {
                throw new System.PlatformNotSupportedException(
                          "GL version 3.0 or GL_ARB_framebuffer_object extension is needed. Neither was found."
                          );
            }
            if (RenderStack.Graphics.Configuration.canUseBaseVertex == false)
            {
                throw new System.PlatformNotSupportedException(
                          "GL version 3.2 or GL_ARB_draw_elements_base_vertex extension is needed. Neither was found."
                          );
            }

            sphereMesh = new GeometryMesh(new Sphere(2.00f, 20, 20), NormalStyle.PointNormals).GetMesh;
            quadMesh   = new GeometryMesh(new QuadXY(1.0f, 1.0f, 0.0f), NormalStyle.PolygonNormals).GetMesh;

            //  Initialize shared resources
            attributeMappings.Add("_position", VertexUsage.Position, 0, 3);
            attributeMappings.Add("_normal", VertexUsage.Normal, 0, 3);
            attributeMappings.Add("_texcoord", VertexUsage.TexCoord, 0, 2);

            UniformMappings.Add("_model_to_world_matrix", LogicalUniform.ModelToWorld);
            UniformMappings.Add("_world_to_model_matrix", LogicalUniform.WorldToModel);
            UniformMappings.Add("_model_to_clip_matrix", LogicalUniform.ModelToClip);
            UniformMappings.Add("_clip_to_model_matrix", LogicalUniform.ClipToModel);
            UniformMappings.Add("_world_to_clip_matrix", LogicalUniform.WorldToClip);
            UniformMappings.Add("_clip_to_world_matrix", LogicalUniform.ClipToWorld);
            UniformMappings.Add("_view_position_in_world", LogicalUniform.ViewPositionInWorld);

            UniformMappings.Add <Floats>("_light_direction", "light_direction");
            UniformMappings.Add <Floats>("_light_color", "light_color");
            UniformMappings.Add <Floats>("_surface_color", "surface_color");

            UniformMappings.Add <Texture>("_texture", "texture");
            UniformMappings.Add <Floats> ("_t", "t");

            parameters["surface_color"]   = new Floats(1.0f, 1.0f, 1.0f);
            parameters["light_direction"] = new Floats(0.0f, 1.0f, 0.0f);
            parameters["light_color"]     = new Floats(1.0f, 1.0f, 1.0f);
            parameters["t"] = new Floats(0.5f);

            //  Initialize resources used by the first pass (render to texture)
            framebuffer = new Framebuffer(128, 128);
            framebuffer.AttachTexture(FramebufferAttachment.ColorAttachment0, PixelFormat.Rgb, PixelInternalFormat.Rgb8);
            framebuffer.AttachRenderBuffer(FramebufferAttachment.DepthAttachment, PixelFormat.DepthComponent, RenderbufferStorage.DepthComponent32, 0);
            framebuffer.Begin();
            framebuffer.Check();
            framebuffer.End();

            cameraOne.Frame.LocalToParent.Set(
                Matrix4.CreateLookAt(
                    new Vector3(0.0f, 0.0f, -4.0f),
                    new Vector3(0.0f, 0.0f, 0.0f),
                    new Vector3(0.0f, 1.0f, 0.0f)
                    )
                );
            cameraOne.FovYRadians    = RenderStack.Math.Conversions.DegreesToRadians(60.0f);
            cameraOne.ProjectionType = ProjectionType.PerspectiveVertical;

            ulong updateSerial = 1;

            cameraOne.Frame.UpdateHierarchical(updateSerial);
            quadFrame.UpdateHierarchical(updateSerial);

            cameraOne.UpdateFrame();
            cameraOne.UpdateViewport(framebuffer);
            cameraOne.UpdateModelFrame(quadFrame);

            if (RenderStack.Graphics.Configuration.glslVersion >= 330)
            {
                diffuseProgram = new ProgramDeprecated(vsDiffuse330, fsDiffuse330);
            }
            else
            {
                diffuseProgram = new ProgramDeprecated(vsDiffuse120, fsDiffuse120);
            }
            diffuseProgram.AttributeMappings = attributeMappings;

            diffuseProgram.Bind(cameraOne);
            diffuseProgram.Bind(parameters);

            //  Initialize resources used by the second pass (render to screen, using texture)
            windowViewport = new Viewport(base.Width, base.Height);

            cameraTwo.Frame.LocalToParent.Set(
                Matrix4.CreateLookAt(
                    new Vector3(0.0f, 0.0f, -2.0f),
                    new Vector3(0.0f, 0.0f, 0.0f),
                    new Vector3(0.0f, 1.0f, 0.0f)
                    )
                );

            cameraTwo.FovYRadians    = RenderStack.Math.Conversions.DegreesToRadians(60.0f);
            cameraTwo.ProjectionType = ProjectionType.PerspectiveVertical;

            cameraTwo.Frame.UpdateHierarchical(updateSerial);
            sphereFrame.UpdateHierarchical(updateSerial);
            cameraTwo.UpdateFrame();
            cameraTwo.UpdateViewport(windowViewport);
            cameraTwo.UpdateModelFrame(sphereFrame);

            parameters["texture"] = framebuffer[FramebufferAttachment.ColorAttachment0];

            if (RenderStack.Graphics.Configuration.glslVersion >= 330)
            {
                texturedProgram = new ProgramDeprecated(vsTextured330, fsTextured330);
            }
            else
            {
                texturedProgram = new ProgramDeprecated(vsTextured120, fsTextured120);
            }
            texturedProgram.AttributeMappings = attributeMappings;

            texturedProgram.Bind(cameraTwo);
            texturedProgram.Bind(parameters);

            diffuseProgram.Use();
            diffuseProgram.ApplyUniforms();

            texturedProgram.Use();
            texturedProgram.ApplyUniforms();

            EnsureUploaded(sphereMesh);
            EnsureUploaded(quadMesh);

            //  Setup some GL state
            GL.Enable(EnableCap.DepthTest);
            GL.Enable(EnableCap.CullFace);

            Resize += new EventHandler <EventArgs>(Application_Resize);
            Unload += new EventHandler <EventArgs>(Application_Unload);

            GL.ClearColor(0.5f, 0.5f, 0.5f, 1.0f);
            GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit | ClearBufferMask.StencilBufferBit);
            SwapBuffers();
            Visible = true;
        }