blob: feb8eb6a9b895eaaf186fb6c6a845e850ea8df48 (
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
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
|
using System;
namespace NW4RTools.Models {
public abstract class VertexDataBase {
public UInt32 Index;
public VertexSettings.CompCount ComponentCount;
public VertexSettings.CompType ComponentType;
public byte Fraction /* Not used for colours */, EntrySize;
public UInt16 EntryCount;
// Todo, decode data when reading
public byte[] RawData;
public float[][] Data;
public VertexDataBase() {
}
public virtual float[] GetEntry(int index) {
float[] ret = new float[GetRealCount()];
float scale = 1.0f / (1 << Fraction);
InputStream ins = new InputStream(RawData);
ins.Seek(index * EntrySize);
for (int i = 0; i < ret.Length; i++) {
switch (ComponentType) {
case VertexSettings.CompType.UInt8:
ret[i] = (float)ins.ReadByte() * scale;
break;
case VertexSettings.CompType.Int8:
ret[i] = (float)ins.ReadSByte() * scale;
break;
case VertexSettings.CompType.UInt16:
ret[i] = (float)ins.ReadUInt16() * scale;
break;
case VertexSettings.CompType.Int16:
ret[i] = (float)ins.ReadInt16() * scale;
break;
case VertexSettings.CompType.Float32:
ret[i] = ins.ReadFloat();
break;
default:
throw new NotImplementedException(String.Format("unimplemented type {0}", (int)ComponentType));
}
}
return ret;
}
public void Parse() {
if (Data != null)
return;
Data = new float[EntryCount][];
for (int i = 0; i < EntryCount; i++) {
Data[i] = GetEntry(i);
}
}
public virtual void Save() {
int elementCount = Data[0].Length;
float scale = 1.0f / (1 << Fraction);
var output = new OutputStream();
switch (ComponentType) {
case VertexSettings.CompType.UInt8:
foreach (var array in Data) {
for (int i = 0; i < elementCount; i++) {
output.WriteByte((byte)(array[i] / scale));
}
}
break;
case VertexSettings.CompType.Int8:
foreach (var array in Data) {
for (int i = 0; i < elementCount; i++) {
output.WriteSByte((sbyte)(array[i] / scale));
}
}
break;
case VertexSettings.CompType.UInt16:
foreach (var array in Data) {
for (int i = 0; i < elementCount; i++) {
output.WriteUInt16((ushort)(array[i] / scale));
}
}
break;
case VertexSettings.CompType.Int16:
foreach (var array in Data) {
for (int i = 0; i < elementCount; i++) {
output.WriteInt16((short)(array[i] / scale));
}
}
break;
case VertexSettings.CompType.Float32:
foreach (var array in Data) {
for (int i = 0; i < elementCount; i++) {
output.WriteFloat(array[i]);
}
}
break;
default:
throw new NotImplementedException(String.Format("unimplemented type {0}", (int)ComponentType));
}
RawData = output.GetBuffer();
}
public abstract int GetRealCount();
}
public class VertexPosData : VertexDataBase {
public Vec3 Minimum, Maximum;
public VertexPosData() {
}
public override int GetRealCount() {
switch (ComponentCount) {
case VertexSettings.CompCount.Position2:
return 2;
case VertexSettings.CompCount.Position3:
return 3;
default:
return -1;
}
}
}
public class VertexNrmData : VertexDataBase {
public VertexNrmData() {
}
public override int GetRealCount() {
switch (ComponentCount) {
case VertexSettings.CompCount.Normal3:
return 3;
default:
return -1;
}
}
}
public class VertexClrData : VertexDataBase {
public VertexClrData() {
}
public override int GetRealCount() {
switch (ComponentCount) {
case VertexSettings.CompCount.Color3:
return 3;
case VertexSettings.CompCount.Color4:
return 4;
default:
return -1;
}
}
public override float[] GetEntry(int index) {
float[] ret = new float[GetRealCount()];
InputStream ins = new InputStream(RawData);
ins.Seek(index * EntrySize);
byte r, g, b, a;
// todo: better solution instead of this cast
switch ((VertexSettings.CompClrType)ComponentType) {
case VertexSettings.CompClrType.RGB565:
ushort v565 = ins.ReadUInt16();
r = (byte)((v565 & 0xF800) >> 8);
g = (byte)((v565 & 0x07E0) >> 3);
b = (byte)((v565 & 0x001F) << 3);
a = 255;
break;
case VertexSettings.CompClrType.RGB8:
r = ins.ReadByte();
g = ins.ReadByte();
b = ins.ReadByte();
a = 255;
break;
case VertexSettings.CompClrType.RGBX8:
r = ins.ReadByte();
g = ins.ReadByte();
b = ins.ReadByte();
a = 255;
ins.Skip(1);
break;
case VertexSettings.CompClrType.RGBA4:
ushort vA4 = ins.ReadUInt16();
r = (byte)((vA4 & 0xF000) >> 8);
g = (byte)((vA4 & 0x0F00) >> 4);
b = (byte)(vA4 & 0x00F0);
a = (byte)((vA4 & 0x000F) << 4);
break;
case VertexSettings.CompClrType.RGBA6:
byte v1 = ins.ReadByte();
byte v2 = ins.ReadByte();
byte v3 = ins.ReadByte();
r = (byte)(v1 & 0xF8);
g = (byte)(((v1 & 7) << 6) | ((v2 & 0xF0) >> 2));
b = (byte)(((v2 & 0xF) << 4) | ((v3 & 0xC) >> 4));
a = (byte)((v3 & 0x3F) << 2);
break;
case VertexSettings.CompClrType.RGBA8:
r = ins.ReadByte();
g = ins.ReadByte();
b = ins.ReadByte();
a = ins.ReadByte();
break;
default:
throw new NotImplementedException(String.Format("unimplemented type {0}", (int)ComponentType));
}
// this is the easiest way I can think of to handle this atm
// just use floats for EVERYTHING
ret[0] = r * (1.0f / 256);
ret[1] = g * (1.0f / 256);
ret[2] = b * (1.0f / 256);
if (ret.Length > 3)
ret[3] = a * (1.0f / 256);
return ret;
}
public override void Save() {
throw new NotImplementedException();
}
}
public class VertexTexCoordData : VertexDataBase {
public Vec2 Minimum, Maximum;
public VertexTexCoordData() {
}
public override int GetRealCount() {
switch (ComponentCount) {
case VertexSettings.CompCount.TexCoord1:
return 1;
case VertexSettings.CompCount.TexCoord2:
return 2;
default:
return -1;
}
}
}
}
|