コード例 #1
0
        public void Render(List<Mesh> meshes, Camera cam)
        {
            ClearGBuffer();
            RenderGBuffer(meshes, cam);
            RenderLights(cam);

            device.BlendState = BlendState.Opaque;
            device.RasterizerState = RasterizerState.CullCounterClockwise;
            device.DepthStencilState = DepthStencilState.Default;

            Composite(null);
            //Test drawing
            //device.SetRenderTargets(null);
            //RenderTexturedQuad(lightTarget);
        }
コード例 #2
0
ファイル: PrimitiveBatch.cs プロジェクト: kekraft/XNAProject
        /// <summary>
        /// Given a vertex buffer and a primitive type, draws the contents of buffer.
        /// </summary>
        /// <param name="buffer">The vertex buffer to render</param>
        /// <param name="primType">The primtitive type to use</param>
        /// <param name="cam"></param>
        public void RenderVertexBuffer(VertexBuffer buffer, PrimitiveType primType, Camera cam)
        {
            device.SetVertexBuffer(buffer);
            effect.View = currentCam.View;
            effect.Projection = currentCam.Projection;

            effect.CurrentTechnique.Passes[0].Apply();
            int passes = buffer.VertexCount / maxVertsPerDraw;
            int remainder = buffer.VertexCount % maxVertsPerDraw;
            int offset = 0;
            for (int i = 0; i < passes; i++)
            {
                device.DrawPrimitives(primType, offset, maxVertsPerDraw / numVertsPerPrimitive(primType));
                offset += maxVertsPerDraw;
            }

            device.DrawPrimitives(primType, offset, remainder / numVertsPerPrimitive(primType));
        }
コード例 #3
0
ファイル: PrimitiveBatch.cs プロジェクト: kekraft/XNAProject
        public void DrawMesh(Mesh mesh, Matrix world, Camera cam)
        {
            Matrix previousWorld = effect.World;

            device.SetVertexBuffer(mesh.Vertices);
            device.Indices = mesh.Indices;
            effect.View = cam.View;
            effect.Projection = cam.Projection;
            effect.World = world;
            //effect.VertexColorEnabled = false;
            //effect.TextureEnabled = true;
            effect.PreferPerPixelLighting = true;
            effect.EnableDefaultLighting();

            effect.CurrentTechnique.Passes[0].Apply();
            int passes = mesh.Vertices.VertexCount / maxVertsPerDraw;
            int remainder = mesh.Vertices.VertexCount % maxVertsPerDraw;
            int offset = 0;
            for (int i = 0; i < passes; i++)
            {
                device.DrawPrimitives(PrimitiveType.TriangleList, offset, maxVertsPerDraw / numVertsPerPrimitive(PrimitiveType.TriangleList));
                offset += maxVertsPerDraw;
            }

            //device.DrawPrimitives(PrimitiveType.TriangleList, offset, remainder / numVertsPerPrimitive(PrimitiveType.TriangleList));
            device.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, mesh.Vertices.VertexCount, 0, mesh.Indices.IndexCount / 3);

            effect.VertexColorEnabled = true;
            effect.LightingEnabled = false;
            effect.World = previousWorld;
            //effect.TextureEnabled = false;
            //effect.PreferPerPixelLighting = false;
        }
コード例 #4
0
ファイル: PrimitiveBatch.cs プロジェクト: kekraft/XNAProject
        /// <summary>
        /// Begins the batch process for drawing 3D primitives
        /// </summary>
        /// <param name="primType">Only line list and triangle list are supported</param>
        /// <param name="cam">The camera to render with</param>
        public void Begin(PrimitiveType primType, Camera cam)
        {
            if (hasBegun)
            {
                throw new Exception("Can't call Begin until current batch has ended");
            }

            vertCounter = 0;
            vertsPerPrimitive = numVertsPerPrimitive(primType);
            currentCam = cam;
            currentType = primType;

            hasBegun = true;
        }
コード例 #5
0
        public void DrawMesh(Mesh mesh, Matrix world, Camera cam, Vector3 color)
        {
            Matrix previousWorld = effect.World;

            device.SetVertexBuffer(mesh.Vertices);
            device.Indices = mesh.Indices;

            device.BlendState = BlendState.AlphaBlend;
            effect.View = cam.View;
            effect.Projection = cam.Projection;
            effect.World = world;
            effect.VertexColorEnabled = false;
            effect.TextureEnabled = false;

            //Custom Effect:
            effect.SpecularColor = new Vector3(.025f, .05f, .025f);
            effect.EmissiveColor = new Vector3(0.02f, 0.02f, 0.02f);
            //effect.EmissiveColor = color;
            Vector3 lastDiffuse = effect.DiffuseColor;
            effect.DiffuseColor = color;
            effect.FogEnabled = true;
            effect.FogEnd = 35f;
            effect.FogColor = new Vector3(.025f, .05f, .025f);
            //End custom effect

            if (mesh.Texture != null)
            {
                effect.Texture = mesh.Texture;
                effect.TextureEnabled = true;
            }
            effect.PreferPerPixelLighting = true;
            effect.EnableDefaultLighting();
            effect.CurrentTechnique.Passes[0].Apply();
            int passes = mesh.Vertices.VertexCount / maxVertsPerDraw;
            int remainder = mesh.Vertices.VertexCount % maxVertsPerDraw;
            int offset = 0;
            for (int i = 0; i < passes; i++)
            {
                device.DrawPrimitives(PrimitiveType.TriangleList, offset, maxVertsPerDraw / numVertsPerPrimitive(PrimitiveType.TriangleList));
                offset += maxVertsPerDraw;
            }

            //device.DrawPrimitives(PrimitiveType.TriangleList, offset, remainder / numVertsPerPrimitive(PrimitiveType.TriangleList));
            device.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, mesh.Vertices.VertexCount, 0, mesh.Indices.IndexCount / 3);

            effect.VertexColorEnabled = true;
            effect.LightingEnabled = false;
            effect.World = previousWorld;
            effect.TextureEnabled = false;
            effect.Alpha = 1;
            //device.BlendState = BlendState.Opaque;
            //effect.PreferPerPixelLighting = false;
            effect.DiffuseColor = lastDiffuse;
        }
コード例 #6
0
        public void DrawMesh(Mesh mesh, Matrix world, Camera cam, Effect customEffect)
        {
            device.SetVertexBuffer(mesh.Vertices);
            device.Indices = mesh.Indices;

            device.BlendState = BlendState.AlphaBlend;

            customEffect.Parameters["View"].SetValue(cam.View);
            customEffect.Parameters["Projection"].SetValue(cam.Projection);
            customEffect.Parameters["World"].SetValue(world);

            if (mesh.Texture != null)
            {
                customEffect.Parameters["Texture"].SetValue(mesh.Texture);
                customEffect.Parameters["NormalMap"].SetValue(mesh.NormalMap);
            }

            customEffect.CurrentTechnique.Passes[0].Apply();
            int passes = mesh.Vertices.VertexCount / maxVertsPerDraw;
            int remainder = mesh.Vertices.VertexCount % maxVertsPerDraw;
            int offset = 0;
            for (int i = 0; i < passes; i++)
            {
                device.DrawPrimitives(PrimitiveType.TriangleList, offset, maxVertsPerDraw / numVertsPerPrimitive(PrimitiveType.TriangleList));
                offset += maxVertsPerDraw;
            }

            device.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, mesh.Vertices.VertexCount, 0, mesh.Indices.IndexCount / 3);
        }
コード例 #7
0
ファイル: Map.cs プロジェクト: bschwind/Sky-Slider
 public void DrawInstanced(GameTime g, PrimitiveBatch batch, Camera cam, Effect customEffect)
 {
     foreach (byte b in blockTransforms.Keys)
     {
         batch.DrawInstancedMesh(BlockData.GetMeshFromID(b), cam, customEffect, blockTransforms[b]);
     }
 }
コード例 #8
0
ファイル: Map.cs プロジェクト: bschwind/Sky-Slider
        /// <summary>
        /// Draws each block
        /// </summary>
        /// <param name="g"></param>
        /// <param name="batch"></param>
        /// <param name="cam"></param>
        public void DebugDraw(GameTime g, PrimitiveBatch batch, Camera cam, Effect customEffect)
        {
            for (int x = 0; x < width; x++)
            {
                for (int y = 0; y < height; y++)
                {
                    for (int z = 0; z < depth; z++)
                    {
                        byte type = blocks[x, y, z].Type;
                        if (type == 0)
                        {
                            continue;
                        }

                        batch.DrawMesh(BlockData.GetMeshFromID(blocks[x, y, z].Type), BlockData.GetRotationMatrix(blocks[x,y,z]) * Matrix.CreateTranslation(new Vector3(x + 0.5f, y + 0.5f, z + 0.5f)), cam, customEffect);
                    }
                }
            }
        }
コード例 #9
0
        private void RenderLights(Camera cam)
        {
            device.SetRenderTarget(lightTarget);
            //Clear the light target to black
            device.Clear(Color.Transparent);
            device.BlendState = LightMapBS;
            device.DepthStencilState = DepthStencilState.DepthRead;
            //Color Sampler
            device.Textures[0] = colorTarget;
            device.SamplerStates[0] = SamplerState.PointClamp;
            //Normal Sampler
            device.Textures[1] = normalTarget;
            device.SamplerStates[1] = SamplerState.PointClamp;
            //Depth
            device.Textures[2] = depthTarget;
            device.SamplerStates[2] = SamplerState.PointClamp;

            Matrix inverseView = Matrix.Invert(cam.View);
            Matrix inverseViewProj = Matrix.Invert(cam.View * cam.Projection);

            LightsEffect.Parameters["InverseViewProj"].SetValue(inverseViewProj);
            LightsEffect.Parameters["InverseView"].SetValue(inverseView);
            LightsEffect.Parameters["CamPos"].SetValue(cam.Pos);
            LightsEffect.Parameters["GBufferSize"].SetValue(new Vector2(Config.ScreenWidth, Config.ScreenHeight));

            LightsEffect.CurrentTechnique = LightsEffect.Techniques["DirectionalLight"];
            LightsEffect.CurrentTechnique.Passes[0].Apply();

            foreach (GlobalLight g in globalLights)
            {
                RenderGlobalLight(g);
            }
        }
コード例 #10
0
        private void RenderGBuffer(List<Mesh> meshes, Camera cam)
        {
            device.DepthStencilState = DepthStencilState.Default;

            DeferredEffect.CurrentTechnique = DeferredEffect.Techniques["GenerateBuffer"];
            DeferredEffect.CurrentTechnique.Passes[0].Apply();
            DeferredEffect.Parameters["View"].SetValue(cam.View);
            DeferredEffect.Parameters["Projection"].SetValue(cam.Projection);

            device.SetRenderTargets(colorTarget, normalTarget, depthTarget);

            foreach (Mesh m in meshes)
            {
                DeferredEffect.Parameters["World"].SetValue(Matrix.Identity);
                device.Textures[0] = m.Texture;
                device.SetVertexBuffer(m.Vertices);
                device.Indices = m.Indices;

                device.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, m.Vertices.VertexCount, 0, m.Indices.IndexCount / 3);
            }
        }