編輯代碼:固定
大家好,我有我的窗口下面的事件處理程序:我想繪製一個3d立方體,但它不起作用。裏面
private void buttonView_Click(object sender, RoutedEventArgs e)
{
//Camera
PerspectiveCamera camera = new PerspectiveCamera();
camera.LookDirection = new Vector3D(5, -2, -3);
camera.Position = new Point3D(-5, 2, 3);
camera.UpDirection = new Vector3D(0, 1, 0);
camera.NearPlaneDistance = 1;
camera.FarPlaneDistance = 10;
//Lighting
DirectionalLight light = new DirectionalLight(Colors.White, new Vector3D(-3, -4, -5));
//Cube
Cube cube = new Cube();
GeometryModel3D cubeModel = new GeometryModel3D();
cubeModel.Geometry = cube.Mesh;
cubeModel.Material = new DiffuseMaterial(Brushes.Red);
//ModelGroup
Model3DGroup modelGroup = new Model3DGroup();
modelGroup.Children.Add(light);
modelGroup.Children.Add(cubeModel);
//Model
ModelVisual3D model = new ModelVisual3D();
model.Content = modelGroup;
//Viewport
Viewport3D view = new Viewport3D();
view.Camera = camera;
view.Children.Add(model);
//Show it all
Frame f = new Frame();
f.Content = view;
grid1.Children.Add(f);
}
,這是我的立方體類:
public class Cube : Primitive
{
Point3D[] positions = new Point3D[] {
new Point3D(0, 1, 1),
new Point3D(0, 0, 1),
new Point3D(1, 1, 1),
new Point3D(1, 0, 1),
new Point3D(1, 1, 0),
new Point3D(0, 0, 0),
new Point3D(0, 1, 1),
new Point3D(0, 0, 1),
new Point3D(0, 1, 0),
new Point3D(0, 1, 0),
new Point3D(1, 1, 1),
new Point3D(0, 1, 1),
new Point3D(1, 1, 1),
new Point3D(1, 0, 1),
new Point3D(1, 1, 0),
new Point3D(1, 0, 0),
new Point3D(1, 0, 1),
new Point3D(0, 0, 1),
new Point3D(1, 0, 0),
new Point3D(0, 0, 0),
new Point3D(1, 1, 0),
new Point3D(1, 0, 0),
new Point3D(0, 1, 0),
new Point3D(0, 0, 0)
};
int[] vertices = new int[] {
0,
1,
2,
1,
3,
2,
4,
5,
6,
5,
7,
6,
8,
9,
10,
9,
11,
10,
12,
13,
14,
13,
15,
14,
16,
17,
18,
17,
19,
18,
20,
21,
22,
21,
23,
22
};
public Cube()
{
this.Mesh.Positions = new Point3DCollection(positions);
this.Mesh.TriangleIndices = new Int32Collection(vertices);
}
}
public abstract class Primitive
{
public Primitive()
{
this.Mesh = new MeshGeometry3D();
}
public MeshGeometry3D Mesh { get; protected set; }
}
然而,當我點擊按鈕,什麼也沒有顯示。 我的事件處理程序中是否有錯誤?
你的立方體看起來並不像一個魔方給我=( – Luiscencio 2010-07-21 18:48:43