Changeset 3657 in orxonox.OLD for orxonox/trunk/src/lib/graphics/importer/primitive_model.cc
- Timestamp:
- Mar 27, 2005, 12:22:09 AM (19 years ago)
- File:
-
- 1 copied
Legend:
- Unmodified
- Added
- Removed
-
orxonox/trunk/src/lib/graphics/importer/primitive_model.cc
r3655 r3657 1 2 3 1 /* 4 2 orxonox - the future of 3D-vertical-scrollers … … 12 10 13 11 ### File Specific: 14 main-programmer: ...12 main-programmer: Benjamin Grauer 15 13 co-programmer: ... 16 14 */ 17 15 18 #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_WORLD_ENTITY 19 20 #include "proto_class.h" 21 22 #include "stdincl.h" // maybe 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 23 24 24 using namespace std; … … 29 29 \todo this constructor is not jet implemented - do it 30 30 */ 31 ProtoClass::ProtoClass () 32 { 33 this->setClassName ("ProtoClass"); 31 PrimitiveModel::PrimitiveModel () 32 { 33 this->initialize(); 34 35 this->cubeModel(); 36 37 this->importToGL (); 38 39 this->cleanup(); 40 } 41 42 /** 43 \brief Creates a 3D-Model of Primitive-Type type 44 45 if you want to just display a Cube/Sphere/Cylinder/... without any material. 46 47 \todo implement Cube/Sphere/Cylinder/... 48 */ 49 PrimitiveModel::PrimitiveModel(PRIMITIVE type, float size, unsigned int detail) 50 { 51 this->initialize(); 52 53 switch (type) 54 { 55 default: 56 case CUBE: 57 this->cubeModel(); 58 break; 59 case SPHERE: 60 this->sphereModel(size, detail); 61 break; 62 case CYLINDER: 63 this->cylinderModel(); 64 break; 65 case CONE: 66 this->coneModel(size, detail); 67 break; 68 case PLANE: 69 this->planeModel(size, detail); 70 break; 71 } 72 this->importToGL (); 73 74 this->cleanup(); 34 75 } 35 76 … … 39 80 40 81 */ 41 Pr otoClass::~ProtoClass()82 PrimitiveModel::~PrimitiveModel () 42 83 { 43 84 // delete what has to be deleted here 44 85 } 45 86 46 /** 47 \brief nonsense - delete this method 48 \param realy nothing to give 49 \returns true or false - probably nothing? 50 51 this is just to show the doxygen abilities (this for example is an extension for a long comment) 52 */ 53 bool ProtoClass::doNonSense (int nothing) {} 87 88 89 90 /** 91 \brief Includes a default model 92 93 This will inject a Cube, because this is the most basic model. 94 */ 95 void PrimitiveModel::cubeModel(void) 96 { 97 this->addVertex (-0.5, -0.5, 0.5); 98 this->addVertex (0.5, -0.5, 0.5); 99 this->addVertex (-0.5, 0.5, 0.5); 100 this->addVertex (0.5, 0.5, 0.5); 101 this->addVertex (-0.5, 0.5, -0.5); 102 this->addVertex (0.5, 0.5, -0.5); 103 this->addVertex (-0.5, -0.5, -0.5); 104 this->addVertex (0.5, -0.5, -0.5); 105 106 this->addVertexTexture (0.0, 0.0); 107 this->addVertexTexture (1.0, 0.0); 108 this->addVertexTexture (0.0, 1.0); 109 this->addVertexTexture (1.0, 1.0); 110 this->addVertexTexture (0.0, 2.0); 111 this->addVertexTexture (1.0, 2.0); 112 this->addVertexTexture (0.0, 3.0); 113 this->addVertexTexture (1.0, 3.0); 114 this->addVertexTexture (0.0, 4.0); 115 this->addVertexTexture (1.0, 4.0); 116 this->addVertexTexture (2.0, 0.0); 117 this->addVertexTexture (2.0, 1.0); 118 this->addVertexTexture (-1.0, 0.0); 119 this->addVertexTexture (-1.0, 1.0); 120 121 this->addVertexNormal (0.0, 0.0, 1.0); 122 this->addVertexNormal (0.0, 0.0, 1.0); 123 this->addVertexNormal (0.0, 0.0, 1.0); 124 this->addVertexNormal (0.0, 0.0, 1.0); 125 this->addVertexNormal (0.0, 1.0, 0.0); 126 this->addVertexNormal (0.0, 1.0, 0.0); 127 this->addVertexNormal (0.0, 1.0, 0.0); 128 this->addVertexNormal (0.0, 1.0, 0.0); 129 this->addVertexNormal (0.0, 0.0, -1.0); 130 this->addVertexNormal (0.0, 0.0, -1.0); 131 this->addVertexNormal (0.0, 0.0, -1.0); 132 this->addVertexNormal (0.0, 0.0, -1.0); 133 this->addVertexNormal (0.0, -1.0, 0.0); 134 this->addVertexNormal (0.0, -1.0, 0.0); 135 this->addVertexNormal (0.0, -1.0, 0.0); 136 this->addVertexNormal (0.0, -1.0, 0.0); 137 this->addVertexNormal (1.0, 0.0, 0.0); 138 this->addVertexNormal (1.0, 0.0, 0.0); 139 this->addVertexNormal (1.0, 0.0, 0.0); 140 this->addVertexNormal (1.0, 0.0, 0.0); 141 this->addVertexNormal (-1.0, 0.0, 0.0); 142 this->addVertexNormal (-1.0, 0.0, 0.0); 143 this->addVertexNormal (-1.0, 0.0, 0.0); 144 this->addVertexNormal (-1.0, 0.0, 0.0); 145 146 /* normaleLess-testingMode 147 this->addFace ("1 2 4 3"); 148 this->addFace ("3 4 6 5"); 149 this->addFace ("5 6 8 7"); 150 this->addFace ("7 8 2 1"); 151 this->addFace ("2 8 6 4"); 152 this->addFace ("7 1 3 5"); 153 */ 154 155 this->addFace (4, 3, 1,1,1, 2,2,2, 4,4,3, 3,3,4); 156 this->addFace (4, 3, 3,3,5, 4,4,6, 6,6,7, 5,5,8); 157 this->addFace (4, 3, 5,5,9, 6,6,10, 8,8,11, 7,7,12); 158 this->addFace (4, 3, 7,7,13, 8,8,14, 2,10,15, 1,9,16); 159 this->addFace (4, 3, 2,2,17, 8,11,18, 6,12,19, 4,4,20); 160 this->addFace (4, 3, 7,13,21, 1,1,22, 3,3,23, 5,14,24); 161 162 } 163 164 /** 165 \brief Builds a Sphere into the Model. 166 \param size The radius of the Sphere. 167 \param detail The detail of the Sphere. 168 */ 169 void PrimitiveModel::sphereModel(float size, unsigned int detail) 170 { 171 int vertexCount = 0; 172 if (detail <= 0) 173 detail = 1; 174 // detail = 2; // make it even 175 float df = (float)detail; 176 177 // defining the Vertices 178 for (float i = 0; i < df *2.0; i+=1.0) 179 { 180 float vi = i/df *PI; 181 for (float j = -df / 2.0 +1.0; j < df / 2.0; j+=1.0 *df/(df+1.0)) 182 { 183 float vj = j/df *PI; 184 this->addVertex(size * cos(vi) * cos(vj), 185 size * sin(vj), 186 size * sin(vi) * cos(vj)); 187 this->addVertexTexture(i / (df *2.0), (j-1.0)/(df)+.5); 188 vertexCount++; 189 } 190 } 191 this->addVertex(0, -size, 0); 192 this->addVertex(0, size, 0); 193 194 // defining the binding Faces. 195 unsigned int v1, v2, v3, v4; 196 for (int i = 0; i <= detail * 2 -1; i++) 197 { 198 for (int j = 0; j <= detail; j++) 199 { 200 201 v1 = i*detail + j; 202 v4 = i*detail + (j+1); 203 204 if (i == detail*2 -1) 205 { 206 v2 = j; 207 v3 = j+1; 208 } 209 else 210 { 211 v2 = (i+1)*detail + j; 212 v3 = (i+1)*detail + (j+1); 213 } 214 215 if (j == 0) 216 { 217 v1 = vertexCount+1; 218 this->addFace(3, TEXCOORD, v1, v1, v3, v3, v4, v4); 219 } 220 else if (j == detail) 221 { 222 v3 = vertexCount+2; 223 this->addFace(3, TEXCOORD, v1, v1, v2, v2, v3, v3); 224 } 225 else 226 this->addFace(4, TEXCOORD, v1, v1, v2, v2, v3, v3, v4, v4); 227 } 228 } 229 } 230 /** 231 \brief Creates a Cylinder. 232 */ 233 void PrimitiveModel::cylinderModel(float size, unsigned int detail) 234 { 235 // check if devision by zero 236 if (detail <= 3) 237 detail = 3; 238 int count = 0; 239 // defining Points of the Cylinder. 240 for (float phi = 0.0; phi < 2.0*PI; phi += 2.0*PI/(float)detail) 241 { 242 this->addVertex(size*cos(phi), size*sin(phi), -size); 243 this->addVertex(size*cos(phi), size*sin(phi), size); 244 count ++; 245 } 246 this->addVertex(0, 0, -size); 247 this->addVertex(0, 0, size); 248 249 250 if (count != detail) 251 PRINTF(1)("calculation error, count should be %d but is %d.\n", detail, count); 252 253 // adding Faces 254 for (int i = 0; i < detail-1; i++) 255 { 256 int p1, p2, p3, p4; 257 p1 = 2*i+1; 258 p2 = 2*i+2; 259 if (i <= detail); 260 p3 = 2*i+4; 261 p4 = 2*i+3; 262 // something is wrong here 263 this->addFace(4, 0, p1, p2, p3, p4); 264 this->addFace(3, 0, p4, p1, 2*detail+1); 265 this->addFace(3, 0, p2, p3, 2*detail+2); 266 } 267 addFace(4,0, 2*detail-1, 2*detail, 2, 1); 268 this->addFace(3, VERTEX, 1, 2*detail-1, 2*detail+1); 269 this->addFace(3, VERTEX, 2*detail, 2, 2*detail+2); 270 } 271 272 /** 273 \brief creates a cone inside of this Model 274 \param size The size of the cone 275 \param detail the Detail-level of this cone 276 */ 277 void PrimitiveModel::coneModel(float size, unsigned int detail) 278 { 279 this->addVertex(0,-size,0); 280 this->addVertex(0,size,0); 281 if (detail <= 0) 282 detail = 1; 283 float df = (float)detail; 284 285 // defining the Vertices 286 for (float i = 0; i < df; i+=1.0) 287 { 288 float vi = i/df *2.0*PI; 289 this->addVertex(size* sin(vi), 290 -size, 291 size* cos(vi)); 292 } 293 294 //defining Faces 295 for (int i = 0; i < detail; i++) 296 { 297 unsigned int v1, v2; 298 v1 = i+3; 299 if (i == detail -1) 300 v2 = 3; 301 else 302 v2 = i+4; 303 this->addFace(3, VERTEX, 1, v1, v2); 304 this->addFace(3, VERTEX, 2, v1, v2); 305 } 306 } 307 308 /** 309 \brief creates a Plane inside of this Model 310 \param size The size of this plane 311 \param detail the Detail-level of this plane. 312 */ 313 void PrimitiveModel::planeModel(float size, unsigned int detail) 314 { 315 //defining vertices 316 for (int i = 0; i < detail; i++) 317 for (int j = 0; j < detail; j++) 318 { 319 this->addVertex(((float)i/(float)(detail-1) -.5)*size, 320 0, 321 ((float)j/(float)(detail-1) -.5)*size); 322 this->addVertexTexture((float)i/(float)(detail-1), 323 (float)j/(float)(detail-1)); 324 } 325 //defining Faces 326 unsigned int v1, v2, v3, v4; 327 for (int i = 0; i < detail-1; i++) 328 for (int j = 1; j < detail; j++) 329 { 330 v1 = i*detail + j; 331 v2 = (i+1)*detail + j; 332 v3 = (i+1)*detail + (j+1); 333 v4 = i*detail + (j+1); 334 this->addFace(4, TEXCOORD, v1, v1, v2, v2, v3, v3, v4, v4); 335 } 336 }
Note: See TracChangeset
for help on using the changeset viewer.