【发布时间】:2015-03-27 13:08:21
【问题描述】:
努力让这个自定义网格正常工作。 GUI 表单上的结果是白色背景上的红十字(DirectX 设备错误)。我可以轻松地让 Mesh.Box 或 Mesh.Sphere 工作,但尝试创建自定义 Mesh 失败。 我浏览了无数的例子,但仍然没有乐趣。
希望你能帮忙。
private Mesh meshSubject = null;
int numberVerts = 36;
short[] indices = {
0,1,2, // Front Face
1,3,2, // Front Face
4,5,6, // Back Face
6,5,7, // Back Face
0,5,4, // Top Face
0,2,5, // Top Face
1,6,7, // Bottom Face
1,7,3, // Bottom Face
0,6,1, // Left Face
4,6,0, // Left Face
2,3,7, // Right Face
5,2,7 // Right Face
};
private void OnDeviceReset(object sender, EventArgs e)
{
// THESE TEST MESHES WORK
//meshSubject = Mesh.Box(device, 0.8f, 0.18f, 2.2f);
//meshSubject = Mesh.Sphere(device, 0.5f, 8,1000);
// **** start of custom mesh - THIS CUSTOM MESH DOES NOT WORK :-(
Mesh meshSubject = new Mesh(indices.Length / 3, numberVerts, MeshFlags.Managed, CustomVertex.PositionColored.Format, device);
IndexBuffer indicesBuff = meshSubject.IndexBuffer;
VertexBuffer verticesBuff = meshSubject.VertexBuffer;
GraphicsStream data = verticesBuff.Lock(0, 0, LockFlags.None);
data.Write(new CustomVertex.PositionColored(-1.0f, 1.0f, 1.0f, 0x00ff00ff));
data.Write(new CustomVertex.PositionColored(-1.0f, -1.0f, 1.0f, 0x00ff00ff));
data.Write(new CustomVertex.PositionColored(1.0f, 1.0f, 1.0f, 0x00ff00ff));
data.Write(new CustomVertex.PositionColored(1.0f, -1.0f, 1.0f, 0x00ff00ff));
data.Write(new CustomVertex.PositionColored(-1.0f, 1.0f, -1.0f, 0x00ff00ff));
data.Write(new CustomVertex.PositionColored(1.0f, 1.0f, -1.0f, 0x00ff00ff));
data.Write(new CustomVertex.PositionColored(-1.0f, -1.0f, -1.0f, 0x00ff00ff));
data.Write(new CustomVertex.PositionColored(1.0f, -1.0f, -1.0f, 0x00ff00ff));
verticesBuff.Unlock();
indicesBuff.SetData(indices, 0, LockFlags.None);
device.SetStreamSource(0, verticesBuff, 0);
//**** end of custom mesh *******************
device.RenderState.Ambient = Color.White;
device.Lights[0].Type = LightType.Directional;
device.Lights[0].Direction = new Vector3(0.3f, -0.5f, 0.2f);
device.Lights[0].Diffuse = Color.White;
device.Lights[0].Update();
device.Lights[1].Type = LightType.Directional;
device.Lights[1].Direction = new Vector3(0.0f, 1.0f, -3.0f);
device.Lights[1].Diffuse = Color.White;
device.Lights[1].Update();
device.Lights[0].Enabled = true;
device.Lights[1].Enabled = true;
device.Transform.Projection = Matrix.PerspectiveFovLH((float)Math.PI / 5.0F, (float)this.ClientSize.Width / (float)this.ClientSize.Height, 2.0f, 80.0f);
}
protected override void OnPaint(PaintEventArgs e)
{
//Render();
Form.ActiveForm.Update();
device.BeginScene();
device.VertexFormat = CustomVertex.TransformedColored.Format;
Color meshColor = Color.White;
Material material = new Material();
// Begin the scene and clear the back buffer to black.
device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.Black, 1.0f, 0);
SetupMatrices();
meshSubject.DrawSubset(0);
device.VertexFormat = CustomVertex.PositionNormal.Format;
device.DrawPrimitives(PrimitiveType.TriangleList, 0, 12);
device.Lights[2].Enabled = true;
material.Diffuse = Color.Olive;
device.Material = material;
device.EndScene();
device.Present();
this.Invalidate();
}
【问题讨论】:
-
调试输出说什么?有什么例外吗?好像你才刚刚开始。您使用 DX9 有什么原因吗?
-
嗨 Nico,调试或异常没有输出,我刚刚开始我的 3D 图形编程之旅。之所以使用DX9,是因为有书好几年了。我在我的代码中看到 Mesh 为空的问题。我不知道如何将 Mesh 对象公开给需要它的函数。我的 DX C# 书只解释了如何从文件 .x 格式加载网格并将其用于基本的 3D 空间游戏。我真的很想编写自定义网格,但我只找到了示例的片段。我更喜欢使用 OpenGL,但我花钱买了这本书,所以想学习 directx 方法。
-
哦,你有两个
Mesh变量。作为类中的一个字段和方法OnDeviceReset中的第二个字段。后者隐藏了前者。只需删除声明就可以了(只需写meshSubject = new Mesh(.... -
不幸的是,Mesh 对象需要索引数组,因此它不能存在于函数或非静态字段之外。 Mesh meshSubject = new Mesh(indices.Length / 3, numberVerts, MeshFlags.Managed, CustomVertex.PositionColored.Format, device);
-
什么意思?您将
private Mesh meshSubject作为类字段,在方法中有第二个Mesh meshSubject = new Mesh(...),尽管您想从方法中引用该字段。因此,删除额外的声明,只在方法中使用meshSubject = new Mesh(...)。