Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 6310 in orxonox.OLD for trunk/src/lib/graphics/importer


Ignore:
Timestamp:
Dec 27, 2005, 12:24:27 PM (19 years ago)
Author:
bensch
Message:

orxonox/trunk: merged the VertexArrayModel from heightmap and added some simple importer facility.

Location:
trunk/src/lib/graphics/importer
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/lib/graphics/importer/vertex_array_model.cc

    r6309 r6310  
    6464  actcBeginInput(tc);
    6565  for(unsigned int i = 0; i < model.getTriangleCount(); i++)
    66       actcAddTriangle(tc, model.getTriangles()[i].indexToVertices[0], model.getTriangles()[i].indexToVertices[1], model.getTriangles()[i].indexToVertices[2]);
     66      actcAddTriangle(tc,
     67                      model.getTriangles()[i].indexToVertices[0],
     68                      model.getTriangles()[i].indexToVertices[1],
     69                      model.getTriangles()[i].indexToVertices[2]);
    6770  actcEndInput(tc);
    6871
     
    7275  for (unsigned int i = 0; i < model.getVertexCount(); i+=3)
    7376    this->addVertex(model.getVertexArray()[i], model.getVertexArray()[i+1], model.getVertexArray()[i+2]);
     77  for (unsigned int i = 0; i < model.getVertexCount(); i+=3)
     78    this->addColor(model.getVertexArray()[i], model.getVertexArray()[i+1], model.getVertexArray()[i+2]);
    7479  for (unsigned int i = 0; i < model.getNormalsCount(); i+=3)
    7580    this->addNormal(model.getNormalsArray()[i], model.getNormalsArray()[i+1], model.getNormalsArray()[i+2]);
    76   for (unsigned int i = 0; i < model.getTexCoordCount(); i+=2)
    77     this->addTexCoor(model.getTexCoordArray()[i], model.getTexCoordArray()[i+1]);
     81//   for (unsigned int i = 0; i < model.getTexCoordCount(); i+=2)
     82//     this->addTexCoor(model.getTexCoordArray()[i], model.getTexCoordArray()[i+1]);
     83  for (unsigned int i = 0; i < model.getVertexCount(); i+=3)
     84    this->addTexCoor(1,2);
    7885
    7986
    8087  int prim;
    81   unsigned int v1, v2, v3;
     88  uint v1, v2, v3;
    8289
    8390  actcBeginOutput(tc);
    8491  while((prim = actcStartNextPrim(tc, &v1, &v2) != ACTC_DATABASE_EMPTY))
    8592  {
     93    this->newStripe();
     94
    8695    this->addIndice(v1);
    8796    this->addIndice(v2);
     
    95104      printf("%d\n", v3);
    96105    }
    97     this->newStripe();
    98106  }
    99107  actcEndOutput(tc);
    100108
    101109  this->finalize();
     110  this->debug();
    102111}
    103112
     
    129138void VertexArrayModel::draw() const
    130139{
    131   PRINTF(4)("drawing the 3D-VertexArrayModels\n");
    132 
    133   glEnableClientState(GL_VERTEX_ARRAY |
    134                       GL_TEXTURE_COORD_ARRAY |
    135                       GL_NORMAL_ARRAY);
    136   //  glEnableClientState(GL_INDEX_ARRAY);
    137 
    138   glVertexPointer(3, GL_FLOAT, 0, this->vertices.getArray());
    139   glNormalPointer(GL_FLOAT, 0, this->normals.getArray());
    140   glTexCoordPointer(2, GL_FLOAT, 0, this->texCoords.getArray());
     140  PRINTF(4)("drawing 3D-VertexArrayModel %s\n", this->getName());
     141  glEnableClientState(GL_VERTEX_ARRAY );
     142  glEnableClientState(GL_TEXTURE_COORD_ARRAY );
     143  glEnableClientState(GL_NORMAL_ARRAY );
     144  glEnableClientState(GL_COLOR_ARRAY );
     145
     146
     147  glVertexPointer(3, GL_FLOAT, 0, &this->vertices[0]);
     148  glNormalPointer(GL_FLOAT, 0, &this->normals[0]);
     149  glTexCoordPointer(2, GL_FLOAT, 0, &this->texCoords[0]);
     150  glColorPointer(3, GL_FLOAT, 0, &this->colors[0]);
    141151
    142152  for (GLuint i = 1; i < this->stripes.size(); ++i)
    143153    {
    144       glDrawRangeElements(GL_TRIANGLE_STRIP,
    145                           this->stripes[i-1],
    146                           this->stripes[i],
    147                           this->indices.getCount(),
    148                           GL_UNSIGNED_BYTE,
    149                           this->indices.getArray());
     154      glDrawElements( GL_TRIANGLE_STRIP,
     155                      this->stripes[i] - this->stripes[i-1],
     156                      GL_UNSIGNED_INT,
     157                      &this->indices[this->stripes[i-1]] );
    150158    }
    151159}
     
    160168void VertexArrayModel::newStripe()
    161169{
    162   this->stripes.push_back(this->vertices.getCount()-1);
     170  // no stripes of size 0
     171  if (this->stripes.empty() || this->indices.size() != this->stripes.back())
     172    this->stripes.push_back(this->indices.size());
    163173}
    164174
     
    172182void VertexArrayModel::addVertex(float x, float y, float z)
    173183{
    174   this->vertices.addEntry(x, y, z);
     184  this->vertices.push_back(x);
     185  this->vertices.push_back(y);
     186  this->vertices.push_back(z);
    175187  this->pModelInfo.numVertices++;
    176188}
     
    187199void VertexArrayModel::addNormal(float x, float y, float z)
    188200{
    189   this->normals.addEntry(x, y, z);
     201  this->normals.push_back(x);
     202  this->normals.push_back(y);
     203  this->normals.push_back(z);
    190204  this->pModelInfo.numNormals++;
    191205}
     
    193207
    194208/**
    195  * adds a Texture Coordinate
     209 * @brief adds a Texture Coordinate
    196210 * @param u The u coordinate of the TextureCoordinate.
    197211 * @param v The y coordinate of the TextureCoordinate.
     
    201215void VertexArrayModel::addTexCoor(float u, float v)
    202216{
    203   this->texCoords.addEntry(u);
    204   this->texCoords.addEntry(v);
     217  this->texCoords.push_back(u);
     218  this->texCoords.push_back(v);
    205219  this->pModelInfo.numTexCoor++;
     220}
     221
     222/**
     223 * @brief adds a new Color
     224 * @param r the Red Component of the VertexColor to add.
     225 * @param g the Green Component of the VertexColor to add.
     226 * @param b the Blue of the VertexColor to add.
     227 */
     228void VertexArrayModel::addColor(float r, float g, float b)
     229{
     230  this->colors.push_back(r);
     231  this->colors.push_back(g);
     232  this->colors.push_back(b);
     233  // FIXME
    206234}
    207235
     
    212240 * @param type The information Passed with each Vertex
    213241*/
    214 void VertexArrayModel::addIndice(GLubyte indice)
    215 {
    216   this->indices.addEntry(indice);
     242void VertexArrayModel::addIndice(GLuint indice)
     243{
     244  this->indices.push_back(indice);
    217245}
    218246
     
    224252{
    225253  // finalize the Arrays
    226   this->vertices.finalizeArray();
    227   this->texCoords.finalizeArray();
    228   this->normals.finalizeArray();
    229   this->indices.finalizeArray();
    230 
    231254  this->newStripe();
    232 
    233   /*
    234     glEnableClientState(GL_VERTEX_ARRAY |
    235     GL_TEXTURE_COORD_ARRAY |
    236     GL_NORMAL_ARRAY);
    237   */
    238 
    239255  this->bFinalized = true;
    240256}
     257
    241258
    242259
     
    246263/////////////
    247264/**
    248  * @brief Includes a default model
    249  *
    250  * This will inject a Cube, because this is the most basic model.
    251  */
    252 void VertexArrayModel::planeModel()
    253 {
    254   unsigned int i, j;
    255   for (i = 0; i < 20; i++)
    256     {
    257       for (j = 0; j < 20; j++)
    258         {
    259           this->addVertex(i* 50, .5, (j)*50);
    260           this->addNormal(0, 1, 0);
    261           this->addTexCoor((float)i/20.0, (float)j/20.0);
    262 
    263         }
    264     }
    265   for (i = 0; i < 20; i++)
    266     {
    267       this->addIndice(i);
    268       this->addIndice(i+20);
    269     }
    270 }
     265* @brief Includes a default model
     266*
     267* This will inject a Cube, because this is the most basic model.
     268*/
     269void VertexArrayModel::planeModel(float sizeX, float sizeY, unsigned int resolutionX, unsigned int resolutionY)
     270{
     271  GLuint i, j;
     272  for (i = 0; i < resolutionY; i++)
     273    {
     274      for (j = 0; j < resolutionX; j++)
     275        {
     276          this->addVertex((float)i - (float)sizeY/2.0, 0.0, (float)j - (float)sizeX/2.0);
     277          this->addNormal(0.0, 1, 0.0);
     278          this->addTexCoor((float)i/(float)resolutionY, (float)j/(float)resolutionY);
     279          this->addColor((float)i/20.0, 0.0, (float)j/20.0);
     280        }
     281    }
     282
     283  for (i = 0; i < resolutionY-1; i++)
     284  {
     285    for (j = 0; j < resolutionX; j++)
     286    {
     287      this->addIndice( resolutionY*i + j );
     288      this->addIndice( resolutionY*(i+1) + j );
     289    }
     290    this->newStripe();
     291  }
     292}
     293
     294#include <cmath>
     295
     296/**
     297 * @brief builds a Triangle Stripped sphere
     298 * @param radius: radius
     299 * @param loops: the count of loops
     300 * @param segmentsPerLoop how many Segments per loop
     301 */
     302void VertexArrayModel::spiralSphere(const float radius, const unsigned int loops, const unsigned int segmentsPerLoop)
     303{
     304  for (unsigned int loopSegmentNumber = 0; loopSegmentNumber < segmentsPerLoop; ++loopSegmentNumber)
     305  {
     306    float theta = 0;
     307    float phi = loopSegmentNumber * 2 * PI / segmentsPerLoop;
     308    float sinTheta = std::sin(theta);
     309    float sinPhi = std::sin(phi);
     310    float cosTheta = std::cos(theta);
     311    float cosPhi = std::cos(phi);
     312    this->addVertex(radius * cosPhi * sinTheta, radius * sinPhi * sinTheta, radius * cosTheta);
     313    this->addNormal(radius * cosPhi * sinTheta, radius * sinPhi * sinTheta, radius * cosTheta);
     314    this->addTexCoor(0,0); /// FIXME
     315    this->addColor(.125,.436,.246); ///FIXME
     316  }
     317  for (unsigned int loopNumber = 0; loopNumber <= loops; ++loopNumber)
     318  {
     319    for (unsigned int loopSegmentNumber = 0; loopSegmentNumber < segmentsPerLoop; ++loopSegmentNumber)
     320    {
     321      float theta = (loopNumber * PI / loops) + ((PI * loopSegmentNumber) / (segmentsPerLoop * loops));
     322      if (loopNumber == loops)
     323      {
     324        theta = PI;
     325      }
     326      float phi = loopSegmentNumber * 2 * PI / segmentsPerLoop;
     327      float sinTheta = std::sin(theta);
     328      float sinPhi = std::sin(phi);
     329      float cosTheta = std::cos(theta);
     330      float cosPhi = std::cos(phi);
     331      this->addVertex(radius * cosPhi * sinTheta, radius * sinPhi * sinTheta, radius * cosTheta);
     332      this->addNormal(radius * cosPhi * sinTheta, radius * sinPhi * sinTheta, radius * cosTheta);
     333      this->addTexCoor(0,0); //FIXME
     334      this->addColor(.125,.436,.246);
     335
     336    }
     337  }
     338  for (unsigned int loopSegmentNumber = 0; loopSegmentNumber < segmentsPerLoop; ++loopSegmentNumber)
     339  {
     340    this->addIndice(loopSegmentNumber);
     341    this->addIndice(segmentsPerLoop + loopSegmentNumber);
     342  }
     343  for (unsigned int loopNumber = 0; loopNumber < loops; ++loopNumber)
     344  {
     345    for (unsigned int loopSegmentNumber = 0; loopSegmentNumber < segmentsPerLoop; ++loopSegmentNumber)
     346    {
     347      this->addIndice( ((loopNumber + 1) * segmentsPerLoop) + loopSegmentNumber);
     348      this->addIndice( ((loopNumber + 2) * segmentsPerLoop) + loopSegmentNumber);
     349    }
     350  }
     351}
     352
     353
     354/**
     355 * @brief print out some nice debug information about this VertexArrayModel.
     356 */
     357void VertexArrayModel::debug() const
     358{
     359  PRINT(0)("VertexArrayModel (%s): debug\n", this->getName());
     360  PRINT(0)("Stripes: %d; Indices: %d; Vertices: %d; Normals %d; TextCoords %d; Colors %d\n",
     361            this->stripes.size(),
     362            this->indices.size(),
     363            this->vertices.size()/3,
     364            this->normals.size()/3,
     365            this->texCoords.size()/2,
     366            this->colors.size() )/3;
     367  for (GLuint i = 1; i < this->stripes.size(); ++i)
     368  {
     369    PRINT(0)("Stripe-%d (s:%d:e:%d):: ", i, this->stripes[i-1], this->stripes[i]);
     370    for (GLuint j = this->stripes[i-1] ; j < this->stripes[i]; j++)
     371    {
     372      PRINT(0)("%d->", this->indices[j]);
     373    }
     374    PRINT(0)("\n");
     375  }
     376}
  • trunk/src/lib/graphics/importer/vertex_array_model.h

    r6308 r6310  
    1111#include "glincl.h"
    1212
    13 #include "array.h"
    1413#include <vector>
    1514
     
    4039  void addNormal(float x, float y, float z);
    4140  void addTexCoor(float u, float v);
    42   void addIndice(GLubyte indice);
     41  void addColor(float r, float g, float b);
     42
     43  void addIndice(GLuint indice);
    4344
    4445  void finalize();
    4546
    4647  //
    47   void planeModel();
     48  void planeModel(float sizeX, float sizeY, unsigned int resolutionX, unsigned int resolutionY);
     49  void spiralSphere(const float radius, const unsigned int loops, const unsigned int segmentsPerLoop);
     50
     51  void debug() const;
    4852
    4953 private:
     
    5357  bool                       bFinalized;       //!< Sets the Object to be finalized.
    5458
    55   tArray<GLfloat>            vertices;        //!< The Array that handles the Vertices.
    56   tArray<GLfloat>            normals;         //!< The Array that handles the Normals.
    57   tArray<GLfloat>            texCoords;       //!< The Array that handles the VertexTextureCoordinates.
     59  std::vector<GLfloat>       vertices;        //!< The Array that handles the Vertices.
     60  std::vector<GLfloat>       normals;         //!< The Array that handles the Normals.
     61  std::vector<GLfloat>       texCoords;       //!< The Array that handles the VertexTextureCoordinates.
     62  std::vector<GLfloat>       colors;          //!< The Array that handles Colors.
    5863
    59   tArray<GLubyte>            indices;         //!< The Array that tells us what Vertex is connected to which other one.
     64  std::vector<GLuint>        indices;         //!< The Array that tells us what Vertex is connected to which other one.
    6065
    6166  std::vector<GLuint>        stripes;         //!< A lsit of Stripes of this Model.
Note: See TracChangeset for help on using the changeset viewer.