private void FormChanged(object sender, System.EventArgs e) { if (!suspendEvents) { // Update WW setings from form World.Settings.EnableSunShading = chkSunShading.Checked; World.Settings.SunSynchedWithTime = !chkSunFixed.Checked; World.Settings.SunHeading = MathEngine.DegreesToRadians(tbSunHeading.Value); World.Settings.SunElevation = MathEngine.DegreesToRadians(tbSunElevation.Value); // Disable controls if needed if (chkSunShading.Checked) { chkSunFixed.Enabled = true; } else { chkSunFixed.Enabled = false; } if (chkSunFixed.Checked && chkSunFixed.Enabled) { tbSunHeading.Enabled = true; tbSunElevation.Enabled = true; } else { tbSunHeading.Enabled = false; tbSunElevation.Enabled = false; } // Draw heading and elevation graphics DrawLightGraphics(); } }
///<summary> /// Goes to the extent specified by the bounding box for the QTS layer /// </summary> protected virtual void OnGotoExtentClick(object sender, EventArgs e) { lock (this.ParentList.ChildObjects.SyncRoot) { for (int i = 0; i < this.ParentList.ChildObjects.Count; i++) { RenderableObject ro = (RenderableObject)this.ParentList.ChildObjects[i]; if (ro.Name.Equals(name)) { if (ro is QuadTileSet) { QuadTileSet qts = (QuadTileSet)ro; DrawArgs.Camera.SetPosition((qts.North + qts.South) / 2, (qts.East + qts.West) / 2); double perpendicularViewRange = (qts.North - qts.South > qts.East - qts.West ? qts.North - qts.South : qts.East - qts.West); double altitude = qts.LayerRadius * Math.Sin(MathEngine.DegreesToRadians(perpendicularViewRange * 0.5)); DrawArgs.Camera.Altitude = altitude; break; } if (ro is ShapeFileLayer) { ShapeFileLayer slayer = (ShapeFileLayer)ro; DrawArgs.Camera.SetPosition((slayer.North + slayer.South) / 2, (slayer.East + slayer.West) / 2); double perpendicularViewRange = (slayer.North - slayer.South > slayer.East - slayer.West ? slayer.North - slayer.South : slayer.East - slayer.West); double altitude = slayer.MaxAltitude; DrawArgs.Camera.Altitude = altitude; break; } } } } }
/// <summary> /// 初始化一个“CameraBase”实例 /// </summary> /// <param name="targetPosition"></param> /// <param name="radius">Planet's radius in meters</param> public CameraBase(Vector3 targetPosition, double radius) { this._worldRadius = radius; this._distance = 2 * _worldRadius; this._altitude = this._distance; maximumAltitude = 20 * _worldRadius; //this._orientation = MathEngine.EulerToQuaternion(0,0,0); //this.m_Orientation = Quaternion4d.EulerToQuaternion(0,0,0); this.m_Orientation = Quaternion4d.EulerToQuaternion( MathEngine.DegreesToRadians(targetPosition.Y), MathEngine.DegreesToRadians(targetPosition.X), MathEngine.DegreesToRadians(targetPosition.Z)); }
/// <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) { Initialize(drawArgs); } if (!isInitialized) { return; } if (isRendering) { // Avoid recursion when we render our right image from inside render return; } isRendering = true; try { Device device = drawArgs.device; CameraBase camera = drawArgs.WorldCamera; Matrix View = camera.ViewMatrix; float eyeDist = m_interocularDistance / 100 * (float)camera.Distance; device.Transform.View *= Matrix.Translation(eyeDist, 0f, 0f); device.Transform.View *= Matrix.RotationY((float)MathEngine.DegreesToRadians(m_focusAngle)); RenderRightEye(drawArgs, m_rightTexture, camera); // Combine results device.RenderState.ZBufferEnable = false; device.VertexFormat = CustomVertex.TransformedTextured.Format; device.TextureState[0].ColorOperation = TextureOperation.SelectArg1; device.TextureState[0].ColorArgument1 = TextureArgument.TextureColor; device.RenderState.ColorWriteEnable = ColorWriteEnable.Green | ColorWriteEnable.Blue; device.SetTexture(0, m_rightTexture); device.DrawUserPrimitives(PrimitiveType.TriangleStrip, 2, windowQuad); // Reset state device.SetTexture(0, null); device.RenderState.ColorWriteEnable = ColorWriteEnable.RedGreenBlueAlpha; } finally { isRendering = false; } }
public override void SetPosition(double lat, double lon, double heading, double _altitude, double tilt, double bank) { if (double.IsNaN(lat)) { lat = this._latitude.Degrees; } if (double.IsNaN(lon)) { lon = this._longitude.Degrees; } if (double.IsNaN(heading)) { heading = this._heading.Degrees; } if (double.IsNaN(bank)) { bank = _targetBank.Degrees; } this._targetOrientation = Quaternion4d.EulerToQuaternion( MathEngine.DegreesToRadians(lon), MathEngine.DegreesToRadians(lat), MathEngine.DegreesToRadians(heading)); Point3d v = Quaternion4d.QuaternionToEuler(this._targetOrientation); this._targetLatitude.Radians = v.Y; this._targetLongitude.Radians = v.X; this._targetHeading.Radians = v.Z; if (!double.IsNaN(tilt)) { this.Tilt = Angle.FromDegrees(tilt); } if (!double.IsNaN(_altitude)) { Altitude = _altitude; } this.Bank = Angle.FromDegrees(bank); }
///<summary> /// 使照相机ZOOM到图层的位置。 /// </summary> protected virtual void OnGotoClick(object sender, EventArgs e) { lock (this.ParentList.ChildObjects.SyncRoot) { for (int i = 0; i < this.ParentList.ChildObjects.Count; i++) { RenderableObject ro = (RenderableObject)this.ParentList.ChildObjects[i]; if (ro.Name.Equals(name)) { if (ro is QuadTileSet) { QuadTileSet qts = (QuadTileSet)ro; DrawArgs.Camera.SetPosition((qts.North + qts.South) / 2, (qts.East + qts.West) / 2); double perpendicularViewRange = (qts.North - qts.South > qts.East - qts.West ? qts.North - qts.South : qts.East - qts.West); double altitude = qts.LayerRadius * Math.Sin(MathEngine.DegreesToRadians(perpendicularViewRange * 0.5)); DrawArgs.Camera.Altitude = altitude; break; } else { double north = Convert.ToDouble(ro.MetaData["north"]); double south = Convert.ToDouble(ro.MetaData["south"]); double west = Convert.ToDouble(ro.MetaData["west"]); double east = Convert.ToDouble(ro.MetaData["east"]); DrawArgs.Camera.SetPosition((north + south) / 2, (west + east) / 2); } if (ro is Icon) { Icon ico = (Icon)ro; DrawArgs.Camera.SetPosition(ico.Latitude, ico.Longitude); DrawArgs.Camera.Altitude /= 2; break; } } } } }
/// <summary> /// Sets camera position. /// </summary> /// <param name="lat">Latitude in decimal degrees</param> /// <param name="lon">Longitude in decimal degrees</param> /// <param name="heading">Heading in decimal degrees</param> /// <param name="_altitude">Altitude above ground level in meters</param> /// <param name="tilt">Tilt in decimal degrees</param> /// <param name="bank">Camera bank (roll) in decimal degrees</param> public virtual void SetPosition(double lat, double lon, double heading, double _altitude, double tilt, double bank) { if (double.IsNaN(lat)) { lat = this._latitude.Degrees; } if (double.IsNaN(lon)) { lon = this._longitude.Degrees; } if (double.IsNaN(heading)) { heading = this._heading.Degrees; } if (double.IsNaN(bank)) { bank = this._bank.Degrees; } m_Orientation = Quaternion4d.EulerToQuaternion( MathEngine.DegreesToRadians(lon), MathEngine.DegreesToRadians(lat), MathEngine.DegreesToRadians(heading)); Point3d p = Quaternion4d.QuaternionToEuler(m_Orientation); _latitude.Radians = p.Y; _longitude.Radians = p.X; _heading.Radians = p.Z; if (!double.IsNaN(tilt)) { Tilt = Angle.FromDegrees(tilt); } if (!double.IsNaN(_altitude)) { this.Altitude = _altitude; } this.Bank = Angle.FromDegrees(bank); }
public override void Render(DrawArgs drawArgs) { // Vector3 here = MathEngine.SphericalToCartesian(drawArgs.WorldCamera.Latitude, drawArgs.WorldCamera.Longitude, this.layerRadius); Matrix currentWorld = drawArgs.device.Transform.World; drawArgs.device.RenderState.Lighting = true; drawArgs.device.RenderState.ZBufferEnable = true; drawArgs.device.Lights[0].Diffuse = System.Drawing.Color.White; drawArgs.device.Lights[0].Type = LightType.Point; drawArgs.device.Lights[0].Range = 100000; drawArgs.device.Lights[0].Position = new Vector3(this.layerRadius, 0, 0); drawArgs.device.Lights[0].Enabled = true; drawArgs.device.RenderState.CullMode = Cull.None; drawArgs.device.Transform.World = Matrix.Identity; drawArgs.device.Transform.World *= Matrix.Scaling(this.scaleFactor, this.scaleFactor, this.scaleFactor); //drawArgs.device.Transform.World *= Matrix.RotationX(MathEngine.RadiansToDegrees(90)); drawArgs.device.Transform.World *= Matrix.Translation(0, 0, -this.layerRadius); drawArgs.device.Transform.World *= Matrix.RotationY((float)MathEngine.DegreesToRadians(90 - this.lat)); drawArgs.device.Transform.World *= Matrix.RotationZ((float)MathEngine.DegreesToRadians(180 + this.lon)); //drawArgs.device.Transform.World *= Matrix.RotationQuaternion(drawArgs.WorldCamera.CurrentOrientation); drawArgs.device.TextureState[0].ColorOperation = TextureOperation.Disable; drawArgs.device.RenderState.NormalizeNormals = true; for (int i = 0; i < this.meshMaterials.Length; i++) { drawArgs.device.Material = this.meshMaterials[i]; this.mesh.DrawSubset(i); } drawArgs.device.Transform.World = currentWorld; drawArgs.device.RenderState.CullMode = Cull.Clockwise; drawArgs.device.RenderState.Lighting = false; }
public override void Render(DrawArgs drawArgs) { if (errorMsg != null) { //System.Windows.Forms.MessageBox.Show( errorMsg, "Model failed to load.", MessageBoxButtons.OK, MessageBoxIcon.Error); errorMsg = null; IsOn = false; isInitialized = false; return; } if (!IsVisible(drawArgs.WorldCamera)) { // Mesh is not in view, unload it to save memory if (isInitialized) { Dispose(); } return; } if (!isInitialized) { return; } drawArgs.device.RenderState.CullMode = Cull.None; drawArgs.device.RenderState.Lighting = true; drawArgs.device.RenderState.AmbientColor = 0x808080; drawArgs.device.RenderState.NormalizeNormals = true; drawArgs.device.Lights[0].Diffuse = Color.FromArgb(255, 255, 255); drawArgs.device.Lights[0].Type = LightType.Directional; drawArgs.device.Lights[0].Direction = new Vector3(1f, 1f, 1f); drawArgs.device.Lights[0].Enabled = true; drawArgs.device.SamplerState[0].AddressU = TextureAddress.Wrap; drawArgs.device.SamplerState[0].AddressV = TextureAddress.Wrap; drawArgs.device.RenderState.AlphaBlendEnable = true; drawArgs.device.TextureState[0].ColorArgument1 = TextureArgument.TextureColor; drawArgs.device.TextureState[0].ColorOperation = TextureOperation.SelectArg1; // Put the light somewhere up in space drawArgs.device.Lights[0].Position = new Vector3( (float)worldXyz.X * 2f, (float)worldXyz.Y * 1f, (float)worldXyz.Z * 1.5f); Matrix currentWorld = drawArgs.device.Transform.World; drawArgs.device.Transform.World = Matrix.RotationX((float)MathEngine.DegreesToRadians(Rotx)); drawArgs.device.Transform.World *= Matrix.RotationZ((float)MathEngine.DegreesToRadians(Roty)); drawArgs.device.Transform.World *= Matrix.RotationZ((float)MathEngine.DegreesToRadians(Rotz)); drawArgs.device.Transform.World *= Matrix.Scaling(Scale, Scale, Scale); // Move the mesh to desired location on earth if (isVertExaggerable == true) { vertExaggeration = World.Settings.VerticalExaggeration; } else { vertExaggeration = 1; } drawArgs.device.Transform.World *= Matrix.Translation(0, 0, (float)drawArgs.WorldCamera.WorldRadius + (currentElevation * Convert.ToInt16(isElevationRelative2Ground) + Altitude) * vertExaggeration); drawArgs.device.Transform.World *= Matrix.RotationY((float)MathEngine.DegreesToRadians(90 - Latitude)); drawArgs.device.Transform.World *= Matrix.RotationZ((float)MathEngine.DegreesToRadians(Longitude)); drawArgs.device.Transform.World *= Matrix.Translation( (float)-drawArgs.WorldCamera.ReferenceCenter.X, (float)-drawArgs.WorldCamera.ReferenceCenter.Y, (float)-drawArgs.WorldCamera.ReferenceCenter.Z ); for (int i = 0; i < meshMaterials.Length; i++) { // Set the material and texture for this subset drawArgs.device.Material = meshMaterials[i]; drawArgs.device.SetTexture(0, meshTextures[i]); // Draw the mesh subset mesh.DrawSubset(i); } drawArgs.device.Transform.World = currentWorld; drawArgs.device.RenderState.Lighting = false; }
private void RenderBar(DrawArgs drawArgs) { bool lighting = drawArgs.device.RenderState.Lighting; drawArgs.device.RenderState.Lighting = false; Matrix translation = Matrix.Translation( (float)(m_cartesianPoint.X - drawArgs.WorldCamera.ReferenceCenter.X), (float)(m_cartesianPoint.Y - drawArgs.WorldCamera.ReferenceCenter.Y), (float)(m_cartesianPoint.Z - drawArgs.WorldCamera.ReferenceCenter.Z) ); if (m_extrudeVertices == null) { CreateExtrude(drawArgs.device); } if (m_useScaling) { double targetPercent = (m_targetScalar - m_scalarMinimum) / (m_scalarMaximum - m_scalarMinimum); if (m_currentPercent != targetPercent) { double delta = Math.Abs(targetPercent - m_currentPercent); delta *= 0.1; if (m_currentPercent < targetPercent) { m_currentPercent += delta; } else { m_currentPercent -= delta; } } if (getColor(m_currentPercent).ToArgb() != m_extrudeVertices[0].Color) { UpdateColor(m_currentPercent); } RenderedHeight = m_currentPercent * World.Settings.VerticalExaggeration * m_height; drawArgs.device.Transform.World = Matrix.Scaling(m_scaleX, m_scaleY, (float)-m_currentPercent * World.Settings.VerticalExaggeration * (float)m_height); } else { if (m_color != m_extrudeVertices[0].Color) { UpdateColor(m_color); } RenderedHeight = m_height * World.Settings.VerticalExaggeration; drawArgs.device.Transform.World = Matrix.Scaling(m_scaleX, m_scaleY, (float)-m_height * World.Settings.VerticalExaggeration); } drawArgs.device.Transform.World *= Matrix.RotationY((float)-MathEngine.DegreesToRadians(90)); drawArgs.device.Transform.World *= Matrix.RotationY((float)-MathEngine.DegreesToRadians(m_latitude)); drawArgs.device.Transform.World *= Matrix.RotationZ((float)MathEngine.DegreesToRadians(m_longitude)); drawArgs.device.Transform.World *= translation; drawArgs.device.VertexFormat = CustomVertex.PositionColored.Format; drawArgs.device.TextureState[0].ColorOperation = TextureOperation.SelectArg1; drawArgs.device.TextureState[0].ColorArgument1 = TextureArgument.Diffuse; drawArgs.device.TextureState[0].AlphaArgument1 = TextureArgument.Diffuse; drawArgs.device.TextureState[0].AlphaOperation = TextureOperation.SelectArg1; drawArgs.device.DrawIndexedUserPrimitives(PrimitiveType.TriangleList, 0, m_extrudeVertices.Length, m_extrudeIndices.Length / 3, m_extrudeIndices, true, m_extrudeVertices); drawArgs.device.DrawIndexedUserPrimitives(PrimitiveType.LineList, 0, m_outlineVertices.Length, m_outlineIndices.Length / 2, m_outlineIndices, true, m_outlineVertices); drawArgs.device.RenderState.Lighting = lighting; }
/// <summary> /// Create child tile terrain mesh /// </summary> protected void CreateElevatedMesh(ChildLocation corner, CustomVertex.PositionTextured[] vertices, double meshBaseRadius, float[,] heightData) { // Figure out child lat/lon boundaries (radians) double north = MathEngine.DegreesToRadians(North); double west = MathEngine.DegreesToRadians(West); // Texture coordinate offsets float TuOffset = 0; float TvOffset = 0; switch (corner) { case ChildLocation.NorthWest: // defaults are all good break; case ChildLocation.NorthEast: west = MathEngine.DegreesToRadians(0.5 * (West + East)); TuOffset = 0.5f; break; case ChildLocation.SouthWest: north = MathEngine.DegreesToRadians(0.5 * (North + South)); TvOffset = 0.5f; break; case ChildLocation.SouthEast: north = MathEngine.DegreesToRadians(0.5 * (North + South)); west = MathEngine.DegreesToRadians(0.5 * (West + East)); TuOffset = 0.5f; TvOffset = 0.5f; break; } double latitudeRadianSpan = MathEngine.DegreesToRadians(LatitudeSpan); double longitudeRadianSpan = MathEngine.DegreesToRadians(LongitudeSpan); double layerRadius = (double)QuadTileSet.LayerRadius; double scaleFactor = 1.0 / vertexCountElevated; int terrainLongitudeIndex = (int)(TuOffset * vertexCountElevated); int terrainLatitudeIndex = (int)(TvOffset * vertexCountElevated); int vertexCountElevatedPlus1 = vertexCountElevated / 2 + 1; double radius = 0; int vertexIndex = 0; for (int latitudeIndex = -1; latitudeIndex <= vertexCountElevatedPlus1; latitudeIndex++) { int latitudePoint = latitudeIndex; if (latitudePoint < 0) { latitudePoint = 0; } else if (latitudePoint >= vertexCountElevatedPlus1) { latitudePoint = vertexCountElevatedPlus1 - 1; } double latitudeFactor = latitudePoint * scaleFactor; double latitude = north - latitudeFactor * latitudeRadianSpan; // Cache trigonometric values double cosLat = Math.Cos(latitude); double sinLat = Math.Sin(latitude); for (int longitudeIndex = -1; longitudeIndex <= vertexCountElevatedPlus1; longitudeIndex++) { int longitudePoint = longitudeIndex; if (longitudePoint < 0) { longitudePoint = 0; } else if (longitudePoint >= vertexCountElevatedPlus1) { longitudePoint = vertexCountElevatedPlus1 - 1; } if (longitudeIndex != longitudePoint || latitudeIndex != latitudePoint) { if (heightData != null && (!renderStruts || m_CurrentOpacity < 255)) { radius = layerRadius + heightData[terrainLatitudeIndex + latitudePoint, terrainLongitudeIndex + longitudePoint] * verticalExaggeration; } else { radius = meshBaseRadius; } // // Mesh base (flat) // radius = meshBaseRadius; } else { // Top of mesh (real terrain) radius = layerRadius + heightData[terrainLatitudeIndex + latitudeIndex, terrainLongitudeIndex + longitudeIndex] * verticalExaggeration; } double longitudeFactor = longitudePoint * scaleFactor; // Texture coordinates vertices[vertexIndex].Tu = TuOffset + (float)longitudeFactor; vertices[vertexIndex].Tv = TvOffset + (float)latitudeFactor; // Convert from spherical (radians) to cartesian double longitude = west + longitudeFactor * longitudeRadianSpan; double radCosLat = radius * cosLat; vertices[vertexIndex].X = (float)(radCosLat * Math.Cos(longitude) - localOrigin.X); vertices[vertexIndex].Y = (float)(radCosLat * Math.Sin(longitude) - localOrigin.Y); vertices[vertexIndex].Z = (float)(radius * sinLat - localOrigin.Z); vertexIndex++; } } }
private void RenderFlag(DrawArgs drawArgs, double offset) { if (m_effect == null) { string outerrors = ""; System.Reflection.Assembly assembly = System.Reflection.Assembly.GetExecutingAssembly(); Stream effectStream = assembly.GetManifestResourceStream("WorldWind.Shaders.flag.fx"); m_effect = Effect.FromStream( drawArgs.device, effectStream, null, null, ShaderFlags.None, null, out outerrors); if (outerrors != null && outerrors.Length > 0) { Log.Write(Log.Levels.Error, outerrors); } } if (m_vertexBuffer == null) { drawArgs.device.DeviceReset += new EventHandler(device_DeviceReset); device_DeviceReset(drawArgs.device, null); } if (m_flagPoleVertices == null) { CreateFlagPole(drawArgs.device); } Vector3 pos = MathEngine.SphericalToCartesian(m_latitude, m_longitude, World.EquatorialRadius + World.Settings.VerticalExaggeration * ScaleZ + offset); Vector3 surfacePos = MathEngine.SphericalToCartesian(m_latitude, m_longitude, World.EquatorialRadius + offset); Vector3 rc = new Vector3( (float)drawArgs.WorldCamera.ReferenceCenter.X, (float)drawArgs.WorldCamera.ReferenceCenter.Y, (float)drawArgs.WorldCamera.ReferenceCenter.Z ); drawArgs.device.Transform.World = Matrix.Scaling(World.Settings.VerticalExaggeration * ScaleX * 0.01f, World.Settings.VerticalExaggeration * ScaleY * 0.01f, -World.Settings.VerticalExaggeration * 2 * ScaleZ); drawArgs.device.Transform.World *= Matrix.RotationY((float)-MathEngine.DegreesToRadians(90)); drawArgs.device.Transform.World *= Matrix.RotationY((float)-MathEngine.DegreesToRadians(m_latitude)); drawArgs.device.Transform.World *= Matrix.RotationZ((float)MathEngine.DegreesToRadians(m_longitude)); drawArgs.device.Transform.World *= Matrix.Translation(surfacePos - rc); drawArgs.device.VertexFormat = CustomVertex.PositionColored.Format; drawArgs.device.TextureState[0].ColorOperation = TextureOperation.SelectArg1; drawArgs.device.TextureState[0].ColorArgument1 = TextureArgument.Diffuse; drawArgs.device.TextureState[0].AlphaArgument1 = TextureArgument.Diffuse; drawArgs.device.TextureState[0].AlphaOperation = TextureOperation.SelectArg1; drawArgs.device.DrawIndexedUserPrimitives(PrimitiveType.TriangleList, 0, m_flagPoleVertices.Length, m_flagPoleIndices.Length / 3, m_flagPoleIndices, true, m_flagPoleVertices); drawArgs.device.DrawIndexedUserPrimitives(PrimitiveType.LineList, 0, m_outlineFlagPoleVertices.Length, m_outlineFlagPoleIndices.Length / 2, m_outlineFlagPoleIndices, true, m_outlineFlagPoleVertices); m_angle += .04f; if (m_angle > 360) { m_angle = 0; } drawArgs.device.VertexFormat = CustomVertex.PositionNormalTextured.Format; drawArgs.device.Transform.World = Matrix.Scaling(World.Settings.VerticalExaggeration * ScaleX, World.Settings.VerticalExaggeration * ScaleY, World.Settings.VerticalExaggeration * ScaleZ); drawArgs.device.Transform.World *= Matrix.RotationY((float)-MathEngine.DegreesToRadians(m_latitude)); drawArgs.device.Transform.World *= Matrix.RotationZ((float)MathEngine.DegreesToRadians(m_longitude)); drawArgs.device.Transform.World *= Matrix.Translation(pos - rc); Matrix worldViewProj = drawArgs.device.Transform.World * drawArgs.device.Transform.View * drawArgs.device.Transform.Projection; System.DateTime currentTime = TimeKeeper.CurrentTimeUtc; Point3d sunPosition = SunCalculator.GetGeocentricPosition(currentTime); Vector3 sunVector = new Vector3( (float)-sunPosition.X, (float)-sunPosition.Y, (float)-sunPosition.Z); m_effect.Technique = "VertexAndPixelShader"; m_effect.SetValue("angle", (float)m_angle); m_effect.SetValue("attentuation", Attentuation); m_effect.SetValue("World", drawArgs.device.Transform.World); m_effect.SetValue("View", drawArgs.device.Transform.View); m_effect.SetValue("Projection", drawArgs.device.Transform.Projection); m_effect.SetValue("Tex0", m_texture); m_effect.SetValue("lightDir", new Vector4(sunVector.X, sunVector.Y, sunVector.Z, 0)); drawArgs.device.Indices = m_indexBuffer; drawArgs.device.SetStreamSource(0, m_vertexBuffer, 0); int numPasses = m_effect.Begin(0); for (int i = 0; i < numPasses; i++) { m_effect.BeginPass(i); drawArgs.device.DrawIndexedPrimitives( PrimitiveType.TriangleList, 0, 0, m_vertices.Length, 0, m_indices.Length / 3); m_effect.EndPass(); } m_effect.End(); drawArgs.device.Indices = null; drawArgs.device.Transform.World = drawArgs.WorldCamera.WorldMatrix; drawArgs.device.Transform.View = drawArgs.WorldCamera.ViewMatrix; }
private void renderHighlight(DrawArgs drawArgs) { bool lighting = drawArgs.device.RenderState.Lighting; drawArgs.device.RenderState.Lighting = false; if (m_highlightVertices == null) { m_highlightVertices = new CustomVertex.PositionColoredTextured[4]; m_highlightVertices[0].X = -0.5f; m_highlightVertices[0].Y = 0.5f; m_highlightVertices[0].Z = 0.0f; m_highlightVertices[0].Tu = 0.0f; m_highlightVertices[0].Tv = 0.0f; m_highlightVertices[1].X = -0.5f; m_highlightVertices[1].Y = -0.5f; m_highlightVertices[1].Z = 0.0f; m_highlightVertices[1].Tu = 0.0f; m_highlightVertices[1].Tv = 1.0f; m_highlightVertices[2].X = 0.5f; m_highlightVertices[2].Y = 0.5f; m_highlightVertices[2].Z = 0.0f; m_highlightVertices[2].Tu = 1.0f; m_highlightVertices[2].Tv = 0.0f; m_highlightVertices[3].X = 0.5f; m_highlightVertices[3].Y = -0.5f; m_highlightVertices[3].Z = 0.0f; m_highlightVertices[3].Tu = 1.0f; m_highlightVertices[3].Tv = 1.0f; } if (HighlightTexture == null) { HighlightTexture = ImageHelper.LoadTexture(highlightTexturePath); } drawArgs.device.Transform.World = Matrix.RotationY((float)MathEngine.DegreesToRadians(90)); drawArgs.device.Transform.World *= Matrix.Scaling(World.Settings.VerticalExaggeration * ScaleX, World.Settings.VerticalExaggeration * ScaleY, World.Settings.VerticalExaggeration * ScaleZ); drawArgs.device.Transform.World *= Matrix.RotationY((float)-MathEngine.DegreesToRadians(m_latitude)); drawArgs.device.Transform.World *= Matrix.RotationZ((float)MathEngine.DegreesToRadians(m_longitude)); Vector3 surfacePos = MathEngine.SphericalToCartesian(m_latitude, m_longitude, World.EquatorialRadius); Vector3 rc = new Vector3( (float)drawArgs.WorldCamera.ReferenceCenter.X, (float)drawArgs.WorldCamera.ReferenceCenter.Y, (float)drawArgs.WorldCamera.ReferenceCenter.Z ); drawArgs.device.Transform.World *= Matrix.Translation(surfacePos - rc); drawArgs.device.TextureState[0].ColorOperation = TextureOperation.SelectArg1; drawArgs.device.TextureState[0].ColorArgument1 = TextureArgument.TextureColor; drawArgs.device.TextureState[0].AlphaArgument1 = TextureArgument.TextureColor; drawArgs.device.TextureState[0].AlphaOperation = TextureOperation.SelectArg1; drawArgs.device.RenderState.ZBufferEnable = false; drawArgs.device.SetTexture(0, HighlightTexture); drawArgs.device.VertexFormat = CustomVertex.PositionColoredTextured.Format; drawArgs.device.DrawUserPrimitives(PrimitiveType.TriangleStrip, 2, m_highlightVertices); drawArgs.device.RenderState.ZBufferEnable = true; drawArgs.device.RenderState.Lighting = lighting; }
///<summary> /// 使照相机ZOOM到图层的位置。 /// </summary> protected virtual void OnGotoClick(object sender, EventArgs e) { lock (this.ParentList.ChildObjects.SyncRoot) { for (int i = 0; i < this.ParentList.ChildObjects.Count; i++) { RenderableObject ro = (RenderableObject)this.ParentList.ChildObjects[i]; if (ro.Name.Equals(name)) { if (ro is QuadTileSet) {//瓦片集 QuadTileSet qts = (QuadTileSet)ro; //设置摄像机位置 DrawArgs.Camera.SetPosition((qts.North + qts.South) / 2, (qts.East + qts.West) / 2); //垂直视域范围 double perpendicularViewRange = (qts.North - qts.South > qts.East - qts.West ? qts.North - qts.South : qts.East - qts.West); //高度,海拔 DrawArgs.Camera.Altitude = qts.LayerRadius * Math.Sin(MathEngine.DegreesToRadians(perpendicularViewRange * 0.5)); break; } else if (ro is Icon) {//图标 Icon ico = (Icon)ro; DrawArgs.Camera.SetPosition(ico.Latitude, ico.Longitude); if (ico.Altitude != 0) { DrawArgs.Camera.Altitude = ico.Altitude; } else { DrawArgs.Camera.Altitude = ico.MaximumDisplayDistance - 100000; } break; } else if (ro is Icons) { double lat = 0, lon = 0; Icons icons = ro as Icons; foreach (Icon icon in icons.ChildObjects) { lat += icon.Latitude; lon += icon.Longitude; } lat /= ((Icons)ro).ChildObjects.Count; lon /= ((Icons)ro).ChildObjects.Count; DrawArgs.Camera.SetPosition(lat, lon); if (icons.MaximumDisplayDistance > 16521634) { icons.MaximumDisplayDistance = 16521634; } DrawArgs.Camera.Altitude = icons.MaximumDisplayDistance - 100000; break; } else if (ro is GCP) { GCP gcp = (GCP)ro; DrawArgs.Camera.SetPosition(gcp.Latitude, gcp.Longitude); break; } else if (ro is GCPs) { double lat = 0, lon = 0; foreach (GCP gcp in ((GCPs)ro).ChildObjects) { lat += gcp.Latitude; lon += gcp.Longitude; } lat /= ((GCPs)ro).ChildObjects.Count; lon /= ((GCPs)ro).ChildObjects.Count; DrawArgs.Camera.SetPosition(lat, lon); break; } else { DrawArgs.Camera.SetPosition((ro.Extension.North + ro.Extension.South) / 2, (ro.Extension.West + ro.Extension.East) / 2); //垂直视域范围 double perpendicularViewRange = (ro.Extension.North - ro.Extension.South > ro.Extension.East - ro.Extension.West ? ro.Extension.North - ro.Extension.South : ro.Extension.East - ro.Extension.West); //高度,海拔 double alt = (float)(ro.World.EquatorialRadius) * Math.Sin(MathEngine.DegreesToRadians(perpendicularViewRange * 1.1)); if (alt > ro.MaximumDisplayDistance - 100000) { alt = ro.MaximumDisplayDistance - 100000; } DrawArgs.Camera.Altitude = alt; } } } } }
public override void Render(DrawArgs drawArgs) { if (errorMsg != null) { //System.Windows.Forms.MessageBox.Show( errorMsg, "Model failed to load.", MessageBoxButtons.OK, MessageBoxIcon.Error); errorMsg = null; IsOn = false; isInitialized = false; return; } if (!IsVisible(drawArgs.WorldCamera)) { // Mesh is not in view, unload it to save memory if (isInitialized) { Dispose(); } return; } if (!isInitialized) { return; } //Donot render for other planets if (!drawArgs.CurrentWorld.IsEarth) { return; } drawArgs.device.RenderState.CullMode = Cull.None; drawArgs.device.RenderState.Lighting = true; drawArgs.device.RenderState.AmbientColor = 0x808080; drawArgs.device.RenderState.NormalizeNormals = true; drawArgs.device.Lights[0].Diffuse = Color.FromArgb(255, 255, 255); drawArgs.device.Lights[0].Type = LightType.Directional; drawArgs.device.Lights[0].Direction = new Vector3(1f, 1f, 1f); drawArgs.device.Lights[0].Enabled = true; drawArgs.device.SamplerState[0].AddressU = TextureAddress.Wrap; drawArgs.device.SamplerState[0].AddressV = TextureAddress.Wrap; drawArgs.device.RenderState.AlphaBlendEnable = true; drawArgs.device.TextureState[0].ColorArgument1 = TextureArgument.TextureColor; drawArgs.device.TextureState[0].ColorOperation = TextureOperation.SelectArg1; // Put the light somewhere up in space drawArgs.device.Lights[0].Position = new Vector3( (float)worldXyz.X * 2f, (float)worldXyz.Y * 1f, (float)worldXyz.Z * 1.5f); Matrix currentWorld = drawArgs.device.Transform.World; drawArgs.device.Transform.World = Matrix.RotationX((float)MathEngine.DegreesToRadians(RotX)); drawArgs.device.Transform.World *= Matrix.RotationZ((float)MathEngine.DegreesToRadians(RotY)); drawArgs.device.Transform.World *= Matrix.RotationZ((float)MathEngine.DegreesToRadians(RotZ)); drawArgs.device.Transform.World *= Matrix.Scaling(Scale, Scale, Scale); // Move the mesh to desired location on earth if (IsVertExaggerable) { vertExaggeration = World.Settings.VerticalExaggeration; } else { vertExaggeration = 1; } drawArgs.device.Transform.World *= Matrix.Translation(0, 0, (float)drawArgs.WorldCamera.WorldRadius + Altitude * vertExaggeration); drawArgs.device.Transform.World *= Matrix.RotationY((float)MathEngine.DegreesToRadians(90 - Latitude)); drawArgs.device.Transform.World *= Matrix.RotationZ((float)MathEngine.DegreesToRadians(Longitude)); drawArgs.device.Transform.World *= Matrix.Translation( (float)-drawArgs.WorldCamera.ReferenceCenter.X, (float)-drawArgs.WorldCamera.ReferenceCenter.Y, (float)-drawArgs.WorldCamera.ReferenceCenter.Z ); Device device = drawArgs.device; // Draw the mesh with effect if (isBumpmapped) { setupBumpEffect(drawArgs); } else { setupReflectionEffect(drawArgs); } //render the effect bool alphastate = device.RenderState.AlphaBlendEnable; device.RenderState.AlphaBlendEnable = true; effect.Begin(0); effect.BeginPass(0); //drawArgs.device.SetTexture(0, texture); m_meshElems[0].mesh.DrawSubset(0); effect.EndPass(); effect.End(); device.RenderState.AlphaBlendEnable = alphastate; drawArgs.device.Transform.World = currentWorld; drawArgs.device.RenderState.Lighting = false; }
/// <summary> /// Gets called when user left clicks. /// RenderableObject abstract member (needed) /// Called from UI thread = UI code safe in this function /// </summary> public override bool PerformSelectionAction(DrawArgs drawArgs) { // Note: The following assignment to m is // to match the assignment to // drawArgs.device.Transform.World in Render(). // Changes to that matrix should be mirrored here // until the assignment is centralized. Matrix m = Matrix.RotationX((float)MathEngine.DegreesToRadians(RotX)); m *= Matrix.RotationY((float)MathEngine.DegreesToRadians(RotY)); m *= Matrix.RotationZ((float)MathEngine.DegreesToRadians(RotZ)); m *= Matrix.Scaling(Scale, Scale, Scale); if (IsVertExaggerable == true) { vertExaggeration = World.Settings.VerticalExaggeration; } else { vertExaggeration = 1; } m *= Matrix.Translation(0, 0, (float)(drawArgs.WorldCamera.WorldRadius + ((currentElevation + Altitude) * vertExaggeration))); m *= Matrix.RotationY((float)MathEngine.DegreesToRadians(90 - Latitude)); m *= Matrix.RotationZ((float)MathEngine.DegreesToRadians(Longitude)); m *= Matrix.Translation( (float)-drawArgs.WorldCamera.ReferenceCenter.X, (float)-drawArgs.WorldCamera.ReferenceCenter.Y, (float)-drawArgs.WorldCamera.ReferenceCenter.Z ); m = Matrix.Invert(m); Vector3 v1 = new Vector3(); v1.X = DrawArgs.LastMousePosition.X; v1.Y = DrawArgs.LastMousePosition.Y; v1.Z = drawArgs.WorldCamera.Viewport.MinZ; v1.Unproject(drawArgs.WorldCamera.Viewport, drawArgs.WorldCamera.ProjectionMatrix, drawArgs.WorldCamera.ViewMatrix, drawArgs.WorldCamera.WorldMatrix); Vector3 v2 = new Vector3(); v2.X = DrawArgs.LastMousePosition.X; v2.Y = DrawArgs.LastMousePosition.Y; v2.Z = drawArgs.WorldCamera.Viewport.MaxZ; v2.Unproject(drawArgs.WorldCamera.Viewport, drawArgs.WorldCamera.ProjectionMatrix, drawArgs.WorldCamera.ViewMatrix, drawArgs.WorldCamera.WorldMatrix); v1.TransformCoordinate(m); v2.TransformCoordinate(m); bool sel = false; foreach (MeshElem me in m_meshElems) { sel |= (me.mesh.Intersect(v1, v2 - v1)); } if (sel) { } return(sel); }
public override void Render(DrawArgs drawArgs) { if (errorMsg != null) { //System.Windows.Forms.MessageBox.Show( errorMsg, "Model failed to load.", MessageBoxButtons.OK, MessageBoxIcon.Error); errorMsg = null; IsOn = false; isInitialized = false; return; } if (!IsVisible(drawArgs.WorldCamera)) { // Mesh is not in view, unload it to save memory //if (isInitialized) //Dispose(); return; } if (!isInitialized) { this.Update(drawArgs); } // save current state Matrix currentWorld = drawArgs.device.Transform.World; Cull cullMode = drawArgs.device.RenderState.CullMode; bool lighting = drawArgs.device.RenderState.Lighting; int ambientColor = drawArgs.device.RenderState.AmbientColor; bool normalizeNormals = drawArgs.device.RenderState.NormalizeNormals; drawArgs.device.RenderState.CullMode = Cull.None; drawArgs.device.RenderState.Lighting = true; drawArgs.device.RenderState.AmbientColor = 0xCCCCCC; // 0x808080; drawArgs.device.RenderState.NormalizeNormals = true; drawArgs.device.Lights[0].Diffuse = Color.FromArgb(255, 255, 255); drawArgs.device.Lights[0].Ambient = TintColor; drawArgs.device.Lights[0].Type = LightType.Directional; drawArgs.device.Lights[0].Direction = new Vector3(1f, 1f, 1f); drawArgs.device.Lights[0].Enabled = true; drawArgs.device.SamplerState[0].AddressU = TextureAddress.Wrap; drawArgs.device.SamplerState[0].AddressV = TextureAddress.Wrap; drawArgs.device.RenderState.AlphaBlendEnable = true; drawArgs.device.TextureState[0].ColorArgument1 = TextureArgument.TextureColor; drawArgs.device.TextureState[0].ColorOperation = TextureOperation.SelectArg1; // Put the light somewhere up in space drawArgs.device.Lights[0].Position = new Vector3( (float)worldXyz.X * 2f, (float)worldXyz.Y * 1f, (float)worldXyz.Z * 1.5f); drawArgs.device.Transform.World = Matrix.RotationX((float)MathEngine.DegreesToRadians(RotX)); drawArgs.device.Transform.World *= Matrix.RotationY((float)MathEngine.DegreesToRadians(RotY)); drawArgs.device.Transform.World *= Matrix.RotationZ((float)MathEngine.DegreesToRadians(RotZ)); drawArgs.device.Transform.World *= Matrix.Scaling(Scale, Scale, Scale); // Move the mesh to desired location on earth if (IsVertExaggerable == true) { vertExaggeration = World.Settings.VerticalExaggeration; } else { vertExaggeration = 1; } drawArgs.device.Transform.World *= Matrix.Translation(0, 0, (float)(drawArgs.WorldCamera.WorldRadius + ((currentElevation + Altitude) * vertExaggeration))); drawArgs.device.Transform.World *= Matrix.RotationY((float)MathEngine.DegreesToRadians(90 - Latitude)); drawArgs.device.Transform.World *= Matrix.RotationZ((float)MathEngine.DegreesToRadians(Longitude)); drawArgs.device.Transform.World *= Matrix.Translation( (float)-drawArgs.WorldCamera.ReferenceCenter.X, (float)-drawArgs.WorldCamera.ReferenceCenter.Y, (float)-drawArgs.WorldCamera.ReferenceCenter.Z ); foreach (MeshElem me in m_meshElems) { try { for (int i = 0; i < me.meshMaterials.Length; i++) { // Set the material and texture for this subset drawArgs.device.Material = me.meshMaterials[i]; drawArgs.device.SetTexture(0, me.meshTextures[i]); // Draw the mesh subset me.mesh.DrawSubset(i); } } catch (Exception) { // Utility.Log.Write(name + caught); } } drawArgs.device.Transform.World = currentWorld; drawArgs.device.RenderState.Lighting = lighting; drawArgs.device.RenderState.CullMode = cullMode; drawArgs.device.RenderState.AmbientColor = ambientColor; drawArgs.device.RenderState.NormalizeNormals = normalizeNormals; }