public override void OnRender(DrawArgs drawArgs) { if (!this.IsInitialized) { return; } Matrix4d proj = drawArgs.WorldCamera.ProjectionMatrix; Cull cull = drawArgs.device.RenderState.CullMode; bool iszub = drawArgs.device.RenderState.ZBufferEnable; float aspectRatio = (float)drawArgs.WorldCamera.Viewport.Width / drawArgs.WorldCamera.Viewport.Height; float zNear = (float)drawArgs.WorldCamera.Altitude * 0.1f; double distToCenterOfPlanet = (drawArgs.WorldCamera.Altitude + World.EquatorialRadius); double tangentalDistance = Math.Sqrt(distToCenterOfPlanet * distToCenterOfPlanet - World.EquatorialRadius * World.EquatorialRadius); double amosphereThickness = Math.Sqrt(OutterRadius * OutterRadius + World.EquatorialRadius * World.EquatorialRadius); drawArgs.device.Transform.Projection = Matrix.PerspectiveFovRH((float)drawArgs.WorldCamera.Fov.Radians, aspectRatio, zNear, (float)(tangentalDistance + amosphereThickness)); this.Engine.RenderFrame(drawArgs); drawArgs.device.RenderState.CullMode = cull; drawArgs.device.RenderState.ZBufferEnable = iszub; drawArgs.device.Transform.Projection = ConvertDX.FromMatrix4d(proj); }
/// <summary> /// This is where we do our rendering /// Called from UI thread = UI code safe in this function /// </summary> public override void Render(DrawArgs drawArgs) { if (!isInitialized) { return; } if (world.Name != "Earth") { return; // Earth only } // Check for update if (!isDownloading && DateTime.Now > latestTime.AddHours(refreshHours)) { if (retryCount < maxRetry && DateTime.Now > lastDownloadTime.AddSeconds(retryDelaySeconds)) { StartDownload(cachePath, "clouds_" + DateTimeStamp(DateTime.Now) + ".jpg"); } } // Camera & Device shortcuts ;) CameraBase camera = drawArgs.WorldCamera; //Device device = drawArgs.device; // Render cloud layer if (texture != null) { double cloudAlt = 20e3; // clouds altitude in meters (20 x 10e3) if (camera.Altitude < 4000e3) { return; } double sphereRadius = camera.WorldRadius + cloudAlt; // Create sphere if (layerMesh == null) { layerMesh = TexturedSphere(drawArgs.device, (float)sphereRadius, 64, 64); } // set texture drawArgs.device.SetTexture(0, texture); drawArgs.device.TextureState[0].ColorOperation = TextureOperation.Modulate; drawArgs.device.TextureState[0].ColorArgument1 = TextureArgument.TextureColor; drawArgs.device.TextureState[0].ColorArgument2 = TextureArgument.Diffuse; drawArgs.device.TextureState[0].AlphaOperation = TextureOperation.SelectArg1; drawArgs.device.TextureState[0].AlphaArgument1 = TextureArgument.TextureColor; drawArgs.device.VertexFormat = CustomVertex.PositionNormalTextured.Format; // save world and projection transform Matrix origWorld = drawArgs.device.Transform.World; Matrix origProjection = drawArgs.device.Transform.Projection; // Save fog status and disable fog bool origFog = drawArgs.device.RenderState.FogEnable; drawArgs.device.RenderState.FogEnable = false; // Set new projection (to avoid being clipped) - probably better ways of doing this? double aspectRatio = (double)drawArgs.device.Viewport.Width / drawArgs.device.Viewport.Height; drawArgs.device.Transform.Projection = ConvertDX.FromMatrix4d(Matrix4d.PerspectiveFovRH(camera.Fov.Radians, aspectRatio, 1, double.MaxValue)); //translate to the camera reference center drawArgs.device.Transform.World = Matrix.Translation( (float)-drawArgs.WorldCamera.ReferenceCenter.X, (float)-drawArgs.WorldCamera.ReferenceCenter.Y, (float)-drawArgs.WorldCamera.ReferenceCenter.Z ); // draw drawArgs.device.RenderState.ZBufferEnable = false; layerMesh.DrawSubset(0); // Restore device states drawArgs.device.Transform.World = origWorld; drawArgs.device.Transform.Projection = origProjection; drawArgs.device.RenderState.FogEnable = origFog; drawArgs.device.RenderState.ZBufferEnable = true; } // Render progress bar if downloading if (isDownloading) { if (progressBar == null) { progressBar = new WorldWind.VisualControl.ProgressBar(40, 4); } progressBar.Draw(drawArgs, drawArgs.screenWidth - 34, drawArgs.screenHeight - 10, ProgressPercent, downloadProgressColor); drawArgs.device.RenderState.ZBufferEnable = true; } }
/// <summary> /// This is where we do our rendering /// Called from UI thread = UI code safe in this function /// </summary> public override void Render(DrawArgs drawArgs) { if (!isInitialized || (this.world.Name == "Earth" && World.Settings.EnableAtmosphericScattering)) { return; } // Camera & Device shortcuts ;) CameraBase camera = drawArgs.WorldCamera; Device device = drawArgs.device; //if(camera.Altitude > 200e3) return; Point3d cameraPos = camera.Position; double distToCenterOfPlanet = (camera.Altitude + camera.WorldRadius); // Create or refresh SkyGradient dome if (mesh == null || lastAltitude != camera.Altitude) { // Compute distance to horizon and dome radius double tangentalDistance = Math.Sqrt(distToCenterOfPlanet * distToCenterOfPlanet - camera.WorldRadius * camera.WorldRadius); double domeRadius = tangentalDistance; // horizon latitude double horizonLat = (-Math.PI / 2 + Math.Acos(tangentalDistance / distToCenterOfPlanet)) * 180 / Math.PI; // zenith latitude double zenithLat = 90; if (camera.Altitude >= thickness) { double tangentalDistanceZenith = Math.Sqrt(distToCenterOfPlanet * distToCenterOfPlanet - (camera.WorldRadius + thickness) * (camera.WorldRadius + thickness)); zenithLat = (-Math.PI / 2 + Math.Acos(tangentalDistanceZenith / distToCenterOfPlanet)) * 180 / Math.PI; } if (camera.Altitude < thickness && camera.Altitude > thickness * 0.8) { zenithLat = (thickness - camera.Altitude) / (thickness - thickness * 0.8) * 90; } // new mesh if (mesh != null) { mesh.Dispose(); } mesh = ColoredSphere(device, domeRadius, horizonLat, zenithLat, 128, 24); lastAltitude = camera.Altitude; } // set texture to null device.SetTexture(0, null); device.TextureState[0].ColorOperation = TextureOperation.BlendCurrentAlpha; device.VertexFormat = CustomVertex.PositionColored.Format; // save world and projection transform Matrix origWorld = device.Transform.World; Matrix origProjection = device.Transform.Projection; // move SkyGradient dome Matrix4d SkyGradientTrans; Point3d cameraCoord = MathEngine.CartesianToSpherical(cameraPos.X, cameraPos.Y, cameraPos.Z); double camLat = cameraCoord.Y; double camLon = cameraCoord.Z; SkyGradientTrans = Matrix4d.Translation(0, 0, distToCenterOfPlanet); SkyGradientTrans = Matrix4d.Multiply(SkyGradientTrans, Matrix4d.RotationY(-camLat + Math.PI / 2)); SkyGradientTrans = Matrix4d.Multiply(SkyGradientTrans, Matrix4d.RotationZ(camLon)); device.Transform.World = ConvertDX.FromMatrix4d(SkyGradientTrans); // Recenter Recenter(drawArgs); // Save fog status bool origFog = device.RenderState.FogEnable; device.RenderState.FogEnable = false; // Set new one (to avoid being clipped) - probably better ways of doing this? double aspectRatio = (double)device.Viewport.Width / device.Viewport.Height; device.Transform.Projection = ConvertDX.FromMatrix4d(Matrix4d.PerspectiveFovRH(camera.Fov.Radians, aspectRatio, 1000, 30000000.0)); // draw //device.RenderState.FillMode = FillMode.WireFrame; mesh.DrawSubset(0); // Restore device states device.RenderState.FillMode = FillMode.Solid; device.Transform.World = origWorld; device.Transform.Projection = origProjection; device.RenderState.FogEnable = origFog; device.RenderState.ZBufferEnable = true; // Fog effect if (useFog) { // Compute distance to horizon and camera altitude double tangentalDistance = Math.Sqrt(distToCenterOfPlanet * distToCenterOfPlanet - camera.WorldRadius * camera.WorldRadius); double a = camera.Altitude; device.RenderState.FogEnable = true; device.RenderState.FogColor = horizonColor; device.RenderState.FogTableMode = FogMode.Linear; device.RenderState.FogStart = (float)(a * nearFactor); device.RenderState.FogEnd = (float)(tangentalDistance * farFactor); } }
private void RenderOutterSphere(DrawArgs drawArgs) { try { if (this.m_outerSphereList.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 (skyFromAtmosphere == null) { drawArgs.device.DeviceReset += new EventHandler(device_DeviceReset); device_DeviceReset(drawArgs.device, null); } vCamera = drawArgs.WorldCamera.Position.Vector(); drawArgs.device.VertexFormat = CustomVertex.PositionColored.Format; drawArgs.device.TextureState[0].ColorOperation = TextureOperation.Disable; Frustum frustum = new Frustum(); frustum.Update(ConvertDX.ToMatrix4d(Matrix.Multiply(drawArgs.device.Transform.World, Matrix.Multiply(drawArgs.device.Transform.View, drawArgs.device.Transform.Projection)))); if (!World.Settings.ForceCpuAtmosphere && m_canDoShaders) { UpdateLightVector(); skyFromAtmosphere.Technique = "Sky"; skyFromAtmosphere.SetValue("v3CameraPos", new Vector4(vCamera.X, vCamera.Y, vCamera.Z, 0)); skyFromAtmosphere.SetValue("v3LightPos", Vector4.Normalize(new Vector4(m_vLightDirection.X, m_vLightDirection.Y, m_vLightDirection.Z, 0))); skyFromAtmosphere.SetValue("WorldViewProj", Matrix.Multiply(drawArgs.device.Transform.World, Matrix.Multiply(drawArgs.device.Transform.View, drawArgs.device.Transform.Projection))); skyFromAtmosphere.SetValue("v3InvWavelength", new Vector4(1.0f / m_fWavelength4[0], 1.0f / m_fWavelength4[1], 1.0f / m_fWavelength4[2], 0)); skyFromAtmosphere.SetValue("fCameraHeight", vCamera.Length()); skyFromAtmosphere.SetValue("fCameraHeight2", vCamera.LengthSq()); skyFromAtmosphere.SetValue("fInnerRadius", m_fInnerRadius); skyFromAtmosphere.SetValue("fInnerRadius2", m_fInnerRadius * m_fInnerRadius); skyFromAtmosphere.SetValue("fOuterRadius", m_fOuterRadius); skyFromAtmosphere.SetValue("fOuterRadius2", m_fOuterRadius * m_fOuterRadius); skyFromAtmosphere.SetValue("fKrESun", m_Kr * m_ESun); skyFromAtmosphere.SetValue("fKmESun", m_Km * m_ESun); skyFromAtmosphere.SetValue("fKr4PI", m_Kr4PI); skyFromAtmosphere.SetValue("fKm4PI", m_Km4PI); skyFromAtmosphere.SetValue("fScale", 1.0f / (m_fOuterRadius - m_fInnerRadius)); skyFromAtmosphere.SetValue("fScaleDepth", m_fRayleighScaleDepth); skyFromAtmosphere.SetValue("fScaleOverScaleDepth", (1.0f / (m_fOuterRadius - m_fInnerRadius)) / m_fRayleighScaleDepth); skyFromAtmosphere.SetValue("g", m_g); skyFromAtmosphere.SetValue("g2", m_g * m_g); skyFromAtmosphere.SetValue("nSamples", m_nSamples); skyFromAtmosphere.SetValue("fSamples", m_nSamples); int numPasses = skyFromAtmosphere.Begin(0); for (int i = 0; i < m_outerSphereList.Count; i++) { if (!frustum.Intersects(m_outerSphereList[i].BoundingBox) && !frustum.Contains(m_outerSphereList[i].BoundingBox)) { continue; } for (int j = 0; j < numPasses; j++) { skyFromAtmosphere.BeginPass(j); this.m_outerSphereList[i].Draw(drawArgs.device); skyFromAtmosphere.EndPass(); } } skyFromAtmosphere.End(); } else { for (int i = 0; i < m_outerSphereList.Count; i++) { if (!frustum.Intersects(m_outerSphereList[i].BoundingBox) && !frustum.Contains(m_outerSphereList[i].BoundingBox)) { continue; } CustomVertex.PositionColoredTextured[] pBuffer = this.m_outerSphereList[i].GetVertexBuffer(); for (int m = 0; m < this.m_outerSphereList[i].GetVertexCount(); m++) { if (!drawArgs.WorldCamera.ViewFrustum.ContainsPoint(Vector3d.FromVector3(pBuffer[m].Position))) { continue; } if (Vector3.Dot(drawArgs.WorldCamera.Position.Vector(), pBuffer[m].Position) > 0) { this.SetColor(ref pBuffer[m], drawArgs); } else { pBuffer[m].Color = Color.FromArgb(0, 0, 0, 0).ToArgb(); } } this.m_outerSphereList[i].Draw(drawArgs.device); } } } } catch (Exception e) { Log.Write(e.StackTrace); } finally { } }
private void RenderCompass(DrawArgs drawArgs) { //if (m_nVertices == null) // CreateVertices(ref m_nVertices, 0.25f * (float)Math.PI, 0.75f * (float)Math.PI, samples, System.Drawing.Color.Red.ToArgb()); //if (m_wVertices == null) // CreateVertices(ref m_wVertices, 0.78f * (float)Math.PI, 1.22f * (float)Math.PI, samples, System.Drawing.Color.Gray.ToArgb()); //if (m_sVertices == null) // CreateVertices(ref m_sVertices, 1.25f * (float)Math.PI, 1.75f * (float)Math.PI, samples, System.Drawing.Color.Gray.ToArgb()); //if (m_eVertices == null) // CreateVertices(ref m_eVertices, 1.78f * (float)Math.PI, 2.22f * (float)Math.PI, samples, System.Drawing.Color.Gray.ToArgb()); //if (m_arrowVertices == null) // CreateArrows(ref m_arrowVertices); if (m_compassVertices == null) { CreateBitmapVertices(drawArgs); } drawArgs.device.Transform.World = Matrix.RotationZ((float)drawArgs.WorldCamera.Heading.Radians); drawArgs.device.Transform.World *= Matrix.RotationX(0.7f * (float)drawArgs.WorldCamera.Tilt.Radians); drawArgs.device.Transform.View = Matrix.LookAtLH( new Vector3(0, 0, -1.5f), new Vector3(0, 0, 0), new Vector3(0, 1, 0)); bool lighting = drawArgs.device.RenderState.Lighting; drawArgs.device.RenderState.Lighting = false; drawArgs.device.Transform.Projection = Matrix.PerspectiveFovLH(0.45f * (float)Math.PI, (float)m_Size.Width / (float)m_Size.Height, 0.01f, 10.0f); Cull cull = drawArgs.device.RenderState.CullMode; drawArgs.device.RenderState.CullMode = Cull.None; //drawArgs.device.VertexFormat = CustomVertex.PositionColored.Format; //drawArgs.device.DrawUserPrimitives(PrimitiveType.TriangleStrip, m_nVertices.Length - 2, m_nVertices); //drawArgs.device.DrawUserPrimitives(PrimitiveType.TriangleStrip, m_wVertices.Length - 2, m_wVertices); //drawArgs.device.DrawUserPrimitives(PrimitiveType.TriangleStrip, m_eVertices.Length - 2, m_eVertices); //drawArgs.device.DrawUserPrimitives(PrimitiveType.TriangleStrip, m_sVertices.Length - 2, m_sVertices); //drawArgs.device.DrawUserPrimitives(PrimitiveType.TriangleList, m_arrowVertices.Length / 3, m_arrowVertices); drawArgs.device.VertexFormat = CustomVertex.PositionColoredTextured.Format; drawArgs.device.TextureState[0].ColorOperation = TextureOperation.Modulate; drawArgs.device.TextureState[0].ColorArgument1 = TextureArgument.Diffuse; drawArgs.device.TextureState[0].ColorArgument2 = TextureArgument.TextureColor; drawArgs.device.TextureState[0].AlphaOperation = TextureOperation.Modulate; drawArgs.device.TextureState[0].AlphaArgument1 = TextureArgument.Diffuse; drawArgs.device.TextureState[0].AlphaArgument2 = TextureArgument.TextureColor; drawArgs.device.SetTexture(0, m_compassTexture); drawArgs.device.DrawUserPrimitives(PrimitiveType.TriangleStrip, m_compassVertices.Length - 2, m_compassVertices); drawArgs.device.RenderState.Lighting = lighting; drawArgs.device.RenderState.CullMode = cull; drawArgs.device.Transform.World = ConvertDX.FromMatrix4d(drawArgs.WorldCamera.WorldMatrix); drawArgs.device.Transform.View = ConvertDX.FromMatrix4d(drawArgs.WorldCamera.ViewMatrix); drawArgs.device.Transform.Projection = ConvertDX.FromMatrix4d(drawArgs.WorldCamera.ProjectionMatrix); }