Пример #1
0
        public void Render(double delta_time)
        {
            #region Depth
            var light                = new Vector3d(10, 10, 0);
            var light_target         = new Vector3d(0, 0, 0);
            var depth_render_context = new MartinZottmann.Game.Graphics.RenderContext()
            {
                Window     = RenderContext.Window,
                Projection = Matrix4d.CreateOrthographicOffCenter(-10, 10, -10, 10, 0, 100),
                //Projection = Matrix4d.Perspective(MathHelper.PiOver2, Camera.Aspect, 0.1, 30),
                View    = Matrix4d.LookAt(light, light_target, Vector3d.UnitY),
                Model   = Matrix4d.Identity,
                Program = ResourceManager.Programs["depth"]
            };
            var depth_bias     = Matrix4d.Scale(0.5, 0.5, 0.5) * Matrix4d.CreateTranslation(0.5, 0.5, 0.5); // Map [-1, 1] to [0, 1]
            var depth_bias_MVP = RenderContext.InvertedView * depth_render_context.ProjectionViewModel * depth_bias;
            ResourceManager.Programs["standard"].UniformLocations["in_DepthBiasMVP"].Set(depth_bias_MVP);
            ResourceManager.Programs["standard"].UniformLocations["in_Texture"].Set(0);
            ResourceManager.Programs["standard"].UniformLocations["in_ShadowTexture"].Set(1);
            ResourceManager.Programs["standard"].UniformLocations["in_LightPosition"].Set(light);
            //GL.Enable(EnableCap.PolygonOffsetFill);
            //GL.PolygonOffset(2.0f, 4.0f);
            //GL.CullFace(CullFaceMode.Front);
            using (new Bind(frame_buffer))
            {
                GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
                GL.Viewport(0, 0, 1024, 1024);
                Draw(delta_time, depth_render_context);
            }
            //GL.CullFace(CullFaceMode.Back);
            //GL.Disable(EnableCap.PolygonOffsetFill);
            #endregion

            #region Render
            GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
            GL.Viewport(0, 0, RenderContext.Window.Width, RenderContext.Window.Height);
            Draw(delta_time, RenderContext);
            #endregion

            //screen.Render(delta_time, screen_render_context);

            #region Debug
            if (game_state_nodes.First.GameState.Debug)
            {
                GL.Viewport(0, 0, RenderContext.Window.Width, RenderContext.Window.Height);
                var debug_render_context = new MartinZottmann.Game.Graphics.RenderContext()
                {
                    Window     = RenderContext.Window,
                    Projection = Matrix4d.CreateOrthographicOffCenter(-1, 1, -1, 1, -1, 1),
                    View       = Matrix4d.Identity,
                    Model      = Matrix4d.Scale(0.25) * Matrix4d.CreateTranslation(-0.75, -0.75, 0)
                };
                var debug_screen = new Model();
                var shape        = new MartinZottmann.Engine.Graphics.Shapes.Quad();
                shape.Vertices[0].Texcoord.Y = 0;
                shape.Vertices[1].Texcoord.Y = 0;
                shape.Vertices[2].Texcoord.Y = 1;
                shape.Vertices[3].Texcoord.Y = 1;
                debug_screen.Mesh(shape);
                debug_screen.Program = ResourceManager.Programs["plain_texture"];
                debug_screen.Program.UniformLocations["in_ModelViewProjection"].Set(debug_render_context.ProjectionViewModel);
                debug_screen.Program.UniformLocations["in_Texture"].Set(0);
                debug_screen.Texture = RenderContext.DepthTexture;
                debug_screen.Draw();
            }
            #endregion
        }
Пример #2
0
        protected void Draw(double delta_time, MartinZottmann.Game.Graphics.RenderContext render_context)
        {
            foreach (var graphic_node in graphic_nodes)
            {
                if (null != graphic_node.Graphic.Model)
                {
                    render_context.Model = graphic_node.Base.Model;

                    var program = null == render_context.Program
                        ? graphic_node.Graphic.Model.Program
                        : render_context.Program;

                    foreach (var s in program.UniformLocations)
                    {
                        switch (s.Key)
                        {
                        case "alpha_cutoff": s.Value.Set(render_context.alpha_cutoff); break;

                        case "in_DepthBiasMVP": break;

                        case "in_ShadowTexture":
                            render_context.DepthTexture.Bind();
                            s.Value.Set(render_context.DepthTexture.BindId);
                            break;

                        case "in_LightPosition": s.Value.Set(new Vector3d(10, 10, 10)); break;     // @todo

                        case "in_Model": s.Value.Set(render_context.Model); break;

                        case "in_View": s.Value.Set(render_context.View); break;

                        case "in_ModelView": s.Value.Set(render_context.ViewModel); break;

                        case "in_ModelViewProjection": s.Value.Set(render_context.ProjectionViewModel); break;

                        case "in_NormalView": s.Value.Set(render_context.Normal); break;

                        case "in_Texture":
                            graphic_node.Graphic.Model.Texture.Bind();
                            s.Value.Set(graphic_node.Graphic.Model.Texture.BindId);
                            break;

                        default: throw new NotImplementedException(s.Key);
                        }
                    }

                    graphic_node.Graphic.Model.Draw(program);

                    foreach (var s in program.UniformLocations)
                    {
                        switch (s.Key)
                        {
                        case "alpha_cutoff": break;

                        case "in_DepthBiasMVP": break;

                        case "in_ShadowTexture":
                            render_context.DepthTexture.UnBind();
                            break;

                        case "in_LightPosition": break;

                        case "in_Model": break;

                        case "in_View": break;

                        case "in_ModelView": break;

                        case "in_ModelViewProjection": break;

                        case "in_NormalView": break;

                        case "in_Texture":
                            graphic_node.Graphic.Model.Texture.UnBind();
                            break;

                        default: throw new NotImplementedException(s.Key);
                        }
                    }
                }
#if DEBUG
                if (game_state_nodes.First.GameState.Debug)
                {
                    if (null != graphic_node.ModelDebugOrientation)
                    {
                        GL.LineWidth(5);
                        graphic_node.ModelDebugOrientation.Program.UniformLocations["in_ModelViewProjection"].Set(Matrix4d.CreateTranslation(graphic_node.Base.WorldPosition) * Camera.ViewMatrix * Camera.ProjectionMatrix);
                        graphic_node.ModelDebugOrientation.Draw();
                        GL.LineWidth(1);
                    }
                    if (null != graphic_node.ModelDebugPhysic)
                    {
                        graphic_node.ModelDebugPhysic.Program.UniformLocations["in_Color"].Set(graphic_node.Base.Mark);
                        graphic_node.ModelDebugPhysic.Program.UniformLocations["in_ModelViewProjection"].Set(Matrix4d.CreateTranslation(graphic_node.Base.WorldPosition) * Camera.ViewMatrix * Camera.ProjectionMatrix);
                        graphic_node.ModelDebugPhysic.Draw();
                    }
                }
#endif
            }
        }