C#XNA VertexBuffer无法渲染纹理
我正在尝试制作3D XNA游戏,但当我尝试加载我的模型时,它无法渲染纹理和照明效果,看起来大部分只是黑色,但模型形状是正确的 游戏中的外观: 我们应该看看: 代码:C#XNA VertexBuffer无法渲染纹理,c#,xna,C#,Xna,我正在尝试制作3D XNA游戏,但当我尝试加载我的模型时,它无法渲染纹理和照明效果,看起来大部分只是黑色,但模型形状是正确的 游戏中的外观: 我们应该看看: 代码: 课堂 { 私人国际楼层宽度; 私人室内地面照明; 私人垂直缓冲地板缓冲; 私有索引缓冲索引缓冲; 专用图形设备; 公共楼层(图形设备、内部宽度、内部高度) { 这个装置=装置; 这是指地板宽度=宽度; 此。地板高度=高度; BuildFloorBuffer(); } 私有void BuildFloorBuffer() { 列表索
课堂
{
私人国际楼层宽度;
私人室内地面照明;
私人垂直缓冲地板缓冲;
私有索引缓冲索引缓冲;
专用图形设备;
公共楼层(图形设备、内部宽度、内部高度)
{
这个装置=装置;
这是指地板宽度=宽度;
此。地板高度=高度;
BuildFloorBuffer();
}
私有void BuildFloorBuffer()
{
列表索引列表=新列表();
列表顶点列表=新列表();
foreach(Game1.myModel.mesh中的ModelMesh网格)
{
foreach(网格中的ModelMeshPart零件。MeshParts)
{
VertexPositionNormalTexture[]vertexArray=新的VertexPositionNormalTexture[part.VertexBuffer.VertexCount];
part.VertexBuffer.GetData(vertexArray);
AddRange(vertexArray.ToList());
short[]indexArray=新short[part.IndexBuffer.IndexCount];
part.IndexBuffer.GetData(indexArray);
indexList.AddRange(indexArray);
}
}
floorBuffer=新的VertexBuffer(设备,VertexExpositionNormalTexture.VertexDeclaration,vertexList.Count,BufferUsage.None);
SetData(vertexList.ToArray());
indexBuffer=新的indexBuffer(设备,IndexElementSize.SixteenBits,sizeof(短)*indexList.Count,BUFFERUSE.None);
SetData(indexList.ToArray());
}
公共虚空绘制(摄影机、基本效果)
{
effect.VertexColorEnabled=假;
effect.TextureEnabled=真;
effect.LightingEnabled=真;
effect.View=camera.View;
effect.Projection=camera.Projection;
效果。世界=矩阵。同一性;
effect.EnableDefaultLighting();
foreach(EffectPass-in-effect.currentTechnology.passs)
{
pass.Apply();
设备索引=索引缓冲;
设备。SetVertexBuffer(floorBuffer);
device.DrawIndexedPrimitions(PrimitiveType.TriangelList,0,0,floorBuffer.VertexCount,0,floorBuffer.VertexCount);
}
}
}
为什么不让网格自己绘制呢?可能有一个索引缓冲区,您正在忽略它。@NicoSchertler,因为我有相机,但它不能正常工作,所以一直在屏幕上。索引缓冲区?我在哪里需要它?这听起来不是一个合理的理由。索引缓冲区是ModelMeshPart
的成员。您应该在设备上设置它,就像顶点缓冲区一样。将所有缓冲区合并为一个缓冲区需要注意一点,因为索引是移位的。因此,我强烈建议使用ModelMesh
@NicoSchertler的内置功能,因为它根本无法按我所希望的方式工作。添加了IndexBuffer,没有更改。然后需要。
class Floor
{
private int floorWidth;
private int floorHeight;
private VertexBuffer floorBuffer;
private IndexBuffer indexBuffer;
private GraphicsDevice device;
public Floor(GraphicsDevice device, int width, int height)
{
this.device = device;
this.floorWidth = width;
this.floorHeight = height;
BuildFloorBuffer();
}
private void BuildFloorBuffer()
{
List<short> indexList = new List<short>();
List<VertexPositionNormalTexture> vertexList = new List<VertexPositionNormalTexture>();
foreach (ModelMesh mesh in Game1.myModel.Meshes)
{
foreach (ModelMeshPart part in mesh.MeshParts)
{
VertexPositionNormalTexture[] vertexArray = new VertexPositionNormalTexture[part.VertexBuffer.VertexCount];
part.VertexBuffer.GetData<VertexPositionNormalTexture>(vertexArray);
vertexList.AddRange(vertexArray.ToList());
short[] indexArray = new short[part.IndexBuffer.IndexCount];
part.IndexBuffer.GetData<short>(indexArray);
indexList.AddRange(indexArray);
}
}
floorBuffer = new VertexBuffer(device, VertexPositionNormalTexture.VertexDeclaration, vertexList.Count, BufferUsage.None);
floorBuffer.SetData<VertexPositionNormalTexture>(vertexList.ToArray());
indexBuffer = new IndexBuffer(device, IndexElementSize.SixteenBits, sizeof(short) * indexList.Count, BufferUsage.None);
indexBuffer.SetData<short>(indexList.ToArray());
}
public void Draw(Camera camera, BasicEffect effect)
{
effect.VertexColorEnabled = false;
effect.TextureEnabled = true;
effect.LightingEnabled = true;
effect.View = camera.View;
effect.Projection = camera.Projection;
effect.World = Matrix.Identity;
effect.EnableDefaultLighting();
foreach(EffectPass pass in effect.CurrentTechnique.Passes)
{
pass.Apply();
device.Indices = indexBuffer;
device.SetVertexBuffer(floorBuffer);
device.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, floorBuffer.VertexCount, 0, floorBuffer.VertexCount);
}
}
}