30 #pragma pack( push, packing )
34 #define PATH_MAX _MAX_PATH
36 #elif defined( __GNUC__ )
37 #define PACK_STRUCT __attribute__((packed))
40 #error you must byte-align these structures with the appropriate compiler directives
45 typedef unsigned char byte;
46 typedef unsigned short word;
62 word m_vertexIndices[3];
63 float m_vertexNormals[3][3];
65 byte m_smoothingGroup;
70 static unsigned int Texture[15];
84 #pragma pack( pop, packing )
100 :
WObject( world, name, tm, false ) {
110 world->pushObject(
this );
115 for ( i = 0; i < m_numMeshes; i++ ) {
116 delete[] m_pMeshes[i].m_pTriangleIndices;
119 if ( m_pMeshes != NULL ) {
124 if ( m_pMaterials != NULL ) {
125 delete[] m_pMaterials;
129 if ( m_pTriangles != NULL ) {
130 delete[] m_pTriangles;
134 if ( m_pVertices != NULL ) {
135 delete[] m_pVertices;
145 QFile inputFile( filename );
146 if ( !inputFile.open( QIODevice::ReadOnly ) ) {
149 QByteArray bBuffer = inputFile.readAll();
150 const char *pPtr = bBuffer.data();
153 if ( strncmp( pHeader->m_ID,
"MS3D000000", 10 ) != 0 ) {
157 if ( pHeader->m_version < 3 ) {
161 int nVertices = *( word* )pPtr;
162 m_numVertices = nVertices;
163 m_pVertices =
new Vertex[nVertices];
164 pPtr +=
sizeof( word );
166 for ( i = 0; i < nVertices; i++ ) {
168 m_pVertices[i].m_boneID = pVertex->m_boneID;
169 memcpy( m_pVertices[i].m_location, pVertex->m_vertex,
sizeof(
float )*3 );
172 int nTriangles = *( word* )pPtr;
173 m_numTriangles = nTriangles;
174 m_pTriangles =
new Triangle[nTriangles];
175 pPtr +=
sizeof( word );
176 for ( i = 0; i < nTriangles; i++ ) {
178 int vertexIndices[3] = { pTriangle->m_vertexIndices[0], pTriangle->m_vertexIndices[1], pTriangle->m_vertexIndices[2] };
179 float t[3] = { 1.0f-pTriangle->m_t[0], 1.0f-pTriangle->m_t[1], 1.0f-pTriangle->m_t[2] };
180 memcpy( m_pTriangles[i].m_vertexNormals, pTriangle->m_vertexNormals,
sizeof(
float )*3*3 );
181 memcpy( m_pTriangles[i].m_s, pTriangle->m_s,
sizeof(
float )*3 );
182 memcpy( m_pTriangles[i].m_t, t,
sizeof(
float )*3 );
183 memcpy( m_pTriangles[i].m_vertexIndices, vertexIndices,
sizeof(
int )*3 );
186 int nGroups = *( word* )pPtr;
187 m_numMeshes = nGroups;
188 m_pMeshes =
new Mesh[nGroups];
189 pPtr +=
sizeof( word );
190 for ( i = 0; i < nGroups; i++ ) {
191 pPtr +=
sizeof( byte );
193 word nTriangles = *( word* )pPtr;
194 pPtr +=
sizeof( word );
195 int *pTriangleIndices =
new int[nTriangles];
196 for (
int j = 0; j < nTriangles; j++ ) {
197 pTriangleIndices[j] = *( word* )pPtr;
198 pPtr +=
sizeof( word );
200 char materialIndex = *(
char* )pPtr;
201 pPtr +=
sizeof( char );
202 m_pMeshes[i].m_materialIndex = materialIndex;
203 m_pMeshes[i].m_numTriangles = nTriangles;
204 m_pMeshes[i].m_pTriangleIndices = pTriangleIndices;
206 int nMaterials = *( word* )pPtr;
207 m_numMaterials = nMaterials;
208 m_pMaterials =
new Material[nMaterials];
209 pPtr +=
sizeof( word );
210 for ( i = 0; i < nMaterials; i++ ) {
212 memcpy( m_pMaterials[i].m_ambient, pMaterial->m_ambient,
sizeof(
float )*4 );
213 memcpy( m_pMaterials[i].m_diffuse, pMaterial->m_diffuse,
sizeof(
float )*4 );
214 memcpy( m_pMaterials[i].m_specular, pMaterial->m_specular,
sizeof(
float )*4 );
215 memcpy( m_pMaterials[i].m_emissive, pMaterial->m_emissive,
sizeof(
float )*4 );
216 m_pMaterials[i].m_shininess = pMaterial->m_shininess;
217 m_pMaterials[i].m_pTextureFilename = QString( pMaterial->m_texture );