Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/266.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 具有自定义顶点声明的纹理基本体_C#_Xna 4.0 - Fatal编程技术网

C# 具有自定义顶点声明的纹理基本体

C# 具有自定义顶点声明的纹理基本体,c#,xna-4.0,C#,Xna 4.0,我在XNA4.0上遇到了问题,甚至无法用谷歌搜索它。它发生在我的主项目以及我的测试项目中,这是一个非常简单的版本,可以减少不必要的代码 我需要使用自己的自定义顶点声明,并使用它来绘制纹理基本体(或者为什么不使用模型) 绘制和纹理可以很好地使用BasicFect和任何内置的顶点声明(如VertexositionColorTexture)……但是如果我在自定义顶点声明中使用BasicFect,纹理绘制不正确到底是怎么回事?我希望在一个VD中保留所有内置类型的组合。我唯一的解决办法是我应该制作一个新的

我在XNA4.0上遇到了问题,甚至无法用谷歌搜索它。它发生在我的主项目以及我的测试项目中,这是一个非常简单的版本,可以减少不必要的代码

我需要使用自己的自定义顶点声明,并使用它来绘制纹理基本体(或者为什么不使用模型)

绘制和纹理可以很好地使用BasicFect和任何内置的顶点声明(如VertexositionColorTexture)……但是如果我在自定义顶点声明中使用BasicFect,纹理绘制不正确到底是怎么回事?我希望在一个VD中保留所有内置类型的组合。我唯一的解决办法是我应该制作一个新的顶点/像素着色器,但它会有帮助吗?如果会,我该怎么做

我试图上传图片来描述,但我需要至少10个声誉,所以我会用文字解释: 使用我的自定义VD,我的正方形(和任何其他形状)对象的纹理似乎是平铺的,而不是缩放/拟合的。此外,旋转对象时纹理不会旋转

以下是我的自定义顶点声明:

namespace WindowsGame2
{
  public struct VertexPositionNormalColorTexture : IVertexType
  {
  public Vector3 Position;
  public Vector3 Normal;
  public Color Color;
  public Vector2 TextureCoordinate;

  VertexDeclaration IVertexType.VertexDeclaration
  {
     get { return VertexDeclaration; }
  }

  public readonly static VertexDeclaration VertexDeclaration = 
     new VertexDeclaration(
     new VertexElement(0, VertexElementFormat.Vector3, VertexElementUsage.Position, 0),
     new VertexElement(sizeof(float) * 3, VertexElementFormat.Vector3, VertexElementUsage.Normal, 0),
     new VertexElement((sizeof(float) * 3) * 2, VertexElementFormat.Color, VertexElementUsage.Color, 0),
     new VertexElement((sizeof(float) * 3) * 3, VertexElementFormat.Vector2, VertexElementUsage.TextureCoordinate, 0)
     );

  public VertexPositionNormalColorTexture(Vector3 p)
  {
     Position = p;
     Normal = Vector3.Zero;
     Color = Color.White;
     TextureCoordinate = Vector2.Zero;
  }

  public VertexPositionNormalColorTexture(Vector3 p, Color c)
  {
     Position = p;
     Normal = Vector3.Zero;
     Color = c;
     TextureCoordinate = Vector2.Zero;
  }

  public VertexPositionNormalColorTexture(Vector3 p, Vector2 t)
  {
     Position = p;
     Normal = Vector3.Zero;
     Color = Color.White;
     TextureCoordinate = t;
  }

  public VertexPositionNormalColorTexture(Vector3 p, Color c, Vector2 t)
  {
     Position = p;
     Normal = Vector3.Zero;
     Color = c;
     TextureCoordinate = t;
  }

  public VertexPositionNormalColorTexture(Vector3 p, Vector3 n, Color c)
  {
     Position = p;
     Normal = n;
     Color = c;
     TextureCoordinate = Vector2.Zero;
  }

  public VertexPositionNormalColorTexture(Vector3 p, Vector3 n, Vector2 t)
  {
     Position = p;
     Normal = n;
     Color = Color.White;
     TextureCoordinate = t;
  }

  public VertexPositionNormalColorTexture(Vector3 p, Vector3 n, Color c, Vector2 t)
  {
     Position = p;
     Normal = n;
     Color = c;
     TextureCoordinate = t;
  }
}
}
new VertexElement((sizeof(float) * 3) * 2, VertexElementFormat.Color, VertexElementUsage.Color, 0),
 new VertexElement((sizeof(float) * 3) * 3, VertexElementFormat.Vector2, VertexElementUsage.TextureCoordinate, 0)
游戏类:

using System;
using System.Collections.Generic;
using System.Linq;
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 WindowsGame2
{
  public class Game1 : Microsoft.Xna.Framework.Game
  {
  GraphicsDeviceManager graphics;
  SpriteBatch spriteBatch;
  ViewerManager viewer;

  List<VertexPositionNormalColorTexture> vertices;
  List<short> indices;
  Texture2D thumbnail;

  VertexBuffer vertexBuf;
  IndexBuffer indexBuf;

  RasterizerState rasterizerState;
  BasicEffect basicEffect;

  Matrix worldMatrix;
  Matrix viewMatrix;
  Matrix projectionMatrix;

  public Game1()
  {
     graphics = new GraphicsDeviceManager(this);
     Content.RootDirectory = "Content";
  }

  protected override void Initialize()
  {
     viewer = new ViewerManager(graphics, new Vector3(0.0f, 0.0f, 5.0f), new Vector3(0.0f, 0.0f, 0.0f), 500);

     vertices = new List<VertexPositionNormalColorTexture>() {

        new VertexPositionNormalColorTexture(new Vector3(-1, -1, 0), Color.Yellow, new Vector2(0, 1)),
        new VertexPositionNormalColorTexture(new Vector3(-1, 1, 0), Color.Yellow, new Vector2(0, 0)),
        new VertexPositionNormalColorTexture(new Vector3(1, 1, 0), Color.Yellow, new Vector2(1, 0)),

        new VertexPositionNormalColorTexture(new Vector3(-1, -1, 0), Color.Yellow, new Vector2(0, 1)),
        new VertexPositionNormalColorTexture(new Vector3(1, 1, 0), Color.Yellow, new Vector2(1, 0)),
        new VertexPositionNormalColorTexture(new Vector3(1, -1, 0), Color.Yellow, new Vector2(1, 1)),
     };
     indices = new List<short>() {
        0, 1, 2, 3, 4, 5
     };

     basicEffect = new BasicEffect(graphics.GraphicsDevice);

     worldMatrix = Matrix.CreateTranslation(0.0f, 0.0f, 0.0f) * Matrix.CreateScale(3);
     viewMatrix = Matrix.CreateLookAt(new Vector3(0.0f, 0.0f, 5.0f), new Vector3(0.0f, 0.0f, 0.0f), Vector3.Up);
     projectionMatrix = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(90), graphics.GraphicsDevice.Viewport.AspectRatio, 1f, 50f);

     vertexBuf = new VertexBuffer(graphics.GraphicsDevice, VertexPositionNormalColorTexture.VertexDeclaration, 500, BufferUsage.WriteOnly);
     indexBuf = new IndexBuffer(graphics.GraphicsDevice, IndexElementSize.SixteenBits, 500, BufferUsage.WriteOnly);

     rasterizerState = new RasterizerState();
     rasterizerState.CullMode = CullMode.None;

     base.Initialize();
  }

  protected override void LoadContent()
  {
     spriteBatch = new SpriteBatch(GraphicsDevice);
     thumbnail = this.Content.Load<Texture2D>("GameThumbnail");
  }

  protected override void UnloadContent()
  {
     this.Content.Unload();
  }

  protected override void Update(GameTime gameTime)
  {
     if (Keyboard.GetState().IsKeyDown(Keys.Escape))
        this.Exit();

     base.Update(gameTime);
  }

  protected override void Draw(GameTime gameTime)
  {
     GraphicsDevice.Clear(Color.CornflowerBlue);
     graphics.GraphicsDevice.RasterizerState = rasterizerState;

     basicEffect.World = worldMatrix;
     basicEffect.View = viewMatrix;
     basicEffect.Projection = projectionMatrix;

     basicEffect.VertexColorEnabled = true;
     basicEffect.TextureEnabled = true;
     basicEffect.Texture = thumbnail;

     vertexBuf.SetData<VertexPositionNormalColorTexture>(vertices.ToArray());
     indexBuf.SetData<short>(indices.ToArray());

     graphics.GraphicsDevice.SetVertexBuffer(vertexBuf);
     graphics.GraphicsDevice.Indices = indexBuf;

     foreach (EffectPass pass in basicEffect.CurrentTechnique.Passes)
     {
        pass.Apply();

        graphics.GraphicsDevice.DrawUserIndexedPrimitives(
           PrimitiveType.TriangleList,
           vertices.ToArray(),
           0,
           vertices.Count,
           indices.ToArray(),
           0,
           2,
           VertexPositionNormalColorTexture.VertexDeclaration);
     }
     graphics.GraphicsDevice.Indices = null;
     graphics.GraphicsDevice.SetVertexBuffer(null);

     base.Draw(gameTime);
  }
 }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用Microsoft.Xna.Framework;
使用Microsoft.Xna.Framework.Audio;
使用Microsoft.Xna.Framework.Content;
使用Microsoft.Xna.Framework.GamerServices;
使用Microsoft.Xna.Framework.Graphics;
使用Microsoft.Xna.Framework.Input;
使用Microsoft.Xna.Framework.Media;
命名空间WindowsGame2
{
公共类游戏1:Microsoft.Xna.Framework.Game
{
图形管理器图形;
SpriteBatch SpriteBatch;
ViewerManager查看器;
列出顶点;
列出索引;
纹理2D缩略图;
VertexBuffer vertexBuf;
IndexBuffer indexBuf;
光栅化状态光栅化状态;
基本效果基本效果;
矩阵世界矩阵;
矩阵视图矩阵;
矩阵投影矩阵;
公共游戏1()
{
graphics=新的GraphicsDeviceManager(此);
Content.RootDirectory=“Content”;
}
受保护的覆盖无效初始化()
{
查看器=新的ViewerManager(图形、新矢量3(0.0f、0.0f、5.0f)、新矢量3(0.0f、0.0f、0.0f)、500);
顶点=新列表(){
新VertexPositionNormalColorTexture(新矢量3(-1,-1,0),Color.Yellow,新矢量2(0,1)),
新VertexPositionNormalColorTexture(新矢量3(-1,1,0),Color.Yellow,新矢量2(0,0)),
新VertexPositionNormalColorTexture(新矢量3(1,1,0),Color.Yellow,新矢量2(1,0)),
新VertexPositionNormalColorTexture(新矢量3(-1,-1,0),Color.Yellow,新矢量2(0,1)),
新VertexPositionNormalColorTexture(新矢量3(1,1,0),Color.Yellow,新矢量2(1,0)),
新VertexPositionNormalColorTexture(新矢量3(1,-1,0),Color.Yellow,新矢量2(1,1)),
};
索引=新列表(){
0, 1, 2, 3, 4, 5
};
BasicFect=新的BasicFect(graphics.GraphicsDevice);
worldMatrix=矩阵.CreateTranslation(0.0f,0.0f,0.0f)*矩阵.CreateScale(3);
viewMatrix=Matrix.CreateLookAt(新矢量3(0.0f,0.0f,5.0f),新矢量3(0.0f,0.0f,0.0f),矢量3.Up);
projectionMatrix=Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(90),graphics.GraphicsDevice.Viewport.AspectRatio,1f,50f);
vertexBuf=新的VertexBuffer(graphics.GraphicsDevice,VertexExpositionNormalColorTexture.VertexDeclaration,500,BufferUsage.WriteOnly);
indexBuf=新的IndexBuffer(graphics.GraphicsDevice,IndexElementSize.SixteenBits,500,BufferUsage.WriteOnly);
光栅化状态=新光栅化状态();
光栅化状态.CullMode=CullMode.None;
base.Initialize();
}
受保护的覆盖void LoadContent()
{
spriteBatch=新spriteBatch(图形设备);
缩略图=this.Content.Load(“游戏缩略图”);
}
受保护的覆盖无效UnloadContent()
{
this.Content.Unload();
}
受保护覆盖无效更新(游戏时间游戏时间)
{
if(Keyboard.GetState().IsKeyDown(Keys.Escape))
这是Exit();
更新(游戏时间);
}
受保护覆盖无效绘制(游戏时间游戏时间)
{
图形设备。清晰(颜色:矢车菊蓝);
graphics.GraphicsDevice.RasterizerState=光栅化状态;
基本效果世界=世界矩阵;
基本效果视图=视图矩阵;
基本效果投影=投影矩阵;
basicEffect.VertexColorEnabled=真;
basicEffect.TextureEnabled=true;
基本效果纹理=缩略图;
SetData(顶点.ToArray());
SetData(index.ToArray());
graphics.GraphicsDevice.SetVertexBuffer(vertexBuf);
graphics.GraphicsDevice.Indexs=indexBuf;
foreach(在basicEffect.currentTechnical.passs中传递EffectPass)
{
pass.Apply();
graphics.GraphicsDevice.DrawUserIndexedPrimitives(
PrimitiveType.TriangleList,
顶点。ToArray(),
0,
数一数,
index.ToArray(),
0,
2.
VertexExpositionNormalColorTexture.VertexDeclaration);
}
graphics.GraphicsDevice.Indexes=null;
graphics.GraphicsDevice.SetVertexBuffer(空);
基础。抽签(游戏时间);
}
}
}

我发现了这个问题。我为一个顶点分配了太多内存,这导致了奇怪的纹理。在顶点声明的这一部分:

namespace WindowsGame2
{
  public struct VertexPositionNormalColorTexture : IVertexType
  {
  public Vector3 Position;
  public Vector3 Normal;
  public Color Color;
  public Vector2 TextureCoordinate;

  VertexDeclaration IVertexType.VertexDeclaration
  {
     get { return VertexDeclaration; }
  }

  public readonly static VertexDeclaration VertexDeclaration = 
     new VertexDeclaration(
     new VertexElement(0, VertexElementFormat.Vector3, VertexElementUsage.Position, 0),
     new VertexElement(sizeof(float) * 3, VertexElementFormat.Vector3, VertexElementUsage.Normal, 0),
     new VertexElement((sizeof(float) * 3) * 2, VertexElementFormat.Color, VertexElementUsage.Color, 0),
     new VertexElement((sizeof(float) * 3) * 3, VertexElementFormat.Vector2, VertexElementUsage.TextureCoordinate, 0)
     );

  public VertexPositionNormalColorTexture(Vector3 p)
  {
     Position = p;
     Normal = Vector3.Zero;
     Color = Color.White;
     TextureCoordinate = Vector2.Zero;
  }

  public VertexPositionNormalColorTexture(Vector3 p, Color c)
  {
     Position = p;
     Normal = Vector3.Zero;
     Color = c;
     TextureCoordinate = Vector2.Zero;
  }

  public VertexPositionNormalColorTexture(Vector3 p, Vector2 t)
  {
     Position = p;
     Normal = Vector3.Zero;
     Color = Color.White;
     TextureCoordinate = t;
  }

  public VertexPositionNormalColorTexture(Vector3 p, Color c, Vector2 t)
  {
     Position = p;
     Normal = Vector3.Zero;
     Color = c;
     TextureCoordinate = t;
  }

  public VertexPositionNormalColorTexture(Vector3 p, Vector3 n, Color c)
  {
     Position = p;
     Normal = n;
     Color = c;
     TextureCoordinate = Vector2.Zero;
  }

  public VertexPositionNormalColorTexture(Vector3 p, Vector3 n, Vector2 t)
  {
     Position = p;
     Normal = n;
     Color = Color.White;
     TextureCoordinate = t;
  }

  public VertexPositionNormalColorTexture(Vector3 p, Vector3 n, Color c, Vector2 t)
  {
     Position = p;
     Normal = n;
     Color = c;
     TextureCoordinate = t;
  }
}
}
new VertexElement((sizeof(float) * 3) * 2, VertexElementFormat.Color, VertexElementUsage.Color, 0),
 new VertexElement((sizeof(float) * 3) * 3, VertexElementFormat.Vector2, VertexElementUsage.TextureCoordinate, 0)
类型Color的大小实际上不是float/int,而是byte。因此我不得不这样说:

new VertexElement((sizeof(float) * 3) * 2 + 4, VertexElementFormat.Vector2, VertexElementUsage.TextureCoordinate, 0)