Index: orxonox/branches/dave/src/object.cc
===================================================================
--- orxonox/branches/dave/src/object.cc	(revision 2860)
+++ orxonox/branches/dave/src/object.cc	(revision 3151)
@@ -119,5 +119,5 @@
 
 /**
-   \brief finalizes an Object.
+  \brief finalizes an Object.
    This funcion is needed, to close the glList and all the other lists.
 */
@@ -226,6 +226,7 @@
   if (verbose >= 2)
     printf("Adding new Group\n");
+  group->name = "";
   group->faceMode = -1;
-  group->name = "";
+  group->faceCount =0;  
   if ((group->listNumber = glGenLists(1)) == 0 )
     {
@@ -246,5 +247,6 @@
       group->firstVertexTexture = currentGroup->firstVertexTexture + currentGroup->vTexture->getCount()/2;
     }
-
+  if (verbose >=2)
+    printf ("Creating new Arrays, with starting points v:%i, vt:%i, vn:%i .\n", group->firstVertex, group->firstVertexTexture, group->firstNormal);
   group->vertices = new Array();
   group->normals = new Array();
@@ -256,9 +258,21 @@
 /**
    \brief finalizes a Group.
+   \param group the group to finalize.
 */
 bool Object::finalizeGroup(Group* group)
 {
+  if (verbose >=2)
+    printf ("Finalize group %s.\n", group->name);
   glEnd();
   glEndList();
+}
+/**
+   \brief deletes the Arrays of the Group to save space.
+   \param group the group to delete the arrays from.
+*/
+bool Object::cleanupGroup(Group* group)
+{
+  if (verbose >=2)
+    printf ("cleaning up group %s.\n", group->name);
   
   delete group->vertices;
@@ -266,4 +280,5 @@
   delete group->vTexture;
 }
+
 /**
    \brief Reads in the .obj File and sets all the Values.
@@ -282,8 +297,8 @@
     }
   objFileName = fileName;
-  char Buffer[500];
+  char Buffer[10000];
   while(!OBJ_FILE->eof())
     {
-      OBJ_FILE->getline(Buffer, 500);
+      OBJ_FILE->getline(Buffer, 10000);
       if (verbose >=4)
 	printf ("Read input line: %s\n",Buffer);
@@ -330,4 +345,5 @@
     }
   OBJ_FILE->close();
+  return true;
 
 }
@@ -363,17 +379,37 @@
     {
       currentGroup->vertices->finalizeArray();
-      glVertexPointer(3, GL_FLOAT, 0, currentGroup->vertices->getArray());
+      //      glVertexPointer(3, GL_FLOAT, 0, currentGroup->vertices->getArray());
       currentGroup->normals->finalizeArray();
-      glNormalPointer(GL_FLOAT, 0, currentGroup->normals->getArray());
+      //      glNormalPointer(GL_FLOAT, 0, currentGroup->normals->getArray());
       currentGroup->vTexture->finalizeArray();
     }
 
   readingVertices = false;
-  char subbuffer1[20];
-  char subbuffer2[20];
-  char subbuffer3[20];
-  char subbuffer4[20] ="";
-  sscanf (faceString, "%s %s %s %s", subbuffer1, subbuffer2, subbuffer3, subbuffer4);
-  if (!strcmp(subbuffer4, ""))
+  currentGroup->faceCount++;
+
+  int elemCount = 0;
+  
+  FaceElement* firstElem = new FaceElement;
+  FaceElement* tmpElem = firstElem;
+
+  
+  while(strcmp (faceString, "\0"))
+    {
+      if (elemCount>0)
+	  tmpElem = tmpElem->next = new FaceElement;
+      tmpElem->next = NULL;
+
+
+      sscanf (faceString, "%s", tmpElem->value);
+      faceString += strlen(tmpElem->value);
+      if (strcmp (faceString, "\0"))
+	faceString++;
+      elemCount++;
+
+
+    }
+  
+  
+  if (elemCount == 3)
     {
       if (currentGroup->faceMode != 3)
@@ -386,11 +422,8 @@
       currentGroup->faceMode = 3;
       if (verbose >=3)
-	printf ("found triag: %s, %s, %s\n", subbuffer1, subbuffer2, subbuffer3);
-      addGLElement(subbuffer1);
-      addGLElement(subbuffer2);
-      addGLElement(subbuffer3);
-      return true;
-    }
-  else
+	printf ("found triag.\n");
+    }
+  
+  else if (elemCount == 4)
     {
       if (currentGroup->faceMode != 4)
@@ -402,11 +435,25 @@
       currentGroup->faceMode = 4;
       if (verbose >=3 )
-	printf ("found quad: %s, %s, %s, %s\n", subbuffer1, subbuffer2, subbuffer3, subbuffer4);
-      addGLElement(subbuffer1);
-      addGLElement(subbuffer2);
-      addGLElement(subbuffer3);
-      addGLElement(subbuffer4);
-      return true;
-    }
+	printf ("found quad.\n");
+    }
+  
+  else if (elemCount > 4)
+    {
+      if (currentGroup->faceMode != -1)
+	glEnd();
+      glBegin(GL_POLYGON);
+      if (verbose >=3)
+	printf ("Polygon with %i faces found.", elemCount);
+      currentGroup->faceMode = elemCount;
+    }
+
+  tmpElem = firstElem;
+  while (tmpElem != NULL)
+    {
+      //      printf ("%s\n", tmpElem->value);
+      addGLElement(tmpElem->value);
+      tmpElem = tmpElem->next;
+    }
+
 }
 
@@ -423,21 +470,27 @@
 {
   if (verbose >=3)
-    printf ("importing grafical Element.... including to openGL\n");
+    printf ("importing grafical Element to openGL\n");
   char* vertex = elementString;
 
   char* texture;
-  texture = strstr (vertex, "/");
-  texture[0] = '\0';
-  texture ++;
-  glTexCoord2fv(currentGroup->vTexture->getArray()+(atoi(texture)-1 - currentGroup->firstVertexTexture)*2);
-
-  char* normal;
-  if ((normal = strstr (texture, "/")) !=NULL)
-    {
-      normal[0] = '\0';
-      normal ++;
-      //glArrayElement(atoi(vertex)-1);
-      glNormal3fv(currentGroup->normals->getArray() +(atoi(normal)-1 - currentGroup->firstNormal)*3);
-    }
+  if ((texture = strstr (vertex, "/")) != NULL)
+    {
+      texture[0] = '\0';
+      texture ++;
+      if (verbose>=3)
+	printf ("includeing texture #%i, and mapping it to group texture #%i, textureArray has %i entries.\n", atoi(texture), (atoi(texture)-1 - currentGroup->firstVertexTexture)*3, currentGroup->vTexture->getCount());
+      glTexCoord2fv(currentGroup->vTexture->getArray()+(atoi(texture)-1 - currentGroup->firstVertexTexture)*2);
+
+      char* normal;
+      if ((normal = strstr (texture, "/")) !=NULL)
+	{
+	  normal[0] = '\0';
+	  normal ++;
+	  //glArrayElement(atoi(vertex)-1);
+	  glNormal3fv(currentGroup->normals->getArray() +(atoi(normal)-1 - currentGroup->firstNormal)*3);
+	}
+    }
+  if (verbose>=3)
+    printf ("includeing vertex #%i, and mapping it to group vertex #%i, vertexArray has %i entries.\n", atoi(vertex), (atoi(vertex)-1 - currentGroup->firstVertex)*3, currentGroup->vertices->getCount());
   glVertex3fv(currentGroup->vertices->getArray() +(atoi(vertex)-1 - currentGroup->firstVertex)*3);
 
@@ -488,23 +541,22 @@
 bool Object::readGroup (char* groupString)
 {
-  //  printf ("test\n");
-  if (!strcmp(groupString, "default"))
-    {
-      if (groupCount != 0)
-	{
-	  Group* newGroup = new Group;
-	  finalizeGroup(currentGroup);
-	  currentGroup->nextGroup = newGroup;
-	  initGroup(newGroup);
-	  currentGroup = newGroup; // must be after init see initGroup for more info
-	}
-      ++groupCount;
-    }
-  else
-    {
-      
-      currentGroup->name = new char [strlen(groupString)];      
+  // setting the group name if not default.
+  if (strcmp(currentGroup->name, "default"))
+    {
+      currentGroup->name = (char*) malloc ( strlen(groupString) * sizeof (char));
       strcpy(currentGroup->name, groupString);
     }
+  if (groupCount != 0 && currentGroup->faceCount>0)
+    {
+      Group* newGroup = new Group;
+      finalizeGroup(currentGroup);
+      currentGroup->nextGroup = newGroup;
+      initGroup(newGroup);
+      cleanupGroup(currentGroup); // deletes the arrays of the group; must be after initGroup.
+      currentGroup = newGroup; // must be after init see initGroup for more info
+    }
+
+  ++groupCount;
+
 }
 
