Index: orxonox/branches/md2_loader/ChangeLog
===================================================================
--- orxonox/branches/md2_loader/ChangeLog	(revision 4170)
+++ orxonox/branches/md2_loader/ChangeLog	(revision 4172)
@@ -1,2 +1,6 @@
+2005-05-10	Benjamin Grauer <bensch@orxonox.ethz.ch>
+	Merged the gui into the executable
+	Windows compiles again.
+
 2005-04-27	orxonox
 	Tagged Version 0.2.3-pre-alpha
Index: orxonox/branches/md2_loader/src/defs/debug.h
===================================================================
--- orxonox/branches/md2_loader/src/defs/debug.h	(revision 4170)
+++ orxonox/branches/md2_loader/src/defs/debug.h	(revision 4172)
@@ -59,7 +59,7 @@
 #define DEBUG_MODULE_PNODE              0
 #define DEBUG_MODULE_WORLD_ENTITY       0
-#define DEBUG_MODULE_COMMAND_NODE       4
+#define DEBUG_MODULE_COMMAND_NODE       0
 #define DEBUG_MODULE_GRAPHICS           0
-#define DEBUG_MODULE_LOAD               2
+#define DEBUG_MODULE_LOAD               0
 
 #define DEBUG_MODULE_IMPORTER           2
Index: orxonox/branches/md2_loader/src/lib/graphics/importer/abstract_model.h
===================================================================
--- orxonox/branches/md2_loader/src/lib/graphics/importer/abstract_model.h	(revision 4170)
+++ orxonox/branches/md2_loader/src/lib/graphics/importer/abstract_model.h	(revision 4172)
@@ -30,12 +30,75 @@
 using namespace std;
 
-//template<class T> class tList;
-;
+
+
+//! this is a small and performant 3D vector
+typedef float sVec3D[3];
+
+//! small and performant 2D vector
+typedef float sVec2D[2];
+
+//! compressed vertex data: char insetead of float, the value will be expanded by the scale value. only for loading
+typedef struct
+{
+  byte v[3];
+  unsigned char lightNormalIndex;
+} sVertex;
+
+//! compressed texture offset data: coords scaled by the texture size. Only for loading
+typedef struct
+{
+  short s,t;
+} sTexCoor;
+
+
+//! holds tha informations about a md2 frame
+typedef struct
+{
+  sVec3D scale;                        //!< scales values of the model
+  sVec3D translate;                    //!< translates the model
+  char name[16];                       //!< frame name: something like "run32"
+  sVertex pVertices[1];                //!< first vertex of thes frame
+} sFrame;
+
+
+//! holds the information about a triangle
+typedef struct
+{
+  short indexToVertices[3];             //!< index to the verteces of the triangle
+  short indexToTexCoor[3];              //!< index to the texture coordinates
+} sTriangle;
+
+
+//! a md2 animation definition
+typedef struct
+{
+  int firstFrame;                       //!< first frame of the animation
+  int lastFrame;                        //!< last frame of the animation
+  int fps;                              //!< speed: number of frames per second
+} sAnim;
+
+//! animation state definition
+typedef struct
+{
+  int startFrame;
+  int endFrame;
+  int fps;
+
+  float localTime;
+  float lastTime;
+  float interpolationState;            //!< the state of the animation [0..1]
+  
+  int type;                            //!< animation type
+
+  int currentFrame;
+  int nextFrame;
+} sAnimState;
+
 
 //! This is our 3D point class.  CONFLICTING with Vector.cc
 class CVector3 
 {
-public:
-	float x, y, z;
+ public:
+  float x, y, z;
 };
 
@@ -43,6 +106,6 @@
 class CVector2 
 {
-public:
-	float x, y;
+ public:
+  float x, y;
 };
 
@@ -50,6 +113,6 @@
 struct tFace
 {
-	int vertIndex[3];			// indicies for the verts that make up this triangle
-	int coordIndex[3];			// indicies for the tex coords to texture this face
+  int vertIndex[3];			// indicies for the verts that make up this triangle
+  int coordIndex[3];			// indicies for the tex coords to texture this face
 };
 
@@ -57,12 +120,12 @@
 struct tMaterialInfo
 {
-	char  strName[255];			// The texture name
-	char  strFile[255];			// The texture file name (If this is set it's a texture map)
-	byte  color[3];	// The color of the object (R, G, B)
-	int   texureId;				// the texture ID
-	float uTile;				// u tiling of texture  
-	float vTile;				// v tiling of texture	
-	float uOffset;			    // u offset of texture
-	float vOffset;				// v offset of texture
+  char  strName[255];			// The texture name
+  char  strFile[255];			// The texture file name (If this is set it's a texture map)
+  byte  color[3];	// The color of the object (R, G, B)
+  int   texureId;				// the texture ID
+  float uTile;				// u tiling of texture  
+  float vTile;				// v tiling of texture	
+  float uOffset;			        // u offset of texture
+  float vOffset;				// v offset of texture
 } ;
 
@@ -70,14 +133,14 @@
 struct t3DObject 
 {
-	int  numOfVerts;			// The number of verts in the model
-	int  numOfFaces;			// The number of faces in the model
-	int  numTexVertex;			// The number of texture coordinates
-	int  materialID;			// The texture ID to use, which is the index into our texture array
-	bool bHasTexture;			// This is TRUE if there is a texture map for this object
-	char strName[255];			// The name of the object
-	CVector3  *pVerts;			// The object's vertices
-	CVector3  *pNormals;	         	// The object's normals
-	CVector2  *pTexVerts;	            	// The texture's UV coordinates
-	tFace *pFaces;				// The faces information of the object
+  int  numOfVerts;			// The number of verts in the model
+  int  numOfFaces;			// The number of faces in the model
+  int  numTexVertex;			// The number of texture coordinates
+  int  materialID;			// The texture ID to use, which is the index into our texture array
+  bool bHasTexture;			// This is TRUE if there is a texture map for this object
+  char strName[255];			// The name of the object
+  CVector3  *pVerts;			// The object's vertices
+  CVector3  *pNormals;	         	// The object's normals
+  CVector2  *pTexVerts;	            	// The texture's UV coordinates
+  tFace *pFaces;				// The faces information of the object
 };
 
@@ -85,7 +148,7 @@
 struct tAnimationInfo
 {
-	char strName[255];			// This stores the name of the animation (Jump, Pain, etc..)
-	int startFrame;				// This stores the first frame number for this animation
-	int endFrame;				// This stores the last frame number for this animation
+  char strName[255];			// This stores the name of the animation (Jump, Pain, etc..)
+  int startFrame;				// This stores the first frame number for this animation
+  int endFrame;				// This stores the last frame number for this animation
 };
 
@@ -93,12 +156,12 @@
 struct t3DModel 
 {
-	int numOfObjects;					// The number of objects in the model
-	int numOfMaterials;					// The number of materials for the model
-	int numOfAnimations;				// The number of animations in this model (NEW)
-	int currentAnim;					// The current index into pAnimations list (NEW)
-	int currentFrame;					// The current frame of the current animation (NEW)
-	vector<tAnimationInfo> animationList; // The list of animations (NEW)
-	vector<tMaterialInfo> materialList;	// The list of material information (Textures and colors)
-	vector<t3DObject> objectList;			// The object list for our model
+  int numOfObjects;					// The number of objects in the model
+  int numOfMaterials;					// The number of materials for the model
+  int numOfAnimations;				// The number of animations in this model (NEW)
+  int currentAnim;					// The current index into pAnimations list (NEW)
+  int currentFrame;					// The current frame of the current animation (NEW)
+  vector<tAnimationInfo> animationList; // The list of animations (NEW)
+  vector<tMaterialInfo> materialList;	// The list of material information (Textures and colors)
+  vector<t3DObject> objectList;			// The object list for our model
 };
 
Index: orxonox/branches/md2_loader/src/lib/graphics/importer/md2Model.cc
===================================================================
--- orxonox/branches/md2_loader/src/lib/graphics/importer/md2Model.cc	(revision 4170)
+++ orxonox/branches/md2_loader/src/lib/graphics/importer/md2Model.cc	(revision 4172)
@@ -16,4 +16,5 @@
 
 #include "md2Model.h"
+#include "material.h"
 
 #include <fstream>
@@ -107,8 +108,6 @@
     {
       pModel->currentFrame = nextFrame;
-      printf("changing frame\n");
       this->localTime = 0.0f;
     }
-  printf("t = %f\n", this->localTime);
   return (this->localTime / kAnimationSpeed);
 }
@@ -455,2 +454,186 @@
   if( this->pFrames) delete this->pFrames;
 }
+
+
+/********************************************************************************
+ *   MD2LOADER2                                                                 *
+ ********************************************************************************/
+
+MD2Model2::MD2Model2()
+{
+  this->pVertices = NULL;
+  this->pGLCommands = NULL;
+  this->pLightNormals = NULL; 
+
+  this->numFrames = 0;
+  this->numVertices = 0;
+  this->numGLCommands = 0;
+  
+  this->textureID = 0;
+  this->scaleFactor = 1.0f;
+
+  //this->setAnim(0);
+}
+
+
+MD2Model2::~MD2Model2()
+{
+  delete [] this->pVertices;
+  delete [] this->pGLCommands;
+  delete [] this->pLightNormals;
+}
+
+
+
+bool MD2Model2::loadModel(const char *fileName)
+{
+  FILE *pFile;                            //file stream
+  tMd2Header header;
+  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;
+    }
+  fread(&header, 1, sizeof(tMd2Header), pFile);
+  /* check for the header version: make sure its a md2 file :) */
+  if( unlikely(header.version != MD2_VERSION) && unlikely(header.ident != MD2_IDENT))
+    {
+      PRINTF(1)("Couldn't load file %s: invalid file format: stop loading\n", fileName);
+      return false;
+    }
+
+  /* got the data: map it to locals */
+  this->numFrames = header.numFrames;
+  this->numVertices = header.numVertices;
+  this->numGLCommands = 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 * header.frameSize];
+
+  /* read frame data from the file to a temp buffer */
+  fseek(pFile, header.offsetFrames, SEEK_SET);
+  fread(buffer, header.frameSize, this->numFrames, pFile);
+  /* read opengl commands */
+  fseek(pFile, header.offsetGlCommands, SEEK_SET);
+  fread(this->pGLCommands, sizeof(int), this->numGLCommands, pFile);
+
+  for(int i = 0; i < this->numFrames; ++i)
+    {
+      frame = (sFrame*)(buffer + 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[i][0] = (frame->pVertices[i].v[0] * frame->scale[0]) + frame->translate[0];
+	  pVertex[i][1] = (frame->pVertices[i].v[1] * frame->scale[1]) + frame->translate[1];
+	  pVertex[i][2] = (frame->pVertices[i].v[2] * frame->scale[2]) + frame->translate[2];	  
+
+	  pNormals[i] = frame->pVertices[i].lightNormalIndex;
+	}
+    }
+
+  delete [] buffer;
+  fclose(pFile);
+}
+
+
+bool MD2Model2::loadSkin(const char* fileName)
+{
+  this->material = new Material("md2ModelTest");
+  this->material->setDiffuseMap(fileName);
+  this->material->setIllum(3);
+  this->material->setAmbient(1.0, 1.0, 1.0);
+}
+
+
+void MD2Model2::draw()
+{
+  glPushMatrix();
+  /* rotate because id software uses another orientation then the openGL default */
+  /* \todo: rotate the axis already when loading... easy :) */
+  glRotatef(-90.0, 1.0, 0.0, 0.0);
+  glRotatef(-90.0, 0.0, 0.0, 1.0);
+
+  this->renderFrame();
+
+  glPopMatrix();
+}
+
+
+/**
+   \brief initializes an array of vert with the current frame scaled vertices
+
+   we won't use the pVertices array directly, since its much easier and we need
+   saving of data anyway
+*/
+void MD2Model2::interpolate(sVec3D* verticesList)
+{
+  for(int i = 0; i < this->numFrames; ++i)
+    {
+      verticesList[i][0] = this->pVertices[i + (this->numFrames * this->animationState.currentFrame)][0] * this->scaleFactor;
+      verticesList[i][1] = this->pVertices[i + (this->numFrames * this->animationState.currentFrame)][1] * this->scaleFactor;
+      verticesList[i][2] = this->pVertices[i + (this->numFrames * this->animationState.currentFrame)][2] * this->scaleFactor;
+    }
+}
+
+
+/* hhmmm... id used a very different way to do lightning...*/
+void MD2Model2::processLighting()
+{
+}
+
+
+void MD2Model2::renderFrame()
+{
+  static sVec3D verticesList[MD2_MAX_VERTICES]; /* performance: created only once in a lifetime */
+  int* pCommands = this->pGLCommands;
+
+  /* some face culling stuff */
+  glPushAttrib(GL_POLYGON_BIT);
+  glFrontFace(GL_CW);
+  glEnable(GL_CULL_FACE);
+  glCullFace(GL_BACK);
+     
+  this->interpolate(verticesList);
+  this->material->select();
+
+  /* draw the triangles */
+  /* \todo: take int i out of while loop */
+  while( int i = *(pCommands++)) /* strange looking while loop for maximum performance */
+    {
+      if( i < 0)
+	{
+	  glBegin(GL_TRIANGLE_FAN);
+	  i = -i;
+	}
+      else
+	{
+	  glBegin(GL_TRIANGLE_STRIP);
+	}
+
+      for(; i > 0; --i, pCommands += 3) /* down counting for loop, next 3 gl commands */
+	{
+	  /* for quake2 lighting */
+	  //float l = shadedots[this->pLightNormals[pCommands[2]]];
+	  //glColor3f(l * lcolor[0], l * lcolor[1], l * lcolor[2]);
+
+	  glTexCoord2f( ((float *)pCommands)[0], ((float *)pCommands)[1] );
+	  //glNormal3fv(anorms[this->pLightNormals[2]]);
+	  glVertex3fv(verticesList[pCommands[2]]);
+	}
+      glEnd();
+    }
+  glDisable(GL_CULL_FACE);
+  glPopAttrib();
+}
+
Index: orxonox/branches/md2_loader/src/lib/graphics/importer/md2Model.h
===================================================================
--- orxonox/branches/md2_loader/src/lib/graphics/importer/md2Model.h	(revision 4170)
+++ orxonox/branches/md2_loader/src/lib/graphics/importer/md2Model.h	(revision 4172)
@@ -25,4 +25,6 @@
 
 //! These are the needed defines for the max values when loading .MD2 files
+#define MD2_IDENT                       (('2'<<24) + ('P'<<16) + ('D'<<8) + 'I')
+#define MD2_VERSION                     8
 #define MD2_MAX_TRIANGLES		4096
 #define MD2_MAX_VERTICES		2048
@@ -31,4 +33,7 @@
 #define MD2_MAX_SKINS			32
 #define MD2_MAX_FRAMESIZE		(MD2_MAX_VERTICES * 4 + 128)
+
+#define NUM_VERTEX_NORMALS              162
+#define SHADEDOT_QUANT                  16
 
 //! This stores the speed of the animation between each key frame - currently conflicting with the animation framework
@@ -107,4 +112,5 @@
 typedef char tMd2Skin[64];
 
+
 //! This is a MD2 Model class
 class MD2Model : public AbstractModel {
@@ -137,4 +143,53 @@
 };
 
+/* forward definitions */
+class Material;
+
+//! This is a MD2 Model class
+class MD2Model2 : public AbstractModel {
+
+public:
+  MD2Model2();
+  virtual ~MD2Model2();
+  
+  bool loadModel(const char* filename);
+  bool loadSkin(const char* filename);
+  
+  void drawFrame(int frame);
+  void draw();
+  
+  void setAnim(int type);
+  void scaleModel(float scaleFactor) { this->scaleFactor = scaleFactor;}
+
+  void tick(float dtS);
+
+private:
+  void animate(float time);  
+  void processLighting();
+  void interpolate(sVec3D* verticesList);
+  void renderFrame();
+
+ public:
+  /* these variables are static, because they are all the same for every model */
+  static sVec3D anorms[NUM_VERTEX_NORMALS];
+  static float anormsDots[SHADEDOT_QUANT][256];
+  static sAnim animationList[21];
+
+ private:
+  int numFrames;
+  int numVertices;
+  int numGLCommands;
+
+  sVec3D* pVertices;
+  int* pGLCommands;
+  int* pLightNormals;
+
+  unsigned int textureID;
+  Material* material;
+  sAnimState animationState;
+  float scaleFactor;
+};
+
+
 //! A class that handles all of the loading code
 class MD2Loader : public BaseObject {
Index: orxonox/branches/md2_loader/src/world_entities/test_entity.cc
===================================================================
--- orxonox/branches/md2_loader/src/world_entities/test_entity.cc	(revision 4170)
+++ orxonox/branches/md2_loader/src/world_entities/test_entity.cc	(revision 4172)
@@ -28,5 +28,8 @@
 TestEntity::TestEntity () : WorldEntity()
 {  
-  this->md2Model = new MD2Model();
+  //this->md2Model = new MD2Model();
+
+  MD2Model2* model2 = new MD2Model2();
+  model2->loadModel("../data/models/tris.md2");
 
   this->material = new Material("Clown");
@@ -43,5 +46,5 @@
 void TestEntity::tick (float time) 
 {
-  this->md2Model->tick(time);
+  //this->md2Model->tick(time);
 }
 
@@ -68,7 +71,7 @@
 
   /* TESTGING TESTING TESTING */
-  this->material->select();
+  //this->material->select();
   //this->md2Model->draw(this->model);
-  this->md2Model->animate();
+  //this->md2Model->animate();
 
   glPopMatrix();
