Index: orxonox/trunk/src/lib/graphics/importer/md2Model.cc
===================================================================
--- orxonox/trunk/src/lib/graphics/importer/md2Model.cc	(revision 4279)
+++ orxonox/trunk/src/lib/graphics/importer/md2Model.cc	(revision 4280)
@@ -21,5 +21,4 @@
 
 #include <fstream>
-
 
 
@@ -74,4 +73,5 @@
 MD2Model::MD2Model()
 {
+  /*
   this->pVertices = NULL;
   this->pGLCommands = NULL;
@@ -83,4 +83,8 @@
   
   this->scaleFactor = 1.0f;
+  */
+
+  /* this creates the data container via ressource manager */
+  this->data = new MD2Data();
 
   this->setAnim(BOOM);
@@ -90,7 +94,9 @@
 MD2Model::~MD2Model()
 {
+  /*
   delete [] this->pVertices;
   delete [] this->pGLCommands;
   delete [] this->pLightNormals;
+  */
 }
 
@@ -99,4 +105,7 @@
 bool MD2Model::loadModel(const char* fileName)
 {
+  return this->data->loadModel(fileName);
+  
+
   FILE *pFile;                            //file stream
   char* buffer;                           //buffer for frame data
@@ -164,4 +173,6 @@
 bool MD2Model::loadSkin(const char* fileName)
 {
+  return this->data->loadSkin(fileName);
+
   this->skinFileName = new char[strlen(fileName)+1];
   strcpy(this->skinFileName, fileName);
@@ -214,33 +225,4 @@
 
 
-void MD2Model::animate()
-{
-  if( this->animationState.localTime - this->animationState.lastTime > (1.0f / this->animationState.fps))
-    {
-      this->animationState.currentFrame = this->animationState.nextFrame;
-      this->animationState.nextFrame++;
-      
-      if( this->animationState.nextFrame > this->animationState.endFrame)
-	this->animationState.nextFrame = this->animationState.startFrame;
-      this->animationState.lastTime = this->animationState.localTime;
-    }
-
-  if( this->animationState.currentFrame > (this->numFrames - 1) )
-    this->animationState.currentFrame = 0;
-  if( this->animationState.nextFrame > (this->numFrames - 1) )
-    this->animationState.nextFrame = 0;
-
-  this->animationState.interpolationState = this->animationState.fps * 
-    (this->animationState.localTime - this->animationState.lastTime);
-}
-
-
-/* hhmmm... id used a very different way to do lightning... */
-void MD2Model::processLighting()
-{
-  shadeDots = anormsDots[((int)(md2Angle*(SHADEDOT_QUANT / 360.0)))&(SHADEDOT_QUANT - 1)];
-}
-
-
 void MD2Model::tick(float time)
 {
@@ -303,4 +285,33 @@
 
 
+void MD2Model::animate()
+{
+  if( this->animationState.localTime - this->animationState.lastTime > (1.0f / this->animationState.fps))
+    {
+      this->animationState.currentFrame = this->animationState.nextFrame;
+      this->animationState.nextFrame++;
+      
+      if( this->animationState.nextFrame > this->animationState.endFrame)
+	this->animationState.nextFrame = this->animationState.startFrame;
+      this->animationState.lastTime = this->animationState.localTime;
+    }
+
+  if( this->animationState.currentFrame > (this->numFrames - 1) )
+    this->animationState.currentFrame = 0;
+  if( this->animationState.nextFrame > (this->numFrames - 1) )
+    this->animationState.nextFrame = 0;
+
+  this->animationState.interpolationState = this->animationState.fps * 
+    (this->animationState.localTime - this->animationState.lastTime);
+}
+
+
+/* hhmmm... id used a very different way to do lightning... */
+void MD2Model::processLighting()
+{
+  shadeDots = anormsDots[((int)(md2Angle*(SHADEDOT_QUANT / 360.0)))&(SHADEDOT_QUANT - 1)];
+}
+
+
 void MD2Model::debug()
 {
@@ -314,2 +325,104 @@
   PRINT(0)("===================================================\n\n");
 }
+
+
+
+
+
+MD2Data::MD2Data()
+{
+  this->pVertices = NULL;
+  this->pGLCommands = NULL;
+  this->pLightNormals = NULL; 
+
+  this->numFrames = 0;
+  this->numVertices = 0;
+  this->numGLCommands = 0;
+  
+  this->scaleFactor = 1.0f;
+}
+
+
+MD2Data::~MD2Data()
+{
+  delete [] this->pVertices;
+  delete [] this->pGLCommands;
+  delete [] this->pLightNormals;
+}
+
+
+
+bool MD2Data::loadModel(const char* fileName)
+{
+  FILE *pFile;                            //file stream
+  char* buffer;                           //buffer for frame data
+  sFrame* frame;                          //temp frame
+  sVec3D *pVertex;
+  int* pNormals;
+
+  pFile = fopen(fileName, "rb");
+  if( unlikely(!pFile)) 
+    {
+      PRINTF(1)("Couldn't open the MD2 File for loading. Exiting.\n");
+      return false;
+    }
+  this->header = new MD2Header;
+  fread(this->header, 1, sizeof(MD2Header), pFile);
+  /* check for the header version: make sure its a md2 file :) */
+  if( unlikely(this->header->version != MD2_VERSION) && unlikely(this->header->ident != MD2_IDENT))
+    {
+      PRINTF(1)("Couldn't load file %s: invalid file format: stop loading\n", fileName);
+      return false;
+    }
+
+  this->fileName = new char[strlen(fileName)+1];
+  strcpy(this->fileName, fileName);
+  /* got the data: map it to locals */
+  this->numFrames = this->header->numFrames;
+  this->numVertices = this->header->numVertices;
+  this->numTriangles = this->header->numTriangles;
+  this->numGLCommands = this->header->numGlCommands;
+  /* allocate memory for the data storage */
+  this->pVertices = new sVec3D[this->numVertices * this->numFrames];
+  this->pGLCommands = new int[this->numGLCommands];
+  this->pLightNormals = new int[this->numVertices * this->numFrames];
+  buffer = new char[this->numFrames * this->header->frameSize];
+
+  /* read frame data from the file to a temp buffer */
+  fseek(pFile, this->header->offsetFrames, SEEK_SET);
+  fread(buffer, this->header->frameSize, this->numFrames, pFile);
+  /* read opengl commands */
+  fseek(pFile, this->header->offsetGlCommands, SEEK_SET);
+  fread(this->pGLCommands, sizeof(int), this->numGLCommands, pFile);
+
+  for(int i = 0; i < this->numFrames; ++i)
+    {
+      frame = (sFrame*)(buffer + this->header->frameSize * i); 
+      pVertex = this->pVertices + this->numVertices  * i;
+      pNormals = this->pLightNormals + this->numVertices * i;
+
+      for(int j = 0; j < this->numVertices; ++j)
+	{
+	  /* SPEEDUP: *(pVerts + i + 0) = (*(frame->pVertices + i + 0)...  */
+	  pVertex[j][0] = (frame->pVertices[j].v[0] * frame->scale[0]) + frame->translate[0];
+	  pVertex[j][1] = (frame->pVertices[j].v[2] * frame->scale[2]) + frame->translate[2];
+	  pVertex[j][2] = -1.0 * (frame->pVertices[j].v[1] * frame->scale[1] + frame->translate[1]);
+	  
+	  pNormals[j] = frame->pVertices[j].lightNormalIndex;
+	}
+    }
+
+  delete [] buffer;
+  fclose(pFile);
+}
+
+
+bool MD2Data::loadSkin(const char* fileName)
+{
+  this->skinFileName = new char[strlen(fileName)+1];
+  strcpy(this->skinFileName, fileName);
+  this->material = new Material("md2ModelTest");
+  this->material->setDiffuseMap(fileName);
+  this->material->setIllum(3);
+  this->material->setAmbient(1.0, 1.0, 1.0);
+}
