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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
|
using System;
using System.Collections.Generic;
using NW4RTools;
using NW4RTools.Models;
using OpenTK;
using OpenTK.Graphics;
using OpenTK.Graphics.OpenGL;
namespace NW4RTools.Models.OpenGL {
public class GLModel {
public ResFile SourceFile { get; private set; }
public Model SourceModel { get; private set; }
public GLModel(ResFile rf, string modelName) {
SourceFile = rf;
SourceModel = rf.GetGroup<Model>("3DModels(NW4R)")[modelName];
// cache some stuff
try {
m_textureGroup = rf.GetGroup<Texture>("Textures(NW4R)");
} catch (KeyNotFoundException) {
// boo
}
}
private class MyNode {
Node RealNode;
MyNode Parent;
List<MyNode> Children;
}
private ResDict<Texture> m_textureGroup;
private Dictionary<string, GLTexture> m_glTextures;
private Dictionary<Material, GLDisplayList> m_glMaterials;
private Dictionary<Shape, GLDisplayList> m_glShapes;
private Matrix4[] m_matrices;
private MyNode[] m_nodesByMtxID;
private MyNode[] m_nodes;
#region Initialisation
public void Prepare(IGraphicsContext context) {
// convert every texture
m_glTextures = new Dictionary<string, GLTexture>();
foreach (var material in SourceModel.Materials) {
foreach (var texInfo in material.Value.TextureInfos) {
if (!m_glTextures.ContainsKey(texInfo.TextureName)) {
var newTex = new GLTexture();
newTex.Load(m_textureGroup[texInfo.TextureName]);
m_glTextures.Add(texInfo.TextureName, newTex);
}
}
}
// convert every material
m_glMaterials = new Dictionary<Material, GLDisplayList>();
foreach (var material in SourceModel.Materials.Values) {
m_glMaterials.Add(material, MakeMaterialDisplayList(material));
}
// convert every shape
m_glShapes = new Dictionary<Shape, GLDisplayList>();
foreach (var shape in SourceModel.Shapes.Values) {
m_glShapes.Add(shape, MakeShapeDisplayList(shape));
}
// allocate stuff for nodes
m_matrices = new Matrix4[SourceModel.MatrixIDtoNodeID.Length];
// build the hierarchy
BuildNodeHierarchy();
}
private void BuildNodeHierarchy() {
var nodeTree = SourceModel.Bytecode["NodeTree"];
foreach (var insn in nodeTree.Instructions) {
if (insn.GetOp() == ByteCode.OpType.AssignNodeToParentMtx) {
MyNode n = new MyNode();
}
}
}
#endregion
private static readonly TextureUnit[] TextureUnits = new TextureUnit[] { TextureUnit.Texture0, TextureUnit.Texture1, TextureUnit.Texture2, TextureUnit.Texture3, TextureUnit.Texture4, TextureUnit.Texture5, TextureUnit.Texture6, TextureUnit.Texture7, TextureUnit.Texture8, TextureUnit.Texture9,
TextureUnit.Texture10, TextureUnit.Texture11, TextureUnit.Texture12, TextureUnit.Texture13, TextureUnit.Texture14, TextureUnit.Texture15 };
// GX_CLAMP, GX_REPEAT, GX_MIRROR
private static readonly TextureWrapMode[] GXtoGLTextureWrapModes = new TextureWrapMode[] { TextureWrapMode.ClampToEdge, TextureWrapMode.Repeat, TextureWrapMode.MirroredRepeat };
private Material m_currentMaterial;
private void ClearCache() {
m_currentMaterial = null;
}
private void LoadMaterial(Material m) {
if (m == m_currentMaterial)
return;
m_currentMaterial = m;
m_glMaterials[m].Execute();
}
private void DrawShape(ByteCode.DrawShapeInstruction insn) {
LoadMaterial(SourceModel.Materials[insn.MaterialID]);
// HACKY TEST !!
GL.MatrixMode(MatrixMode.Modelview);
var node = SourceModel.Nodes[insn.NodeID];
var m = node.NodeMatrix;
/*double cosX = Math.Cos(node.Rotation.x / 180 * Math.PI);
double cosY = Math.Cos(node.Rotation.y / 180 * Math.PI);
double cosZ = Math.Cos(node.Rotation.z / 180 * Math.PI);
double sinX = Math.Sin(node.Rotation.x / 180 * Math.PI);
double sinY = Math.Sin(node.Rotation.y / 180 * Math.PI);
double sinZ = Math.Sin(node.Rotation.z / 180 * Math.PI);
var nodeMatrix = new Matrix4d();
nodeMatrix.M11 = node.Scale.x * cosY * cosZ;
nodeMatrix.M12 = node.Scale.y * (sinX * cosZ * sinY - cosX * sinZ);
nodeMatrix.M13 = node.Scale.z * (sinX * sinZ + cosX * cosZ * sinY);
nodeMatrix.M14 = node.Translation.x;
nodeMatrix.M21 = node.Scale.x * sinZ * cosY;
nodeMatrix.M22 = node.Scale.y * (sinX * sinZ * sinY + cosZ * cosX);
nodeMatrix.M23 = node.Scale.z * (cosX * sinZ * sinY - sinX * cosZ);
nodeMatrix.M24 = node.Translation.y;
nodeMatrix.M31 = -node.Scale.x * sinY;
nodeMatrix.M32 = node.Scale.y * sinX * cosY;
nodeMatrix.M33 = node.Scale.z * cosX * cosY;
nodeMatrix.M34 = node.Translation.z;
nodeMatrix.M41 = 0;
nodeMatrix.M42 = 0;
nodeMatrix.M43 = 0;
nodeMatrix.M44 = 1;
GL.PushMatrix();
GL.MultTransposeMatrix(ref nodeMatrix);
//*/
GL.PushMatrix();
GL.MultTransposeMatrix(new float[] { m.v00, m.v01, m.v02, m.v03, m.v10, m.v11, m.v12, m.v13, m.v20, m.v21,
m.v22, m.v23, 0, 0, 0, 1 });
//GL.Translate(node.Translation.x, node.Translation.y, node.Translation.z);
var shape = SourceModel.Shapes[insn.ShapeID];
m_glShapes[shape].Execute();
GL.PopMatrix();
}
public void Render(IGraphicsContext context) {
// This'll be fun.
ClearCache();
//CalculateNodes();
if (SourceModel.Bytecode.ContainsKey("DrawOpa")) {
foreach (var insn in SourceModel.Bytecode["DrawOpa"].Instructions) {
if (insn is ByteCode.DrawShapeInstruction) {
DrawShape(insn as ByteCode.DrawShapeInstruction);
}
}
}
if (SourceModel.Bytecode.ContainsKey("DrawXlu")) {
foreach (var insn in SourceModel.Bytecode["DrawXlu"].Instructions) {
if (insn is ByteCode.DrawShapeInstruction) {
DrawShape(insn as ByteCode.DrawShapeInstruction);
}
}
}
}
private void BindTextureInfo(TextureInfo info) {
m_glTextures[info.TextureName].Bind(TextureTarget.Texture2D);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int)GXtoGLTextureWrapModes[(int)info.WrapS]);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)GXtoGLTextureWrapModes[(int)info.WrapT]);
}
#region Display Lists
private GLDisplayList MakeMaterialDisplayList(Material m) {
var displayList = new GLDisplayList();
displayList.Begin();
if (m.TextureInfos.Count > 0) {
TextureInfo[] texInfoArray = new TextureInfo[8];
foreach (var texInfo in m.TextureInfos) {
texInfoArray[texInfo.TexMapID] = texInfo;
}
GL.Enable(EnableCap.Texture2D);
for (int i = 0; i < 8; i++) {
GL.ActiveTexture(TextureUnits[i]);
GL.TexEnv(TextureEnvTarget.TextureEnv, TextureEnvParameter.TextureEnvMode, (int)TextureEnvMode.Modulate);
if (texInfoArray[i] == null) {
GL.BindTexture(TextureTarget.Texture2D, 0);
} else {
BindTextureInfo(texInfoArray[i]);
//GL.Enable(EnableCap.Texture2D);
}
}
} else {
GL.Disable(EnableCap.Texture2D);
}
displayList.End();
return displayList;
}
private GLDisplayList MakeShapeDisplayList(Shape shape) {
var displayList = new GLDisplayList();
displayList.Begin();
var firstDL = new InputStream(shape.DisplayList1);
firstDL.Seek(0x0C);
UInt32 vtxDesc1 = firstDL.ReadUInt32();
firstDL.Seek(0x12);
UInt32 vtxDesc2 = firstDL.ReadUInt32();
firstDL.Seek(0x22);
UInt32 vtxAttr1 = firstDL.ReadUInt32();
firstDL.Seek(0x28);
UInt32 vtxAttr2 = firstDL.ReadUInt32();
firstDL.Seek(0x2E);
UInt32 vtxAttr3 = firstDL.ReadUInt32();
var vs = new VertexSettings();
vs.SetDesc(vtxDesc1, vtxDesc2);
vs.SetAttrFmt(vtxAttr1, vtxAttr2, vtxAttr3);
InputStream dl = new InputStream(shape.DisplayList2);
while (true) {
if (dl.AtEnd)
break;
byte cmd = dl.ReadByte();
if (cmd == 0)
break;
if ((cmd & (int)GXCommand.DrawPrimitiveMask) != 0) {
PrimitiveType prim = (PrimitiveType)((cmd >> (byte)GXCommand.PrimitiveShiftAmount) & 7);
int vtxCount = dl.ReadUInt16();
if ((cmd & 7) != 0)
Console.WriteLine("WARNING!! Not using vertex attribute table 0");
// first, parse it into a list of vertices
GXIndexedVertex[] vtxs = new GXIndexedVertex[vtxCount];
for (int i = 0; i < vtxCount; i++) {
vtxs[i].LoadFrom(dl, vs);
}
switch (prim) {
case PrimitiveType.Quads:
GL.Begin(BeginMode.Quads);
break;
case PrimitiveType.Triangles:
GL.Begin(BeginMode.Triangles);
break;
case PrimitiveType.TriangleStrip:
GL.Begin(BeginMode.TriangleStrip);
break;
case PrimitiveType.TriangleFan:
GL.Begin(BeginMode.TriangleFan);
break;
case PrimitiveType.Lines:
GL.Begin(BeginMode.Lines);
break;
case PrimitiveType.LineStrip:
GL.Begin(BeginMode.LineStrip);
break;
case PrimitiveType.Points:
GL.Begin(BeginMode.Points);
break;
default:
Console.WriteLine("UNIMPLEMENTED PRIMITIVE TYPE {0}", prim);
throw new NotImplementedException();
}
for (int i = 0; i < vtxCount; i++) {
for (int j = 0; j < 8; j++) {
if (vs.TexCoordDesc[j] != VertexSettings.DescType.None) {
GL.MultiTexCoord2(TextureUnits[j], shape.TexCoordData[j].Data[vtxs[i].TexCoords[j]]);
}
}
if (vs.NormalDesc != VertexSettings.DescType.None)
GL.Normal3(shape.NrmData.Data[vtxs[i].Normal]);
GL.Vertex3(shape.PosData.Data[vtxs[i].Position]);
}
GL.End();
} else {
// some other command
switch ((GXCommand)cmd) {
case GXCommand.LoadPosMtxFromArray:
int targetID = dl.ReadUInt16();
int sourceID = (dl.ReadUInt16() & 0xFFF) / 12;
Console.WriteLine("Load PosMtx from array: {0} => {1}", targetID, sourceID);
break;
case GXCommand.LoadNrmMtxFromArray:
dl.Skip(4);
Console.WriteLine("UNIMPLEMENTED: Load NrmMtx from array");
break;
case GXCommand.LoadTexCoordMtxFromArray:
dl.Skip(4);
Console.WriteLine("UNIMPLEMENTED: Load TexCoordMtx from array");
break;
case GXCommand.LoadLightFromArray:
dl.Skip(4);
Console.WriteLine("UNIMPLEMENTED: Load Light from array");
break;
default:
Console.WriteLine("UNKNOWN DL COMMAND");
break;
}
}
}
displayList.End();
return displayList;
}
#endregion
}
}
|