summaryrefslogtreecommitdiff
path: root/NW4RTools/ObjWriter.cs
blob: 592347b4ddfa52db4e1b73210bc662df0f552d38 (plain)
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
using System;
using System.IO;
using System.Collections.Generic;
using NW4RTools.Models;

namespace NW4RTools {
	public class ObjWriter {
		public static void WriteModel(TextWriter tw, ResFile file, string modelName) {
			new ObjWriter(file).SaveModel(tw, modelName);
		}



		ResFile CurrentFile;
		Models.Model CurrentModel;
		TextWriter Output;

		private ObjWriter(ResFile file) {
			CurrentFile = file;
		}

		Dictionary<Models.VertexPosData, int> VtxPosOffsets;
		Dictionary<Models.VertexNrmData, int> VtxNrmOffsets;
		Dictionary<Models.VertexTexCoordData, int> VtxTexCoordOffsets;

		public void SaveModel(TextWriter tw, string modelName) {
			Output = tw;
			CurrentModel = CurrentFile.GetGroup<Model>("3DModels(NW4R)")[modelName];

			Output.WriteLine("# {0} exported by Treeki's NW4RTools", modelName);
			Output.WriteLine("# {0}", DateTime.Now);
			Output.WriteLine();

			// Write vertex data pool
			int Offset;
			
			VtxPosOffsets = new Dictionary<VertexPosData, int>();
			Offset = 1;
			foreach (var kv in CurrentModel.VtxPosData) {
				VtxPosOffsets[kv.Value] = Offset;
				Output.WriteLine("# Vertex Positions: {0} [offset {1}]", kv.Key, Offset);
				
				for (int i = 0; i < kv.Value.EntryCount; i++) {
					float[] v = kv.Value.GetEntry(i);
					Output.WriteLine("v {0} {1} {2}", v[0], v[1], v[2]);
				}
				
				Offset += kv.Value.EntryCount;
			}
			
			VtxNrmOffsets = new Dictionary<VertexNrmData, int>();
			Offset = 1;
			foreach (var kv in CurrentModel.VtxNrmData) {
				VtxNrmOffsets[kv.Value] = Offset;
				Output.WriteLine("# Vertex Normals: {0} [offset {1}]", kv.Key, Offset);
				
				for (int i = 0; i < kv.Value.EntryCount; i++) {
					float[] v = kv.Value.GetEntry(i);
					Output.WriteLine("vn {0} {1} {2}", v[0], v[1], v[2]);
				}
				
				Offset += kv.Value.EntryCount;
			}
			
			VtxTexCoordOffsets = new Dictionary<VertexTexCoordData, int>();
			Offset = 1;
			foreach (var kv in CurrentModel.VtxTexCoordData) {
				VtxTexCoordOffsets[kv.Value] = Offset;
				Output.WriteLine("# Vertex TexCoords: {0} [offset {1}]", kv.Key, Offset);
				
				for (int i = 0; i < kv.Value.EntryCount; i++) {
					float[] v = kv.Value.GetEntry(i);
					Output.WriteLine("vt {0} {1}", v[0], v[1]);
				}
				
				Offset += kv.Value.EntryCount;
			}

			Output.WriteLine();

			// Write shapes
			// TODO: replace with something using the Bytecode
			foreach (var kv in CurrentModel.Shapes) {
				Output.WriteLine("g {0}", kv.Key);

				WriteShape(kv.Value);

				Output.WriteLine();
			}

			Output.Flush();
		}


		private void WriteShape(Models.Shape shape) {
			// first, parse the first DisplayList to get the attr info
			// for now we'll hardcode the offsets. must be fixed later

			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);

			// get the Matrix to use
			// todo: how to apply this?!
			Matrix m = CurrentModel.Nodes[CurrentModel.MatrixIDtoNodeID[shape.MatrixID]].NodeMatrix;

			// now parse the second DisplayList

			int posOffset = VtxPosOffsets[shape.PosData];
			int nrmOffset = shape.NrmData == null ? -1 : VtxNrmOffsets[shape.NrmData];
			int tcOffset = shape.TexCoordData[0] == null ? -1 : VtxTexCoordOffsets[shape.TexCoordData[0]];
			// TODO: Better DisplayList parsing, in a similar fashion to ByteCode
			var dl = new InputStream(shape.DisplayList2);

			while (true) {
				if (dl.AtEnd)
					break;

				byte cmd = dl.ReadByte();
				if (cmd == 0)
					break;

				PrimitiveType prim = (PrimitiveType)((cmd >> 3) & 7);
				int vtxCount = dl.ReadUInt16();
				Output.WriteLine("# Primitive: {0} ({1} vertices)", prim, vtxCount);

				// first, parse it into a list of vertices
				GXIndexedVertex[] vtxs = new GXIndexedVertex[vtxCount];
				string[] pVtxs = new string[vtxCount];

				for (int i = 0; i < vtxCount; i++) {
					vtxs[i].LoadFrom(dl, vs);

					string tc = (vtxs[i].TexCoords[0] == -1) ? "" : (tcOffset + vtxs[i].TexCoords[0]).ToString();
					string n = (vtxs[i].Normal == -1) ? "" : (nrmOffset + vtxs[i].Normal).ToString();
					pVtxs[i] = String.Format(" {0}/{1}/{2}", posOffset + vtxs[i].Position, tc, n);
				}

				switch (prim) {
				case PrimitiveType.Triangles:
					for (int i = 0; i < vtxCount; i += 3) {
						Output.WriteLine("f {0} {1} {2}", pVtxs[i], pVtxs[i + 1], pVtxs[i + 2]);
					}

					break;

				case PrimitiveType.TriangleStrip:
					// De-stripify it!
					for (int i = 2; i < vtxCount; i++) {
						Output.Write("f");
						if ((i & 1) == 0) {
							// Even number
							Output.Write(pVtxs[i - 2]);
							Output.Write(pVtxs[i - 1]);
							Output.Write(pVtxs[i]);
						} else {
							// Odd number
							Output.Write(pVtxs[i - 1]);
							Output.Write(pVtxs[i - 2]);
							Output.Write(pVtxs[i]);
						}
						Output.WriteLine();
					}

					break;

				default:
					Output.WriteLine("# UNIMPLEMENTED");
					return;
				}
			}
		}
	}
}