Xna 三维世界中的动态二维渲染
好的,我正在学习我在XNA的第一次严肃体验。这是我当前的任务:下载一个文件。从该文件解析一个“形状”列表。每个“形状”包含一个点列表。这些点是要在地图上绘制的直线的顶点。事实上,它是整个美国的一张县地图,因此“形状”的数量并非微不足道。我们需要能够放大和缩小这张地图,所以它需要在3d空间中进行2d渲染。我想知道这方面最好的策略是什么 我已经尝试过简单地使用DrawUserIndexedPrimitives,但是draw函数需要花费很长的时间来处理这个问题 因此,我想我应该尝试在LoadContent中绘制一系列RenderTarget2D,然后将这些纹理保存到Draw函数中进行绘制。但到目前为止,我能得到的似乎只是一系列紫色的盒子 编辑: 以下是我当前的代码:Xna 三维世界中的动态二维渲染,xna,drawing,Xna,Drawing,好的,我正在学习我在XNA的第一次严肃体验。这是我当前的任务:下载一个文件。从该文件解析一个“形状”列表。每个“形状”包含一个点列表。这些点是要在地图上绘制的直线的顶点。事实上,它是整个美国的一张县地图,因此“形状”的数量并非微不足道。我们需要能够放大和缩小这张地图,所以它需要在3d空间中进行2d渲染。我想知道这方面最好的策略是什么 我已经尝试过简单地使用DrawUserIndexedPrimitives,但是draw函数需要花费很长的时间来处理这个问题 因此,我想我应该尝试在LoadConte
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System.Runtime.InteropServices;
namespace First
{
class FirstShape : DrawableGameComponent
{
const int kSizeMultiplier = 10;
public FirstShape(string inFilePath, Game inGame) : base(inGame)
{
DrawOrder = 1000;
mFile = inFilePath;
}
protected override void LoadContent()
{
mSpriteBatch = new SpriteBatch(GraphicsDevice);
FirstMemoryStream stream = FirstMemoryStream.MakeSeekableStream(File.OpenRead(mFile));
// skip headers
stream.Seek(100, 0);
for (int i = 0; stream.Position != stream.Length; i++)
{
stream.Seek(stream.Position + 12, 0);
double minX = stream.ReadDouble();
double minY = stream.ReadDouble();
double maxX = stream.ReadDouble();
double maxY = stream.ReadDouble();
int numParts = stream.ReadInt();
int numPoints = stream.ReadInt();
VertexPositionColor[] points = new VertexPositionColor[numPoints];
stream.Seek(stream.Position + (4 * numParts), 0);
int top, bottom, left, right;
float x2, y2;
x2 = (float)stream.ReadDouble();
y2 = (float)stream.ReadDouble();
Vector2 projectedPoint = Vertex(x2, y2);
left = right = (int)Math.Round(projectedPoint.X * kSizeMultiplier);
top = bottom = (int)Math.Round(maxY - (projectedPoint.Y * kSizeMultiplier));
points[0].Position.X = left;
points[0].Position.Y = top;
for (int j = 1; j < points.Length; j++)
{
float x1 = x2;
float y1 = y2;
x2 = (float)stream.ReadDouble();
y2 = (float)stream.ReadDouble();
Vector2 p1 = Vertex(x1, y1);
Vector2 p2 = Vertex(x2, y2);
p1.X *= kSizeMultiplier;
p1.Y *= kSizeMultiplier;
p2.X *= kSizeMultiplier;
p2.Y *= kSizeMultiplier;
points[j].Position.X = (int)Math.Round(p2.X);
points[j].Position.Y = (int)Math.Round(maxY - p2.Y);
if (points[j].Position.X < left)
left = (int)points[j].Position.X;
if (points[j].Position.X > right)
right = (int)points[j].Position.X;
if (points[j].Position.Y < top)
top = (int)points[j].Position.Y;
if (points[j].Position.Y > bottom)
bottom = (int)points[j].Position.Y;
}
if (mTopLeft.X == 0 || mTopLeft.X > left)
mTopLeft.X = left;
if (mTopLeft.Y == 0 || mTopLeft.Y > top)
mTopLeft.Y = top;
for (int j = 0; j < points.Length; j++)
{
points[j].Color = Color;
}
int width = (right - left) + 1;
int height = (bottom - top) + 1;
mTextures.Add(new FirstImage(GraphicsDevice, width, height, new Vector2(left, top)));
GraphicsDevice.SetRenderTarget(mTextures.Last());
GraphicsDevice.Indices = new IndexBuffer(GraphicsDevice, IndexElementSize.SixteenBits, points.Length, BufferUsage.None);
GraphicsDevice.SetVertexBuffer(new VertexBuffer(GraphicsDevice, VertexPositionColor.VertexDeclaration, points.Length, BufferUsage.None));
BasicEffect basicEffect = new BasicEffect(GraphicsDevice);
basicEffect.LightingEnabled = false;
basicEffect.VertexColorEnabled = true;
foreach (EffectPass pass in basicEffect.CurrentTechnique.Passes)
{
pass.Apply();
GraphicsDevice.DrawIndexedPrimitives(PrimitiveType.LineStrip, 0, 0, points.Length, 0, points.Length - 1);
}
}
GraphicsDevice.SetRenderTarget(null);
stream.Close();
for (int i = 0; i < mTextures.Count; i++)
{
mTextures[i].Position -= mTopLeft;
}
}
public override void Draw(GameTime inTime)
{
mSpriteBatch.Begin();
for(int i = 0; i < mTextures.Count; i++)
{
mSpriteBatch.Draw(mTextures[i], mTextures[i].Position, Color.White);
}
mSpriteBatch.End();
}
private Vector2 Vertex(float inX, float inY)
{
return FirstProjector.Project(new Vector2(inX, inY));
}
public Color Color { get; set; }
private string mFile;
private List<FirstImage> mTextures = new List<FirstImage>();
private SpriteBatch mSpriteBatch;
private Vector2 mTopLeft = new Vector2(0.0f, 0.0f);
private Vector2 mBottomRight = new Vector2(0.0f, 0.0f);
}
class FirstImage : RenderTarget2D
{
public FirstImage(GraphicsDevice inDevice, int inWidth, int inHeight, Vector2 inPosition) : base(inDevice, inWidth, inHeight, false, SurfaceFormat.Color, DepthFormat.None)
{
Position = inPosition;
}
public Vector2 Position {get; set;}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.IO;
使用Microsoft.Xna.Framework;
使用Microsoft.Xna.Framework.Graphics;
使用System.Runtime.InteropServices;
名称空间优先
{
类FirstShape:DrawableGameComponent
{
常数int kSizeMultiplier=10;
公共第一形状(字符串填充路径,游戏inGame):基础(inGame)
{
提取顺序=1000;
mFile=填充路径;
}
受保护的覆盖void LoadContent()
{
mSpriteBatch=新的SpriteBatch(图形设备);
FirstMemoryStream stream=FirstMemoryStream.MakeSeekableStream(File.OpenRead(mFile));
//跳过标题
stream.Seek(100,0);
for(int i=0;stream.Position!=stream.Length;i++)
{
stream.Seek(stream.Position+12,0);
double minX=stream.ReadDouble();
double minY=stream.ReadDouble();
double maxX=stream.ReadDouble();
double maxY=stream.ReadDouble();
int numParts=stream.ReadInt();
int numPoints=stream.ReadInt();
VertexPositionColor[]点=新的VertexPositionColor[numPoints];
stream.Seek(stream.Position+(4*numParts),0);
int顶部、底部、左侧、右侧;
浮动x2,y2;
x2=(float)stream.ReadDouble();
y2=(float)stream.ReadDouble();
向量2投影点=顶点(x2,y2);
左=右=(int)数学圆(projectedPoint.X*kSizeMultiplier);
top=bottom=(int)Math.Round(maxY-(projectedPoint.Y*kSizeMultiplier));
点[0]。位置.X=左侧;
点[0]。位置。Y=顶部;
对于(int j=1;j右侧)
右=(int)点[j]。位置.X;
if(点[j].位置.Y<顶部)
top=(int)点[j]。位置.Y;
if(点[j].位置.Y>底部)
底部=(int)点[j]。位置.Y;
}
if(mTopLeft.X==0 | | mTopLeft.X>左)
mTopLeft.X=左;
if(mTopLeft.Y==0 | | mTopLeft.Y>top)
mTopLeft.Y=顶部;
对于(int j=0;j