📄 latlon.cs
字号:
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using Microsoft.WindowsMobile.DirectX;
using Microsoft.WindowsMobile.DirectX.Direct3D;
using WorldWindow;
namespace cfWorldWind
{
class LatLon
{
Material latLonMaterial;
public LatLon()
{
}
public class LatLonLineList
{
public VertexBuffer vertexBuffer;
public int numberOfPrimitives;
}
//public void DrawLatLonLines(Device device, float latitude, float longitude, float altitude, float viewRange)
public List<LatLonLineList> InitializeLatLonLines(Device device, int dLat)
{
latLonMaterial = new Material();
latLonMaterial.Diffuse = Color.Gray;
latLonMaterial.Ambient = Color.Gray;
List<LatLonLineList> alLatLonBuffers = new List<LatLonLineList>();
//this calculation happens during init now
//int dLat = 10;
//double vr = drawArgs.WorldCamera.ViewRange.Radians * 0.75;
//double vr = viewRange * .75;
//if (vr < 0.1)
// dLat = 1;
//else if (vr < 0.6)
// dLat = 2;
//else if (vr < 1.1)
// dLat = 5;
int dLon = dLat;
//if (drawArgs.WorldCamera.Latitude.Radians + vr / 2 > Math.PI / 2 || drawArgs.WorldCamera.Latitude.Radians - vr / 2 < -Math.PI / 2)
//if (MathEngine.DegreesToRadians(latitude) + vr / 2 > Math.PI / 2 || MathEngine.DegreesToRadians(latitude) - vr / 2 < -Math.PI / 2)
//{
// Pole visible, 10 degree longitude spacing forced
// dLon = 10;
//dLat = 10;
//}
//int minLon = dLon >= 10 ? -180 : (int)drawArgs.WorldCamera.Longitude.Degrees / dLon * dLon - 18 * dLon;
//int maxLon = dLon >= 10 ? 180 : (int)drawArgs.WorldCamera.Longitude.Degrees / dLon * dLon + 18 * dLon;
//int minLat = (int)drawArgs.WorldCamera.Latitude.Degrees / dLat * dLat - 9 * dLat;
//int maxLat = (int)drawArgs.WorldCamera.Latitude.Degrees / dLat * dLat + 9 * dLat;
//this calculation doesnt draw all the lines
//int minLon = dLon >= 10 ? -180 : (int)longitude / dLon * dLon - 18 * dLon;
//int maxLon = dLon >= 10 ? 180 : (int)longitude / dLon * dLon + 18 * dLon;
//int minLat = (int)latitude / dLat * dLat - 9 * dLat;
//int maxLat = (int)latitude / dLat * dLat + 9 * dLat;
//if (maxLat - minLat >= 180 || dLon == 10)
//{
// minLat = -90;
// maxLat = 90;
//}
int minLon = -180;
int maxLon = 180;
int minLat = -90;
int maxLat = 90;
double radius = Settings.Radius;
//if (drawArgs.device.RenderState.ZBufferEnable)
if (device.RenderState.ZBufferEnable)
{
double bRadius = radius * 1.01f;
//double nRadius = radius + 0.015f * drawArgs.WorldCamera.Altitude;
double nRadius = radius + 0.015f * Settings.Radius;
radius = nRadius < bRadius ? nRadius : bRadius;
}
//drawArgs.device.TextureState[0].ColorOperation = TextureOperation.Disable;
//drawArgs.device.VertexFormat = CustomVertex.PositionColored.Format;
//ColorOperation defaults to Modulate
//device.TextureState[0].ColorOperation = TextureOperation.Disable;
//device.VertexFormat = CustomVertex.PositionColored.Format;
System.Drawing.Font f = new System.Drawing.Font("Arial", 8F, System.Drawing.FontStyle.Regular);
Microsoft.WindowsMobile.DirectX.Direct3D.Font dispFont = new Microsoft.WindowsMobile.DirectX.Direct3D.Font(device, f);
// Draw latitude lines
for (int i = minLon; i < maxLon; i = i + dLon)
{
int itr = 0;
int numPoints = (maxLat - minLat) / dLat + 1;
CustomVertex.PositionColored[] curLine = new CustomVertex.PositionColored[numPoints];
for (int j = minLat; j <= maxLat; j = j + dLat)
{
Vector3 pos = MathEngine.SphericalToCartesian((float)j, (float)i, radius);
curLine[itr].X = pos.X;
curLine[itr].Y = pos.Y;
curLine[itr].Z = pos.Z;
//curLine[itr].Color = World.Settings.latLonLinesColor;
curLine[itr].Color = Color.Gray.ToArgb();
itr++;
}
//drawArgs.device.DrawUserPrimitives(PrimitiveType.LineStrip, numPoints - 1, curLine);
VertexBuffer vb = new VertexBuffer(typeof(CustomVertex.PositionColored), numPoints, device, 0, CustomVertex.PositionColored.Format, Pool.SystemMemory);
GraphicsStream stm = vb.Lock(0, 0, 0);
stm.Write(curLine);
vb.Unlock();
//device.SetStreamSource(0, vb, 0);
//device.DrawPrimitives(PrimitiveType.LineStrip, 0, numPoints-1);
LatLonLineList llll = new LatLonLineList();
llll.vertexBuffer = vb;
llll.numberOfPrimitives = numPoints - 1;
alLatLonBuffers.Add(llll);
/*
// Draw longitude label
//float lat = (float)(drawArgs.WorldCamera.Latitude + drawArgs.WorldCamera.ViewRange * 0.15).Degrees;
float lat = (float)(MathEngine.RadiansToDegrees(MathEngine.DegreesToRadians(latitude) + viewRange * 0.15));
if (lat > 70)
lat = 70;
Vector3 v = MathEngine.SphericalToCartesian(lat, (float)i, radius);
//if (drawArgs.WorldCamera.ViewFrustum.ContainsPoint(v))
//{
string s = "";
if (i > 0)
s = i.ToString() + "E";
else if (i < 0)
s = Math.Abs(i).ToString() + "W";
else
s = i.ToString();
//v.Project(drawArgs.device.Viewport, drawArgs.device.Transform.Projection, drawArgs.device.Transform.View, drawArgs.device.Transform.World);
//drawArgs.defaultDrawingFont.DrawText(null, s, new System.Drawing.Rectangle((int)v.X + 2, (int)v.Y, drawArgs.screenWidth, drawArgs.screenHeight), DrawTextFormat.NoClip, World.Settings.latLonLinesColor);
v.Project(device.Viewport, device.Transform.Projection, device.Transform.View, device.Transform.World);
dispFont.DrawText(null, s, new System.Drawing.Rectangle((int)v.X + 2, (int)v.Y, device.Viewport.Width, device.Viewport.Height), DrawTextFormat.NoClip, Color.Gray);
//}
*/
}
// Draw longitude lines
for (int iLatitude = minLat; iLatitude <= maxLat; iLatitude = iLatitude + dLat)
{
/*
// Draw latitude label
//float lon = (float)(drawArgs.WorldCamera.Longitude - drawArgs.WorldCamera.ViewRange * 0.25f).Degrees;
float lon = (float)(MathEngine.RadiansToDegrees(MathEngine.DegreesToRadians(longitude) - viewRange * 0.25f));
Vector3 v = MathEngine.SphericalToCartesian((float)iLatitude, lon, radius);
//if (drawArgs.WorldCamera.ViewFrustum.ContainsPoint(v))
//{
v.Project(device.Viewport, device.Transform.Projection, device.Transform.View, device.Transform.World);
int latLabel = iLatitude;
if (latLabel > 90)
latLabel = 180 - latLabel;
else if (latLabel < -90)
latLabel = -180 - latLabel;
string s = Math.Abs(latLabel).ToString();
if (latLabel > 0)
s += "N";
else if (latLabel < 0)
s += "S";
//drawArgs.defaultDrawingFont.DrawText(null, s, new System.Drawing.Rectangle((int)v.X, (int)v.Y, drawArgs.screenWidth, drawArgs.screenHeight), DrawTextFormat.NoClip, World.Settings.latLonLinesColor);
dispFont.DrawText(null, s, new System.Drawing.Rectangle((int)v.X, (int)v.Y, device.Viewport.Width, device.Viewport.Height), DrawTextFormat.NoClip, Color.Gray);
//}
*/
int numPoints = (maxLon - minLon) / dLon + 1;
int itr = 0;
CustomVertex.PositionColored[] curLine = new CustomVertex.PositionColored[numPoints];
for (int j = minLon; j <= maxLon; j = j + dLon)
{
Vector3 pos = MathEngine.SphericalToCartesian((float)iLatitude, (float)j, radius);
curLine[itr].X = pos.X;
curLine[itr].Y = pos.Y;
curLine[itr].Z = pos.Z;
if (iLatitude == 0)
{
curLine[itr].Color = Color.Red.ToArgb(); //TODO this color is ignored
}
else
{
curLine[itr].Color = Color.Gray.ToArgb(); //TODO this color is ignored
}
itr++;
}
//drawArgs.device.DrawUserPrimitives(PrimitiveType.LineStrip, numPoints - 1, curLine);
VertexBuffer vb = new VertexBuffer(typeof(CustomVertex.PositionColored), numPoints, device, 0, CustomVertex.PositionColored.Format, Pool.SystemMemory);
GraphicsStream stm = vb.Lock(0, 0, 0);
stm.Write(curLine);
vb.Unlock();
//device.SetStreamSource(0, vb, 0);
//device.DrawPrimitives(PrimitiveType.LineStrip, 0, numPoints - 1);
LatLonLineList llll = new LatLonLineList();
llll.vertexBuffer = vb;
llll.numberOfPrimitives = numPoints - 1;
alLatLonBuffers.Add(llll);
}
return alLatLonBuffers;
}
public void RenderLatLonLines(Device device, List<LatLonLineList> alLatLonBuffers)
{
//WTF
//the default value is Modules, so reset that at the end
//device.TextureState[0].ColorOperation = TextureOperation.Disable;
//TODO draw labels on lines?
//TODO viewFrustum to remove labels?
if (alLatLonBuffers != null)
{
//TODO how am i supposed to do this instead?
device.Material = latLonMaterial;
device.SetTexture(0, null);
foreach (LatLonLineList llll in alLatLonBuffers)
{
device.SetStreamSource(0, llll.vertexBuffer, 0);
device.DrawPrimitives(PrimitiveType.LineStrip, 0, llll.numberOfPrimitives);
}
}
//device.TextureState[0].ColorOperation = TextureOperation.Modulate;
}
public void Dispose(List<LatLonLineList> latLonList)
{
if (latLonList != null)
{
foreach (LatLonLineList llll in latLonList)
{
llll.vertexBuffer.Dispose();
llll.vertexBuffer = null;
}
latLonList.Clear();
latLonList = null;
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -