Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: minor changes at model

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