我正在关注Chad Carter的XNA 3.0 Game Studio Unleashed Book。
我在第4章,以下列表应该在游戏窗口上呈现纹理三角形,但对于我的生活,我无法弄清楚它为什么不是。我只是得到了普通的矢车菊蓝屏。
我正在使用带有XNA 3.1的Visual Studio 2008
有书的人,我在第66页的顶部,我将纹理应用于三角形。
任何帮助都非常感激。
namespace XNADemo
{
/// <summary>
/// This is the main type for your game
/// </summary>
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
private Matrix projection;
private Matrix view;
private Matrix world;
private Vector3 cameraPosition = new Vector3(0.0f, 0.0f, 3.0f);
private Vector3 cameraTarget = Vector3.Zero;
private Vector3 cameraUpVector = Vector3.Up;
private VertexPositionNormalTexture[] vertices;
private Texture2D texture;
private BasicEffect effect;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
/// <summary>
/// Allows the game to perform any initialization it needs to before starting to run.
/// This is where it can query for any required services and load any non-graphic
/// related content. Calling base.Initialize will enumerate through any components
/// and initialize them as well.
/// </summary>
protected override void Initialize()
{
// TODO: Add your initialization logic here
InitializeCamera();
InitializeVertices();
base.Initialize();
}
/// <summary>
/// LoadContent will be called once per game and is the place to load
/// all of your content.
/// </summary>
protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);
texture = Content.Load<Texture2D>("texture");
effect = new BasicEffect(graphics.GraphicsDevice, null);
}
/// <summary>
/// UnloadContent will be called once per game and is the place to unload
/// all content.
/// </summary>
protected override void UnloadContent()
{
// TODO: Unload any non ContentManager content here
}
/// <summary>
/// Allows the game to run logic such as updating the world,
/// checking for collisions, gathering input, and playing audio.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Update(GameTime gameTime)
{
// Allows the game to exit
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
// TODO: Add your update logic here
base.Update(gameTime);
}
/// <summary>
/// This is called when the game should draw itself.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
world = Matrix.Identity;
graphics.GraphicsDevice.VertexDeclaration = new VertexDeclaration
(graphics.GraphicsDevice, VertexPositionNormalTexture.VertexElements);
effect.Projection = projection;
effect.View = view;
effect.EnableDefaultLighting();
effect.World = world;
effect.TextureEnabled = true;
effect.Texture = texture;
effect.Begin();
foreach (EffectPass pass in effect.CurrentTechnique.Passes)
{
pass.Begin();
graphics.GraphicsDevice.DrawUserPrimitives
(PrimitiveType.TriangleList, vertices, 0, vertices.Length / 3);
pass.End();
}
effect.End();
base.Draw(gameTime);
}
private void InitializeCamera()
{
float aspectRatio = (float)graphics.GraphicsDevice.Viewport.Width /
(float)graphics.GraphicsDevice.Viewport.Height;
Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4, aspectRatio, 0.0001f, 1000.0f, out projection);
Matrix.CreateLookAt(ref cameraPosition, ref cameraTarget, ref cameraUpVector, out view);
}
private void InitializeVertices()
{
Vector3 position;
Vector2 textureCoordinates;
vertices = new VertexPositionNormalTexture[3];
//top left
position = new Vector3(-1, 1, 0);
textureCoordinates = new Vector2(0, 0);
vertices[0] = new VertexPositionNormalTexture(position, Vector3.Forward, textureCoordinates);
//bottom right
position = new Vector3(1, -1, 0);
textureCoordinates = new Vector2(1, 1);
vertices[1] = new VertexPositionNormalTexture(position, Vector3.Forward, textureCoordinates);
//bottom left
position = new Vector3(-1, -1, 0);
textureCoordinates = new Vector2(0, 1);
vertices[1] = new VertexPositionNormalTexture(position, Vector3.Forward, textureCoordinates);
}
}
}
答案 0 :(得分:3)
我相信左下顶点的顶点索引应该是2
而不是1
//bottom right position = new Vector3(1, -1, 0); textureCoordinates = new Vector2(1, 1); vertices[1] = new VertexPositionNormalTexture(position, Vector3.Forward, textureCoordinates); //bottom left position = new Vector3(-1, -1, 0); textureCoordinates = new Vector2(0, 1); //vertices[1] = new VertexPositionNormalTexture(position, vertices[2] = new VertexPositionNormalTexture(position, Vector3.Forward, textureCoordinates);
另一种可能性是设置的cullmode和定义顶点的顺序。您可能正在查看三角形背面的内容,因此未呈现。
答案 1 :(得分:1)
我正在使用相同的软件并且目前正在使用同一本书。教我自己最终如何制作2D游戏。
关于你的问题,我不喜欢编程我会承认,但扫描你的代码(这是在寻找一个关于同一本书的问题的答案我自己)我立即注意到一些差异,并想到也许你'我试过个性化代码?首先你在 main void initialize()中运行你的inizialize方法,除非我想念这本书,并且因为我的版本正在工作,所以它渲染三角形我只是遇到了创建一个问题的问题。 square,特别是以下代码;
graphics.GraphicsDevice.DrawUserPrimitives(
PrimitiveType.TriangleList, vertices, 0,
vertices.Length, indices, 0 , indices.Length / 3);
那是本书内容的C / P,我的问题是没有重载方法接受7个参数所以即时寻求绕过它并更好地理解DrawUserPrimitives方法,任何帮助都将不胜感激。
回到你的问题,我在LoadContent()下运行这些函数,这可能会导致你出现问题,我不确定只是因为我还在学习自己,以下是我可以用来比较的代码如果你愿意,我建议你开始一个新的项目和C / P代码,这样你就有了一个工作版本并且知道它正在工作,从而完成你想要的所有个性化/定制。这就是我的朋友总是对我说的,“让它工作,然后改进它。”
namespace XNA_Demo
{
/// <summary>
/// This is the main type for your game
/// </summary>
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
private Matrix projection;
private Matrix view;
private Matrix world;
private Vector3 cameraPosition = new Vector3(0.0f, 0.0f, 3.0f);
private Vector3 cameraTarget = Vector3.Zero;
private Vector3 cameraUpVector = Vector3.Up;
private VertexPositionNormalTexture[] vertices;
private Texture2D texture;
private short[] indices;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
/// <summary>
/// Allows the game to perform any initialization it needs to before starting to run.
/// This is where it can query for any required services and load any non-graphic
/// related content. Calling base.Initialize will enumerate through any components
/// and initialize them as well.
/// </summary>
protected override void Initialize()
{
// TODO: Add your initialization logic here
float aspectRatio = (float)graphics.GraphicsDevice.Viewport.Width /
(float)graphics.GraphicsDevice.Viewport.Height;
Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4, aspectRatio,
0.0001f, 1000.0f, out projection);
Matrix.CreateLookAt(ref cameraPosition, ref cameraTarget,
ref cameraUpVector, out view);
base.Initialize();
}
/// <summary>
/// LoadContent will be called once per game and is the place to load
/// all of your content.
/// </summary>
protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);
// TODO: use this.Content to load your game content here
InitializeVertices();
InitializeIndices();
texture = Content.Load<Texture2D>("MGS4vet");
}
/// <summary>
/// UnloadContent will be called once per game and is the place to unload
/// all content.
/// </summary>
protected override void UnloadContent()
{
// TODO: Unload any non ContentManager content here
}
/// <summary>
/// Allows the game to run logic such as updating the world,
/// checking for collisions, gathering input, and playing audio.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Update(GameTime gameTime)
{
// Allows the game to exit
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
// TODO: Add your update logic here
base.Update(gameTime);
}
/// <summary>
/// This is called when the game should draw itself.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
// TODO: Add your drawing code here
graphics.GraphicsDevice.VertexDeclaration = new
VertexDeclaration(graphics.GraphicsDevice,
VertexPositionNormalTexture.VertexElements);
BasicEffect effect = new BasicEffect(graphics.GraphicsDevice, null);
world = Matrix.Identity;
effect.World = world;
effect.Projection = projection;
effect.View = view;
effect.EnableDefaultLighting();
effect.TextureEnabled = true;
effect.Texture = texture;
effect.Begin();
foreach (EffectPass pass in effect.CurrentTechnique.Passes)
{
pass.Begin();
graphics.GraphicsDevice.DrawUserPrimitives(
PrimitiveType.TriangleList, vertices, 0,
vertices.Length / 3);
pass.End();
}
effect.End();
base.Draw(gameTime);
}
private void InitializeVertices()
{
Vector3 position;
Vector2 textureCoordinates;
vertices = new VertexPositionNormalTexture[4];
//top left
position = new Vector3(-1, 1, 0);
textureCoordinates = new Vector2(0, 0);
vertices[0] = new VertexPositionNormalTexture(position, Vector3.Forward,
textureCoordinates);
//bottom right
position = new Vector3(1, -1, 0);
textureCoordinates = new Vector2(1, 1);
vertices[1] = new VertexPositionNormalTexture(position, Vector3.Forward,
textureCoordinates);
//bottom left
position = new Vector3(-1, -1, 0);
textureCoordinates = new Vector2(0, 1);
vertices[2] = new VertexPositionNormalTexture(position, Vector3.Forward,
textureCoordinates);
//top right
position = new Vector3(1, 1, 0);
textureCoordinates = new Vector2(1, 0);
vertices[3] = new VertexPositionNormalTexture(position, Vector3.Forward,
textureCoordinates);
}
private void InitializeIndices()
{
//6 vertices make up 2 triangles which make up our rectangle
indices = new short[6];
//triangle 1 (bottom portion)
indices[0] = 0; // top left
indices[1] = 1; // bottom right
indices[2] = 2; // bottom left
//triangle 2 (top portion)
indices[3] = 0; // top left
indices[4] = 3; // top right
indices[5] = 1; // bottom right
}
}
}
希望我至少得到一些帮助,
问候:)