1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
|
using System;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Media3D;
namespace WpfCustomGeometries
{
public class CustomCube
{
// Define private fields:
private double length = 1.0;
private double width = 1.0;
private double height = 1.0;
private Point3D center = new Point3D();
// Define public properties:
public double Length
{
get { return length; }
set { length = value; }
}
public double Width
{
get { return width; }
set { width = value; }
}
public double Height
{
get { return height; }
set { height = value; }
}
public Point3D Center
{
get { return center; }
set { center = value; }
}
// Get-only property generates MeshGeometry3D object:
public MeshGeometry3D Mesh3D
{
get { return GetMesh3D(); }
}
private MeshGeometry3D GetMesh3D()
{
MeshGeometry3D mesh = new MeshGeometry3D();
Point3D[] verts = new Point3D[36];
double hx = 0.5 * Length;//X
double hy = 0.5 * Height;//Y
double hz = 0.5 * Width; //Z
// center
for (int i = 0; i < 36; i++)
{
verts[i] += (Vector3D)Center;
}
// Front face
verts[0] += (Vector3D)new Point3D(+hx, +hy, +hz);
verts[1] += (Vector3D)new Point3D(-hx, +hy, +hz);
verts[2] += (Vector3D)new Point3D(-hx, -hy, +hz);
verts[3] += (Vector3D)new Point3D(-hx, -hy, +hz);
verts[4] += (Vector3D)new Point3D(+hx, -hy, +hz);
verts[5] += (Vector3D)new Point3D(+hx, +hy, +hz);
for (int i = 0; i < 6; i++)
{
mesh.Positions.Add(verts[i]);
mesh.TriangleIndices.Add(i);
}
//normals
for (int i = 0; i < 6; i += 3)
{
CalculateNormalFaces(i, i + 1, i + 2, mesh);
}
// Back face (remember this is facing *away* from the camera, so vertices should be
//clockwise order)
verts[6] += (Vector3D)new Point3D(-hx, +hy, -hz);
verts[7] += (Vector3D)new Point3D(+hx, +hy, -hz);
verts[8] += (Vector3D)new Point3D(+hx, -hy, -hz);
verts[9] += (Vector3D)new Point3D(+hx, -hy, -hz);
verts[10] += (Vector3D)new Point3D(-hx, -hy, -hz);
verts[11] += (Vector3D)new Point3D(-hx, +hy, -hz);
for (int i = 6; i < 12; i++)
{
mesh.Positions.Add(verts[i]);
mesh.TriangleIndices.Add(i);
}
//normals
for (int i = 6; i < 12; i += 3)
{
CalculateNormalFaces(i, i + 1, i + 2, mesh);
}
// Top face
verts[12] += (Vector3D)new Point3D(-hx, +hy, +hz);
verts[13] += (Vector3D)new Point3D(+hx, +hy, +hz);
verts[14] += (Vector3D)new Point3D(+hx, +hy, -hz);
verts[15] += (Vector3D)new Point3D(+hx, +hy, -hz);
verts[16] += (Vector3D)new Point3D(-hx, +hy, -hz);
verts[17] += (Vector3D)new Point3D(-hx, +hy, +hz);
for (int i = 12; i < 18; i++)
{
mesh.Positions.Add(verts[i]);
mesh.TriangleIndices.Add(i);
}
//normals
for (int i = 12; i < 18; i += 3)
{
CalculateNormalFaces(i, i + 1, i + 2, mesh);
}
// Bottom face (remember this is facing *away* from the camera, so vertices should be
//clockwise order)
verts[18] += (Vector3D)new Point3D(-hx, -hy, +hz);
verts[19] += (Vector3D)new Point3D(-hx, -hy, -hz);
verts[20] += (Vector3D)new Point3D(+hx, -hy, -hz);
verts[21] += (Vector3D)new Point3D(+hx, -hy, -hz);
verts[22] += (Vector3D)new Point3D(+hx, -hy, +hz);
verts[23] += (Vector3D)new Point3D(-hx, -hy, +hz);
for (int i = 18; i < 24; i++)
{
mesh.Positions.Add(verts[i]);
mesh.TriangleIndices.Add(i);
}
//normals
for (int i = 18; i < 24; i += 3)
{
CalculateNormalFaces(i, i + 1, i + 2, mesh);
}
// Left face
verts[24] += (Vector3D)new Point3D(-hx, +hy, +hz);
verts[25] += (Vector3D)new Point3D(-hx, +hy, -hz);
verts[26] += (Vector3D)new Point3D(-hx, -hy, -hz);
verts[27] += (Vector3D)new Point3D(-hx, -hy, -hz);
verts[28] += (Vector3D)new Point3D(-hx, -hy, +hz);
verts[29] += (Vector3D)new Point3D(-hx, +hy, +hz);
for (int i = 24; i < 30; i++)
{
mesh.Positions.Add(verts[i]);
mesh.TriangleIndices.Add(i);
}
//normals
for (int i = 24; i < 30; i += 3)
{
CalculateNormalFaces(i, i + 1, i + 2, mesh);
}
// Right face (remember this is facing *away* from the camera, so vertices should be
//clockwise order)
verts[30] += (Vector3D)new Point3D(+hx, +hy, +hz);
verts[31] += (Vector3D)new Point3D(+hx, -hy, +hz);
verts[32] += (Vector3D)new Point3D(+hx, -hy, -hz);
verts[33] += (Vector3D)new Point3D(+hx, -hy, -hz);
verts[34] += (Vector3D)new Point3D(+hx, +hy, -hz);
verts[35] += (Vector3D)new Point3D(+hx, +hy, +hz);
for (int i = 30; i < 36; i++)
{
mesh.Positions.Add(verts[i]);
mesh.TriangleIndices.Add(i);
}
//normals
for (int i = 30; i < 36; i+=3)
{
CalculateNormalFaces(i, i+1, i+2, mesh);
}
// textures coord. default wpf
mesh.Freeze();
return mesh;
}
private void CalculateNormalFaces(int i, int j, int k, MeshGeometry3D mesh)
{
Vector3D v0 = new Vector3D(
mesh.Positions[j].X - mesh.Positions[i].X,
mesh.Positions[j].Y - mesh.Positions[i].Y,
mesh.Positions[j].Z - mesh.Positions[i].Z);
Vector3D v1 = new Vector3D(
mesh.Positions[k].X - mesh.Positions[j].X,
mesh.Positions[k].Y - mesh.Positions[j].Y,
mesh.Positions[k].Z - mesh.Positions[j].Z);
/* 3 normales par faces(1 normale/vertex) */
mesh.Normals.Add(Vector3D.CrossProduct(v0, v1));
mesh.Normals.Add(Vector3D.CrossProduct(v0, v1));
mesh.Normals.Add(Vector3D.CrossProduct(v0, v1));
}
private Vector3D CalculateNormal(Point3D p0, Point3D p1, Point3D p2)
{
Vector3D v0 = new Vector3D(
p1.X - p0.X, p1.Y - p0.Y, p1.Z - p0.Z);
Vector3D v1 = new Vector3D(
p2.X - p1.X, p2.Y - p1.Y, p2.Z - p1.Z);
return Vector3D.CrossProduct(v0, v1);
}
}
} |
Partager