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