原文轉自:http://www.cnblogs.com/wuhenke/archive/2009/12/12/1622752.html
今天主要看了Show Planet Axis、Show Position 、Show Cross Hairs功能,主要是它們在菜單調用方式上都是很類似。代碼如下:
Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->顯示位置信息
private void menuItemShowPosition_Click(object sender, System.EventArgs e)
{
World.Settings.ShowPosition = !World.Settings.ShowPosition;
this.toolBarButtonPosition.Pushed = World.Settings.ShowPosition;
this.menuItemShowPosition.Checked = World.Settings.ShowPosition;
this.worldWindow.Invalidate();
}
Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--> //顯示中心十字標
private void menuItemShowCrosshairs_Click(object sender, System.EventArgs e)
{
//控制中心十字標顯示與否
World.Settings.ShowCrosshairs = !World.Settings.ShowCrosshairs;
this.menuItemShowCrosshairs.Checked = World.Settings.ShowCrosshairs;
this.worldWindow.Invalidate();
}
從上面的代碼看,我們只能驚歎代碼封裝的很好,同樣都調用this.worldWindow.Invalidate();難道Invalidate()函數萬能?!請參考我的Invalidate()方法學習(資料收集),原來該方法是界面區域失效,發送了重繪事件,將會調用WorldWindow.cs中重載了的OnPaint()。OnPaint方法裏主要是調用了 Render()方法。所以我們的關鍵是看Render()中如何實現上面三個功能。(其實Render()中實現的功能很多,主要是控制界面繪製方面的,以後還會提到它的)
Render()實現上面三個功能也大量使用了DirectX和Direct 3D方面的知識,請網上搜索學習相關知識或參看我的Direct3D學習(資料收集)。
顯示中心十字線功能
Render()中實現代碼爲
787行 if (World.Settings.ShowCrosshairs)
this.DrawCrossHairs();
實現顯示十字標代碼
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->實現顯示十字標代碼
protected void DrawCrossHairs()
{
int crossHairSize = 10;
if(this.crossHairs == null)
{
crossHairs = new Line(m_Device3d);//構造線對象
}
Vector2[] vertical = new Vector2[2];
Vector2[] horizontal = new Vector2[2];
// Vector2[] test = new Vector2[2];//這是我試驗添加的,效果請看下面的截圖
horizontal[0].X = this.Width / 2 - crossHairSize;
horizontal[0].Y = this.Height / 2;
horizontal[1].X = this.Width / 2 + crossHairSize;
horizontal[1].Y = this.Height / 2;
vertical[0].X = this.Width / 2;
vertical[0].Y = this.Height / 2 - crossHairSize;
vertical[1].X = this.Width / 2;
vertical[1].Y = this.Height / 2 + crossHairSize;
// test[0].X = this.Width / 2;
// test[0].Y = this.Height / 2 + crossHairSize;
//test[1].X = this.Width / 2 + crossHairSize;
//test[1].Y = this.Height / 2;
crossHairs.Begin();
crossHairs.Draw(horizontal, crossHairColor);
crossHairs.Draw(vertical, crossHairColor);
// crossHairs.Draw(test,Color.Red.ToArgb());
crossHairs.End();
}
上面註銷部分(我加了紅線)是我試驗添加的,效果請看上面的截圖。其實就是劃幾條兩點之間的線。
顯示位置信息
是在Render()中調用RenderPositionInfo()方法的,請看該段實現代碼。
實現顯示位置座標信息
private const int positionAlphaStep = 20;
private int positionAlpha = 255;
private int positionAlphaMin = 40;
private int positionAlphaMax = 205;
protected void RenderPositionInfo()
{
// Render some Development information to screen
string captionText = _caption;
captionText += "\n" + this.drawArgs.UpperLeftCornerText;
if(World.Settings.ShowPosition)
{
string alt = null;
double agl = this.drawArgs.WorldCamera.AltitudeAboveTerrain;
/*if(agl>100000)
alt = string.Format("{0:f2}km", agl/1000);
else
alt = string.Format("{0:f0}m", agl);*/
alt = ConvertUnits.GetDisplayString(agl);
string dist = null;
double dgl = this.drawArgs.WorldCamera.Distance;
/*if(dgl>100000)
dist = string.Format("{0:f2}km", dgl/1000);
else
dist = string.Format("{0:f0}m", dgl);*/
dist = ConvertUnits.GetDisplayString(dgl);
// Heading from 0 - 360
double heading = this.drawArgs.WorldCamera.Heading.Degrees;
if(heading<0)
heading+=360;
//構造顯示位置座標信息字符串
captionText += String.Format("Latitude: {0}\nLongitude: {1}\nHeading: {2:f2}\nTilt: {3}\nAltitude: {4}\nDistance: {5}\nFOV: {6}",
this.drawArgs.WorldCamera.Latitude,
this.drawArgs.WorldCamera.Longitude,
heading,
this.drawArgs.WorldCamera.Tilt,
alt,
dist,
this.drawArgs.WorldCamera.Fov );
if(agl < 300000)
{
captionText += String.Format("\nTerrain Elevation: {0:n} meters\n", this.drawArgs.WorldCamera.TerrainElevation);
}
}
if(this.showDiagnosticInfo)
captionText +=
"\nAvailable Texture Memory: " + (m_Device3d.AvailableTextureMemory/1024).ToString("N0") + " kB"+
"\nBoundary Points: " + this.drawArgs.numBoundaryPointsRendered.ToString() + " / " + this.drawArgs.numBoundaryPointsTotal.ToString() + " : " + this.drawArgs.numBoundariesDrawn.ToString() +
"\nTiles Drawn: " + (this.drawArgs.numberTilesDrawn * 0.25f).ToString() +
"\n" + this.drawArgs.WorldCamera +
"\nFPS: " + this.fps.ToString("f1") +
"\nRO: " + m_World.RenderableObjects.Count.ToString("f0") +
"\nmLat: " + this.cLat.Degrees.ToString() +
"\nmLon: " + this.cLon.Degrees.ToString() +
"\n" + TimeKeeper.CurrentTimeUtc.ToLocalTime().ToLongTimeString();
captionText = captionText.Trim();
//定義要畫出文本的樣式
DrawTextFormat dtf = DrawTextFormat.NoClip | DrawTextFormat.WordBreak | DrawTextFormat.Right;
int x = 7;
int y = _menuBar!=null && World.Settings.ShowToolbar ? 65 : 7;
//定義盛放位置文本的矩形框
Rectangle textRect = Rectangle.FromLTRB(x,y, this.Width-8, this.Height-8 );
//我添加的測試用代碼
Rectangle testRect = Rectangle.FromLTRB(x, y, this.Width, this.Height);
// Hide position info when toolbar is open
if (_menuBar.IsActive) //如果上面的_menuBar處於活動狀態,則更改位置文本的Alpha值
{
positionAlpha -= positionAlphaStep;
if (positionAlpha<positionAlphaMin)
{
positionAlpha=positionAlphaMin;
}
}
else
{
positionAlpha += positionAlphaStep;
if(positionAlpha>positionAlphaMax)
positionAlpha = positionAlphaMax;
}
int positionBackColor = positionAlpha << 24;
int positionForeColor = (int)((uint)(positionAlpha << 24) + 0xffffffu);
使用Font對象defaultDrawingFont的DrawText()實現繪製位置信息
this.drawArgs.defaultDrawingFont.DrawText( null, captionText, textRect, dtf, positionBackColor);
textRect.Offset(-1,-1);
this.drawArgs.defaultDrawingFont.DrawText( null, captionText, textRect, dtf, positionForeColor);
//下面這行是我添加的,測試用
this.drawArgs.defaultDrawingFont.DrawText(null, "無痕客在研究WorldWind應用!", textRect, dtf,Color.Red.ToArgb());
}
上面需要注意的知識點就是:使用Font對象(實例:defaultDrawingFont)的DrawText()實現繪製文本信息。
地球軸線繪製
WorldWindow.cs的Render()方法中調用了World.cs中Render()方法,該方法又調用了DrawAxis()
方法實現地球軸線繪製。
453行 if (Settings.showPlanetAxis)
this.DrawAxis(drawArgs);
畫地球軸線
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->
private void DrawAxis(DrawArgs drawArgs)
{
CustomVertex.PositionColored[] axis = new CustomVertex.PositionColored[2];
Vector3 topV = MathEngine.SphericalToCartesian(90, 0, this.EquatorialRadius + 0.15f *
this.EquatorialRadius);
axis[0].X = topV.X;
axis[0].Y = topV.Y;
axis[0].Z = topV.Z;
axis[0].Color = System.Drawing.Color.Pink.ToArgb();
Vector3 botV = MathEngine.SphericalToCartesian(-90, 0, this.EquatorialRadius + 0.15f *
this.EquatorialRadius);
axis[1].X = botV.X;
axis[1].Y = botV.Y;
axis[1].Z = botV.Z;
axis[1].Color = System.Drawing.Color.Pink.ToArgb();
drawArgs.device.VertexFormat = CustomVertex.PositionColored.Format;
drawArgs.device.TextureState[0].ColorOperation = TextureOperation.Disable;
drawArgs.device.Transform.World = Matrix.Translation(
(float)-drawArgs.WorldCamera.ReferenceCenter.X,
(float)-drawArgs.WorldCamera.ReferenceCenter.Y,
(float)-drawArgs.WorldCamera.ReferenceCenter.Z
);
drawArgs.device.DrawUserPrimitives(PrimitiveType.LineStrip, 1, axis);
drawArgs.device.Transform.World = drawArgs.WorldCamera.WorldMatrix;
}