summaryrefslogtreecommitdiff
path: root/NW4RTools/Models/VertexData.cs
blob: 5b1a50a1bbea86bc70430962d03435c243090dcd (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
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[] Data;

		public VertexDataBase() {
		}

		public float[] GetEntry(int index) {
			float[] ret = new float[GetRealCount()];

			float scale = 1.0f / (1 << Fraction);

			//Console.WriteLine("Getting index {0}, count {1} size {2} length {3} offset {4}", index, EntryCount, EntrySize, Data.Length, index * EntrySize);
			InputStream ins = new InputStream(Data);
			ins.Seek(index * EntrySize);

			for (int i = 0; i < ret.Length; i++) {
				switch (ComponentType) {
				case VertexSettings.CompType.UInt8:
					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;
					//Console.WriteLine("Output into {0}: {3:X} {1} with scale {2}", i, ret[i], scale, v);
					break;
				case VertexSettings.CompType.Float32:
					ret[i] = ins.ReadFloat();
					break;
				default:
					throw new NotImplementedException(String.Format("unimplemented type {0}", (int)ComponentType));
				}
			}

			return ret;
		}

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