我不知道发生了什么,所以这是我的代码。 我认为这是一个加载模型的问题,因为当我评论它的工作原理时。在网格上只是一个非常简单的第一人称相机,我正在尝试放置一棵树。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
namespace Series3D1
{
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
GraphicsDevice device;
Effect effect;
VertexPositionColor[] vertices;
Matrix viewMatrix;
Matrix projectionMatrix;
int[] indices;
float leftrightRot = MathHelper.PiOver2;
float updownRot = -MathHelper.Pi / 10.0f;
const float rotationSpeed = 0.3f;
const float moveSpeed = 10.0f;
// Set the position of the camera in world space, for our view matrix.
Vector3 cameraPosition = new Vector3(50f,1.5f,-50f);
Vector3 cameraLook;
MouseState originalMouseState;
SpriteFont font;
Model tree1;
private int terrainWidth = 100;
private int terrainHeight = 100;
private float[,] heightData;
private bool flyMode = false;
private bool JumpFlag = true;
private int JumpCounter = 1;
private float fallSpeed = -0.0f;
private float jumpSpeed = 1.0f;
private float movementSpeed = 1;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
protected override void Initialize()
{
graphics.PreferredBackBufferWidth = GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Width;
graphics.PreferredBackBufferHeight = GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Height;
graphics.IsFullScreen = true;
graphics.ApplyChanges();
Window.Title = "XNA";
base.Initialize();
}
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
device = graphics.GraphicsDevice;
effect = Content.Load<Effect>("effects"); SetUpCamera();
Mouse.SetPosition(graphics.PreferredBackBufferWidth / 2, graphics.PreferredBackBufferHeight / 2);
originalMouseState = Mouse.GetState();
font = Content.Load<SpriteFont>("font");
tree1 = LoadModel("AlanTree");
PlayMusic();
UpdateViewMatrix();
LoadHeightData();
SetUpVertices();
SetUpIndices();
}
private void UpdateViewMatrix()
{
Matrix cameraRotation = Matrix.CreateRotationX(updownRot) * Matrix.CreateRotationY(leftrightRot);
Vector3 cameraOriginalTarget = new Vector3(0, 0, -1);
Vector3 cameraRotatedTarget = Vector3.Transform(cameraOriginalTarget, cameraRotation);
Vector3 cameraFinalTarget = cameraPosition + cameraRotatedTarget;
Vector3 cameraOriginalUpVector = new Vector3(0, 1, 0);
Vector3 cameraRotatedUpVector = Vector3.Transform(cameraOriginalUpVector, cameraRotation);
viewMatrix = Matrix.CreateLookAt(cameraPosition, cameraFinalTarget, cameraRotatedUpVector);
cameraLook = cameraFinalTarget;
}
private void DrawText()
{
spriteBatch.Begin();
spriteBatch.DrawString(font, "Game V1.0", new Vector2(20, 20), Color.White);
spriteBatch.DrawString(font, "cameraLook: " + cameraLook, new Vector2(20, 40), Color.White);
spriteBatch.DrawString(font, "cameraPostion: " + cameraPosition, new Vector2(20, 60), Color.White);
spriteBatch.DrawString(font, "flyMode?: " + flyMode, new Vector2(20, 80), Color.White);
spriteBatch.End();
}
private Model LoadModel(string assetName)
{
Model newModel = Content.Load<Model>(assetName); foreach (ModelMesh mesh in newModel.Meshes)
foreach (ModelMeshPart meshPart in mesh.MeshParts)
meshPart.Effect = effect.Clone();
return newModel;
}
protected override void UnloadContent()
{
}
private void SetUpVertices()
{
vertices = new VertexPositionColor[terrainWidth * terrainHeight];
for (int x = 0; x < terrainWidth; x++)
{
for (int y = 0; y < terrainHeight; y++)
{
vertices[x + y * terrainWidth].Position = new Vector3(x, heightData[x, y], -y);
vertices[x + y * terrainWidth].Color = Color.White;
}
}
}
private void SetUpIndices()
{
indices = new int[(terrainWidth - 1) * (terrainHeight - 1) * 6];
int counter = 0;
for (int y = 0; y < terrainHeight - 1; y++)
{
for (int x = 0; x < terrainWidth - 1; x++)
{
int lowerLeft = x + y * terrainWidth;
int lowerRight = (x + 1) + y * terrainWidth;
int topLeft = x + (y + 1) * terrainWidth;
int topRight = (x + 1) + (y + 1) * terrainWidth;
indices[counter++] = topLeft;
indices[counter++] = lowerRight;
indices[counter++] = lowerLeft;
indices[counter++] = topLeft;
indices[counter++] = topRight;
indices[counter++] = lowerRight;
}
}
}
private void LoadHeightData()
{
heightData = new float[1000, 1000];
float tempHolder = 0;
for (int i = 0; i < 100; i++) {
for (int j = 0; j < 100; j++) {
heightData[j, i] = tempHolder;
}
}
}
private void SetUpCamera()
{
viewMatrix = Matrix.CreateLookAt(new Vector3(0, 10, 0), new Vector3(0, 0, 0), new Vector3(0, 0, -1));
projectionMatrix = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4, device.Viewport.AspectRatio, 1.0f, 1000.0f);
}
protected override void Update(GameTime gameTime)
{
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
float timeDifference = (float)gameTime.ElapsedGameTime.TotalMilliseconds / 1000.0f;
MouseState currentMouseState = Mouse.GetState();
if (currentMouseState != originalMouseState)
{
float xDifference = currentMouseState.X - originalMouseState.X;
float yDifference = currentMouseState.Y - originalMouseState.Y;
leftrightRot -= rotationSpeed * xDifference * timeDifference;
updownRot -= rotationSpeed * yDifference * timeDifference;
Mouse.SetPosition(graphics.PreferredBackBufferWidth / 2, graphics.PreferredBackBufferHeight / 2);
UpdateViewMatrix();
}
Vector3 moveVector = new Vector3(0, 0, 0);
KeyboardState keyState = Keyboard.GetState();
if (keyState.IsKeyDown(Keys.W))
moveVector += new Vector3(0, 0, -movementSpeed);
if (keyState.IsKeyDown(Keys.S))
moveVector += new Vector3(0, 0, movementSpeed);
if (keyState.IsKeyDown(Keys.D))
moveVector += new Vector3(movementSpeed, 0, 0);
if (keyState.IsKeyDown(Keys.A))
moveVector += new Vector3(-movementSpeed, 0, 0);
if (keyState.IsKeyDown(Keys.Enter))
{
if (flyMode == true)
{
flyMode = false;
}
else if (flyMode == false)
{
flyMode = true;
}
Thread.Sleep(100);
}
if (keyState.IsKeyDown(Keys.LeftControl))
movementSpeed = 1.5f;
else
movementSpeed = 1.0f;
if (flyMode)
{
if (keyState.IsKeyDown(Keys.Space))
moveVector += new Vector3(0, 1, 0);
if (keyState.IsKeyDown(Keys.LeftShift))
moveVector += new Vector3(0, -1, 0);
}
else if (!flyMode) {
if (cameraPosition.Y > 1.5f) {
moveVector += new Vector3(0, fallSpeed, 0);
fallSpeed -= 0.1f;
}
else if (cameraPosition.Y < 1.5f)
{
JumpCounter = 1;
fallSpeed = -0.0f;
}
if (JumpCounter == 10){
}
else if (keyState.IsKeyDown(Keys.Space) & JumpFlag == true){
moveVector += new Vector3(0, jumpSpeed, 0);
fallSpeed = -0.0f;
jumpSpeed += 0.001f;
JumpCounter += 1;
}
}
AddToCameraPosition(moveVector * timeDifference);
base.Update(gameTime);
}
private void PlayMusic()
{
Song song = Content.Load<Song>("Peaceful");
MediaPlayer.Play(song);
}
private void AddToCameraPosition(Vector3 vectorToAdd)
{
Matrix cameraRotation = Matrix.CreateRotationY(leftrightRot);
Vector3 rotatedVector = Vector3.Transform(vectorToAdd, cameraRotation);
cameraPosition += moveSpeed * rotatedVector;
UpdateViewMatrix();
}
protected override void Draw(GameTime gameTime)
{
device.Clear(Color.Black);
RasterizerState rs = new RasterizerState();
rs.CullMode = CullMode.None;
rs.FillMode = FillMode.WireFrame;
device.RasterizerState = rs;
foreach (EffectPass pass in effect.CurrentTechnique.Passes)
{
pass.Apply();
device.DrawUserIndexedPrimitives(PrimitiveType.TriangleList, vertices, 0, vertices.Length, indices, 0, indices.Length / 3, VertexPositionColor.VertexDeclaration);
}
DrawText(); //puts the debug text to the screen
DrawModel();
base.Draw(gameTime);
}
private void DrawModel()
{
Matrix worldMatrix = Matrix.CreateScale(0.0005f, 0.0005f, 0.0005f) * Matrix.CreateRotationY(MathHelper.Pi) * Matrix.CreateTranslation(new Vector3(19, 12, -5));
Matrix[] treeTransforms = new Matrix[tree1.Bones.Count];
tree1.CopyAbsoluteBoneTransformsTo(treeTransforms);
foreach (ModelMesh mesh in tree1.Meshes)
{
foreach (Effect currentEffect in mesh.Effects)
{
currentEffect.CurrentTechnique = currentEffect.Techniques["Colored"];
currentEffect.Parameters["xWorld"].SetValue(treeTransforms[mesh.ParentBone.Index] * worldMatrix);
currentEffect.Parameters["xView"].SetValue(viewMatrix);
currentEffect.Parameters["xProjection"].SetValue(projectionMatrix);
}
mesh.Draw();
}
}
}
}