Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Mar 1, 2005, 9:50:30 AM (19 years ago)
Author:
bensch
Message:

orxonox/branches/trackManager: merged trunk back to trackManager
merged with command
svn merge -r 3369:HEAD trunk/ branches/trackManager
resoloved conflicts in favor of the trunk.

Location:
orxonox/branches/trackManager
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • orxonox/branches/trackManager

    • Property svn:externals set to
  • orxonox/branches/trackManager/src/importer/model.cc

    r3369 r3430  
    1515
    1616#include "model.h"
    17 int verbose = 1; //! \todo should be GLOBAL
     17
     18#include <math.h>
     19#include <stdarg.h>
     20
     21#include "array.h"
     22#include "../vector.h"
     23
    1824using namespace std;
    1925
    2026/**
    21    \brief Creates a 3D-Model, but does not load any 3D-models.
    22 
    23    This Constructor is pretty useless, because why load no model in an model-loader??
    24 */
    25 Model::Model ()
    26 {
    27 
     27   \brief Creates a 3D-Model.
     28
     29   This only initializes a 3D-Model, but does not cleanup the Faces.
     30*/
     31Model::Model(void)
     32{
    2833  this->initialize();
    29 
    30   this->BoxModel();
    31 
     34}
     35
     36/**
     37   \brief Creates a 3D-Model of Primitive-Type type
     38
     39   if you want to just display a Cube/Sphere/Cylinder/... without any material.
     40   
     41   \todo implement Cube/Sphere/Cylinder/...
     42*/
     43Model::Model(PRIMITIVE type)
     44{
     45  this->initialize();
     46
     47  switch (type)
     48    {
     49    default:
     50    case CUBE:
     51      this->cubeModel();
     52      break;
     53    case SPHERE:
     54      this->sphereModel();
     55      break;
     56    case CYLINDER:
     57      this->cylinderModel();
     58      break;
     59
     60    }
    3261  this->importToGL ();
    3362
     
    3665
    3766/**
    38    \brief Crates a 3D-Model and loads in a File.
    39    \param fileName file to parse and load (must be a .obj file)
    40 */
    41 Model::Model(char* fileName)
     67   \brief Creates a 3D-Model. and assigns it a Name.
     68*/
     69Model::Model(char* modelName)
    4270{
    4371  this->initialize();
    44 
    45   this->importFile (fileName);
    46 
    47   this->importToGL ();
    48 
    49   this->cleanup();
    50 }
    51 
    52 /**
    53    \brief Crates a 3D-Model, loads in a File and scales it.
    54    \param fileName file to parse and load (must be a .obj file)
    55    \param scaling The factor that the model will be scaled with.
    56 */
    57 Model::Model(char* fileName, float scaling)
    58 {
    59   this->initialize();
    60   this->scaleFactor = scaling;
    61 
    62   this->importFile (fileName);
    63 
    64   this->importToGL ();
    65 
    66   this->cleanup();
     72  this->setName(modelName);
    6773}
    6874
     
    7278   Looks if any from model allocated space is still in use, and if so deleted it.
    7379*/
    74 Model::~Model()
    75 {
    76   PRINTF(2)("Deleting display Lists.\n");
     80Model::~Model(void)
     81{
     82  PRINTF(3)("Deleting Model ");
     83  if (this->name)
     84    {
     85      PRINT(3)("%s\n", this->name);
     86      delete []this->name;
     87    }
     88  else
     89      PRINT(3)("\n");
     90
     91  PRINTF(3)("Deleting display Lists.\n");
    7792  Group* walker = this->firstGroup;
    7893  while (walker != NULL)
     
    8499    }
    85100
    86   if (this->objPath)
    87     delete []this->objPath;
    88   if (this->objFileName)
    89     delete []this->objFileName;
    90   if (this->mtlFileName)
    91     delete []this->mtlFileName;
    92   PRINTF(2)("Deleting Materials.\n");
     101  PRINTF(3)("Deleting Materials.\n");
    93102  if (this->material)
    94103    delete this->material;
    95104}
    96105
     106/**
     107   \brief Finalizes an Object. This can be done outside of the Class.
     108*/
     109void Model::finalize(void)
     110{
     111  this->importToGL ();
     112 
     113  this->cleanup();
     114
     115  this->finalized = true;
     116}
    97117
    98118/**
     
    139159      walker = walker->next;
    140160    }
    141   PRINTF(1)("Model number %i in %s not Found.\n", groupNumber, this->objFileName);
     161  PRINTF(1)("Model number %i in %s not Found.\n", groupNumber, this->name);
    142162  return;
    143163
     
    164184      walker = walker->next;
    165185    }
    166   PRINTF(1)("Model Named %s in %s not Found.\n", groupName, this->objFileName);
     186  PRINTF(1)("Model Named %s in %s not Found.\n", groupName, this->name);
    167187  return;
    168188}
     
    186206  PRINTF(2)("new 3D-Model is being created\n");
    187207
     208  this->name = NULL;
     209  this->finalized = false;
    188210  // setting the start group;
    189211  this->firstGroup = new Group;
     
    192214 
    193215  this->initGroup (this->currentGroup);
    194   this->objPath = NULL;
    195   this->objFileName = NULL;
    196   this->mtlFileName = NULL;
    197216  this->scaleFactor = 1;
    198217  this->material = new Material();
     
    205224}
    206225
     226void Model::setName(const char* name)
     227{
     228  if (this->name)
     229    delete this->name;
     230  this->name = new char[strlen(name)+1];
     231  strcpy(this->name, name);
     232}
    207233/**
    208234   \brief initializes a new Group model
     
    317343
    318344/**
    319    \brief Imports a obj file and handles the the relative location
    320    \param fileName The file to import
    321 */
    322 bool Model::importFile (char* fileName)
    323 {
    324   PRINTF(3)("preparing to read in file: %s\n", fileName);
    325 
    326 
    327 #ifdef __WIN32__
    328   // win32 path reading
    329   char pathSplitter= '\\';
    330 #else /* __WIN32__ */
    331   // unix path reading
    332   char pathSplitter='/';
    333 #endif /* __WIN32__ */
    334   char* tmpName = fileName;
    335   if (tmpName[0] == pathSplitter)
    336     tmpName++;
    337   char* name = tmpName;
    338   while (( tmpName = strchr (tmpName+1, pathSplitter)))
    339     {
    340       name = tmpName+1;
    341     }
    342   this->objPath = new char[name-fileName];
    343   strncpy(this->objPath, fileName, name-fileName);
    344   this->objPath[name-fileName] = '\0';
    345   if (verbose >=2)
    346     if (strlen(objPath)> 0)
    347       PRINTF(0)("Resolved file %s to folder: %s.\n", name, objPath);
    348     else
    349       PRINTF(0)("Resolved file %s.\n", name);
    350  
    351   if (this->material)
    352     this->material->addTexturePath(this->objPath);
    353   this->objFileName = new char[strlen(name)+1];
    354   strcpy (this->objFileName, name);
    355   this->readFromObjFile ();
    356   return true;
    357 }
    358 
    359 /**
    360    \brief Reads in the .obj File and sets all the Values.
    361    This function does read the file, parses it for the occurence of things like vertices, faces and so on, and executes the specific tasks
    362 */
    363 bool Model::readFromObjFile (void)
    364 {
    365   char* fileName = new char [strlen(objPath)+strlen(objFileName)+1];
    366   if (this->objFileName != NULL && !strcmp(this->objFileName, ""))
    367     return false;
    368   strcpy(fileName, this->objPath);
    369   strcat(fileName, this->objFileName);
    370 
    371   ifstream* OBJ_FILE = new ifstream(fileName);
    372   if (OBJ_FILE->fail())
    373     {
    374       PRINTF(1)("unable to open .OBJ file: %s\n Loading Box Model instead.\n", fileName);
    375       BoxModel();
    376       OBJ_FILE->close();
    377       delete []fileName;
    378       delete OBJ_FILE;
    379       return false;
    380     }
    381   PRINTF(2)("Reading from opened file %s\n", fileName);
    382   char Buffer[10000];
    383   while(!OBJ_FILE->eof())
    384     {
    385       OBJ_FILE->getline(Buffer, 10000);
    386       PRINTF(3)("Read input line: %s\n", Buffer);
    387      
    388 
    389       // case vertice
    390       if (!strncmp(Buffer, "v ", 2))
    391         {
    392           this->readVertex(Buffer+2);
    393         }
    394 
    395       // case face
    396       else if (!strncmp(Buffer, "f ", 2))
    397         {
    398           this->readFace (Buffer+2);
    399         }
    400      
    401       else if (!strncmp(Buffer, "mtllib ", 7))
    402         {
    403           this->readMtlLib (Buffer+7);
    404         }
    405 
    406       else if (!strncmp(Buffer, "usemtl ", 7))
    407         {
    408           this->readUseMtl (Buffer+7);
    409         }
    410 
    411       // case VertexNormal
    412       else if (!strncmp(Buffer, "vn ", 3))
    413         {
    414           this->readVertexNormal(Buffer+3);
    415         }
    416      
    417       // case VertexTextureCoordinate
    418       else if (!strncmp(Buffer, "vt ", 3))
    419         {
    420           this->readVertexTexture(Buffer+3);
    421         }
    422       // case group
    423       else if (!strncmp(Buffer, "g ", 2))
    424         {
    425           this->readGroup (Buffer+2);
    426         }
    427       else if (!strncmp(Buffer, "s ", 2)) //! \todo smoothing groups have to be implemented
    428         {
    429           if (verbose >= 2)
    430             PRINTF(2)("smoothing groups not supportet yet. line: %s\n", Buffer);
    431         }
    432     }
    433   OBJ_FILE->close();
    434   delete OBJ_FILE;
    435   delete []fileName;
    436   return true;
    437 
    438 }
    439 
    440 /**
    441345   \brief parses a group String
    442346   \param groupString the new Group to create
     
    445349   With it you should be able to import .obj-files with more than one Models inside.
    446350*/
    447 bool Model::readGroup (char* groupString)
     351bool Model::addGroup (char* groupString)
    448352{
    449353  PRINTF(3)("Read Group: %s.\n", groupString);
     
    470374   If a vertex line is found this function will inject it into the vertex-Array
    471375*/
    472 bool Model::readVertex (char* vertexString)
     376bool Model::addVertex (char* vertexString)
    473377{
    474378  float subbuffer1;
     
    482386
    483387/**
     388   \brief parses a vertex-String
     389   \param x the X-coordinate of the Vertex to add.
     390   \param y the Y-coordinate of the Vertex to add.
     391   \param z the Z-coordinate of the Vertex to add.
     392   
     393*/
     394bool Model::addVertex(const float x, const float y, const float z)
     395{
     396  PRINTF(4)("reading in a vertex: %f %f %f\n", x, y, z);
     397  this->vertices->addEntry(x*scaleFactor, y*scaleFactor, z*scaleFactor);
     398  return true;
     399}
     400
     401/**
    484402   \brief parses a face-string
    485403   \param faceString The String that will be parsed.
    486404
    487405   If a face line is found this function will add it to the glList.
    488    The function makes a difference between QUADS and TRIANGLES, and will if changed re-open, set and re-close the gl-processe.
    489 */
    490 bool Model::readFace (char* faceString)
     406*/
     407bool Model::addFace (char* faceString)
    491408{
    492409  if (this->currentGroup->faceCount >0)
     
    546463
    547464/**
     465   \brief adds a new Face
     466   \param faceElemCount the number of Vertices to add to the Face.
     467   \param type 0: vertex only, 1: vertex and normal, 2: vertex and Texture, 3 vertex, normal and texture
     468*/
     469bool Model::addFace(const float faceElemCount, int type, ...)
     470{
     471   if (this->currentGroup->faceCount > 0)
     472    this->currentGroup->currentFace = this->currentGroup->currentFace->next = new Face;
     473  this->initFace (this->currentGroup->currentFace);
     474
     475  FaceElement* tmpElem = this->currentGroup->currentFace->firstElem = new FaceElement;
     476  tmpElem->next = NULL;
     477 
     478  va_list itemlist;
     479  va_start (itemlist, type);
     480
     481  for (int i = 0; i < faceElemCount; i++)
     482    {
     483      if (this->currentGroup->currentFace->vertexCount>0)
     484          tmpElem = tmpElem->next = new FaceElement;
     485      tmpElem->next = NULL;
     486
     487      tmpElem->vertexNumber = va_arg (itemlist, int) -1;
     488      if (type >= 2)
     489        tmpElem->texCoordNumber = va_arg (itemlist, int) -1;
     490      if (type == 1 || type ==3)
     491        tmpElem->normalNumber = va_arg(itemlist, int) -1;
     492      this->currentGroup->currentFace->vertexCount++;
     493    }
     494  va_end(itemlist);
     495
     496  this->currentGroup->faceCount += this->currentGroup->currentFace->vertexCount - 2;
     497}
     498
     499/**
    548500   \brief parses a vertexNormal-String
    549501   \param normalString The String that will be parsed.
     
    551503   If a vertexNormal line is found this function will inject it into the vertexNormal-Array
    552504*/
    553 bool Model::readVertexNormal (char* normalString)
     505bool Model::addVertexNormal (char* normalString)
    554506{
    555507  float subbuffer1;
     
    563515
    564516/**
     517   \brief adds a VertexNormal.
     518   \param x The x coordinate of the Normal.
     519   \param y The y coordinate of the Normal.
     520   \param z The z coordinate of the Normal.
     521
     522   If a vertexNormal line is found this function will inject it into the vertexNormal-Array
     523*/
     524bool Model::addVertexNormal(const float x, const float y, const float z)
     525{
     526  PRINTF(3)("found vertex-Normal %f, %f, %f\n", x, y, z);
     527  this->normals->addEntry(x, y, z);
     528}
     529
     530/**
    565531   \brief parses a vertexTextureCoordinate-String
    566532   \param vTextureString The String that will be parsed.
     
    569535   this function will inject it into the vertexTexture-Array
    570536*/
    571 bool Model::readVertexTexture (char* vTextureString)
     537bool Model::addVertexTexture (char* vTextureString)
    572538{
    573539  float subbuffer1;
     
    580546}
    581547
    582 /**
    583     \brief Function to read in a mtl File.
    584     \param mtlFile The .mtl file to read
    585 
    586     This Function parses all Lines of an mtl File.
    587     The reason for it not to be in the materials-class is,
    588     that a material does not have to be able to read itself in from a File.
    589 
    590 */
    591 bool Model::readMtlLib (char* mtlFile)
    592 {
    593   this->mtlFileName = new char [strlen(mtlFile)+1];
    594   strcpy(this->mtlFileName, mtlFile);
    595   char* fileName = new char [strlen(objPath) + strlen(this->mtlFileName)+1];
    596   strcpy(fileName, this->objPath);
    597   strcat(fileName, this->mtlFileName);
    598  
    599 
    600   PRINTF(3)("Opening mtlFile: %s\n", fileName);
    601 
    602   ifstream* MTL_FILE = new ifstream (fileName);
    603   if (MTL_FILE->fail())
    604     {
    605       PRINTF(1)("unable to open file: %s\n", fileName);
    606       MTL_FILE->close();
    607       delete []fileName;
    608       delete MTL_FILE;
    609       return false;
    610     }
    611   char Buffer[500];
    612   Material* tmpMat = material;
    613   while(!MTL_FILE->eof())
    614     {
    615       MTL_FILE->getline(Buffer, 500);
    616       PRINTF(4)("found line in mtlFile: %s\n", Buffer);
    617      
    618 
    619       // create new Material
    620       if (!strncmp(Buffer, "newmtl ", 7))
    621         {
    622           tmpMat = tmpMat->addMaterial(Buffer+7);
    623           //      PRINTF(2)("%s, %p\n", tmpMat->getName(), tmpMat);
    624         }
    625       // setting a illumMode
    626       else if (!strncmp(Buffer, "illum ", 6))
    627         {
    628           tmpMat->setIllum(Buffer+6);
    629 
    630         }
    631       // setting Diffuse Color
    632       else if (!strncmp(Buffer, "Kd ", 3))
    633         {
    634           tmpMat->setDiffuse(Buffer+3);
    635         }
    636       // setting Ambient Color
    637       else if (!strncmp(Buffer, "Ka ", 3))
    638         {
    639           tmpMat->setAmbient(Buffer+3);
    640         }
    641       // setting Specular Color
    642       else if (!strncmp(Buffer, "Ks ", 3))
    643         {
    644           tmpMat->setSpecular(Buffer+3);
    645         }
    646       // setting The Specular Shininess
    647       else if (!strncmp(Buffer, "Ns ", 3))
    648         {
    649           tmpMat->setShininess(Buffer+3);
    650         }
    651       // setting up transparency
    652       else if (!strncmp(Buffer, "d ", 2))
    653         {
    654           tmpMat->setTransparency(Buffer+2);
    655         }
    656       else if (!strncmp(Buffer, "Tf ", 3))
    657         {
    658           tmpMat->setTransparency(Buffer+3);
    659         }
    660      
    661       else if (!strncmp(Buffer, "map_Kd ", 7))
    662         {
    663           tmpMat->setDiffuseMap(Buffer+7);
    664         }
    665       else if (!strncmp(Buffer, "map_Ka ", 7))
    666         {
    667           tmpMat->setAmbientMap(Buffer+7);
    668         }
    669       else if (!strncmp(Buffer, "map_Ks ", 7))
    670         {
    671           tmpMat->setSpecularMap(Buffer+7);
    672         }
    673       else if (!strncmp(Buffer, "bump ", 5))
    674         {
    675           tmpMat->setBump(Buffer+7);
    676         }
    677      
    678 
    679     }
    680   MTL_FILE->close();
    681   delete []fileName;
    682   delete MTL_FILE;
    683   return true;
     548/**
     549   \brief adds a Texture Coordinate
     550   \param u The u coordinate of the TextureCoordinate.
     551   \param v The y coordinate of the TextureCoordinate.
     552
     553   If a TextureCoordinate line is found this function will inject it into the TextureCoordinate-Array
     554*/
     555bool Model::addVertexTexture(const float u, const float v)
     556{
     557  PRINTF(3)("found vertex-Texture %f, %f\n", u, v);
     558  this->vTexture->addEntry(u);
     559  this->vTexture->addEntry(v);
    684560}
    685561
     
    688564   \param matString the Material that will be set.
    689565*/
    690 bool Model::readUseMtl (char* matString)
    691 {
     566bool Model::addUseMtl (char* matString)
     567{
     568  /*
    692569  if (!this->mtlFileName)
    693570    {
     
    695572      return false;
    696573    }
    697      
     574  */     
    698575  if (this->currentGroup->faceCount >0)
    699576    this->currentGroup->currentFace = this->currentGroup->currentFace->next = new Face;
     
    717594  this->vertices->finalizeArray();
    718595  this->vTexture->finalizeArray();
    719   if (normals->getCount() == 0) // vertices-Array must be uilt for this
     596  if (normals->getCount() == 0) // vertices-Array must be built for this
    720597    this->buildVertexNormals();
    721598  this->normals->finalizeArray();
     
    743620                glEnd();
    744621              this->currentGroup->faceMode = 0;
    745               PRINTF(2)("using material %s for coming Faces.\n", tmpFace->materialString);
    746622              Material* tmpMat;
    747623              if ((tmpMat = material->search(tmpFace->materialString)) != NULL)
    748                 tmpMat->select();
     624                {
     625                  tmpMat->select();
     626                  PRINTF(2)("using material %s for coming Faces.\n", tmpFace->materialString);
     627                }
     628              else
     629                PRINTF(1)("material %s not found.\n", tmpFace->materialString);
     630
    749631
    750632            }
     
    893775      normArray[i].normalize();
    894776      PRINTF(3)("Found Normale number %d: (%f; %f, %f).\n", i, normArray[i].x, normArray[i].y, normArray[i].z);
    895 
     777     
    896778      this->normals->addEntry(normArray[i].x, normArray[i].y, normArray[i].z);
    897779
     
    907789   This will inject a Cube, because this is the most basic model.
    908790*/
    909 void Model::BoxModel(void)
    910 {
    911   this->readVertex ("-0.5 -0.5 0.5");
    912   this->readVertex ("0.5 -0.5 0.5");
    913   this->readVertex ("-0.5 0.5 0.5");
    914   this->readVertex ("0.5 0.5 0.5");
    915   this->readVertex ("-0.5 0.5 -0.5");
    916   this->readVertex ("0.5 0.5 -0.5");
    917   this->readVertex ("-0.5 -0.5 -0.5");
    918   this->readVertex ("0.5 -0.5 -0.5");
    919 
    920   this->readVertexTexture ("0.0 0.0");
    921   this->readVertexTexture ("1.0 0.0");
    922   this->readVertexTexture ("0.0 1.0");
    923   this->readVertexTexture ("1.0 1.0");
    924   this->readVertexTexture ("0.0 2.0");
    925   this->readVertexTexture ("1.0 2.0");
    926   this->readVertexTexture ("0.0 3.0");
    927   this->readVertexTexture ("1.0 3.0");
    928   this->readVertexTexture ("0.0 4.0");
    929   this->readVertexTexture ("1.0 4.0");
    930   this->readVertexTexture ("2.0 0.0");
    931   this->readVertexTexture ("2.0 1.0");
    932   this->readVertexTexture ("-1.0 0.0");
    933   this->readVertexTexture ("-1.0 1.0");
    934 
    935   this->readVertexNormal ("0.0 0.0 1.0");
    936   this->readVertexNormal ("0.0 0.0 1.0");
    937   this->readVertexNormal ("0.0 0.0 1.0");
    938   this->readVertexNormal ("0.0 0.0 1.0");
    939   this->readVertexNormal ("0.0 1.0 0.0");
    940   this->readVertexNormal ("0.0 1.0 0.0");
    941   this->readVertexNormal ("0.0 1.0 0.0");
    942   this->readVertexNormal ("0.0 1.0 0.0");
    943   this->readVertexNormal ("0.0 0.0 -1.0");
    944   this->readVertexNormal ("0.0 0.0 -1.0");
    945   this->readVertexNormal ("0.0 0.0 -1.0");
    946   this->readVertexNormal ("0.0 0.0 -1.0");
    947   this->readVertexNormal ("0.0 -1.0 0.0");
    948   this->readVertexNormal ("0.0 -1.0 0.0");
    949   this->readVertexNormal ("0.0 -1.0 0.0");
    950   this->readVertexNormal ("0.0 -1.0 0.0");
    951   this->readVertexNormal ("1.0 0.0 0.0");
    952   this->readVertexNormal ("1.0 0.0 0.0");
    953   this->readVertexNormal ("1.0 0.0 0.0");
    954   this->readVertexNormal ("1.0 0.0 0.0");
    955   this->readVertexNormal ("-1.0 0.0 0.0");
    956   this->readVertexNormal ("-1.0 0.0 0.0");
    957   this->readVertexNormal ("-1.0 0.0 0.0");
    958   this->readVertexNormal ("-1.0 0.0 0.0");
     791void Model::cubeModel(void)
     792{
     793  this->addVertex ("-0.5 -0.5 0.5");
     794  this->addVertex ("0.5 -0.5 0.5");
     795  this->addVertex ("-0.5 0.5 0.5");
     796  this->addVertex ("0.5 0.5 0.5");
     797  this->addVertex ("-0.5 0.5 -0.5");
     798  this->addVertex ("0.5 0.5 -0.5");
     799  this->addVertex ("-0.5 -0.5 -0.5");
     800  this->addVertex ("0.5 -0.5 -0.5");
     801
     802  this->addVertexTexture ("0.0 0.0");
     803  this->addVertexTexture ("1.0 0.0");
     804  this->addVertexTexture ("0.0 1.0");
     805  this->addVertexTexture ("1.0 1.0");
     806  this->addVertexTexture ("0.0 2.0");
     807  this->addVertexTexture ("1.0 2.0");
     808  this->addVertexTexture ("0.0 3.0");
     809  this->addVertexTexture ("1.0 3.0");
     810  this->addVertexTexture ("0.0 4.0");
     811  this->addVertexTexture ("1.0 4.0");
     812  this->addVertexTexture ("2.0 0.0");
     813  this->addVertexTexture ("2.0 1.0");
     814  this->addVertexTexture ("-1.0 0.0");
     815  this->addVertexTexture ("-1.0 1.0");
     816
     817  this->addVertexNormal ("0.0 0.0 1.0");
     818  this->addVertexNormal ("0.0 0.0 1.0");
     819  this->addVertexNormal ("0.0 0.0 1.0");
     820  this->addVertexNormal ("0.0 0.0 1.0");
     821  this->addVertexNormal ("0.0 1.0 0.0");
     822  this->addVertexNormal ("0.0 1.0 0.0");
     823  this->addVertexNormal ("0.0 1.0 0.0");
     824  this->addVertexNormal ("0.0 1.0 0.0");
     825  this->addVertexNormal ("0.0 0.0 -1.0");
     826  this->addVertexNormal ("0.0 0.0 -1.0");
     827  this->addVertexNormal ("0.0 0.0 -1.0");
     828  this->addVertexNormal ("0.0 0.0 -1.0");
     829  this->addVertexNormal ("0.0 -1.0 0.0");
     830  this->addVertexNormal ("0.0 -1.0 0.0");
     831  this->addVertexNormal ("0.0 -1.0 0.0");
     832  this->addVertexNormal ("0.0 -1.0 0.0");
     833  this->addVertexNormal ("1.0 0.0 0.0");
     834  this->addVertexNormal ("1.0 0.0 0.0");
     835  this->addVertexNormal ("1.0 0.0 0.0");
     836  this->addVertexNormal ("1.0 0.0 0.0");
     837  this->addVertexNormal ("-1.0 0.0 0.0");
     838  this->addVertexNormal ("-1.0 0.0 0.0");
     839  this->addVertexNormal ("-1.0 0.0 0.0");
     840  this->addVertexNormal ("-1.0 0.0 0.0");
    959841
    960842  /* normaleLess-testingMode
    961   this->readFace ("1 2 4 3");
    962   this->readFace ("3 4 6 5");
    963   this->readFace ("5 6 8 7");
    964   this->readFace ("7 8 2 1");
    965   this->readFace ("2 8 6 4");
    966   this->readFace ("7 1 3 5");
     843  this->addFace ("1 2 4 3");
     844  this->addFace ("3 4 6 5");
     845  this->addFace ("5 6 8 7");
     846  this->addFace ("7 8 2 1");
     847  this->addFace ("2 8 6 4");
     848  this->addFace ("7 1 3 5");
    967849  */
    968850
    969   this->readFace ("1/1/1 2/2/2 4/4/3 3/3/4");
    970   this->readFace ("3/3/5 4/4/6 6/6/7 5/5/8");
    971   this->readFace ("5/5/9 6/6/10 8/8/11 7/7/12");
    972   this->readFace ("7/7/13 8/8/14 2/10/15 1/9/16");
    973   this->readFace ("2/2/17 8/11/18 6/12/19 4/4/20");
    974   this->readFace ("7/13/21 1/1/22 3/3/23 5/14/24");
    975 
    976 }
     851  this->addFace ("1/1/1 2/2/2 4/4/3 3/3/4");
     852  this->addFace ("3/3/5 4/4/6 6/6/7 5/5/8");
     853  this->addFace ("5/5/9 6/6/10 8/8/11 7/7/12");
     854  this->addFace ("7/7/13 8/8/14 2/10/15 1/9/16");
     855  this->addFace ("2/2/17 8/11/18 6/12/19 4/4/20");
     856  this->addFace ("7/13/21 1/1/22 3/3/23 5/14/24");
     857
     858}
     859
     860
     861void Model::sphereModel()
     862{
     863  int detail = 30;
     864  if (detail <= 0) detail = 1;
     865  float df = (float)detail;
     866  for (float i = 0.0; i < df/2; i+=1.0)
     867    {
     868      for (float j = 0.0; j < df; j+=1.0)
     869      {
     870        float vz = i/df *2.0*PI - PI/2.0;
     871        this->addVertex(cos(j/df*2.0*PI) * cos(vz) ,
     872                        sin(j/df*2.0*PI) * cos(vz),
     873                        sin(vz));
     874        //if (j==0.0)
     875        //printf ("%f %f\n", vz, sin (vz));
     876        if (i==0.0)
     877                  printf("%f, %f\n", j/df*2.0*PI, cos(j/df*PI));
     878      }
     879    }
     880  vertices->debug();
     881  for (int i = 0; i < detail/2; i++)
     882    for (int j = 1; j < detail; j++)
     883      {
     884        unsigned int v1,v2,v3,v4;
     885        v1 = i*detail +j;
     886
     887        /*      if (j+1 == detail)
     888          {
     889            v2 = i*detail +1;
     890            v3 = i*detail+detail + 1;
     891          }
     892          else*/
     893          {
     894            v2 = i*detail +j+1;
     895            v3 = i*detail+detail + j+1;
     896          }
     897        v4 = i*detail+detail + j;
     898        //printf("%i %i %i %i\n", v1, v2, v3, v4);
     899        this->addFace(4, 0, v1, v2, v3, v4);
     900      }
     901}
     902
     903/**
     904   \brief Creates a Cylinder.
     905*/
     906void Model::cylinderModel(void)
     907{
     908  unsigned int detail = 20;
     909  float size = 1.0;
     910
     911  // check if devision by zero
     912  if (detail <= 3)
     913    detail = 3;
     914  int count = 0;
     915  // defining Points of the Cylinder.
     916  for (float phi = 0.0; phi < 2.0*PI; phi += 2.0*PI/(float)detail)
     917    {
     918      this->addVertex(size*cos(phi), size*sin(phi), -size);
     919      this->addVertex(size*cos(phi), size*sin(phi), size);
     920      count ++;
     921    }
     922  this->addVertex(0, 0, -size);
     923  this->addVertex(0, 0, size);
     924
     925 
     926  if (count != detail)
     927    cout << "calculation error, count should be " << detail << " but is " << count << endl;
     928  vertices->debug();
     929
     930  // adding Faces
     931  for (int i = 0; i < detail-1; i++)
     932    {
     933      int p1, p2, p3, p4;
     934      p1 = 2*i+1;
     935      p2 = 2*i+2;
     936      if (i <= detail);
     937      p3 = 2*i+4;
     938      p4 = 2*i+3;
     939      cout <<i+1 <<": "<< p1 <<" "<< p2 <<" "<< p3 <<" "<< p4 <<endl;
     940      this->addFace(4, 0, p1, p2, p3, p4);
     941      this->addFace(3, 0, p4, p1, 2*detail+1);
     942      this->addFace(3, 0, p2, p3, 2*detail+2);
     943    }
     944  addFace(4,0, 2*detail-1, 2*detail, 2, 1);
     945  this->addFace(3, 0, 1, 2*detail-1, 2*detail+1);
     946  this->addFace(3, 0, 2*detail, 2, 2*detail+2);
     947
     948}
Note: See TracChangeset for help on using the changeset viewer.