-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathMesh.cs
193 lines (167 loc) · 6.33 KB
/
Mesh.cs
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
using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
namespace XNALara
{
public class Mesh
{
private GraphicsDevice graphicsDevice;
private TextureManager textureManager;
private MeshDesc meshDesc;
private VertexFormat vertexFormat;
private string name;
private bool isVisible;
private VertexBuffer vertexBuffer;
private IndexBuffer indexBuffer;
private VertexDeclaration vertexDeclaration;
private int vertexCount;
private int faceCount;
private int vertexDataSize;
private Texture2D[] textures;
private short[] boneIndexMap;
private object[] renderParams;
private bool isTransparentVertices;
private bool isTransparentTextures;
private bool isShadeless;
private BoundingBox boundingBox;
public Mesh(GraphicsDevice graphicsDevice, TextureManager textureManager,
MeshDesc meshDesc, VertexFormat vertexFormat) {
this.graphicsDevice = graphicsDevice;
this.textureManager = textureManager;
this.meshDesc = meshDesc;
this.vertexFormat = vertexFormat;
this.name = meshDesc.name;
vertexBuffer = vertexFormat.BuildVertexBuffer(graphicsDevice, meshDesc);
indexBuffer = new IndexBuffer(graphicsDevice, typeof(ushort), meshDesc.indices.Length, BufferUsage.WriteOnly);
indexBuffer.SetData(meshDesc.indices);
vertexDeclaration = new VertexDeclaration(graphicsDevice, vertexFormat.VertexElements);
vertexCount = meshDesc.vertices.Length;
faceCount = meshDesc.indices.Length / 3;
vertexDataSize = vertexFormat.SizeInBytes;
int textureCount = meshDesc.textures.Length;
textures = new Texture2D[textureCount];
for (int i = 0; i < textureCount; i++) {
string textureFilename = meshDesc.textures[i].filename;
Texture2D texture = textureManager.GetTexture(textureFilename, meshDesc.textures[i].useMipmaps);
if (texture == null) {
Console.WriteLine("Texture \"{0}\" not found!", textureFilename);
}
textures[i] = texture;
}
isTransparentVertices = false;
foreach (MeshDesc.Vertex vertex in meshDesc.vertices) {
if (vertex.color.W < 1.0) {
isTransparentVertices = true;
break;
}
}
isTransparentTextures = false;
foreach (Texture2D texture in textures) {
if (textureManager.IsTransparent(texture)) {
isTransparentTextures = true;
break;
}
}
boneIndexMap = meshDesc.boneIndexMap;
if (meshDesc.renderParams != null) {
renderParams = meshDesc.renderParams;
}
isShadeless = meshDesc.isShadeless;
isVisible = true;
boundingBox = CalculateBoundingBox(meshDesc);
}
public GraphicsDevice GraphicsDevice {
get { return graphicsDevice; }
}
public TextureManager TextureManager {
get { return textureManager; }
}
public MeshDesc MeshDesc {
get { return meshDesc; }
}
public VertexFormat VertexFormat {
get { return vertexFormat; }
}
public string Name {
get { return name; }
}
public bool IsVisible {
set { isVisible = value; }
get { return isVisible; }
}
public BoundingBox BoundingBox {
get { return boundingBox; }
}
public short[] BoneIndexMap {
get { return boneIndexMap; }
}
public Texture2D[] Textures {
get { return textures; }
}
public object[] RenderParams {
set { renderParams = value; }
get { return renderParams; }
}
public bool IsTransparent {
get { return isTransparentVertices || isTransparentTextures; }
}
public bool IsShadeless {
get { return isShadeless; }
}
public void Render() {
if (!isVisible || faceCount <= 0) {
return;
}
graphicsDevice.Vertices[0].SetSource(vertexBuffer, 0, vertexDataSize);
graphicsDevice.Indices = indexBuffer;
graphicsDevice.VertexDeclaration = vertexDeclaration;
graphicsDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, vertexCount, 0, faceCount);
}
public void RenderSinglePass(Effect effect, params string[] textureParams) {
if (!isVisible) {
return;
}
for (int i = 0; i < textureParams.Length; i++) {
effect.Parameters[textureParams[i]].SetValue(textures[i]);
}
effect.Begin();
EffectPass pass = effect.CurrentTechnique.Passes[0];
pass.Begin();
Render();
pass.End();
effect.End();
}
private BoundingBox CalculateBoundingBox(MeshDesc meshDesc) {
float xMin = float.PositiveInfinity;
float yMin = float.PositiveInfinity;
float zMin = float.PositiveInfinity;
float xMax = float.NegativeInfinity;
float yMax = float.NegativeInfinity;
float zMax = float.NegativeInfinity;
foreach (MeshDesc.Vertex vertex in meshDesc.vertices) {
Vector3 v = vertex.position;
if (v.X < xMin) {
xMin = v.X;
}
if (v.Y < yMin) {
yMin = v.Y;
}
if (v.Z < zMin) {
zMin = v.Z;
}
if (v.X > xMax) {
xMax = v.X;
}
if (v.Y > yMax) {
yMax = v.Y;
}
if (v.Z > zMax) {
zMax = v.Z;
}
}
Vector3 min = new Vector3(xMin, yMin, zMin);
Vector3 max = new Vector3(xMax, yMax, zMax);
return new BoundingBox(min, max);
}
}
}