[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 | /** |
---|
[3657] | 27 | \brief 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 | |
---|
[3657] | 31 | \todo implement Cube/Sphere/Cylinder/... |
---|
| 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 | /** |
---|
| 58 | \brief 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 | /** |
---|
[3657] | 67 | \brief Builds a Sphere into the Model. |
---|
[3684] | 68 | \param size The diameter of the Sphere. |
---|
[3657] | 69 | \param detail The detail of the Sphere. |
---|
| 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; |
---|
| 86 | this->addVertex(size * cos(vi) * cos(vj), |
---|
| 87 | size * sin(vj), |
---|
| 88 | size * sin(vi) * cos(vj)); |
---|
| 89 | this->addVertexTexture(i / (df *2.0), (j-1.0)/(df)+.5); |
---|
| 90 | } |
---|
[3657] | 91 | } |
---|
| 92 | this->addVertex(0, -size, 0); |
---|
[4112] | 93 | this->addVertexTexture(0,0); |
---|
[3657] | 94 | this->addVertex(0, size, 0); |
---|
[4112] | 95 | this->addVertexTexture(0 ,1); |
---|
[3657] | 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++) |
---|
[4678] | 102 | { |
---|
[4112] | 103 | |
---|
[4678] | 104 | v1 = i*detail + j-1; |
---|
| 105 | v4 = i*detail + j; |
---|
| 106 | |
---|
| 107 | if (i == detail*2 -1) |
---|
| 108 | { |
---|
| 109 | v2 = j-1; |
---|
| 110 | v3 = j; |
---|
| 111 | } |
---|
| 112 | else |
---|
| 113 | { |
---|
| 114 | v2 = (i+1)*detail + j-1; |
---|
| 115 | v3 = (i+1)*detail + j; |
---|
| 116 | } |
---|
| 117 | |
---|
| 118 | if (j == 0) |
---|
| 119 | { |
---|
| 120 | v1 = this->getVertexCount() - 2; |
---|
| 121 | this->addFace(3, VERTEX_TEXCOORD, v1, v1, v3, v3, v4, v4); |
---|
| 122 | } |
---|
| 123 | else if (j == detail) |
---|
| 124 | { |
---|
| 125 | v3 = this->getVertexCount()-1; |
---|
| 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 | } |
---|
[3657] | 131 | } |
---|
| 132 | } |
---|
| 133 | /** |
---|
| 134 | \brief Creates a Cylinder. |
---|
| 135 | */ |
---|
| 136 | void 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; |
---|
[4112] | 159 | p1 = 2*i; |
---|
| 160 | p2 = 2*i+1; |
---|
| 161 | p3 = 2*i+3; |
---|
| 162 | p4 = 2*i+2; |
---|
[3657] | 163 | // something is wrong here |
---|
[3895] | 164 | this->addFace(4, VERTEX_ONLY, p1, p2, p3, p4); |
---|
[4112] | 165 | this->addFace(3, VERTEX_ONLY, p4, p1, 2*detail); |
---|
| 166 | this->addFace(3, VERTEX_ONLY, p2, p3, 2*detail+1); |
---|
[3657] | 167 | } |
---|
[3896] | 168 | // caps |
---|
[4112] | 169 | this->addFace(4, VERTEX_ONLY, 2*detail-2, 2*detail-1, 1, 0); |
---|
| 170 | this->addFace(3, VERTEX_ONLY, 0, 2*detail-2, 2*detail); |
---|
| 171 | this->addFace(3, VERTEX_ONLY, 2*detail-1, 1, 2*detail+1); |
---|
[3657] | 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 | */ |
---|
| 179 | void PrimitiveModel::coneModel(float size, unsigned int detail) |
---|
| 180 | { |
---|
| 181 | this->addVertex(0,-size,0); |
---|
| 182 | this->addVertex(0,size,0); |
---|
[4678] | 183 | if (detail <= 0) |
---|
[3657] | 184 | detail = 1; |
---|
| 185 | float df = (float)detail; |
---|
[4678] | 186 | |
---|
[3657] | 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), |
---|
[4678] | 192 | -size, |
---|
| 193 | size* cos(vi)); |
---|
[3657] | 194 | } |
---|
| 195 | |
---|
| 196 | //defining Faces |
---|
| 197 | for (int i = 0; i < detail; i++) |
---|
| 198 | { |
---|
| 199 | unsigned int v1, v2; |
---|
[4112] | 200 | v1 = i+2; |
---|
[3657] | 201 | if (i == detail -1) |
---|
[4678] | 202 | v2 = 2; |
---|
[3657] | 203 | else |
---|
[4678] | 204 | v2 = i+3; |
---|
[4112] | 205 | this->addFace(3, VERTEX_ONLY, 0, v1, v2); |
---|
[4678] | 206 | this->addFace(3, VERTEX_ONLY, 1, v1, v2); |
---|
[3657] | 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 | */ |
---|
| 215 | void 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 | { |
---|
[4678] | 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)); |
---|
[3657] | 226 | } |
---|
| 227 | //defining Faces |
---|
| 228 | unsigned int v1, v2, v3, v4; |
---|
| 229 | for (int i = 0; i < detail-1; i++) |
---|
[4112] | 230 | for (int j = 0; j < detail-1; j++) |
---|
[3657] | 231 | { |
---|
[4678] | 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); |
---|
[3657] | 237 | } |
---|
| 238 | } |
---|