我的 directX 网格有问题。当我绘制超过 4-5 个网格(我使用球体)时,Direct3dX 异常出现在我初始化网格的行中。在那种情况下,我也有疯狂的滞后。我使用 C#。
那就是我的球体绘图功能:
public void draw(Device device) { device.Transform.World =Matrix.Translation(v3CurMeshPos); device.Material = m; device.RenderState.Ambient = color; mesh = Mesh.Sphere(device, radius, 100, 100); // problem line mesh.DrawSubset(0); }
那就是我的初始化和使用directX绘图:
private void devicePanel1_OnCreateDevice(object sender, DirectxGraph.DeviceEventArgs e)
{
device = e.Device;
device.Transform.Projection = Matrix.PerspectiveFovLH((float)Math.PI / 4, devicePanel1.Width / devicePanel1.Height, 1f, 1000f);
device.RenderState.Lighting = true;
device.RenderState.CullMode = Cull.None;
device.Lights[0].Type = LightType.Directional;
device.Lights[0].Position = new Vector3(10, 10, 0);
device.Lights[0].Direction = new Vector3(1, -3, -1);
device.Lights[0].Enabled = true;
}
private void updateCamera()
{
v3CamPos = new Vector3(0, 0, Util.distance);
v3CamLookAt = new Vector3(0, 0, 0);
device.Transform.View = Matrix.RotationYawPitchRoll(Util.rotX, Util.rotY, Util.rotZ) * Matrix.LookAtLH(v3CamPos, v3CamLookAt, new Vector3(0, 1, 0));
}
private void devicePanel1_OnRender(object sender, DirectxGraph.DeviceEventArgs e)
{
Draw(device);
}
private void Draw(Device device)
{
if (rotate_forward)
{
if (radioButton1.Checked)
{
Util.rotX += 0.005f;
}
else if (radioButton2.Checked)
{
Util.rotY += 0.005f;
}
else if (radioButton3.Checked)
{
Util.rotZ += 0.005f;
}
}
else if (rotate_backward)
{
if (radioButton1.Checked)
{
Util.rotX -= 0.005f;
}
else if (radioButton2.Checked)
{
Util.rotY -= 0.005f;
}
else if (radioButton3.Checked)
{
Util.rotZ -= 0.005f;
}
}
updateCamera();
if (!graph.isEmpty())
{
foreach (Node node in graph.nodes)
{
node.shape.draw(device);
}
}
}