Exemple #1
0
        private static void DrawMoon()
        {
            Shader          scatterEffect = WorldData.GetObject("scatter.fx") as Shader;
            EffectTechnique tech          = scatterEffect.EffectObj.GetTechniqueByName("RenderMoon");

            Game.Device.InputAssembler.SetPrimitiveTopology(PrimitiveTopology.TriangleList);
            Game.Device.InputAssembler.SetIndexBuffer(moonIndices, Format.R16_UInt, 0);
            Game.Device.InputAssembler.SetVertexBuffers(0, new VertexBufferBinding(moonVerts, 20, 0));
            EffectPass  sunMoonPass = tech.GetPassByIndex(0);
            InputLayout l           = ShaderHelper.ConstructInputLayout(MeshInputElements10.PosTex, sunMoonPass.Description.Signature);

            Game.Device.InputAssembler.SetInputLayout(l);

            int    scale = 15;
            Matrix m     = Matrix.Scaling(1, 1, 1) * Matrix.RotationX(Theta + (float)Math.PI / 2.0f) *
                           Matrix.RotationY(-fPhi + (float)Math.PI / 2.0f) *
                           Matrix.Translation(LightDirection * scale) *
                           Matrix.Translation(Camera.Position);

            scatterEffect.GetVar("WorldViewProjection").AsMatrix().SetMatrix(m * Camera.ViewMatrix * Camera.ProjectionMatrix);
            scatterEffect.GetVar("StarsTex").AsResource().SetResource(moonTex);

            if (Theta < Math.PI / 2.0f || Theta > 3.0f * Math.PI / 2.0f)
            {
                scatterEffect.GetVar("alpha").AsScalar().Set((float)Math.Abs(Math.Sin(Theta + (float)Math.PI / 2.0f)));
            }
            else
            {
                scatterEffect.GetVar("alpha").AsScalar().Set(0.0f);
            }

            sunMoonPass.Apply();
            Game.Device.DrawIndexed(6, 0, 0);
        }
        public static void RenderFullScreenQuad(ShaderResourceView srv)
        {
            Shader e = WorldData.GetObject("FullScreenQuad.fx") as Shader;

            if (e == null)
            {
                return;
            }
            ShaderHelper.UpdateCommonEffectVars(e, Matrix.Identity);
            EffectTechnique t = e.EffectObj.GetTechniqueByName("FullScreen");

            Game.Device.InputAssembler.SetPrimitiveTopology(PrimitiveTopology.TriangleList);

            for (int p = 0; p < t.Description.PassCount; p++)
            {
                EffectPass  pass = t.GetPassByIndex(p);
                InputLayout l    = ShaderHelper.ConstructInputLayout(MeshInputElements10.FullScreenQuad, pass.Description.Signature);
                Game.Device.InputAssembler.SetInputLayout(l);

                EffectResourceVariable diffTex = e.GetVar(ShaderHelper.DiffTex).AsResource();
                if (diffTex != null)
                {
                    diffTex.SetResource(srv);
                }
                pass.Apply();
                Game.Device.Draw(3, 0);
            }
        }
        public static void RenderMesh2(Model m, Vector3 position, string effectName, string technique, Matrix mWorld, int stride)
        {
            if (m == null || m.MeshObj == null)
            {
                return;
            }
            Shader e     = WorldData.GetObject(effectName) as Shader;
            Matrix world = Matrix.Identity + Matrix.Translation(position);

            if (mWorld != null)
            {
                world = mWorld;
            }
            ShaderHelper.UpdateCommonEffectVars(e, world);

            EffectTechnique t = e.EffectObj.GetTechniqueByName(technique);

            D3D10.Buffer indexBuffer   = m.MeshObj.GetDeviceIndexBuffer();
            D3D10.Buffer vertextBuffer = m.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(vertextBuffer, stride, 0));

            for (int p = 0; p < t.Description.PassCount; p++)
            {
                EffectPass  pass = t.GetPassByIndex(p);
                InputLayout l    = ShaderHelper.ConstructInputLayout(m.Mesh3d.inputElements, pass.Description.Signature);
                Game.Device.InputAssembler.SetInputLayout(l);

                for (int subset = 0; subset < m.Mesh3d.NumAttributes; subset++)
                {
                    EffectResourceVariable diffTex = e.GetVar(ShaderHelper.DiffTex).AsResource();
                    if (diffTex != null)
                    {
                        diffTex.SetResource(m.Mesh3d.textureViews[subset]);
                    }
                    pass.Apply();
                    MeshAttributeRange r = m.Mesh3d.attrTable[subset];
                    // * 2 cause adj data is twice as much data
                    //Game.Device.DrawIndexed((r.FaceCount * 3) * 2, (r.FaceStart * 3) * 2, 0);

                    Game.Device.DrawIndexed((r.FaceCount * 3), (r.FaceStart * 3), 0);
                }
            }

            indexBuffer.Dispose();
            vertextBuffer.Dispose();
        }
Exemple #4
0
        private static void DrawClouds(bool renderReflection)
        {
            if (skyBoxCloudsMesh == null || skyBoxCloudsMesh.MeshObj == null)
            {
                return;
            }
            Shader          scatterEffect = WorldData.GetObject("scatter.fx") as Shader;
            EffectTechnique tech          = scatterEffect.EffectObj.GetTechniqueByName("RenderClouds");

            if (renderReflection)
            {
                tech = scatterEffect.EffectObj.GetTechniqueByName("RenderCloudsReflection");
            }

            D3D10.Buffer iBuffer = skyBoxCloudsMesh.MeshObj.GetDeviceIndexBuffer();
            D3D10.Buffer vBuffer = skyBoxCloudsMesh.MeshObj.GetDeviceVertexBuffer(0);

            Game.Device.InputAssembler.SetPrimitiveTopology(PrimitiveTopology.TriangleList);
            Game.Device.InputAssembler.SetIndexBuffer(iBuffer, Format.R16_UInt, 0);
            Game.Device.InputAssembler.SetVertexBuffers(0, new VertexBufferBinding(vBuffer, 56, 0));
            EffectPass  p1     = tech.GetPassByIndex(0);
            InputLayout layout = ShaderHelper.ConstructInputLayout(MeshInputElements10.NormalMesh, p1.Description.Signature);

            Game.Device.InputAssembler.SetInputLayout(layout);

            Matrix mWorld = Matrix.Translation(Camera.Position);

            if (renderReflection)
            {
                mWorld = Matrix.Scaling(1, -1, 1) * Matrix.Translation(Camera.Position);
            }
            scatterEffect.GetVar("WorldViewProj").AsMatrix().SetMatrix(mWorld * Camera.ViewMatrix * Camera.ProjectionMatrix);
            if (settings != null && settings.SkySettings != null)
            {
                scatterEffect.GetVar("cloud1Tile").AsScalar().Set(settings.SkySettings.CloudTile.X);
                scatterEffect.GetVar("cloud2Tile").AsScalar().Set(settings.SkySettings.CloudTile.Y);
                scatterEffect.GetVar("cloudCover").AsScalar().Set(settings.SkySettings.CloudCover);
            }
            scatterEffect.GetVar("scroll").AsVector().Set(t);
            scatterEffect.GetVar("clouds1Tex").AsResource().SetResource(skyClouds1);
            scatterEffect.GetVar("clouds2Tex").AsResource().SetResource(skyClouds2);
            scatterEffect.GetVar("SunColor").AsVector().Set(SunColor);

            p1.Apply();
            Game.Device.DrawIndexed((skyBoxCloudsMesh.Mesh3d.attrTable[0].FaceCount * 3), 0, 0);

            iBuffer.Dispose();
            vBuffer.Dispose();
        }
        void UpdateOcclusion()
        {
            if (gm == null)
            {
                return;
            }
            if (occlusionQueryActive)
            {
                // If the previous query has not yet completed, wait until it does.
                if (!occQuery.IsDataAvailable)
                {
                    return;
                }

                UInt64 pixelCount = occQuery.GetData().Read <UInt64>();
                occlusionAlpha = Math.Min(pixelCount / 25000.0f, 1);
            }


            occQuery.Begin();

            Matrix          m = Matrix.Scaling(3000, 3000, 3000) * Matrix.Translation(-SkyDome.LightDirection * 25000);
            Shader          e = WorldData.GetObject("scatter.fx") as Shader;
            EffectTechnique t = e.EffectObj.GetTechniqueByName("RenderOccluder");

            D3D10.Buffer indexBuffer   = gm.MeshObj.GetDeviceIndexBuffer();
            D3D10.Buffer vertextBuffer = 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(vertextBuffer, 16, 0));
            EffectPass  pass = t.GetPassByIndex(0);
            InputLayout l    = ShaderHelper.ConstructInputLayout(gm.Mesh3d.inputElements, pass.Description.Signature);

            Game.Device.InputAssembler.SetInputLayout(l);
            Matrix proj = Matrix.PerspectiveFovLH(Camera.Fovx, Camera.AspectRatio, 1.0f, 1000000.0f);

            e.GetVar("WorldViewProjection").AsMatrix().SetMatrix(m * Camera.ViewMatrix * proj);
            pass.Apply();
            Game.Device.DrawIndexed((gm.Mesh3d.attrTable[0].FaceCount * 3), (gm.Mesh3d.attrTable[0].FaceStart * 3), 0);
            indexBuffer.Dispose();
            vertextBuffer.Dispose();

            occQuery.End();

            occlusionQueryActive = true;
        }
        public static void DrawInstancedSpheres(GraphicsDeviceManager gdm, List <Matrix> instances)
        {
            Model sphere = WorldData.GetObject("sphereInstanced.mesh") as Model;

            D3D10.Buffer    indexBuffer   = sphere.MeshObj.GetDeviceIndexBuffer();
            D3D10.Buffer    vertextBuffer = sphere.MeshObj.GetDeviceVertexBuffer(0);
            Shader          e             = WorldData.GetObject("Instanced.fx") as Shader;
            EffectTechnique t             = e.EffectObj.GetTechniqueByName("RenderInstanced");

            InputLayout l = ShaderHelper.ConstructInputLayout(MeshInputElements10.PositionOnlyInstanced, t.GetPassByIndex(0).Description.Signature);



            BufferDescription bd = new BufferDescription();

            bd.SizeInBytes    = System.Runtime.InteropServices.Marshal.SizeOf(instances[0]) * instances.Count;
            bd.Usage          = ResourceUsage.Dynamic;
            bd.CpuAccessFlags = CpuAccessFlags.Write;
            bd.BindFlags      = BindFlags.VertexBuffer;

            D3D10.Buffer instanceData = new D3D10.Buffer(gdm.Direct3D10.Device, bd);

            DataStream ds = instanceData.Map(MapMode.WriteDiscard, SlimDX.Direct3D10.MapFlags.None);

            ds.Position = 0;
            ds.WriteRange(instances.ToArray());
            instanceData.Unmap();



            Game.Device.InputAssembler.SetInputLayout(l);
            Game.Device.InputAssembler.SetVertexBuffers(0, new VertexBufferBinding(vertextBuffer, 16, 0), new VertexBufferBinding(instanceData, System.Runtime.InteropServices.Marshal.SizeOf(instances[0]), 0));
            Game.Device.InputAssembler.SetIndexBuffer(indexBuffer, Format.R16_UInt, 0);
            Game.Device.InputAssembler.SetPrimitiveTopology(PrimitiveTopology.TriangleList);

            e.GetVar("WorldViewProj").AsMatrix().SetMatrix(Camera.ViewMatrix * Camera.ProjectionMatrix);

            t.GetPassByIndex(0).Apply();
            Game.Device.DrawIndexedInstanced((sphere.Mesh3d.attrTable[0].FaceCount * 3), instances.Count, 0, 0, 0);
            //Game.Device.DrawIndexed((sphere.attrTable[0].FaceCount * 3) * 2, 0, 0);

            indexBuffer.Dispose();
            vertextBuffer.Dispose();
            sphere.Dispose();
        }
        public static void RenderVertices(VertexBufferBinding vbb, int numVertices, Matrix world, InputElement[] inputElements, string effectName, string technique)
        {
            Shader e = WorldData.GetObject(effectName) as Shader;

            ShaderHelper.UpdateCommonEffectVars(e, world);
            EffectTechnique t = e.EffectObj.GetTechniqueByName(technique);

            Game.Device.InputAssembler.SetPrimitiveTopology(PrimitiveTopology.LineList);
            Game.Device.InputAssembler.SetVertexBuffers(0, vbb);

            for (int p = 0; p < t.Description.PassCount; p++)
            {
                EffectPass  pass = t.GetPassByIndex(p);
                InputLayout l    = ShaderHelper.ConstructInputLayout(inputElements, pass.Description.Signature);
                Game.Device.InputAssembler.SetInputLayout(l);

                pass.Apply();
                Game.Device.Draw(numVertices, 0);
            }
        }
Exemple #8
0
        static void UpdateMieRayleighTextures(RenderTargetView rtv, DepthStencilView dsv)
        {
            // Save the Old viewport and set new one
            Viewport[] oldViews = Game.Device.Rasterizer.GetViewports();
            Game.Device.Rasterizer.SetViewports(vp);

            Game.Device.OutputMerger.SetTargets(new RenderTargetView[] { rayleighRT.rtv, mieRT.rtv });
            Game.Device.ClearRenderTargetView(rayleighRT.rtv, Color.CornflowerBlue);
            Game.Device.ClearRenderTargetView(mieRT.rtv, Color.CornflowerBlue);

            Shader scatterEffect = WorldData.GetObject("scatter.fx") as Shader;

            if (scatterEffect == null)
            {
                return;
            }
            EffectTechnique tech = scatterEffect.EffectObj.GetTechniqueByName("Update");

            Game.Device.InputAssembler.SetPrimitiveTopology(PrimitiveTopology.TriangleStrip);
            InputLayout l = ShaderHelper.ConstructInputLayout(MeshInputElements10.PosTex4, tech.GetPassByIndex(0).Description.Signature);

            Game.Device.InputAssembler.SetInputLayout(l);
            Game.Device.InputAssembler.SetVertexBuffers(0, new VertexBufferBinding(QuadRenderComponent.quadVerts, 24, 0));

            if (settings != null && settings.SkySettings != null)
            {
                scatterEffect.GetVar("NumSamples").AsScalar().Set(settings.SkySettings.NumSamples);
            }
            scatterEffect.GetVar("InvWavelength").AsVector().Set(InvWaveLengths);
            scatterEffect.GetVar("WavelengthMie").AsVector().Set(WaveLengthsMie);
            scatterEffect.GetVar("v3SunDir").AsVector().Set(-LightDirection);

            tech.GetPassByIndex(0).Apply();
            Game.Device.Draw(4, 0);

            Game.Device.OutputMerger.SetTargets(dsv, rtv);
            Game.Device.Rasterizer.SetViewports(oldViews);
        }
        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);
        }
Exemple #10
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 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 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);
        }
Exemple #13
0
        public static void Draw(RenderTargetView rtv, DepthStencilView dsv, bool renderReflection)
        {
            //if (previousTheta != Theta || previousPhi != fPhi)
            if (!renderReflection)
            {
                UpdateMieRayleighTextures(rtv, dsv);
            }

            SunColor = GetSunColor(-Theta, 2);

            Shader scatterEffect = WorldData.GetObject("scatter.fx") as Shader;

            if (scatterEffect == null)
            {
                return;
            }
            EffectTechnique tech = scatterEffect.EffectObj.GetTechniqueByName("Render");

            if (renderReflection)
            {
                tech = scatterEffect.EffectObj.GetTechniqueByName("RenderReflection");
            }

            Game.Device.InputAssembler.SetPrimitiveTopology(PrimitiveTopology.TriangleList);
            Game.Device.InputAssembler.SetIndexBuffer(domeIndices, Format.R16_UInt, 0);
            Game.Device.InputAssembler.SetVertexBuffers(0, new VertexBufferBinding(domeVerts, 20, 0));
            EffectPass  p1     = tech.GetPassByIndex(0);
            InputLayout layout = ShaderHelper.ConstructInputLayout(MeshInputElements10.PosTex, p1.Description.Signature);

            Game.Device.InputAssembler.SetInputLayout(layout);

            scatterEffect.GetVar("cameraPosition").AsVector().Set(new Vector4(Camera.Position, 1));
            scatterEffect.GetVar("txMie").AsResource().SetResource(mieRT.srv);
            scatterEffect.GetVar("txRayleigh").AsResource().SetResource(rayleighRT.srv);
            if (renderReflection)
            {
                scatterEffect.GetVar("WorldViewProjection").AsMatrix().SetMatrix(Matrix.Scaling(1, -1, 1) * Matrix.Translation(Camera.Position) * Camera.ViewMatrix * Camera.ProjectionMatrix);
            }
            else
            {
                scatterEffect.GetVar("WorldViewProjection").AsMatrix().SetMatrix(Matrix.Translation(Camera.Position) * Camera.ViewMatrix * Camera.ProjectionMatrix);
            }
            scatterEffect.GetVar("v3SunDir").AsVector().Set(-LightDirection);

            if (settings != null && settings.SkySettings != null)
            {
                scatterEffect.GetVar("NumSamples").AsScalar().Set(settings.SkySettings.NumSamples);
                scatterEffect.GetVar("fExposure").AsScalar().Set(settings.SkySettings.Exposure);
            }
            scatterEffect.GetVar("StarsTex").AsResource().SetResource(starsTex);
            if (Theta < Math.PI / 2.0f || Theta > 3.0f * Math.PI / 2.0f)
            {
                scatterEffect.GetVar("starIntensity").AsScalar().Set((float)Math.Abs(Math.Sin(Theta + (float)Math.PI / 2.0f)));
            }
            else
            {
                scatterEffect.GetVar("starIntensity").AsScalar().Set(0.0f);
            }

            p1.Apply();
            Game.Device.DrawIndexed(DISize * 3, 0, 0);

            UnbindMRT(tech, scatterEffect.GetVar("txMie").AsResource(), scatterEffect.GetVar("txRayleigh").AsResource());


            if (!renderReflection)
            {
                DrawMoon();
            }
            if (!renderReflection)
            {
                DrawClouds(renderReflection);
            }
            if (!renderReflection)
            {
                lensFlare.Draw();
            }


            previousTheta = Theta;
            previousPhi   = fPhi;

            if (!renderReflection)
            {
                Rain.RenderParticles();
                Snow.RenderParticles();
            }
        }