2 Copyright (C) 2001-2006, William Joseph.
5 This file is part of GtkRadiant.
7 GtkRadiant is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 GtkRadiant is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GtkRadiant; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24 #include "ifilesystem.h"
28 #include "bytestreamutils.h"
32 #include "md3normals.h"
35 const unsigned char MDC_IDENT[4] = { 'I', 'D', 'P', 'C', };
36 const float MDC_XYZ_SCALE = 0.015625f;
37 #define MAX_QPATH 64 // max length of a quake game pathname
39 typedef float float3[3];
43 unsigned int indexes[3]; // not my spelling
46 void istream_read_mdcTriangle(PointerInputStream& inputStream, mdcTriangle_t& triangle)
48 triangle.indexes[0] = istream_read_uint32_le(inputStream);
49 triangle.indexes[1] = istream_read_uint32_le(inputStream);
50 triangle.indexes[2] = istream_read_uint32_le(inputStream);
55 short xyz[3]; // divide by 64
56 short normal; // packed same way as md3
59 void istream_read_mdcXyzNormal(PointerInputStream& inputStream, mdcXyzNormal_t& xyz)
61 xyz.xyz[0] = istream_read_int16_le(inputStream);
62 xyz.xyz[1] = istream_read_int16_le(inputStream);
63 xyz.xyz[2] = istream_read_int16_le(inputStream);
64 xyz.normal = istream_read_int16_le(inputStream);
69 float st[2]; // may need to reverse t
72 void istream_read_mdcSt(PointerInputStream& inputStream, mdcSt_t& st)
74 st.st[0] = istream_read_float32_le(inputStream);
75 st.st[1] = istream_read_float32_le(inputStream);
84 void istream_read_mdcShader(PointerInputStream& inputStream, mdcShader_t& shader)
86 inputStream.read(reinterpret_cast<byte*>(shader.name), MAX_QPATH);
87 shader.flags = istream_read_uint32_le(inputStream);
97 short xyz[3]; // divide by 64
98 short angles[3]; // euler in z x y order... deg = * (360.0 / 32767.0) .. rad = * (PI / 32767.0)
113 char name[MAX_QPATH];
115 unsigned int numCompFrames;
116 unsigned int numBaseFrames;
117 unsigned int numShaders;
118 unsigned int numVerts;
119 unsigned int numTriangles;
120 unsigned int ofsTriangles;
121 unsigned int ofsShaders;
123 unsigned int ofsXyzNormals;
124 unsigned int ofsCompVerts;
125 unsigned int ofsFrameBaseFrames;
126 unsigned int ofsFrameCompFrames;
130 void istream_read_mdcSurface(PointerInputStream& inputStream, mdcSurface_t& surface)
132 inputStream.read(surface.ident, 4);
133 inputStream.read(reinterpret_cast<byte*>(surface.name), MAX_QPATH);
134 surface.flags = istream_read_uint32_le(inputStream);
135 surface.numCompFrames = istream_read_uint32_le(inputStream);
136 surface.numBaseFrames = istream_read_uint32_le(inputStream);
137 surface.numShaders = istream_read_uint32_le(inputStream);
138 surface.numVerts = istream_read_uint32_le(inputStream);
139 surface.numTriangles = istream_read_uint32_le(inputStream);
140 surface.ofsTriangles = istream_read_uint32_le(inputStream);
141 surface.ofsShaders = istream_read_uint32_le(inputStream);
142 surface.ofsSt = istream_read_uint32_le(inputStream);
143 surface.ofsXyzNormals = istream_read_uint32_le(inputStream);
144 surface.ofsCompVerts = istream_read_uint32_le(inputStream);
145 surface.ofsFrameBaseFrames = istream_read_uint32_le(inputStream);
146 surface.ofsFrameCompFrames = istream_read_uint32_le(inputStream);
147 surface.ofsEnd = istream_read_uint32_le(inputStream);
153 unsigned int version;
154 char name[MAX_QPATH];
156 unsigned int numFrames;
157 unsigned int numTags;
158 unsigned int numSurfaces;
159 unsigned int numSkins;
160 unsigned int ofsFrames;
161 unsigned int ofsTagNames;
162 unsigned int ofsTags;
163 unsigned int ofsSurfaces;
167 void istream_read_mdcHeader(PointerInputStream& inputStream, mdcHeader_t& header)
169 inputStream.read(header.ident, 4);
170 header.version = istream_read_uint32_le(inputStream);
171 inputStream.read(reinterpret_cast<byte*>(header.name), MAX_QPATH);
172 header.flags = istream_read_uint32_le(inputStream);
173 header.numFrames = istream_read_uint32_le(inputStream);
174 header.numTags = istream_read_uint32_le(inputStream);
175 header.numSurfaces = istream_read_uint32_le(inputStream);
176 header.numSkins = istream_read_uint32_le(inputStream);
177 header.ofsFrames = istream_read_uint32_le(inputStream);
178 header.ofsTagNames = istream_read_uint32_le(inputStream);
179 header.ofsTags = istream_read_uint32_le(inputStream);
180 header.ofsSurfaces = istream_read_uint32_le(inputStream);
181 header.ofsEnd = istream_read_uint32_le(inputStream);
184 unsigned int MDCSurface_read(Surface& surface, const byte* buffer)
186 mdcSurface_t mdcSurface;
188 PointerInputStream inputStream(buffer);
189 istream_read_mdcSurface(inputStream, mdcSurface);
193 surface.vertices().reserve(mdcSurface.numVerts);
195 PointerInputStream xyzStream(buffer + mdcSurface.ofsXyzNormals);
196 PointerInputStream stStream(buffer + mdcSurface.ofsSt);
197 // read verts into vertex array - xyz, st, normal
198 for(std::size_t i = 0; i < mdcSurface.numVerts; i++)
200 mdcXyzNormal_t mdcXyzNormal;
201 istream_read_mdcXyzNormal(xyzStream, mdcXyzNormal);
203 istream_read_mdcSt(stStream, mdcSt);
205 surface.vertices().push_back(
207 Vertex3f( mdcXyzNormal.xyz[0] * MDC_XYZ_SCALE, mdcXyzNormal.xyz[1] * MDC_XYZ_SCALE, mdcXyzNormal.xyz[2] * MDC_XYZ_SCALE),
208 DecodeNormal(reinterpret_cast<byte*>(&mdcXyzNormal.normal)),
209 TexCoord2f(mdcSt.st[0], mdcSt.st[1])
216 surface.indices().reserve(mdcSurface.numTriangles * 3);
218 PointerInputStream triangleStream(buffer + mdcSurface.ofsTriangles);
220 for(std::size_t i = 0; i < mdcSurface.numTriangles; i++)
222 mdcTriangle_t triangle;
223 istream_read_mdcTriangle(triangleStream, triangle);
224 surface.indices().insert(triangle.indexes[0]);
225 surface.indices().insert(triangle.indexes[1]);
226 surface.indices().insert(triangle.indexes[2]);
232 PointerInputStream inputStream(buffer + mdcSurface.ofsShaders);
233 istream_read_mdcShader(inputStream, shader);
234 surface.setShader(shader.name);
237 surface.updateAABB();
239 return mdcSurface.ofsEnd;
242 void MDCModel_read(Model& model, const byte* buffer)
246 PointerInputStream inputStream(buffer);
247 istream_read_mdcHeader(inputStream, header);
250 const byte* surfacePosition = buffer + header.ofsSurfaces;
252 for(std::size_t i = 0; i < header.numSurfaces; i++)
254 surfacePosition += MDCSurface_read(model.newSurface(), surfacePosition);
260 scene::Node& MDCModel_new(const byte* buffer)
262 ModelNode* modelNode = new ModelNode();
263 MDCModel_read(modelNode->model(), buffer);
264 return modelNode->node();
267 scene::Node& MDCModel_default()
269 ModelNode* modelNode = new ModelNode();
270 Model_constructNull(modelNode->model());
271 return modelNode->node();
274 scene::Node& MDCModel_fromBuffer(unsigned char* buffer)
276 if (!ident_equal(buffer, MDC_IDENT))
278 globalErrorStream() << "MDC read error: incorrect ident\n";
279 return MDCModel_default();
283 return MDCModel_new(buffer);
287 scene::Node& loadMDCModel(ArchiveFile& file)
289 ScopedArchiveBuffer buffer(file);
290 return MDCModel_fromBuffer(buffer.buffer);