C# 将VertexositionColorTexture与BasicFect一起使用时会弄乱三角形
我使用了Microsoft的BasicFect教程和此处的代码示例:go.Microsoft.com/fwlink/?LinkId=198921,一切正常。接下来,我改变了一切,使用vertexPositionNormalTexture,添加了一些小方法来帮助处理纹理,并且能够很好地渲染纹理立方体。我还让立方体旋转了一点。接下来,我想尝试使用vertexPositionNormalTexture。不幸的是,我得到的是这个图像而不是一个立方体。下面是我的一些代码片段,其中包含了主要的修改 画法C# 将VertexositionColorTexture与BasicFect一起使用时会弄乱三角形,c#,visual-studio-2010,xna,C#,Visual Studio 2010,Xna,我使用了Microsoft的BasicFect教程和此处的代码示例:go.Microsoft.com/fwlink/?LinkId=198921,一切正常。接下来,我改变了一切,使用vertexPositionNormalTexture,添加了一些小方法来帮助处理纹理,并且能够很好地渲染纹理立方体。我还让立方体旋转了一点。接下来,我想尝试使用vertexPositionNormalTexture。不幸的是,我得到的是这个图像而不是一个立方体。下面是我的一些代码片段,其中包含了主要的修改 画法
protected override void Draw(GameTime gameTime)
{
graphics.GraphicsDevice.Clear(Color.SteelBlue);
RasterizerState rasterizerState1 = new RasterizerState();
//backface culling
rasterizerState1.CullMode = CullMode.None;
//turn off texture blurring
graphics.GraphicsDevice.SamplerStates[0] = SamplerState.PointClamp;
graphics.GraphicsDevice.RasterizerState = rasterizerState1;
foreach (EffectPass pass in basicEffect.CurrentTechnique.Passes)
{
pass.Apply();
graphics.GraphicsDevice.DrawPrimitives(
PrimitiveType.TriangleList,
0,
12
);
}
base.Draw(gameTime);
}
设置顶点的方法的一部分
private void InitializeCube()
{
Vector3 topLeftFront = new Vector3(-1.0f, 1.0f, 1.0f);
Vector3 bottomLeftFront = new Vector3(-1.0f, -1.0f, 1.0f);
Vector3 topRightFront = new Vector3(1.0f, 1.0f, 1.0f);
Vector3 bottomRightFront = new Vector3(1.0f, -1.0f, 1.0f);
Vector3 topLeftBack = new Vector3(-1.0f, 1.0f, -1.0f);
Vector3 topRightBack = new Vector3(1.0f, 1.0f, -1.0f);
Vector3 bottomLeftBack = new Vector3(-1.0f, -1.0f, -1.0f);
Vector3 bottomRightBack = new Vector3(1.0f, -1.0f, -1.0f);
Vector2 textureTopLeft = new Vector2(0.0f, 0.0f);
Vector2 textureTopRight = new Vector2(.25f, 0.0f);
Vector2 textureBottomLeft = new Vector2(0.0f, .25f);
Vector2 textureBottomRight = new Vector2(.25f, .25f);
Color frontColor = new Color(255, 255, 255);
Color backColor = new Color(255, 0, 0);
Color topColor = new Color(0, 255, 0);
Color bottomColor = new Color(0, 0, 255);
Color leftColor = new Color(0, 255, 255);
Color rightColor = new Color(0, 0, 0);
// Front face.
cubeVertices[0] =
new VertexPositionColorTexture(
topLeftFront, frontColor, GetTexPos(2));
cubeVertices[1] =
new VertexPositionColorTexture(
bottomLeftFront, frontColor, GetTexPos(2) + textureBottomLeft);
cubeVertices[2] =
new VertexPositionColorTexture(
topRightFront, frontColor, GetTexPos(2) + textureTopRight);
cubeVertices[3] =
new VertexPositionColorTexture(
bottomLeftFront, frontColor, GetTexPos(2) + textureBottomLeft);
cubeVertices[4] =
new VertexPositionColorTexture(
bottomRightFront, frontColor, GetTexPos(2) + textureBottomRight);
cubeVertices[5] =
new VertexPositionColorTexture(
topRightFront, frontColor, GetTexPos(2) + textureTopRight);
初始化基本效果
private void InitializeEffect()
{
basicEffect = new BasicEffect(graphics.GraphicsDevice);
basicEffect.World = worldMatrix;
basicEffect.View = viewMatrix;
basicEffect.Projection = projectionMatrix;
//basicEffect.EnableDefaultLighting
}
装载量
protected override void LoadContent()
{
canyonTexture = Content.Load<Texture2D>("CanyonTexture");
textureSheetWidth = canyonTexture.Width / 16;
InitializeTransform();
InitializeEffect();
basicEffect.TextureEnabled = true;
basicEffect.VertexColorEnabled = true;
basicEffect.Texture = canyonTexture;
InitializeCube();
}
protected override void LoadContent()
{
canyonTexture=Content.Load(“canyonTexture”);
纹理宽度=峡谷纹理宽度/16;
InitializeTransform();
InitializeEffect();
basicEffect.TextureEnabled=true;
basicEffect.VertexColorEnabled=真;
基本效果纹理=峡谷纹理;
初始化为();
}
设置VertexBuffer
private void CreateVertexBuffer()
{
vertexDeclaration = new VertexDeclaration(new VertexElement[]
{
new VertexElement(0, VertexElementFormat.Vector3, VertexElementUsage.Position, 0),
new VertexElement(12, VertexElementFormat.Color, VertexElementUsage.Color, 0),
new VertexElement(24, VertexElementFormat.Vector2, VertexElementUsage.TextureCoordinate, 0)
});
vertexBuffer = new VertexBuffer(
graphics.GraphicsDevice,
vertexDeclaration,
number_of_vertices,
BufferUsage.None
);
cubeVertices = new VertexPositionColorTexture[number_of_vertices];
InitializeCube();
vertexBuffer.SetData<VertexPositionColorTexture>(cubeVertices);
graphics.GraphicsDevice.SetVertexBuffer(vertexBuffer);
}
protected override void Initialize()
{
// TODO: Add your initialization logic here
CreateVertexBuffer();
base.Initialize();
}
private void CreateVertexBuffer()
{
vertexDeclaration=新的vertexDeclaration(新的VertexElement[]
{
新的VertexElement(0,VertexElementFormat.Vector3,VertexElementUsage.Position,0),
新的VertexElement(12,VertexElementFormat.Color,VertexElementUsage.Color,0),
新的VertexElement(24,VertexElementFormat.Vector2,VertexElementUsage.TextureCoordination,0)
});
vertexBuffer=新的vertexBuffer(
graphics.GraphicsDevice,
顶点声明,
顶点的数量,
缓冲区使用情况。无
);
cubeVertices=新顶点ExpositionColorTexture[顶点的数量];
初始化为();
SetData(立方体数);
graphics.GraphicsDevice.SetVertexBuffer(vertexBuffer);
}
受保护的覆盖无效初始化()
{
//TODO:在此处添加初始化逻辑
CreateVertexBuffer();
base.Initialize();
}
基本上,您的顶点声明是错误的
Color
只有四个字节宽。因此,后面纹理坐标元素的偏移量应该是16,而不是24
但是,在XNA4.0中,您甚至不需要为此创建顶点声明。只需将VertexPositionColorTexture.VertexDeclaration
或typeof(VertexositionColorTexture)
传递给VertexBuffer的构造函数即可
有一个例子可以解释这一切是如何工作的。基本上你的顶点声明是错误的
Color
只有四个字节宽。因此,后面纹理坐标元素的偏移量应该是16,而不是24
但是,在XNA4.0中,您甚至不需要为此创建顶点声明。只需将VertexPositionColorTexture.VertexDeclaration
或typeof(VertexositionColorTexture)
传递给VertexBuffer的构造函数即可
有一个问题可以解释这一切是如何运作的。嗯,这个问题在imho上会更好。谢谢,如果我没有得到解决方案,我可能会在一天左右的时间内把它发布到那里。是的,如果你没有得到答案,它确实值得一看,我总是在那里得到我的XNA问题的答案。阿基米德会很沮丧的。嗯,这个问题在imho上会更好。谢谢,如果我没有得到解决方案,我可能会在一天左右的时间里把它发布到那里。是的,如果你没有得到答案,它确实值得一看,我总是在那里得到我的XNA问题的答案。阿基米德会很沮丧。谢谢,它现在工作得很好!我怀疑vertex声明有问题,但我不知道是什么。似乎每当我遇到一些问题时,总会有一些内置的XNA功能使一切变得更简单。问题其实是意识到它就在那里!谢谢,它现在工作得很好!我怀疑vertex声明有问题,但我不知道是什么。似乎每当我遇到一些问题时,总会有一些内置的XNA功能使一切变得更简单。问题其实是意识到它就在那里!