Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: resource manager working, player uses it.

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