[4678] | 1 | /* |
---|
[1853] | 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. |
---|
[1855] | 10 | |
---|
| 11 | ### File Specific: |
---|
[3657] | 12 | main-programmer: Benjamin Grauer |
---|
[1855] | 13 | co-programmer: ... |
---|
[1853] | 14 | */ |
---|
| 15 | |
---|
[3657] | 16 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_WORLD_MODEL |
---|
[1853] | 17 | |
---|
[3657] | 18 | #include "primitive_model.h" |
---|
[1853] | 19 | |
---|
[3657] | 20 | #include <math.h> |
---|
| 21 | #include "vector.h" |
---|
| 22 | #include "debug.h" |
---|
[1853] | 23 | |
---|
[1856] | 24 | using namespace std; |
---|
[1853] | 25 | |
---|
[3245] | 26 | /** |
---|
[4836] | 27 | * Creates a 3D-Model of Primitive-Type type |
---|
[1853] | 28 | |
---|
[3657] | 29 | if you want to just display a Cube/Sphere/Cylinder/... without any material. |
---|
[4678] | 30 | |
---|
[4836] | 31 | @todo implement Cube/Sphere/Cylinder/... |
---|
[3657] | 32 | */ |
---|
| 33 | PrimitiveModel::PrimitiveModel(PRIMITIVE type, float size, unsigned int detail) |
---|
| 34 | { |
---|
| 35 | switch (type) |
---|
[7729] | 36 | { |
---|
[3657] | 37 | default: |
---|
[4468] | 38 | case PRIM_CUBE: |
---|
[3657] | 39 | this->cubeModel(); |
---|
| 40 | break; |
---|
[4468] | 41 | case PRIM_SPHERE: |
---|
[3657] | 42 | this->sphereModel(size, detail); |
---|
| 43 | break; |
---|
[4468] | 44 | case PRIM_CYLINDER: |
---|
[3657] | 45 | this->cylinderModel(); |
---|
| 46 | break; |
---|
[4468] | 47 | case PRIM_CONE: |
---|
[3657] | 48 | this->coneModel(size, detail); |
---|
| 49 | break; |
---|
[4468] | 50 | case PRIM_PLANE: |
---|
[3657] | 51 | this->planeModel(size, detail); |
---|
| 52 | break; |
---|
[7729] | 53 | } |
---|
[3911] | 54 | this->finalize(); |
---|
[3657] | 55 | } |
---|
| 56 | |
---|
[3245] | 57 | /** |
---|
[4836] | 58 | * standard deconstructor |
---|
[1853] | 59 | |
---|
[3245] | 60 | */ |
---|
[4678] | 61 | PrimitiveModel::~PrimitiveModel () |
---|
[3543] | 62 | { |
---|
| 63 | // delete what has to be deleted here |
---|
| 64 | } |
---|
[1853] | 65 | |
---|
[3245] | 66 | /** |
---|
[4836] | 67 | * Builds a Sphere into the Model. |
---|
| 68 | * @param size The diameter of the Sphere. |
---|
| 69 | * @param detail The detail of the Sphere. |
---|
[3657] | 70 | */ |
---|
| 71 | void PrimitiveModel::sphereModel(float size, unsigned int detail) |
---|
| 72 | { |
---|
[4678] | 73 | if (detail <= 0) |
---|
[3657] | 74 | detail = 1; |
---|
[6912] | 75 | size /= 2.0; |
---|
[3657] | 76 | // detail = 2; // make it even |
---|
| 77 | float df = (float)detail; |
---|
[4678] | 78 | |
---|
[3657] | 79 | // defining the Vertices |
---|
| 80 | for (float i = 0; i < df *2.0; i+=1.0) |
---|
[7729] | 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)) |
---|
[3657] | 84 | { |
---|
[7729] | 85 | float vj = j/df *PI; |
---|
| 86 | this->addVertexNormal(cos(vi) * cos(vj), |
---|
| 87 | sin(vj), |
---|
| 88 | sin(vi) * cos(vj)); |
---|
[5437] | 89 | |
---|
[7729] | 90 | this->addVertex(size * cos(vi) * cos(vj), |
---|
| 91 | size * sin(vj), |
---|
| 92 | size * sin(vi) * cos(vj)); |
---|
[5437] | 93 | |
---|
[7729] | 94 | this->addVertexTexture( i / (df *2.0), |
---|
| 95 | (j-1.0)/(df)+.5); |
---|
[3657] | 96 | } |
---|
[7729] | 97 | } |
---|
[3657] | 98 | this->addVertex(0, -size, 0); |
---|
[5437] | 99 | this->addVertexNormal(0, -1, 0); |
---|
[4112] | 100 | this->addVertexTexture(0,0); |
---|
[3657] | 101 | this->addVertex(0, size, 0); |
---|
[5437] | 102 | this->addVertexNormal(0, 1, 0); |
---|
[4112] | 103 | this->addVertexTexture(0 ,1); |
---|
[3657] | 104 | |
---|
| 105 | // defining the binding Faces. |
---|
[8145] | 106 | int v1, v2, v3, v4; |
---|
[8316] | 107 | for (unsigned int i = 0; i <= detail * 2 -1; i++) |
---|
[7729] | 108 | { |
---|
[8316] | 109 | for (unsigned int j = 0; j <= detail; j++) |
---|
[3657] | 110 | { |
---|
[4112] | 111 | |
---|
[7729] | 112 | v1 = i*detail + j-1; |
---|
| 113 | v4 = i*detail + j; |
---|
[4678] | 114 | |
---|
[7729] | 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 | } |
---|
[4678] | 125 | |
---|
[7729] | 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); |
---|
[3657] | 138 | } |
---|
[7729] | 139 | } |
---|
[3657] | 140 | } |
---|
| 141 | /** |
---|
[4836] | 142 | * Creates a Cylinder. |
---|
[3657] | 143 | */ |
---|
| 144 | void PrimitiveModel::cylinderModel(float size, unsigned int detail) |
---|
| 145 | { |
---|
| 146 | // check if devision by zero |
---|
| 147 | if (detail <= 3) |
---|
| 148 | detail = 3; |
---|
[8316] | 149 | unsigned int count = 0; |
---|
[3657] | 150 | // defining Points of the Cylinder. |
---|
| 151 | for (float phi = 0.0; phi < 2.0*PI; phi += 2.0*PI/(float)detail) |
---|
[7729] | 152 | { |
---|
| 153 | this->addVertex(size*cos(phi), size*sin(phi), -size); |
---|
| 154 | this->addVertex(size*cos(phi), size*sin(phi), size); |
---|
| 155 | count ++; |
---|
| 156 | } |
---|
[3657] | 157 | this->addVertex(0, 0, -size); |
---|
| 158 | this->addVertex(0, 0, size); |
---|
| 159 | |
---|
| 160 | if (count != detail) |
---|
[7729] | 161 | { |
---|
[3657] | 162 | PRINTF(1)("calculation error, count should be %d but is %d.\n", detail, count); |
---|
[7729] | 163 | } |
---|
[3657] | 164 | // adding Faces |
---|
[8316] | 165 | for (unsigned int i = 0; i < detail-1; i++) |
---|
[7729] | 166 | { |
---|
| 167 | int p1, p2, p3, p4; |
---|
| 168 | p1 = 2*i; |
---|
| 169 | p2 = 2*i+1; |
---|
| 170 | p3 = 2*i+3; |
---|
| 171 | p4 = 2*i+2; |
---|
| 172 | // something is wrong here |
---|
| 173 | this->addFace(4, VERTEX_ONLY, p1, p2, p3, p4); |
---|
| 174 | this->addFace(3, VERTEX_ONLY, p4, p1, 2*detail); |
---|
| 175 | this->addFace(3, VERTEX_ONLY, p2, p3, 2*detail+1); |
---|
| 176 | } |
---|
[3896] | 177 | // caps |
---|
[4112] | 178 | this->addFace(4, VERTEX_ONLY, 2*detail-2, 2*detail-1, 1, 0); |
---|
| 179 | this->addFace(3, VERTEX_ONLY, 0, 2*detail-2, 2*detail); |
---|
| 180 | this->addFace(3, VERTEX_ONLY, 2*detail-1, 1, 2*detail+1); |
---|
[3657] | 181 | } |
---|
| 182 | |
---|
| 183 | /** |
---|
[4836] | 184 | * creates a cone inside of this Model |
---|
| 185 | * @param size The size of the cone |
---|
| 186 | * @param detail the Detail-level of this cone |
---|
[3657] | 187 | */ |
---|
| 188 | void PrimitiveModel::coneModel(float size, unsigned int detail) |
---|
| 189 | { |
---|
| 190 | this->addVertex(0,-size,0); |
---|
| 191 | this->addVertex(0,size,0); |
---|
[4678] | 192 | if (detail <= 0) |
---|
[3657] | 193 | detail = 1; |
---|
| 194 | float df = (float)detail; |
---|
[4678] | 195 | |
---|
[3657] | 196 | // defining the Vertices |
---|
| 197 | for (float i = 0; i < df; i+=1.0) |
---|
[7729] | 198 | { |
---|
| 199 | float vi = i/df *2.0*PI; |
---|
| 200 | this->addVertex(size* sin(vi), |
---|
| 201 | -size, |
---|
| 202 | size* cos(vi)); |
---|
| 203 | } |
---|
[3657] | 204 | |
---|
| 205 | //defining Faces |
---|
[8316] | 206 | for (unsigned int i = 0; i < detail; i++) |
---|
[7729] | 207 | { |
---|
| 208 | unsigned int v1, v2; |
---|
| 209 | v1 = i+2; |
---|
[8316] | 210 | if (i+1 == detail) |
---|
[7729] | 211 | v2 = 2; |
---|
| 212 | else |
---|
| 213 | v2 = i+3; |
---|
| 214 | this->addFace(3, VERTEX_ONLY, 0, v1, v2); |
---|
| 215 | this->addFace(3, VERTEX_ONLY, 1, v1, v2); |
---|
| 216 | } |
---|
[3657] | 217 | } |
---|
| 218 | |
---|
| 219 | /** |
---|
[4836] | 220 | * creates a Plane inside of this Model |
---|
| 221 | * @param size The size of this plane |
---|
| 222 | * @param detail the Detail-level of this plane. |
---|
[3657] | 223 | */ |
---|
| 224 | void PrimitiveModel::planeModel(float size, unsigned int detail) |
---|
| 225 | { |
---|
| 226 | //defining vertices |
---|
[8316] | 227 | for (unsigned int i = 0; i < detail; i++) |
---|
| 228 | for (unsigned int j = 0; j < detail; j++) |
---|
[7729] | 229 | { |
---|
| 230 | this->addVertex(((float)i/(float)(detail-1) -.5)*size, |
---|
| 231 | 0, |
---|
| 232 | ((float)j/(float)(detail-1) -.5)*size); |
---|
| 233 | this->addVertexTexture((float)i/(float)(detail-1), |
---|
| 234 | (float)j/(float)(detail-1)); |
---|
| 235 | } |
---|
[3657] | 236 | //defining Faces |
---|
[8145] | 237 | int v1, v2, v3, v4; |
---|
[8316] | 238 | for (unsigned int i = 0; i < detail-1; i++) |
---|
| 239 | for (unsigned int j = 0; j < detail-1; j++) |
---|
[7729] | 240 | { |
---|
| 241 | v1 = i*detail + j; |
---|
| 242 | v2 = (i+1)*detail + j; |
---|
| 243 | v3 = (i+1)*detail + (j+1); |
---|
| 244 | v4 = i*detail + (j+1); |
---|
| 245 | this->addFace(4, VERTEX_TEXCOORD, v1, v1, v2, v2, v3, v3, v4, v4); |
---|
| 246 | } |
---|
[3657] | 247 | } |
---|