49 LoaderMD2(): verbose(false) {}
52 MD2_MAX_TRIANGLES = 4096,
53 MD2_MAX_VERTICES = 2048,
54 MD2_MAX_TEXCOORDS = 2048,
57 MD2_MAX_FRAMESIZE = MD2_MAX_VERTICES * 4 + 128
85 unsigned char vertex[3];
86 unsigned char light_norm_index;
89 class md2_triangle_info
108 md2_vertex_info vertices[1];
114 unsigned char name[64];
117 md2_header_info header;
118 std::vector<md2_frame_info*> md2_frame;
119 std::vector<md2_uv_info> md2_uv;
120 std::vector<md2_triangle_info> md2_triangle;
121 std::vector<md2_skin_info> md2_skin;
152 file->
read( &loader.header,
sizeof(loader.header) );
156 Log::print(
Say(
"tris %n:\n") << loader.header.numTriangles);
157 Log::print(
Say(
"verts %n:\n") << loader.header.numVertices);
159 Log::print(
Say(
"offs skins %n:\n") << loader.header.offsetSkins);
160 Log::print(
Say(
"offs end %n:\n") << loader.header.offsetEnd);
161 Log::print(
Say(
"offs frames %n:\n") << loader.header.offsetFrames);
162 Log::print(
Say(
"offs gl comm %n:\n") << loader.header.offsetGlCommands);
163 Log::print(
Say(
"offs tex coor %n:\n") << loader.header.offsetTexCoords);
164 Log::print(
Say(
"offs tri %n:\n") << loader.header.offsetTriangles);
170 file->
seekSet(loader.header.offsetFrames);
171 loader.md2_frame.resize(loader.header.numFrames);
172 for(
unsigned i=0; i<loader.md2_frame.size(); ++i)
174 loader.md2_frame[i] = (LoaderMD2::md2_frame_info*)malloc(loader.header.frameSize *
sizeof(
char) );
175 file->
read(loader.md2_frame[i], loader.header.frameSize);
179 loader.md2_uv.resize(loader.header.numTexCoords);
180 file->
seekSet( loader.header.offsetTexCoords );
181 file->
read(&loader.md2_uv[0], loader.header.numTexCoords*
sizeof(LoaderMD2::md2_uv_info) );
184 loader.md2_triangle.resize(loader.header.numTriangles);
185 file->
seekSet(loader.header.offsetTriangles );
186 file->
read(&loader.md2_triangle[0], loader.header.numTriangles*
sizeof(LoaderMD2::md2_triangle_info) );
189 if (loader.header.numSkins)
191 loader.md2_skin.resize(loader.header.numSkins);
192 file->
seekSet( loader.header.offsetSkins );
193 file->
read(&loader.md2_skin[0], loader.header.numSkins*
sizeof(LoaderMD2::md2_skin_info) );
201 std::vector< ref<ArrayFloat3> > vertex_frames;
202 std::vector< ref<ArrayFloat3> > normal_frames;
205 for(
int i=0; i<loader.header.numFrames; ++i)
208 vertex_frames[i]->resize( 3 * loader.header.numTriangles );
214 tex_coords->
resize( 3 * loader.header.numTriangles );
220 VL_CHECK( (
int)loader.md2_triangle.size() == loader.header.numTriangles )
221 for(
int itri=0; itri<loader.header.numTriangles; itri++)
223 for(
int ivert=3; ivert--; ++vert_idx )
226 float u = (float)loader.md2_uv[ loader.md2_triangle[itri].uv_idx[ivert] ].u / loader.header.skinWidth;
227 float v = 1.0f - (
float)loader.md2_uv[ loader.md2_triangle[itri].uv_idx[ivert] ].v / loader.header.skinHeight;
228 tex_coords->
at(vert_idx) =
fvec2(u, v);
231 for(
int iframe=0; iframe<loader.header.numFrames; iframe++)
234 vec.
x() = loader.md2_frame[iframe]->vertices[ loader.md2_triangle[itri].vert_idx[ivert] ].vertex[0] * loader.md2_frame[iframe]->scale[0] + loader.md2_frame[iframe]->translate[0];
235 vec.
y() = loader.md2_frame[iframe]->vertices[ loader.md2_triangle[itri].vert_idx[ivert] ].vertex[2] * loader.md2_frame[iframe]->scale[2] + loader.md2_frame[iframe]->translate[2];
236 vec.
z() = -1 *(loader.md2_frame[iframe]->vertices[ loader.md2_triangle[itri].vert_idx[ivert] ].vertex[1] * loader.md2_frame[iframe]->scale[1] + loader.md2_frame[iframe]->translate[1]);
237 vertex_frames[iframe]->at( vert_idx ) = vec;
245 for(
int iframe=0; iframe<loader.header.numFrames; iframe++)
246 free(loader.md2_frame[iframe]);
260 polygons = cast<DrawElementsUInt>(geometry->
drawCalls().at(0));
VL_CHECK(polygons);
263 for(
int iframe=1; iframe<loader.header.numFrames; ++iframe)
266 new_vertex_frame->
resize( vertex_frames[0]->size() );
268 for(
size_t ivert=0; ivert<vertex_frames[iframe]->size(); ++ivert)
271 new_vertex_frame->
at( remover.
mapOldToNew()[ivert] ) = vertex_frames[iframe]->at(ivert);
273 vertex_frames[iframe] = new_vertex_frame;
277 normal_frames.resize( loader.header.numFrames );
278 for(
int iframe=0; iframe<loader.header.numFrames; iframe++)
282 normal_frames[iframe] = cast<ArrayFloat3>(geometry->
normalArray());
VL_CHECK(normal_frames[iframe]);
286 for(
unsigned i=0; i<vertex_frames.size(); ++i)
288 vertex_frames[i]->setObjectName(
"vertex_frame");
289 res_db->
resources().push_back(vertex_frames[i].
get());
292 for(
unsigned i=0; i<normal_frames.size(); ++i)
294 normal_frames[i]->setObjectName(
"normal_frame");
295 res_db->
resources().push_back(normal_frames[i].
get());
long long read(void *buffer, long long byte_count)
Reads byte_count bytes from a file. Returns the number of bytes actually read.
VLCORE_EXPORT FileSystem * defFileSystem()
Returns the default FileSystem used by VisualizationLibrary.
An abstract class representing a file.
A simple String formatting class.
The String class implements an advanced UTF16 (Unicode BMP) string manipulation engine.
const T_Scalar & z() const
const ArrayAbstract * vertexArray() const
Conventional vertex array.
static void error(const String &message)
Use this function to provide information about run-time errors: file not found, out of memory...
void setVertexArray(ArrayAbstract *data)
Conventional vertex array.
VLGRAPHICS_EXPORT ref< ResourceDatabase > loadMD2(const String &path)
Removes from a Geometry the vertices with the same attributes.
void removeDoubles(Geometry *geom)
virtual ref< VirtualFile > locateFile(const String &full_path, const String &alternate_path=String()) const
Looks for a VirtualFile on the disk and in the currently active FileSystem.
const std::vector< u32 > & mapOldToNew() const
void setNormalArray(ArrayAbstract *data)
Conventional normal array.
virtual void close()=0
Closes the file.
const String & path() const
Returns the path of the file.
size_t size() const
Returns the number of elements of an array.
The Geometry class is a Renderable that implements a polygonal mesh made of polygons, lines and points.
Visualization Library main namespace.
Vector2< float > fvec2
A 2 components vector with float precision.
const std::vector< ref< Object > > & resources() const
static void print(const String &message)
Application message for the user.
const T_Scalar & y() const
virtual bool open(EOpenMode mode)=0
Opens the file in the specified mode.
void computeNormals(bool verbose=false)
Computes the normals in a "smooth" way, i.e.
void setTexCoordArray(int tex_unit, ArrayAbstract *data)
Conventional texture coords arrays.
arr_type * indexBuffer()
The BufferObject containing the indices used to render.
bool seekSet(long long offset)
Changes the current read/write position of a file.
T_VectorType & at(size_t i)
const T_Scalar & x() const
The ref<> class is used to reference-count an Object.
const ArrayAbstract * texCoordArray(int tex_unit) const
Conventional texture coords arrays.
const ArrayAbstract * normalArray() const
Conventional normal array.
The ResourceDatabase class contains and manipulates a set of resources.
Collection< DrawCall > & drawCalls()
Returns the list of DrawCall objects bound to a Geometry.