Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 3909 was 3909, checked in by bensch, 19 years ago

orxonox/trunk: importer: no more fstream c-style code only

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