📄 sceneryinfogamecomponent.cs
字号:
#region Using Statements
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using System.Collections.ObjectModel;
#endregion
namespace SceneryComponent.Components
{
/// <summary>
/// This is a game component that implements IUpdateable.
/// </summary>
public partial class SceneryInfoGameComponent : Microsoft.Xna.Framework.DrawableGameComponent
{
// N鷐ero m醲imo de v閞tices de nodo
private const int _MaxNodeVertexes = 2048;
// N鷐ero m醲imo de 韓dices de nodo
private const int _MaxNodeIndexes = _MaxNodeVertexes * 2;
// V閞tices de nodo
private VertexPositionColor[] m_NodeVertexes = new VertexPositionColor[_MaxNodeVertexes];
// 蚽dices de nodo
private short[] m_NodeIndexes = new short[_MaxNodeIndexes];
// Buffer de v閞tices
private VertexBuffer m_NodesVertexBuffer;
// Buffer de 韓dices
private IndexBuffer m_NodesIndexBuffer;
// Efecto para renderizar el componente
private BasicEffect m_Effect;
// Escenario al que pertenece el objeto de informaci髇
private SceneryGameComponent m_Scenery;
// LOD
private LOD m_Lod = LOD.None;
/// <summary>
/// Obtiene o establece el nivel de detalle a mostrar
/// </summary>
public LOD Lod
{
get
{
return m_Lod;
}
set
{
m_Lod = value;
}
}
/// <summary>
/// Constructor
/// </summary>
/// <param name="game">Juego</param>
public SceneryInfoGameComponent(Game game)
: base(game)
{
m_Scenery = (SceneryGameComponent)game.Services.GetService(typeof(SceneryGameComponent));
}
/// <summary>
/// Inicializar el componente
/// </summary>
public override void Initialize()
{
base.Initialize();
// Matriz proyecci髇
Matrix projectionMatrix = Matrix.CreateOrthographicOffCenter(
0,
this.GraphicsDevice.Viewport.Width,
this.GraphicsDevice.Viewport.Height,
0,
0.0f,
1.0f);
m_Effect = new BasicEffect(this.GraphicsDevice, null);
m_Effect.View = Matrix.Identity;
m_Effect.Projection = projectionMatrix;
m_Effect.World = Matrix.Identity;
m_Effect.VertexColorEnabled = true;
m_Effect.DiffuseColor = new Vector3(1.0f, 1.0f, 1.0f);
m_Effect.AmbientLightColor = new Vector3(1.0f, 1.0f, 1.0f);
m_Effect.LightingEnabled = true;
}
/// <summary>
/// Carga los contenidos gr醘icos del componente
/// </summary>
/// <param name="loadAllContent">Tipo de contenidos a cargar</param>
protected override void LoadGraphicsContent(bool loadAllContent)
{
base.LoadGraphicsContent(loadAllContent);
if (loadAllContent)
{
m_NodesVertexBuffer = new VertexBuffer(this.GraphicsDevice,
VertexPositionColor.SizeInBytes * _MaxNodeVertexes,
ResourceUsage.WriteOnly);
m_NodesIndexBuffer = new IndexBuffer(this.GraphicsDevice,
sizeof(short) * _MaxNodeIndexes,
ResourceUsage.WriteOnly,
ResourceManagementMode.Automatic,
IndexElementSize.SixteenBits);
}
}
/// <summary>
/// Actualiza el estado del componente
/// </summary>
/// <param name="gameTime">Tiempo de juego</param>
public override void Update(GameTime gameTime)
{
base.Update(gameTime);
}
/// <summary>
/// Dibujar el componente
/// </summary>
/// <param name="gameTime">Tiempo de juego</param>
public override void Draw(GameTime gameTime)
{
base.Draw(gameTime);
if (m_Lod == LOD.None)
{
LODDraw(gameTime, LOD.High);
LODDraw(gameTime, LOD.Medium);
LODDraw(gameTime, LOD.Low);
}
else
{
LODDraw(gameTime, m_Lod);
}
}
/// <summary>
/// Dibuja nodos por nivel de detalle
/// </summary>
/// <param name="gameTime">Tiempo de juego</param>
/// <param name="lod">Nivel de detalle</param>
private void LODDraw(GameTime gameTime, LOD lod)
{
Color nodeColor = Color.White;
if (lod == LOD.High)
{
nodeColor = Color.Red;
}
else if (lod == LOD.Medium)
{
nodeColor = Color.Orange;
}
else if (lod == LOD.Low)
{
nodeColor = Color.Green;
}
SceneryInfoNodeDrawn[] nodes = m_Scenery.GetNodesDrawn(lod);
int numVerts = ((nodes.Length * 4) > _MaxNodeVertexes) ? _MaxNodeVertexes : (nodes.Length * 4);
if ((numVerts > 0) && (m_Scenery.Width > 2))
{
// Inicializar el buffer de v閞tices
int n = 0;
Vector3 pos = new Vector3();
float scale = (1.0f / m_Scenery.Width) * 100.0f;
for (int i = 0; i <= numVerts - 4; i += 4)
{
SceneryInfoNodeDrawn node = nodes[n++];
pos.X = scale * node.UpperLeft.X + 10.0f;
pos.Y = scale * node.UpperLeft.Y + 10.0f;
pos.Z = 0.0f;
m_NodeVertexes[i] = new VertexPositionColor(pos, nodeColor);
pos.X = scale * node.LowerRight.X + 10.0f;
pos.Y = scale * node.UpperLeft.Y + 10.0f;
pos.Z = 0.0f;
m_NodeVertexes[i + 1] = new VertexPositionColor(pos, nodeColor);
pos.X = scale * node.LowerRight.X + 10.0f;
pos.Y = scale * node.LowerRight.Y + 10.0f;
pos.Z = 0.0f;
m_NodeVertexes[i + 2] = new VertexPositionColor(pos, nodeColor);
pos.X = scale * node.UpperLeft.X + 10.0f;
pos.Y = scale * node.LowerRight.Y + 10.0f;
pos.Z = 0.0f;
m_NodeVertexes[i + 3] = new VertexPositionColor(pos, nodeColor);
}
// Establecer los v閞tices en el buffer de v閞tices
m_NodesVertexBuffer.SetData<VertexPositionColor>(m_NodeVertexes, 0, numVerts, SetDataOptions.Discard);
// Crear la lista de l韓eas para los nodos
// 4 l韓eas por nodo y 4 v閞tices por nodo
int numLines = numVerts;
int numIndexes = numLines * 2;
int v = 0;
for (int i = 0; i <= numIndexes - 8; i += 8)
{
m_NodeIndexes[i + 0] = (short)(v + 0); m_NodeIndexes[i + 1] = (short)(v + 1);
m_NodeIndexes[i + 2] = (short)(v + 1); m_NodeIndexes[i + 3] = (short)(v + 2);
m_NodeIndexes[i + 4] = (short)(v + 2); m_NodeIndexes[i + 5] = (short)(v + 3);
m_NodeIndexes[i + 6] = (short)(v + 3); m_NodeIndexes[i + 7] = (short)(v + 0);
v += 4;
}
// Establecer los 韓dices en el buffer de 韓dices
m_NodesIndexBuffer.SetData<short>(m_NodeIndexes, 0, numIndexes, SetDataOptions.Discard);
// Dibujar la lista de l韓eas
m_Effect.Begin(SaveStateMode.SaveState);
foreach (EffectPass pass in m_Effect.CurrentTechnique.Passes)
{
pass.Begin();
this.GraphicsDevice.VertexDeclaration = new VertexDeclaration(this.GraphicsDevice, VertexPositionColor.VertexElements);
this.GraphicsDevice.Vertices[0].SetSource(m_NodesVertexBuffer, 0, VertexPositionColor.SizeInBytes);
this.GraphicsDevice.Indices = m_NodesIndexBuffer;
this.GraphicsDevice.DrawIndexedPrimitives(PrimitiveType.LineList, 0, 0, numVerts, 0, numLines);
pass.End();
}
m_Effect.End();
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -