Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/lib/graphics/importer/primitive_model.cc

Last change on this file was 9406, checked in by bensch, 18 years ago

orxonox/trunk: merged the proxy back

merged with commandsvn merge -r9346:HEAD https://svn.orxonox.net/orxonox/branches/proxy .

no conflicts

File size: 5.9 KB
RevLine 
[4678]1/*
[1853]2   orxonox - the future of 3D-vertical-scrollers
3
4   Copyright (C) 2004 orx
5
6   This program is free software; you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation; either version 2, or (at your option)
9   any later version.
[1855]10
11   ### File Specific:
[3657]12   main-programmer: Benjamin Grauer
[1855]13   co-programmer: ...
[1853]14*/
15
[3657]16#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_WORLD_MODEL
[1853]17
[3657]18#include "primitive_model.h"
[1853]19
[3657]20#include "vector.h"
21#include "debug.h"
[1853]22
23
[9406]24
[3245]25/**
[4836]26 *  Creates a 3D-Model of Primitive-Type type
[1853]27
[3657]28   if you want to just display a Cube/Sphere/Cylinder/... without any material.
[4678]29
[4836]30   @todo implement Cube/Sphere/Cylinder/...
[3657]31*/
32PrimitiveModel::PrimitiveModel(PRIMITIVE type, float size, unsigned int detail)
33{
34  switch (type)
[7729]35  {
[3657]36    default:
[4468]37    case PRIM_CUBE:
[3657]38      this->cubeModel();
39      break;
[4468]40    case PRIM_SPHERE:
[3657]41      this->sphereModel(size, detail);
42      break;
[4468]43    case PRIM_CYLINDER:
[3657]44      this->cylinderModel();
45      break;
[4468]46    case PRIM_CONE:
[3657]47      this->coneModel(size, detail);
48      break;
[4468]49    case PRIM_PLANE:
[3657]50      this->planeModel(size, detail);
51      break;
[7729]52  }
[3911]53  this->finalize();
[3657]54}
55
[3245]56/**
[4836]57 *  standard deconstructor
[1853]58
[3245]59*/
[4678]60PrimitiveModel::~PrimitiveModel ()
[3543]61{
62  // delete what has to be deleted here
63}
[1853]64
[3245]65/**
[4836]66 *  Builds a Sphere into the Model.
67 * @param size The diameter of the Sphere.
68 * @param detail The detail of the Sphere.
[3657]69*/
70void PrimitiveModel::sphereModel(float size, unsigned int detail)
71{
[4678]72  if (detail <= 0)
[3657]73    detail = 1;
[6912]74  size /= 2.0;
[3657]75  //  detail = 2; // make it even
76  float df = (float)detail;
[4678]77
[3657]78  // defining the Vertices
79  for (float i = 0; i < df *2.0; i+=1.0)
[7729]80  {
81    float vi = i/df * PI;
82    for (float j = -df / 2.0 +1.0; j <= df / 2.0; j+=1.0 *df/(df+1.0))
[3657]83    {
[7729]84      float vj = j/df *PI;
85      this->addVertexNormal(cos(vi) * cos(vj),
86                            sin(vj),
87                            sin(vi) * cos(vj));
[5437]88
[7729]89      this->addVertex(size * cos(vi) * cos(vj),
90                      size * sin(vj),
91                      size * sin(vi) * cos(vj));
[5437]92
[7729]93      this->addVertexTexture( i / (df *2.0),
94                              (j-1.0)/(df)+.5);
[3657]95    }
[7729]96  }
[3657]97  this->addVertex(0, -size, 0);
[5437]98  this->addVertexNormal(0, -1, 0);
[4112]99  this->addVertexTexture(0,0);
[3657]100  this->addVertex(0, size, 0);
[5437]101  this->addVertexNormal(0, 1, 0);
[4112]102  this->addVertexTexture(0 ,1);
[3657]103
104  // defining the binding Faces.
[8145]105  int v1, v2, v3, v4;
[8316]106  for (unsigned int i = 0; i <= detail * 2 -1; i++)
[7729]107  {
[8316]108    for (unsigned int j = 0; j <= detail; j++)
[3657]109    {
[4112]110
[7729]111      v1 = i*detail + j-1;
112      v4 = i*detail + j;
[4678]113
[7729]114      if (i == detail*2 -1)
115      {
116        v2 = j-1;
117        v3 = j;
118      }
119      else
120      {
121        v2 = (i+1)*detail + j-1;
122        v3 = (i+1)*detail + j;
123      }
[4678]124
[7729]125      if (j == 0)
126      {
127        v1 = this->getVertexCount() - 2;
128        this->addFace(3, VERTEX_TEXCOORD_NORMAL, v1, v1, v1, v3, v3, v3, v4, v4, v4);
129      }
130      else if (j == detail)
131      {
132        v3 = this->getVertexCount()-1;
133        this->addFace(3, VERTEX_TEXCOORD_NORMAL, v1, v1, v1, v2, v2, v2, v3, v3, v3);
134      }
135      else
136        this->addFace(4, VERTEX_TEXCOORD_NORMAL, v1, v1, v1, v2, v2, v2, v3, v3, v3, v4, v4, v4);
[3657]137    }
[7729]138  }
[3657]139}
140/**
[4836]141 *  Creates a Cylinder.
[3657]142*/
143void PrimitiveModel::cylinderModel(float size, unsigned int detail)
144{
145  // check if devision by zero
146  if (detail <= 3)
147    detail = 3;
[8316]148  unsigned int count = 0;
[3657]149  // defining Points of the Cylinder.
150  for (float phi = 0.0; phi < 2.0*PI; phi += 2.0*PI/(float)detail)
[7729]151  {
152    this->addVertex(size*cos(phi), size*sin(phi), -size);
153    this->addVertex(size*cos(phi), size*sin(phi), size);
154    count ++;
155  }
[3657]156  this->addVertex(0, 0, -size);
157  this->addVertex(0, 0, size);
158
159  if (count != detail)
[7729]160  {
[3657]161    PRINTF(1)("calculation error, count should be %d but is %d.\n", detail, count);
[7729]162  }
[3657]163  // adding Faces
[8316]164  for (unsigned int i = 0; i < detail-1; i++)
[7729]165  {
166    int p1, p2, p3, p4;
167    p1 = 2*i;
168    p2 = 2*i+1;
169    p3 = 2*i+3;
170    p4 = 2*i+2;
171    // something is wrong here
172    this->addFace(4, VERTEX_ONLY, p1, p2, p3, p4);
173    this->addFace(3, VERTEX_ONLY, p4, p1, 2*detail);
174    this->addFace(3, VERTEX_ONLY, p2, p3, 2*detail+1);
175  }
[3896]176  // caps
[4112]177  this->addFace(4, VERTEX_ONLY, 2*detail-2, 2*detail-1, 1, 0);
178  this->addFace(3, VERTEX_ONLY, 0, 2*detail-2, 2*detail);
179  this->addFace(3, VERTEX_ONLY, 2*detail-1, 1, 2*detail+1);
[3657]180}
181
182/**
[4836]183 *  creates a cone inside of this Model
184 * @param size The size of the cone
185 * @param detail the Detail-level of this cone
[3657]186*/
187void PrimitiveModel::coneModel(float size, unsigned int detail)
188{
189  this->addVertex(0,-size,0);
190  this->addVertex(0,size,0);
[4678]191  if (detail <= 0)
[3657]192    detail = 1;
193  float df = (float)detail;
[4678]194
[3657]195  // defining the Vertices
196  for (float i = 0; i < df; i+=1.0)
[7729]197  {
198    float vi = i/df *2.0*PI;
199    this->addVertex(size* sin(vi),
200                    -size,
201                    size* cos(vi));
202  }
[3657]203
204  //defining Faces
[8316]205  for (unsigned int i = 0; i < detail; i++)
[7729]206  {
207    unsigned int v1, v2;
208    v1 = i+2;
[8316]209    if (i+1 == detail)
[7729]210      v2 = 2;
211    else
212      v2 = i+3;
213    this->addFace(3, VERTEX_ONLY, 0, v1, v2);
214    this->addFace(3, VERTEX_ONLY, 1, v1, v2);
215  }
[3657]216}
217
218/**
[4836]219 *  creates a Plane inside of this Model
220 * @param size The size of this plane
221 * @param detail the Detail-level of this plane.
[3657]222*/
223void PrimitiveModel::planeModel(float size, unsigned int detail)
224{
225  //defining vertices
[8316]226  for (unsigned int i = 0; i < detail; i++)
227    for (unsigned int j = 0; j < detail; j++)
[7729]228    {
229      this->addVertex(((float)i/(float)(detail-1) -.5)*size,
230                      0,
231                      ((float)j/(float)(detail-1) -.5)*size);
232      this->addVertexTexture((float)i/(float)(detail-1),
233                             (float)j/(float)(detail-1));
234    }
[3657]235  //defining Faces
[8145]236  int v1, v2, v3, v4;
[8316]237  for (unsigned int i = 0; i < detail-1; i++)
238    for (unsigned int j = 0; j < detail-1; j++)
[7729]239    {
240      v1 = i*detail + j;
241      v2 = (i+1)*detail + j;
242      v3 = (i+1)*detail + (j+1);
243      v4 = i*detail + (j+1);
244      this->addFace(4, VERTEX_TEXCOORD, v1, v1, v2, v2, v3, v3, v4, v4);
245    }
[3657]246}
Note: See TracBrowser for help on using the repository browser.