Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/bsp_model/src/lib/graphics/importer/primitive_model.cc @ 7395

Last change on this file since 7395 was 6912, checked in by bensch, 18 years ago

orxonox/trunk: tried to fix the planet

File size: 6.2 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 *  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 PRIM_CUBE:
39      this->cubeModel();
40      break;
41    case PRIM_SPHERE:
42      this->sphereModel(size, detail);
43      break;
44    case PRIM_CYLINDER:
45      this->cylinderModel();
46      break;
47    case PRIM_CONE:
48      this->coneModel(size, detail);
49      break;
50    case PRIM_PLANE:
51      this->planeModel(size, detail);
52      break;
53    }
54  this->finalize();
55}
56
57/**
58 *  standard deconstructor
59
60*/
61PrimitiveModel::~PrimitiveModel ()
62{
63  // delete what has to be deleted here
64}
65
66/**
67 *  Builds a Sphere into the Model.
68 * @param size The diameter of the Sphere.
69 * @param detail The detail of the Sphere.
70*/
71void PrimitiveModel::sphereModel(float size, unsigned int detail)
72{
73  if (detail <= 0)
74    detail = 1;
75  size /= 2.0;
76  //  detail = 2; // make it even
77  float df = (float)detail;
78
79  // defining the Vertices
80  for (float i = 0; i < df *2.0; i+=1.0)
81    {
82      float vi = i/df * PI;
83      for (float j = -df / 2.0 +1.0; j <= df / 2.0; j+=1.0 *df/(df+1.0))
84        {
85          float vj = j/df *PI;
86          this->addVertexNormal(cos(vi) * cos(vj),
87                                sin(vj),
88                                sin(vi) * cos(vj));
89
90          this->addVertex(size * cos(vi) * cos(vj),
91                          size * sin(vj),
92                          size * sin(vi) * cos(vj));
93
94          this->addVertexTexture( i / (df *2.0),
95                                 (j-1.0)/(df)+.5);
96        }
97    }
98  this->addVertex(0, -size, 0);
99  this->addVertexNormal(0, -1, 0);
100  this->addVertexTexture(0,0);
101  this->addVertex(0, size, 0);
102  this->addVertexNormal(0, 1, 0);
103  this->addVertexTexture(0 ,1);
104
105  // defining the binding Faces.
106  unsigned int v1, v2, v3, v4;
107  for (int i = 0; i <= detail * 2 -1; i++)
108    {
109      for (int j = 0; j <= detail; j++)
110        {
111
112          v1 = i*detail + j-1;
113          v4 = i*detail + j;
114
115          if (i == detail*2 -1)
116            {
117              v2 = j-1;
118              v3 = j;
119            }
120          else
121            {
122              v2 = (i+1)*detail + j-1;
123              v3 = (i+1)*detail + j;
124            }
125
126          if (j == 0)
127            {
128              v1 = this->getVertexCount() - 2;
129              this->addFace(3, VERTEX_TEXCOORD_NORMAL, v1, v1, v1, v3, v3, v3, v4, v4, v4);
130            }
131          else if (j == detail)
132            {
133              v3 = this->getVertexCount()-1;
134              this->addFace(3, VERTEX_TEXCOORD_NORMAL, v1, v1, v1, v2, v2, v2, v3, v3, v3);
135            }
136          else
137            this->addFace(4, VERTEX_TEXCOORD_NORMAL, v1, v1, v1, v2, v2, v2, v3, v3, v3, v4, v4, v4);
138        }
139    }
140}
141/**
142 *  Creates a Cylinder.
143*/
144void PrimitiveModel::cylinderModel(float size, unsigned int detail)
145{
146  // check if devision by zero
147  if (detail <= 3)
148    detail = 3;
149  int count = 0;
150  // defining Points of the Cylinder.
151  for (float phi = 0.0; phi < 2.0*PI; phi += 2.0*PI/(float)detail)
152    {
153      this->addVertex(size*cos(phi), size*sin(phi), -size);
154      this->addVertex(size*cos(phi), size*sin(phi), size);
155      count ++;
156    }
157  this->addVertex(0, 0, -size);
158  this->addVertex(0, 0, size);
159
160  if (count != detail)
161    PRINTF(1)("calculation error, count should be %d but is %d.\n", detail, count);
162
163  // adding Faces
164  for (int i = 0; i < detail-1; i++)
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    }
176  // caps
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);
180}
181
182/**
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
186*/
187void PrimitiveModel::coneModel(float size, unsigned int detail)
188{
189  this->addVertex(0,-size,0);
190  this->addVertex(0,size,0);
191  if (detail <= 0)
192    detail = 1;
193  float df = (float)detail;
194
195  // defining the Vertices
196  for (float i = 0; i < df; i+=1.0)
197    {
198      float vi = i/df *2.0*PI;
199      this->addVertex(size* sin(vi),
200                      -size,
201                      size* cos(vi));
202    }
203
204  //defining Faces
205  for (int i = 0; i < detail; i++)
206    {
207      unsigned int v1, v2;
208      v1 = i+2;
209      if (i == detail -1)
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    }
216}
217
218/**
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.
222*/
223void PrimitiveModel::planeModel(float size, unsigned int detail)
224{
225  //defining vertices
226  for (int i = 0; i < detail; i++)
227    for (int j = 0; j < detail; j++)
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      }
235  //defining Faces
236  unsigned int v1, v2, v3, v4;
237  for (int i = 0; i < detail-1; i++)
238    for (int j = 0; j < detail-1; j++)
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      }
246}
Note: See TracBrowser for help on using the repository browser.