iCub-main
MS3D.cpp
Go to the documentation of this file.
1 // -*- mode:C++; tab-width:4; c-basic-offset:4; indent-tabs-mode:nil -*-
2 
3 /*
4 * Copyright (C) 2010 RobotCub Consortium, European Commission FP6 Project IST-004370
5 * Author: Ronny André Reierstad, Vadim Tikhanoff
6 * email: vadim.tikhanoff@iit.it
7 * website: www.robotcub.org
8 * Permission is granted to copy, distribute, and/or modify this program
9 * under the terms of the GNU General Public License, version 2 or any
10 * later version published by the Free Software Foundation.
11 *
12 * A copy of the license can be found at
13 * http://www.robotcub.org/icub/license/gpl.txt
14 *
15 * This program is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
18 * Public License for more details
19 */
28 #include "MS3D.h"
29 
30 using namespace std;
31 
33 // The Model Class
36 {
37  m_numMeshes = 0;
38  m_pMeshes = NULL;
39  m_numMaterials = 0;
40  m_pMaterials = NULL;
41  m_numTriangles = 0;
42  m_pTriangles = NULL;
43  m_numVertices = 0;
44  m_pVertices = NULL;
45 }
46 
48 {
49  int i;
50  for ( i = 0; i < m_numMeshes; i++ )
51  delete[] m_pMeshes[i].m_pTriangleIndices;
52  for ( i = 0; i < m_numMaterials; i++ )
53  delete[] m_pMaterials[i].m_pTextureFilename;
54 
55  m_numMeshes = 0;
56  if ( m_pMeshes != NULL )
57  {
58  delete[] m_pMeshes;
59  m_pMeshes = NULL;
60  }
61 
62  m_numMaterials = 0;
63  if ( m_pMaterials != NULL )
64  {
65  delete[] m_pMaterials;
66  m_pMaterials = NULL;
67  }
68 
69  m_numTriangles = 0;
70  if ( m_pTriangles != NULL )
71  {
72  delete[] m_pTriangles;
73  m_pTriangles = NULL;
74  }
75 
76  m_numVertices = 0;
77  if ( m_pVertices != NULL )
78  {
79  delete[] m_pVertices;
80  m_pVertices = NULL;
81  }
82 }
83 
84 
85 
86 bool Model::loadModelData( const char *filename )
87 {
88  ifstream inputFile( filename, ios::in | ios::binary );//| ios::nocreate );
89  if ( inputFile.fail())
90  return false; // "Couldn't open the model file."
91 
92  char pathTemp[PATH_MAX+1];
93  int pathLength;
94  for ( pathLength = strlen( filename ); pathLength--; ) {
95  if ( filename[pathLength] == '/' || filename[pathLength] == '\\' ) {
96  break;
97  }
98  }
99  strncpy( pathTemp, filename, pathLength );
100 
101  inputFile.seekg( 0, ios::end );
102  long fileSize = (long)inputFile.tellg();
103  inputFile.seekg( 0, ios::beg );
104 
105  char *pBuffer = new char[fileSize];
106  inputFile.read( pBuffer, fileSize );
107  inputFile.close();
108 
109  const char *pPtr = pBuffer;
110  MS3DHeader *pHeader = ( MS3DHeader* )pPtr;
111  pPtr += sizeof( MS3DHeader );
112 
113  if ( strncmp( pHeader->m_ID, "MS3D000000", 10 ) != 0 )
114  return false; // "Not a valid Milkshape3D model file."
115 
116  if ( pHeader->m_version < 3 )
117  return false; // "Unhandled file version. Only Milkshape3D Version 1.3 and 1.4 is supported." );
118 
119  int nVertices = *( ms3d::word* )pPtr;
120  m_numVertices = nVertices;
121  m_pVertices = new Vertex[nVertices];
122  pPtr += sizeof( ms3d::word );
123 
124  int i;
125  for ( i = 0; i < nVertices; i++ )
126  {
127  MS3DVertex *pVertex = ( MS3DVertex* )pPtr;
128  m_pVertices[i].m_boneID = pVertex->m_boneID;
129  memcpy( m_pVertices[i].m_location, pVertex->m_vertex, sizeof( float )*3 );
130  pPtr += sizeof( MS3DVertex );
131  }
132 
133  int nTriangles = *( ms3d::word* )pPtr;
134  m_numTriangles = nTriangles;
135  m_pTriangles = new Triangle[nTriangles];
136  pPtr += sizeof( ms3d::word );
137 
138  for ( i = 0; i < nTriangles; i++ )
139  {
140  MS3DTriangle *pTriangle = ( MS3DTriangle* )pPtr;
141  int vertexIndices[3] = { pTriangle->m_vertexIndices[0], pTriangle->m_vertexIndices[1], pTriangle->m_vertexIndices[2] };
142  float t[3] = { 1.0f-pTriangle->m_t[0], 1.0f-pTriangle->m_t[1], 1.0f-pTriangle->m_t[2] };
143  memcpy( m_pTriangles[i].m_vertexNormals, pTriangle->m_vertexNormals, sizeof( float )*3*3 );
144  memcpy( m_pTriangles[i].m_s, pTriangle->m_s, sizeof( float )*3 );
145  memcpy( m_pTriangles[i].m_t, t, sizeof( float )*3 );
146  memcpy( m_pTriangles[i].m_vertexIndices, vertexIndices, sizeof( int )*3 );
147  pPtr += sizeof( MS3DTriangle );
148  }
149 
150 
151 
152  int nGroups = *( ms3d::word* )pPtr;
153  m_numMeshes = nGroups;
154  m_pMeshes = new Mesh[nGroups];
155  pPtr += sizeof( ms3d::word );
156  for ( i = 0; i < nGroups; i++ )
157  {
158  pPtr += sizeof( ms3d::byte ); // flags
159  pPtr += 32; // name
160 
161  ms3d::word nTriangles = *( ms3d::word* )pPtr;
162  pPtr += sizeof( ms3d::word );
163  int *pTriangleIndices = new int[nTriangles];
164  for ( int j = 0; j < nTriangles; j++ )
165  {
166  pTriangleIndices[j] = *( ms3d::word* )pPtr;
167  pPtr += sizeof( ms3d::word );
168  }
169 
170  char materialIndex = *( char* )pPtr;
171  pPtr += sizeof( char );
172 
173  m_pMeshes[i].m_materialIndex = materialIndex;
174  m_pMeshes[i].m_numTriangles = nTriangles;
175  m_pMeshes[i].m_pTriangleIndices = pTriangleIndices;
176  }
177 
178  int nMaterials = *( ms3d::word* )pPtr;
179  m_numMaterials = nMaterials;
180  m_pMaterials = new Material[nMaterials];
181  pPtr += sizeof( ms3d::word );
182  for ( i = 0; i < nMaterials; i++ )
183  {
184  MS3DMaterial *pMaterial = ( MS3DMaterial* )pPtr;
185  memcpy( m_pMaterials[i].m_ambient, pMaterial->m_ambient, sizeof( float )*4 );
186  memcpy( m_pMaterials[i].m_diffuse, pMaterial->m_diffuse, sizeof( float )*4 );
187  memcpy( m_pMaterials[i].m_specular, pMaterial->m_specular, sizeof( float )*4 );
188  memcpy( m_pMaterials[i].m_emissive, pMaterial->m_emissive, sizeof( float )*4 );
189  m_pMaterials[i].m_shininess = pMaterial->m_shininess;
190  if ( strncmp( pMaterial->m_texture, ".\\", 2 ) == 0 ) {
191  // MS3D 1.5.x relative path
192  strcpy( pathTemp + pathLength, pMaterial->m_texture + 1 );
193  m_pMaterials[i].m_pTextureFilename = new char[strlen( pathTemp )+1];
194  strcpy( m_pMaterials[i].m_pTextureFilename, pathTemp );
195  }
196  else {
197  // MS3D 1.4.x or earlier - absolute path
198  m_pMaterials[i].m_pTextureFilename = new char[strlen( pMaterial->m_texture )+1];
199  strcpy( m_pMaterials[i].m_pTextureFilename, pMaterial->m_texture );
200  }
201  pPtr += sizeof( MS3DMaterial );
202  }
203 
204  reloadTextures();
205 
206  delete[] pBuffer;
207 
208  return true;
209 }
210 
211 
212 
213 void Model::draw(bool wireframed, int TextureNumber)
214 {
215  GLboolean texEnabled = glIsEnabled( GL_TEXTURE_2D );
216  static bool once=true;
217  if (once){
218  reloadTextures();
219  once = false;
220  }
221  for ( int i = 0; i < m_numMeshes; i++ ) // draw in groups
222  {
223  int materialIndex = m_pMeshes[i].m_materialIndex;
224  if ( materialIndex >= 0 )
225  {
226  //glMaterialfv( GL_FRONT, GL_AMBIENT, m_pMaterials[materialIndex].m_ambient );
227  //glMaterialfv( GL_FRONT, GL_DIFFUSE, m_pMaterials[materialIndex].m_diffuse );
228  //glMaterialfv( GL_FRONT, GL_SPECULAR, m_pMaterials[materialIndex].m_specular );
229  //glMaterialfv( GL_FRONT, GL_EMISSION, m_pMaterials[materialIndex].m_emissive );
230  //glMaterialf( GL_FRONT, GL_SHININESS, m_pMaterials[materialIndex].m_shininess );
231 
232  // 1 Aug 08 - bind texture 0 as well
233  //if ( m_pMaterials[materialIndex].m_texture > 0 )
234  //{
235  glBindTexture( GL_TEXTURE_2D, Texture[TextureNumber]);
236  glEnable( GL_TEXTURE_2D );
237  //}
238  //else
239  // glDisable( GL_TEXTURE_2D );
240  }
241  else
242  {
243  glDisable( GL_TEXTURE_2D );
244  }
245 
246  if (wireframed) glBegin( GL_LINES );
247  else glBegin( GL_TRIANGLES );
248  {
249  for ( int j = 0; j < m_pMeshes[i].m_numTriangles; j++ )
250  {
251  int triangleIndex = m_pMeshes[i].m_pTriangleIndices[j];
252  const Triangle* pTri = &m_pTriangles[triangleIndex];
253 
254  for ( int k = 0; k < 3; k++ )
255  {
256  int index = pTri->m_vertexIndices[k];
257 
258  glNormal3fv( pTri->m_vertexNormals[k] );
259  glTexCoord2f( pTri->m_s[k], pTri->m_t[k] );
260  glVertex3fv( m_pVertices[index].m_location );
261  }
262  }
263  }
264  glEnd();
265  }
266 
267  if ( texEnabled )
268  glEnable( GL_TEXTURE_2D );
269  else
270  glDisable( GL_TEXTURE_2D );
271 
272  glDisable( GL_TEXTURE_2D );
273 }
274 
275 
276 
278 {
279  for ( int i = 0; i < m_numMaterials; i++ )
280  {
281  if ( strlen( m_pMaterials[i].m_pTextureFilename ) > 0 )
282  {
283  //m_pMaterials[i].m_pTextureFilename[strlen(m_pMaterials[i].m_pTextureFilename)-3]='r';
284  //m_pMaterials[i].m_pTextureFilename[strlen(m_pMaterials[i].m_pTextureFilename)-2]='a';
285  // m_pMaterials[i].m_pTextureFilename[strlen(m_pMaterials[i].m_pTextureFilename)-1]='w';
286 
287  //yDebug()<<m_pMaterials[i].m_pTextureFilename;
288  //m_pMaterials[i].m_texture = LoadTextureRAW(m_pMaterials[i].m_pTextureFilename,0);
289  }
290  else
291  {
292  m_pMaterials[i].m_texture = 0;
293  }
294  }
295 }
296 
297 //Ronny André Reierstad
298 //www.morrowland.com
299 //apron@morrowland.com
Triangle::m_vertexNormals
float m_vertexNormals[3][3]
Definition: MS3D.h:141
Triangle
Definition: MS3D.h:139
Texture
unsigned int Texture[200]
Definition: rendering.cpp:53
MS3DHeader
Definition: MS3D.h:78
ms3d::word
unsigned short word
Definition: MS3D.h:75
ms3d::byte
unsigned char byte
Definition: MS3D.h:74
Model::loadModelData
bool loadModelData(const char *filename)
Definition: MS3D.cpp:86
MS3DVertex::m_boneID
char m_boneID
Definition: MS3D.h:88
MS3DTriangle::m_t
float m_t[3]
Definition: MS3D.h:97
Mesh
Definition: MS3D.h:124
Triangle::m_s
float m_s[3]
Definition: MS3D.h:142
MS3DTriangle::m_vertexNormals
float m_vertexNormals[3][3]
Definition: MS3D.h:96
Model::Model
Model()
Definition: MS3D.cpp:35
MS3DTriangle::m_s
float m_s[3]
Definition: MS3D.h:97
Vertex
Definition: MS3D.h:146
Model::~Model
virtual ~Model()
Definition: MS3D.cpp:47
Model::draw
void draw(bool wireframed, int TextureNumber)
Definition: MS3D.cpp:213
MS3DVertex
Definition: MS3D.h:84
MS3DMaterial::m_diffuse
float m_diffuse[4]
Definition: MS3D.h:108
Material
Definition: MS3D.h:131
Mesh::m_materialIndex
int m_materialIndex
Definition: MS3D.h:126
MS3DMaterial::m_ambient
float m_ambient[4]
Definition: MS3D.h:107
MS3DTriangle
Definition: MS3D.h:92
MS3DMaterial::m_texture
char m_texture[128]
Definition: MS3D.h:114
Triangle::m_vertexIndices
int m_vertexIndices[3]
Definition: MS3D.h:143
MS3DMaterial::m_specular
float m_specular[4]
Definition: MS3D.h:109
MS3DMaterial::m_emissive
float m_emissive[4]
Definition: MS3D.h:110
Triangle::m_t
float m_t[3]
Definition: MS3D.h:142
MS3DHeader::m_ID
char m_ID[10]
Definition: MS3D.h:80
MS3D.h
This file deals with the loading of 3D objects into the simulator. To be changed by a more generic me...
MS3DHeader::m_version
int m_version
Definition: MS3D.h:81
MS3DMaterial
Definition: MS3D.h:102
Model::reloadTextures
void reloadTextures()
Definition: MS3D.cpp:277
MS3DMaterial::m_shininess
float m_shininess
Definition: MS3D.h:111
MS3DVertex::m_vertex
float m_vertex[3]
Definition: MS3D.h:87
MS3DTriangle::m_vertexIndices
ms3d::word m_vertexIndices[3]
Definition: MS3D.h:95