我对directX并不是真的不了解,但我会尽力解释。首先,我有多个模型对象(带有顶点数据的自定义对象)。目前,我的代码(见下文)可以使用该模型的 vertexBuffer 和 indexBuffer 在场景中渲染单个模型。我想做的是给定一系列模型。将它们全部渲染在一个场景中。这是我当前的代码:
private void Render()
{
device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.White, 1.0f, 0);
device.BeginScene();
float x = (float)Math.Cos(0);
float z = (float)Math.Sin(0);
device.Transform.Projection = Matrix.PerspectiveFovLH((float)Math.PI / 4, this.Width / this.Height, 1f, 50f);
device.Transform.View = Matrix.LookAtLH(new Vector3(x, 6, z), new Vector3(0, 0, 0), new Vector3(0, 1, 0));
device.RenderState.Lighting = true;
device.Lights[0].Type = LightType.Directional;
device.Lights[0].Diffuse = Color.White;
device.Lights[0].Direction = new Vector3(-x, -6, -z);
device.Lights[0].Position = new Vector3(x, 6, z);
device.Lights[0].Enabled = true;
device.Transform.World = model.transform;
device.VertexFormat = CustomVertex.PositionNormalColored.Format;
device.SetStreamSource(0, vertexBuffer, 0);
device.Indices = indexBuffer;
device.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, model.getVertices().Length, 0, model.getIndices().Length / 3);
device.EndScene();
device.Present();
}
public void RenderModel(Model model)
{
this.model = model;
vertexBuffer = new VertexBuffer(typeof(CustomVertex.PositionNormalColored), model.getVertices().Length,
device, Usage.Dynamic | Usage.WriteOnly, CustomVertex.PositionNormalColored.Format, Pool.Default);
vertexBuffer.SetData(model.getVertices(), 0, LockFlags.None);
indexBuffer = new IndexBuffer(typeof(ushort), model.getIndices().Length, device, Usage.WriteOnly, Pool.Default);
indexBuffer.SetData(model.getIndices(), 0, LockFlags.None);
Render();
}