示例#1
0
        public void Render(DrawArgs drawArgs)
        {
            try
            {
                if (m_meshList.Count > 0 && ((!World.Settings.ForceCpuAtmosphere && m_canDoShaders) || m_opticalDepthBuffer1 != null))
                {
                    double horizonSpan = HorizonSpan(drawArgs);
                    if (horizonSpan == 0) return;   // Check if horizon visible (PM 2006-11-28)

                    if (skyFromSpaceEffect == null)
                    {
                        drawArgs.device.DeviceReset += new EventHandler(device_DeviceReset);
                        device_DeviceReset(drawArgs.device, null);
                    }

                    vCamera.X = drawArgs.WorldCamera.Position.X;
                    vCamera.Y = drawArgs.WorldCamera.Position.Y;
                    vCamera.Z = drawArgs.WorldCamera.Position.Z;

                    drawArgs.device.VertexFormat = CustomVertex.PositionColored.Format;
                    drawArgs.device.TextureState[0].ColorOperation = TextureOperation.Disable;
                    if (drawArgs.device.RenderState.Lighting)
                        drawArgs.device.RenderState.Lighting = false;

                    drawArgs.device.Transform.World = Matrix.Translation(
                        (float)-drawArgs.WorldCamera.ReferenceCenter.X,
                        (float)-drawArgs.WorldCamera.ReferenceCenter.Y,
                        (float)-drawArgs.WorldCamera.ReferenceCenter.Z
                        );

                    bool doHighResolution = (drawArgs.WorldCamera.Altitude < 300000);

                    Frustum frustum = new Frustum();

                    frustum.Update(
                        Matrix.Multiply(drawArgs.device.Transform.World,
                        Matrix.Multiply(drawArgs.device.Transform.View, drawArgs.device.Transform.Projection)));

                    if (!World.Settings.ForceCpuAtmosphere && m_canDoShaders)
                    {
                        Effect shader = null;
                        // Update Sun
                        UpdateLightVector();
                        if (vCamera.Length() >= m_fOuterRadius)
                            shader = skyFromSpaceEffect;
                        else
                            shader = skyFromAtmosphere;

                        shader.Technique = "Sky";
                        shader.SetValue("v3CameraPos", new Vector4(vCamera.X, vCamera.Y, vCamera.Z, 0));
                        shader.SetValue("v3LightPos", Vector4.Normalize(new Vector4(m_vLightDirection.X, m_vLightDirection.Y, m_vLightDirection.Z, 0)));
                        shader.SetValue("WorldViewProj", Matrix.Multiply(drawArgs.device.Transform.World, Matrix.Multiply(drawArgs.device.Transform.View, drawArgs.device.Transform.Projection)));
                        shader.SetValue("v3InvWavelength", new Vector4(1.0f / m_fWavelength4[0], 1.0f / m_fWavelength4[1], 1.0f / m_fWavelength4[2], 0));
                        shader.SetValue("fCameraHeight", vCamera.Length());
                        shader.SetValue("fCameraHeight2", vCamera.LengthSq());
                        shader.SetValue("fInnerRadius", m_fInnerRadius);
                        shader.SetValue("fInnerRadius2", m_fInnerRadius * m_fInnerRadius);
                        shader.SetValue("fOuterRadius", m_fOuterRadius);
                        shader.SetValue("fOuterRadius2", m_fOuterRadius * m_fOuterRadius);
                        shader.SetValue("fKrESun", m_Kr * m_ESun);
                        shader.SetValue("fKmESun", m_Km * m_ESun);
                        shader.SetValue("fKr4PI", m_Kr4PI);
                        shader.SetValue("fKm4PI", m_Km4PI);
                        shader.SetValue("fScale", 1.0f / (m_fOuterRadius - m_fInnerRadius));
                        shader.SetValue("fScaleDepth", m_fRayleighScaleDepth);
                        shader.SetValue("fScaleOverScaleDepth", (1.0f / (m_fOuterRadius - m_fInnerRadius)) / m_fRayleighScaleDepth);
                        shader.SetValue("g", m_g);
                        shader.SetValue("g2", m_g * m_g);
                        shader.SetValue("nSamples", m_nSamples);
                        shader.SetValue("fSamples", m_nSamples);

                        for (int i = 0; i < m_meshList.Count; i++)
                        {
                            if (!frustum.Intersects(m_meshList[i].BoundingBox))
                                continue;

                            int numPasses = shader.Begin(0);
                            for (int j = 0; j < numPasses; j++)
                            {
                                shader.BeginPass(j);
                                if (doHighResolution)
                                    drawArgs.device.DrawIndexedUserPrimitives(PrimitiveType.TriangleList, 0, m_meshList[i].HigherResolutionVertices.Length, m_indicesHighResolution.Length / 3, m_indicesHighResolution, true, m_meshList[i].HigherResolutionVertices);
                                else
                                    drawArgs.device.DrawIndexedUserPrimitives(PrimitiveType.TriangleList, 0, m_meshList[i].Vertices.Length, m_indices.Length / 3, m_indices, true, m_meshList[i].Vertices);
                                shader.EndPass();
                            }
                            shader.End();
                        }
                    }
                    else
                    {
                        /*for (int i = 0; i < m_meshList.Count; i++)
                        {
                            if (!frustum.Intersects(m_meshList[i].BoundingBox))
                                continue;

                            UpdateColor(drawArgs, m_meshList[i], doHighResolution);
                            if (doHighResolution)
                                drawArgs.device.DrawIndexedUserPrimitives(PrimitiveType.TriangleList, 0, m_meshList[i].HigherResolutionVertices.Length, m_indicesHighResolution.Length / 3, m_indicesHighResolution, true, m_meshList[i].HigherResolutionVertices);
                            else
                                drawArgs.device.DrawIndexedUserPrimitives(PrimitiveType.TriangleList, 0, m_meshList[i].Vertices.Length, m_indices.Length / 3, m_indices, true, m_meshList[i].Vertices);
                            
                        } */
                        // Update Sun
                        UpdateLightVector();
                        // Use SkyGradient geometry
                        UpdateSkyMesh(drawArgs, horizonSpan);
                        drawArgs.device.RenderState.CullMode = Cull.Clockwise;
                        skyMesh.DrawSubset(0);
                    }
                    drawArgs.device.Transform.World = drawArgs.WorldCamera.WorldMatrix;
                }
            }
            catch (Exception ex)
            {
                Log.Write(ex);
            }
        }
示例#2
0
        private void RenderSun(DrawArgs drawArgs)
        {
            Point3d sunPosition = -SunCalculator.GetGeocentricPosition(TimeKeeper.CurrentTimeUtc);

            Point3d sunSpherical = MathEngine.CartesianToSphericalD(sunPosition.X, sunPosition.Y, sunPosition.Z);
            sunPosition = MathEngine.SphericalToCartesianD(
                Angle.FromRadians(sunSpherical.Y),
                Angle.FromRadians(sunSpherical.Z),
                150000000000);

            Vector3 sunVector = new Vector3((float)sunPosition.X, (float)sunPosition.Y, (float)sunPosition.Z);

            Frustum viewFrustum = new Frustum();

            float aspectRatio = (float)drawArgs.WorldCamera.Viewport.Width / drawArgs.WorldCamera.Viewport.Height;
            Matrix projectionMatrix = Matrix.PerspectiveFovRH((float)drawArgs.WorldCamera.Fov.Radians, aspectRatio, 1.0f, 300000000000);

            viewFrustum.Update(
                Matrix.Multiply(drawArgs.WorldCamera.AbsoluteWorldMatrix,
                Matrix.Multiply(drawArgs.WorldCamera.AbsoluteViewMatrix,
                    projectionMatrix)));

            if (!viewFrustum.ContainsPoint(sunVector))
                return;

            Vector3 translationVector = new Vector3(
                (float)(sunPosition.X - drawArgs.WorldCamera.ReferenceCenter.X),
                (float)(sunPosition.Y - drawArgs.WorldCamera.ReferenceCenter.Y),
                (float)(sunPosition.Z - drawArgs.WorldCamera.ReferenceCenter.Z));

            Vector3 projectedPoint = drawArgs.WorldCamera.Project(translationVector);

            if (m_sunTexture == null)
            {
                m_sunTexture = ImageHelper.LoadTexture(Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath) + "\\Data\\sun.dds");
                m_sunSurfaceDescription = m_sunTexture.GetLevelDescription(0);
            }

            if (m_sprite == null)
            {
                m_sprite = new Sprite(drawArgs.device);
            }

            m_sprite.Begin(SpriteFlags.AlphaBlend);

            // Render icon
            float xscale = (float)m_sunWidth / m_sunSurfaceDescription.Width;
            float yscale = (float)m_sunHeight / m_sunSurfaceDescription.Height;
            m_sprite.Transform = Matrix.Scaling(xscale, yscale, 0);

            m_sprite.Transform *= Matrix.Translation(projectedPoint.X, projectedPoint.Y, 0);
            m_sprite.Draw(m_sunTexture,
                new Vector3(m_sunSurfaceDescription.Width >> 1, m_sunSurfaceDescription.Height >> 1, 0),
                Vector3.Empty,
                System.Drawing.Color.FromArgb(253, 253, 200).ToArgb());

            // Reset transform to prepare for text rendering later
            m_sprite.Transform = Matrix.Identity;
            m_sprite.End();
        }
示例#3
0
文件: World.cs 项目: paladin74/Dapple
        internal void Render(DrawArgs drawArgs)
        {
            try
            {
                if (m_meshList.Count > 0 && m_opticalDepthBuffer1 != null)
                {
                    double horizonSpan = HorizonSpan(drawArgs);
                    if (horizonSpan == 0) return;   // Check if horizon visible (PM 2006-11-28)

                    if (skyFromSpaceEffect == null)
                    {
                        drawArgs.device.DeviceReset += new EventHandler(device_DeviceReset);
                        device_DeviceReset(drawArgs.device, null);
                    }

                    vCamera.X = (float)drawArgs.WorldCamera.Position.X;
                    vCamera.Y = (float)drawArgs.WorldCamera.Position.Y;
                    vCamera.Z = (float)drawArgs.WorldCamera.Position.Z;

                    drawArgs.device.VertexFormat = CustomVertex.PositionColored.Format;
                    drawArgs.device.TextureState[0].ColorOperation = TextureOperation.Disable;
                    if (drawArgs.device.RenderState.Lighting)
                        drawArgs.device.RenderState.Lighting = false;

                    drawArgs.device.Transform.World = Matrix.Translation(
                        (float)-drawArgs.WorldCamera.ReferenceCenter.X,
                        (float)-drawArgs.WorldCamera.ReferenceCenter.Y,
                        (float)-drawArgs.WorldCamera.ReferenceCenter.Z
                        );

                    Frustum frustum = new Frustum();

                    frustum.Update(
                        Matrix4d.Multiply(ConvertDX.ToMatrix4d(drawArgs.device.Transform.World),
                        Matrix4d.Multiply(ConvertDX.ToMatrix4d(drawArgs.device.Transform.View), ConvertDX.ToMatrix4d(drawArgs.device.Transform.Projection))));

                    // Update Sun
                    UpdateLightVector();
                    // Use SkyGradient geometry
                    UpdateSkyMesh(drawArgs, horizonSpan);
                    drawArgs.device.RenderState.CullMode = Cull.Clockwise;
                    skyMesh.DrawSubset(0);

                    drawArgs.device.Transform.World = ConvertDX.FromMatrix4d(drawArgs.WorldCamera.WorldMatrix);
                }
            }
            catch (Exception ex)
            {
                Log.Write(ex);
            }
        }