示例#1
0
        public static void RenderParticles()
        {
            if (!IsOn)
            {
                return;
            }

            //set effect vars
            Matrix g_WorldMatrix       = Matrix.Identity;
            Matrix WorldViewMatrix     = g_WorldMatrix * Camera.ViewMatrix;
            Matrix WorldViewProjMatrix = WorldViewMatrix * Camera.ProjectionMatrix;

            if (Game.Time.TotalGameTime < 2)
            {
                rainEff.GetVar("g_FrameRate").AsScalar().Set(40);
            }
            else
            {
                rainEff.GetVar("g_FrameRate").AsScalar().Set(Game.Time.FramesPerSecond);
            }

            rainEff.GetVar("g_TotalVel").AsVector().Set(Wind.interpolatedWind * settings.RainSpeed);
            rainEff.GetVar("g_eyePos").AsVector().Set(Camera.Position);
            rainEff.GetVar("g_mWorld").AsMatrix().SetMatrix(g_WorldMatrix);
            rainEff.GetVar("g_mWorldView").AsMatrix().SetMatrix(WorldViewMatrix);
            rainEff.GetVar("g_mWorldViewProj").AsMatrix().SetMatrix(WorldViewProjMatrix);
            rainEff.GetVar("g_lightPos").AsVector().Set(Camera.Forward * 5000);
            rainEff.GetVar("g_mProjection").AsMatrix().SetMatrix(Camera.ProjectionMatrix);
            rainEff.GetVar("dirLightIntensity").AsScalar().Set(settings.DirLightIntensity);
            rainEff.GetVar("g_ResponseDirLight").AsScalar().Set(settings.ResponseDirLight);


            List <Vector4> pointLightPositions = new List <Vector4>();
            List <Vector4> pointLightColors    = new List <Vector4>();
            List <Vector4> spotLightPositions  = new List <Vector4>();
            List <Vector4> spotLightColors     = new List <Vector4>();
            List <Vector4> spotLightDirections = new List <Vector4>();
            List <Vector4> spotLightAngles     = new List <Vector4>();

            List <Vector4> areaLightPositions1 = new List <Vector4>();
            List <Vector4> areaLightPositions2 = new List <Vector4>();
            List <Vector4> areaLightColors     = new List <Vector4>();
            List <Vector4> areaLightVals       = new List <Vector4>();
            List <Cell>    cells = MainPlayer.CurrentWorldSpace == null ? new List <Cell>() : MainPlayer.CurrentWorldSpace.CellsToDraw;

            foreach (Cell c in cells)
            {
                Dictionary <String, GameObjectReference> lights = MainPlayer.CurrentWorldSpace.GetLightsToDraw(c);
                foreach (GameObjectReference objRef in lights.Values)
                {
                    if (!objRef.IsOn)
                    {
                        continue;
                    }
                    float intensity = 1;
                    Light l         = objRef.BaseGameObject as Light;
                    if (objRef.Intensity < 1)
                    {
                        intensity = objRef.Intensity;
                    }
                    if (l is PointLight)
                    {
                        pointLightPositions.Add(new Vector4(WorldSpace.GetRealWorldPos(objRef.Position, c), objRef.MaxRange));
                        pointLightColors.Add(new Vector4(objRef.LightColor * intensity, 1));
                    }
                    else if (l is SpotLight)
                    {
                        spotLightPositions.Add(new Vector4(WorldSpace.GetRealWorldPos(objRef.Position, c), objRef.MaxRange));
                        spotLightColors.Add(new Vector4(objRef.LightColor * intensity, 1));
                        Vector3 worldSpaceLightDirection = Vector3.Normalize(WorldSpace.GetRealWorldPos(objRef.Target, c) - WorldSpace.GetRealWorldPos(objRef.Position, c));
                        spotLightDirections.Add(new Vector4(worldSpaceLightDirection, 1));
                        spotLightAngles.Add(new Vector4((float)Math.Cos(objRef.InnerAngle * 0.5f), (float)Math.Cos(objRef.OuterAngle * 0.5f), 1, 1));
                    }
                    else if (l is AreaLight)
                    {
                        areaLightPositions1.Add(new Vector4(WorldSpace.GetRealWorldPos(objRef.Position, c), 1));
                        areaLightPositions2.Add(new Vector4(WorldSpace.GetRealWorldPos(objRef.EndPoint, c), 1));
                        areaLightColors.Add(new Vector4(objRef.LightColor * intensity, 1));
                        areaLightVals.Add(new Vector4(objRef.MaxRange, objRef.LerpInc, 1, 1));
                    }
                }
            }
            if (pointLightPositions.Count > 0)
            {
                rainEff.GetVar("g_PointLightPositions").AsVector().Set(pointLightPositions.ToArray());
            }
            if (pointLightColors.Count > 0)
            {
                rainEff.GetVar("g_PointLightColors").AsVector().Set(pointLightColors.ToArray());
            }
            rainEff.GetVar("numPointLights").AsScalar().Set(pointLightPositions.Count);

            if (spotLightPositions.Count > 0)
            {
                rainEff.GetVar("g_SpotLightPositions").AsVector().Set(spotLightPositions.ToArray());
            }
            if (spotLightColors.Count > 0)
            {
                rainEff.GetVar("g_SpotLightColors").AsVector().Set(spotLightColors.ToArray());
            }
            if (spotLightDirections.Count > 0)
            {
                rainEff.GetVar("g_SpotLightDirections").AsVector().Set(spotLightDirections.ToArray());
            }
            if (spotLightAngles.Count > 0)
            {
                rainEff.GetVar("g_SpotLightAngles").AsVector().Set(spotLightAngles.ToArray());
            }
            rainEff.GetVar("numSpotLights").AsScalar().Set(spotLightPositions.Count);

            if (areaLightPositions1.Count > 0)
            {
                rainEff.GetVar("g_AreaLightPositions1").AsVector().Set(areaLightPositions1.ToArray());
            }
            if (areaLightPositions2.Count > 0)
            {
                rainEff.GetVar("g_AreaLightPositions2").AsVector().Set(areaLightPositions2.ToArray());
            }
            if (areaLightColors.Count > 0)
            {
                rainEff.GetVar("g_AreaLightColors").AsVector().Set(areaLightColors.ToArray());
            }
            if (areaLightVals.Count > 0)
            {
                rainEff.GetVar("g_AreaLightVals").AsVector().Set(areaLightVals.ToArray());
            }
            rainEff.GetVar("numAreaLights").AsScalar().Set(areaLightPositions1.Count);


            if (Game.Time.TotalGameTime > 1)
            {
                EffectTechnique AdvanceParticles          = rainEff.EffectObj.GetTechniqueByName("AdvanceParticles");
                InputLayout     g_pVertexLayoutRainVertex = ShaderHelper.ConstructInputLayout(MeshInputElements10.RainVertex, AdvanceParticles.GetPassByIndex(0).Description.Signature);
                Game.Device.InputAssembler.SetPrimitiveTopology(PrimitiveTopology.PointList);
                Game.Device.InputAssembler.SetInputLayout(g_pVertexLayoutRainVertex);
                D3D10.Buffer pBuffers = firstFrame ? g_pParticleStart : g_pParticleDrawFrom;
                Game.Device.InputAssembler.SetVertexBuffers(0, new VertexBufferBinding(pBuffers, MeshInputElements10.GetStride(MeshInputElements10.RainVertex), 0));

                // Point to the correct output buffer
                pBuffers = g_pParticleStreamTo;
                Game.Device.StreamOutput.SetTargets(new StreamOutputBufferBinding(pBuffers, 0));

                // draw
                AdvanceParticles.GetPassByIndex(0).Apply();
                Game.Device.Draw(g_numRainVertices, 0);

                // Get back to normal
                pBuffers = null;
                Game.Device.StreamOutput.SetTargets(null);

                // Swap buffers
                D3D10.Buffer pTemp = g_pParticleDrawFrom;
                g_pParticleDrawFrom = g_pParticleStreamTo;
                g_pParticleStreamTo = pTemp;

                firstFrame = false;
            }


            //draw rain particles
            EffectTechnique RenderParticlesCheap = rainEff.EffectObj.GetTechniqueByName("RenderParticles");

            Game.Device.InputAssembler.SetPrimitiveTopology(PrimitiveTopology.PointList);
            InputLayout g_pVertexLayoutRainVertex2 = ShaderHelper.ConstructInputLayout(MeshInputElements10.RainVertex, RenderParticlesCheap.GetPassByIndex(0).Description.Signature);

            Game.Device.InputAssembler.SetInputLayout(g_pVertexLayoutRainVertex2);
            Game.Device.InputAssembler.SetVertexBuffers(0, new VertexBufferBinding(g_pParticleDrawFrom, MeshInputElements10.GetStride(MeshInputElements10.RainVertex), 0));
            RenderParticlesCheap.GetPassByIndex(0).Apply();
            Game.Device.Draw((int)(settings.PercentDrawParticles * g_numRainVertices), 0);
        }
        public static void RenderBoundingBox(BoundingBox bb, Matrix mWorld)
        {
            Vector3[] corners = bb.GetCorners();

            DataStream s = new DataStream(corners.Length * Marshal.SizeOf(typeof(Vector3)), true, true);

            s.WriteRange(corners);
            s.Position = 0;

            BufferDescription bufferDescription = new BufferDescription();

            bufferDescription.BindFlags      = BindFlags.VertexBuffer;
            bufferDescription.CpuAccessFlags = CpuAccessFlags.None;
            bufferDescription.OptionFlags    = ResourceOptionFlags.None;
            bufferDescription.SizeInBytes    = corners.Length * Marshal.SizeOf(typeof(Vector3));
            bufferDescription.Usage          = ResourceUsage.Default;

            D3D10.Buffer vertices = new D3D10.Buffer(Game.Device, s, bufferDescription);
            s.Close();


            short[] IB = new short[]
            {
                0, 1,
                1, 2,
                2, 3,
                3, 0,
                0, 4,
                1, 5,
                2, 6,
                3, 7,
                4, 5,
                5, 6,
                6, 7,
                7, 4,
            };
            s = new DataStream(IB.Length * Marshal.SizeOf(typeof(short)), true, true);
            s.WriteRange(IB);
            s.Position                       = 0;
            bufferDescription                = new BufferDescription();
            bufferDescription.BindFlags      = BindFlags.IndexBuffer;
            bufferDescription.CpuAccessFlags = CpuAccessFlags.None;
            bufferDescription.OptionFlags    = ResourceOptionFlags.None;
            bufferDescription.SizeInBytes    = IB.Length * Marshal.SizeOf(typeof(short));
            bufferDescription.Usage          = ResourceUsage.Default;

            D3D10.Buffer indices = new D3D10.Buffer(Game.Device, s, bufferDescription);
            s.Close();



            Shader          effect = WorldData.GetObject("SimpleTexturedQuad.fx") as Shader;
            EffectTechnique tech   = effect.EffectObj.GetTechniqueByName("RenderLightVolume");
            EffectPass      pass   = tech.GetPassByIndex(0);
            InputLayout     layout = ShaderHelper.ConstructInputLayout(MeshInputElements10.PositionOnly3, pass.Description.Signature);

            Game.Device.InputAssembler.SetPrimitiveTopology(PrimitiveTopology.LineList);
            Game.Device.InputAssembler.SetIndexBuffer(indices, Format.R16_UInt, 0);
            Game.Device.InputAssembler.SetVertexBuffers(0, new VertexBufferBinding(vertices, MeshInputElements10.GetStride(MeshInputElements10.PositionOnly3), 0));
            Game.Device.InputAssembler.SetInputLayout(layout);

            effect.GetVar("WorldViewProj").AsMatrix().SetMatrix(mWorld * Camera.ViewMatrix * Camera.ProjectionMatrix);
            pass.Apply();
            Game.Device.DrawIndexed(IB.Length, 0, 0);
        }
        public static void RenderNormals(Model gm, Matrix mWorld)
        {
            byte[] vertices = null;
            byte[] indices = null;
            int    vbSizeInBytes, ibSizeInBytes;

            using (MeshBuffer b = gm.MeshObj.GetVertexBuffer(0))
            {
                vbSizeInBytes = b.SizeInBytes;
                using (DataStream v = b.Map())
                {
                    v.Position = 0;
                    vertices   = new byte[b.SizeInBytes];
                    v.Read(vertices, 0, b.SizeInBytes);
                }
            }
            using (MeshBuffer b = gm.MeshObj.GetIndexBuffer())
            {
                ibSizeInBytes = b.SizeInBytes;
                using (DataStream v = b.Map())
                {
                    v.Position = 0;
                    indices    = new byte[b.SizeInBytes];
                    v.Read(indices, 0, b.SizeInBytes);
                }
            }


            DataStream s = new DataStream(vbSizeInBytes, true, true);

            s.WriteRange(vertices);
            s.Position = 0;
            BufferDescription bufferDescription = new BufferDescription();

            bufferDescription.BindFlags      = BindFlags.VertexBuffer;
            bufferDescription.CpuAccessFlags = CpuAccessFlags.None;
            bufferDescription.OptionFlags    = ResourceOptionFlags.None;
            bufferDescription.SizeInBytes    = vbSizeInBytes;
            bufferDescription.Usage          = ResourceUsage.Default;
            D3D10.Buffer verticesBuffer = new D3D10.Buffer(Game.Device, s, bufferDescription);
            s.Close();

            s = new DataStream(ibSizeInBytes, true, true);
            s.WriteRange(indices);
            s.Position                       = 0;
            bufferDescription                = new BufferDescription();
            bufferDescription.BindFlags      = BindFlags.IndexBuffer;
            bufferDescription.CpuAccessFlags = CpuAccessFlags.None;
            bufferDescription.OptionFlags    = ResourceOptionFlags.None;
            bufferDescription.SizeInBytes    = ibSizeInBytes;
            bufferDescription.Usage          = ResourceUsage.Default;
            D3D10.Buffer indicesBuffer = new D3D10.Buffer(Game.Device, s, bufferDescription);
            s.Close();

            Shader          effect = WorldData.GetObject("debugger.fx") as Shader;
            EffectTechnique tech   = effect.EffectObj.GetTechniqueByName("RenderNormals");
            EffectPass      pass   = tech.GetPassByIndex(0);
            InputLayout     layout = ShaderHelper.ConstructInputLayout(gm.Mesh3d.inputElements, pass.Description.Signature);

            Game.Device.InputAssembler.SetPrimitiveTopology(PrimitiveTopology.TriangleList);
            Game.Device.InputAssembler.SetIndexBuffer(indicesBuffer, Format.R16_UInt, 0);
            Game.Device.InputAssembler.SetVertexBuffers(0, new VertexBufferBinding(verticesBuffer, MeshInputElements10.GetStride(gm.Mesh3d.inputElements), 0));

            Game.Device.InputAssembler.SetInputLayout(layout);

            effect.GetVar("WorldViewProj").AsMatrix().SetMatrix(mWorld * Camera.ViewMatrix * Camera.ProjectionMatrix);
            pass.Apply();
            Game.Device.DrawIndexed(gm.MeshObj.FaceCount * 3, 0, 0);
        }
        public static void DrawLightVolume(Model gm, Matrix mWorld, Vector4 GridColor)
        {
            if (gm == null || gm.Mesh3d == null)
            {
                return;
            }
            Shader          effect = WorldData.GetObject("SimpleTexturedQuad.fx") as Shader;
            EffectTechnique tech   = effect.EffectObj.GetTechniqueByName("RenderLightVolume");
            EffectPass      pass   = tech.GetPassByIndex(0);
            InputLayout     layout = ShaderHelper.ConstructInputLayout(gm.Mesh3d.inputElements, pass.Description.Signature);

            SlimDX.Direct3D10.Buffer indexBuffer  = gm.MeshObj.GetDeviceIndexBuffer();
            SlimDX.Direct3D10.Buffer vertexBuffer = gm.MeshObj.GetDeviceVertexBuffer(0);

            Game.Device.InputAssembler.SetPrimitiveTopology(PrimitiveTopology.TriangleList);
            Game.Device.InputAssembler.SetIndexBuffer(indexBuffer, Format.R16_UInt, 0);
            Game.Device.InputAssembler.SetVertexBuffers(0, new VertexBufferBinding(vertexBuffer, MeshInputElements10.GetStride(gm.Mesh3d.inputElements), 0));
            Game.Device.InputAssembler.SetInputLayout(layout);

            effect.GetVar("color").AsVector().Set(GridColor);
            effect.GetVar("WorldViewProj").AsMatrix().SetMatrix(mWorld * Camera.ViewMatrix * Camera.ProjectionMatrix);
            pass.Apply();
            Game.Device.DrawIndexed((gm.Mesh3d.attrTable[0].FaceCount * 3), 0, 0);
        }