Ejemplo n.º 1
0
        public unsafe override void OnNewFrame(float totalSeconds, float elapsedSeconds)
        {
            float   angle = totalSeconds * 0.125f;
            Matrix4 world = Matrix4.CreateRotationX(angle) * Matrix4.CreateRotationY(2 * angle) * Matrix4.CreateRotationZ(3 * angle);

            world.Transpose();

            Vector3 cameraPosition = new Vector3(5, 3, 0);

            Matrix4 view    = Matrix4.LookAt(cameraPosition, Vector3.Zero, Vector3.UnitZ);
            Matrix4 proj    = Matrix4.CreatePerspectiveFieldOfView((float)Math.PI / 4f, 1f, 0.1f, 1000f);
            Matrix4 invertY = Matrix4.Identity;

            invertY.M22 = -1f;
            Matrix4 viewProjection = view * proj * invertY;

            viewProjection.Transpose();

            Vector3 lightPosition = new Vector3(10, -7, 2);

            transformBuffer.SetDataByMapping((IntPtr)(&world));
            cameraBuffer.SetDataByMapping((IntPtr)(&viewProjection));
            cameraExtraBuffer.SetDataByMapping((IntPtr)(&cameraPosition));
            lightBuffer.SetDataByMapping((IntPtr)(&lightPosition));

            framebuffer.ClearColor(0, new Color4(0.4f, 0.6f, 0.9f, 1.0f));
            framebuffer.ClearDepthStencil(DepthStencil.Both, 1f, 0);

            Context.Actions.ClearWindowColor(new Color4(0, 0, 0, 1));
            Context.Actions.ClearWindowDepthStencil(DepthStencil.Both, 1f, 0);

            Context.States.ScreenClipping.United.Viewport.Set(RenderTargetSize, RenderTargetSize);

            Context.States.Rasterizer.FrontFace.Set(FrontFaceDirection.Cw);
            Context.States.Rasterizer.CullFaceEnable.Set(true);
            Context.States.Rasterizer.CullFace.Set(CullFaceMode.Front);

            Context.States.DepthStencil.DepthTestEnable.Set(true);
            Context.States.DepthStencil.DepthMask.Set(true);

            Context.Bindings.Framebuffers.Draw.Set(framebuffer);

            Context.Bindings.Program.Set(program);
            Context.Bindings.VertexArray.Set(vertexArray);
            Context.Bindings.Buffers.UniformIndexed[0].Set(transformBuffer);
            Context.Bindings.Buffers.UniformIndexed[1].Set(cameraBuffer);
            Context.Bindings.Buffers.UniformIndexed[2].Set(lightBuffer);
            Context.Bindings.Textures.Units[0].Set(diffuseMap);
            Context.Bindings.Samplers[0].Set(sampler);

            // Inside cube

            Context.Actions.Draw.Elements(BeginMode.Triangles, 36, DrawElementsType.UnsignedShort, 0);
            renderTarget.GenerateMipmap();

            // Outside cube
            proj = Matrix4.CreatePerspectiveFieldOfView((float)Math.PI / 4f, (float)GameWindow.ClientSize.Width / GameWindow.ClientSize.Height, 0.1f, 1000f);

            viewProjection = view * proj;
            viewProjection.Transpose();

#if INTEL_WORKAROUND
            cameraOutsideBuffer.SetDataByMapping((IntPtr)(&viewProjection));
#else
            cameraBuffer.SetDataByMapping((IntPtr)(&viewProjection));
#endif
            Context.States.ScreenClipping.United.Viewport.Set(GameWindow.ClientSize.Width, GameWindow.ClientSize.Height);
            Context.States.Rasterizer.FrontFace.Set(FrontFaceDirection.Ccw);

            Context.Bindings.Framebuffers.Draw.Set(null);
#if INTEL_WORKAROUND
            Context.Pipeline.UniformBuffers[1] = cameraOutsideBuffer;
#endif
            Context.Bindings.Textures.Units[0].Set(renderTarget);
            Context.Bindings.Samplers[0].Set(sampler);

            Context.Actions.Draw.Elements(BeginMode.Triangles, 36, DrawElementsType.UnsignedShort, 0);
        }