-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMesh.cs
More file actions
186 lines (152 loc) · 9.55 KB
/
Mesh.cs
File metadata and controls
186 lines (152 loc) · 9.55 KB
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
using HLMapFileLoader;
using Microsoft.Xna.Framework.Graphics;
using System.Collections.Generic;
using System;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xna.Framework;
namespace HLMapFileLoader.Example
{
class Mesh
{
private GraphicsDevice graphicsDevice;
private List<VertexBuffer> listVertexBuffer;
private Brush brush;
public Mesh(GraphicsDevice graphicsDevice, Brush brush)
{
this.listVertexBuffer = new List<VertexBuffer>();
this.graphicsDevice = graphicsDevice;
this.brush = brush;
BrushToVertices(brush);
}
private void BrushToVertices(Brush brush)
{
foreach (Polygon poly in brush.polygons)
{
List<CustomVertexFormat> listVertex = CompleteVertex(poly);
listVertexBuffer.Add(GetVertexBuffer(listVertex));
}
}
private List<CustomVertexFormat> CompleteVertex(Polygon poly)
{
// Vertices from polygon only specify corners
// It needs to describe triangles
// There's probably some easier way to achieve this...
List<CustomVertexFormat> listVertex = new List<CustomVertexFormat>();
Vector3 normal = new Vector3(0, 1, 0);
if (poly.Vertices.Count == 5)
{
listVertex.Add(new CustomVertexFormat(poly.Vertices[1], poly.TextureScales[1], normal));
listVertex.Add(new CustomVertexFormat(poly.Vertices[3], poly.TextureScales[3], normal));
listVertex.Add(new CustomVertexFormat(poly.Vertices[4], poly.TextureScales[4], normal));
listVertex.Add(new CustomVertexFormat(poly.Vertices[0], poly.TextureScales[0], normal));
listVertex.Add(new CustomVertexFormat(poly.Vertices[1], poly.TextureScales[1], normal));
listVertex.Add(new CustomVertexFormat(poly.Vertices[4], poly.TextureScales[4], normal));
listVertex.Add(new CustomVertexFormat(poly.Vertices[1], poly.TextureScales[1], normal));
listVertex.Add(new CustomVertexFormat(poly.Vertices[2], poly.TextureScales[2], normal));
listVertex.Add(new CustomVertexFormat(poly.Vertices[3], poly.TextureScales[3], normal));
}
else
if (poly.Vertices.Count == 3)
{
listVertex.Add(new CustomVertexFormat(poly.Vertices[0], poly.TextureScales[0], normal));
listVertex.Add(new CustomVertexFormat(poly.Vertices[1], poly.TextureScales[1], normal));
listVertex.Add(new CustomVertexFormat(poly.Vertices[2], poly.TextureScales[2], normal));
}
else
if (poly.Vertices.Count == 4)
{
listVertex.Add(new CustomVertexFormat(poly.Vertices[3], poly.TextureScales[3], normal));
listVertex.Add(new CustomVertexFormat(poly.Vertices[0], poly.TextureScales[0], normal));
listVertex.Add(new CustomVertexFormat(poly.Vertices[1], poly.TextureScales[1], normal));
listVertex.Add(new CustomVertexFormat(poly.Vertices[2], poly.TextureScales[2], normal));
listVertex.Add(new CustomVertexFormat(poly.Vertices[3], poly.TextureScales[3], normal));
listVertex.Add(new CustomVertexFormat(poly.Vertices[1], poly.TextureScales[1], normal));
}
else
if (poly.Vertices.Count == 6)
{
listVertex.Add(new CustomVertexFormat(poly.Vertices[0], poly.TextureScales[0], normal));
listVertex.Add(new CustomVertexFormat(poly.Vertices[1], poly.TextureScales[1], normal));
listVertex.Add(new CustomVertexFormat(poly.Vertices[5], poly.TextureScales[5], normal));
listVertex.Add(new CustomVertexFormat(poly.Vertices[1], poly.TextureScales[1], normal));
listVertex.Add(new CustomVertexFormat(poly.Vertices[4], poly.TextureScales[4], normal));
listVertex.Add(new CustomVertexFormat(poly.Vertices[5], poly.TextureScales[5], normal));
listVertex.Add(new CustomVertexFormat(poly.Vertices[1], poly.TextureScales[1], normal));
listVertex.Add(new CustomVertexFormat(poly.Vertices[2], poly.TextureScales[2], normal));
listVertex.Add(new CustomVertexFormat(poly.Vertices[3], poly.TextureScales[3], normal));
listVertex.Add(new CustomVertexFormat(poly.Vertices[1], poly.TextureScales[1], normal));
listVertex.Add(new CustomVertexFormat(poly.Vertices[3], poly.TextureScales[3], normal));
listVertex.Add(new CustomVertexFormat(poly.Vertices[4], poly.TextureScales[4], normal));
}
else
if (poly.Vertices.Count == 7)
{
listVertex.Add(new CustomVertexFormat(poly.Vertices[0], poly.TextureScales[0], normal));
listVertex.Add(new CustomVertexFormat(poly.Vertices[5], poly.TextureScales[5], normal));
listVertex.Add(new CustomVertexFormat(poly.Vertices[6], poly.TextureScales[6], normal));
listVertex.Add(new CustomVertexFormat(poly.Vertices[0], poly.TextureScales[0], normal));
listVertex.Add(new CustomVertexFormat(poly.Vertices[1], poly.TextureScales[1], normal));
listVertex.Add(new CustomVertexFormat(poly.Vertices[5], poly.TextureScales[5], normal));
listVertex.Add(new CustomVertexFormat(poly.Vertices[1], poly.TextureScales[1], normal));
listVertex.Add(new CustomVertexFormat(poly.Vertices[4], poly.TextureScales[4], normal));
listVertex.Add(new CustomVertexFormat(poly.Vertices[5], poly.TextureScales[5], normal));
listVertex.Add(new CustomVertexFormat(poly.Vertices[1], poly.TextureScales[1], normal));
listVertex.Add(new CustomVertexFormat(poly.Vertices[2], poly.TextureScales[2], normal));
listVertex.Add(new CustomVertexFormat(poly.Vertices[4], poly.TextureScales[4], normal));
listVertex.Add(new CustomVertexFormat(poly.Vertices[2], poly.TextureScales[2], normal));
listVertex.Add(new CustomVertexFormat(poly.Vertices[3], poly.TextureScales[3], normal));
listVertex.Add(new CustomVertexFormat(poly.Vertices[4], poly.TextureScales[4], normal));
}
else
if (poly.Vertices.Count == 8)
{
listVertex.Add(new CustomVertexFormat(poly.Vertices[2], poly.TextureScales[2], normal));
listVertex.Add(new CustomVertexFormat(poly.Vertices[3], poly.TextureScales[3], normal));
listVertex.Add(new CustomVertexFormat(poly.Vertices[7], poly.TextureScales[7], normal));
listVertex.Add(new CustomVertexFormat(poly.Vertices[0], poly.TextureScales[0], normal));
listVertex.Add(new CustomVertexFormat(poly.Vertices[1], poly.TextureScales[1], normal));
listVertex.Add(new CustomVertexFormat(poly.Vertices[7], poly.TextureScales[7], normal));
listVertex.Add(new CustomVertexFormat(poly.Vertices[1], poly.TextureScales[1], normal));
listVertex.Add(new CustomVertexFormat(poly.Vertices[2], poly.TextureScales[2], normal));
listVertex.Add(new CustomVertexFormat(poly.Vertices[6], poly.TextureScales[6], normal));
listVertex.Add(new CustomVertexFormat(poly.Vertices[3], poly.TextureScales[3], normal));
listVertex.Add(new CustomVertexFormat(poly.Vertices[4], poly.TextureScales[4], normal));
listVertex.Add(new CustomVertexFormat(poly.Vertices[7], poly.TextureScales[7], normal));
listVertex.Add(new CustomVertexFormat(poly.Vertices[4], poly.TextureScales[4], normal));
listVertex.Add(new CustomVertexFormat(poly.Vertices[6], poly.TextureScales[6], normal));
listVertex.Add(new CustomVertexFormat(poly.Vertices[7], poly.TextureScales[7], normal));
// THE FOLLOWING IS NEW:
listVertex.Add(new CustomVertexFormat(poly.Vertices[1], poly.TextureScales[1], normal));
listVertex.Add(new CustomVertexFormat(poly.Vertices[2], poly.TextureScales[2], normal));
listVertex.Add(new CustomVertexFormat(poly.Vertices[7], poly.TextureScales[7], normal));
}
return listVertex;
}
private VertexBuffer GetVertexBuffer(List<CustomVertexFormat> listVertex)
{
VertexBuffer vertexBuffer = new VertexBuffer(graphicsDevice, CustomVertexFormat.VertexDeclaration, listVertex.Count, BufferUsage.WriteOnly);
vertexBuffer.SetData<CustomVertexFormat>(listVertex.ToArray());
return vertexBuffer;
}
public void Draw(BasicEffect effect, Camera camera)
{
for (int i = 0; i < brush.polygons.Count; i++)
{
Matrix transform = Matrix.CreateTranslation(new Vector3(0, 0, 0));
effect.World = transform;
effect.View = camera.View;
effect.Projection = camera.Projection;
effect.TextureEnabled = true;
effect.Texture = brush.polygons[i].Texture;
foreach (EffectPass pass in effect.CurrentTechnique.Passes)
{
pass.Apply();
graphicsDevice.SetVertexBuffer(listVertexBuffer[i]);
graphicsDevice.DrawPrimitives(PrimitiveType.TriangleList, 0, listVertexBuffer[i].VertexCount / 3);
}
}
}
}
}