Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: diameter instead of radius (makes the spheres smaler

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