| [4597] | 1 | /* | 
|---|
| [3416] | 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: | 
|---|
| [4261] | 12 |    main-programmer: Benjamin Grauer | 
|---|
 | 13 |    co-programmer: ... | 
|---|
| [3411] | 14 | */ | 
|---|
 | 15 |  | 
|---|
| [3590] | 16 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_WORLD_ENTITY | 
|---|
 | 17 |  | 
|---|
| [3796] | 18 | #include "skybox.h" | 
|---|
| [4010] | 19 |  | 
|---|
| [7193] | 20 | #include "util/loading/load_param.h" | 
|---|
 | 21 | #include "util/loading/factory.h" | 
|---|
| [6022] | 22 | #include "static_model.h" | 
|---|
| [6470] | 23 |  | 
|---|
| [6022] | 24 | #include "material.h" | 
|---|
| [6470] | 25 | #include "texture.h" | 
|---|
 | 26 |  | 
|---|
| [6341] | 27 | #include "network_game_manager.h" | 
|---|
 | 28 | #include "converter.h" | 
|---|
| [9869] | 29 | #include "resource_texture.h" | 
|---|
| [3608] | 30 |  | 
|---|
| [9869] | 31 | #include "debug.h" | 
|---|
| [7123] | 32 |  | 
|---|
| [10259] | 33 | #include "state.h" | 
|---|
 | 34 | #include "playable.h" | 
|---|
 | 35 | #include "player.h" | 
|---|
 | 36 |  | 
|---|
| [9869] | 37 | #include "class_id_DEPRECATED.h" | 
|---|
 | 38 | ObjectListDefinitionID(SkyBox, CL_SKYBOX); | 
|---|
 | 39 | CREATE_FACTORY(SkyBox); | 
|---|
| [5357] | 40 |  | 
|---|
| [3416] | 41 | /** | 
|---|
| [5357] | 42 |  * Constructs a SkyBox and takes fileName as a map. | 
|---|
| [4836] | 43 |  * @param fileName the file to take as input for the SkyBox | 
|---|
| [3419] | 44 | */ | 
|---|
| [7221] | 45 | SkyBox::SkyBox(const std::string& fileName) | 
|---|
| [3419] | 46 | { | 
|---|
| [4012] | 47 |   this->preInit(); | 
|---|
| [7221] | 48 |   if (!fileName.empty()) | 
|---|
| [4261] | 49 |     this->setTextureAndType(fileName, ".jpg"); | 
|---|
| [4012] | 50 |   this->postInit(); | 
|---|
| [4010] | 51 | } | 
|---|
 | 52 |  | 
|---|
| [4444] | 53 | /** | 
|---|
| [4836] | 54 |  *  initializes a skybox from a XmlElement | 
|---|
| [4444] | 55 | */ | 
|---|
| [4436] | 56 | SkyBox::SkyBox(const TiXmlElement* root) | 
|---|
| [4010] | 57 | { | 
|---|
| [4012] | 58 |   this->preInit(); | 
|---|
| [4010] | 59 |  | 
|---|
| [6695] | 60 |   if( root != NULL) | 
|---|
 | 61 |     this->loadParams(root); | 
|---|
| [4010] | 62 |  | 
|---|
| [4012] | 63 |   this->postInit(); | 
|---|
| [4010] | 64 | } | 
|---|
 | 65 |  | 
|---|
| [4261] | 66 | void SkyBox::loadParams(const TiXmlElement* root) | 
|---|
 | 67 | { | 
|---|
| [6512] | 68 |   WorldEntity::loadParams(root); | 
|---|
| [4436] | 69 |  | 
|---|
| [5671] | 70 |   LoadParam(root, "Materialset", this, SkyBox, setTexture) | 
|---|
| [4621] | 71 |       .describe("Sets the material on the SkyBox. The string must be the path relative to the data-dir, and without a trailing .jpg"); | 
|---|
 | 72 |  | 
|---|
| [5671] | 73 |   LoadParam(root, "Size", this, SkyBox, setSize) | 
|---|
| [4621] | 74 |       .describe("Sets the Size of the SkyBox (normally this should be 90% of the maximal viewing Distance)."); | 
|---|
| [4261] | 75 | } | 
|---|
 | 76 |  | 
|---|
| [4746] | 77 | void SkyBox::preInit() | 
|---|
| [4010] | 78 | { | 
|---|
| [9869] | 79 |   this->registerObject(this, SkyBox::_objectList); | 
|---|
| [7840] | 80 |   this->toList(OM_BACKGROUND); | 
|---|
| [8037] | 81 |   this->toReflectionList(); | 
|---|
| [6634] | 82 |   //this->size = 100.0; | 
|---|
 | 83 |   this->textureSize = 1024.0f; | 
|---|
| [4620] | 84 |  | 
|---|
| [4597] | 85 |   for (int i = 0; i < 6; i++) | 
|---|
| [3801] | 86 |     { | 
|---|
 | 87 |       this->material[i] = new Material(); | 
|---|
 | 88 |       this->material[i]->setIllum(3); | 
|---|
| [3805] | 89 |       this->material[i]->setDiffuse(0.0,0.0,0.0); | 
|---|
 | 90 |       this->material[i]->setSpecular(0.0,0.0,0.0); | 
|---|
 | 91 |       this->material[i]->setAmbient(2.0, 2.0, 2.0); | 
|---|
| [6470] | 92 |  | 
|---|
| [6519] | 93 |       this->cubeTexture[i] = NULL; | 
|---|
| [3801] | 94 |     } | 
|---|
| [4444] | 95 |   this->setParentMode(PNODE_MOVEMENT); | 
|---|
| [6341] | 96 |  | 
|---|
| [7221] | 97 |   this->textureName = ""; | 
|---|
| [4012] | 98 | } | 
|---|
| [3803] | 99 |  | 
|---|
| [4746] | 100 | void SkyBox::postInit() | 
|---|
| [4012] | 101 | { | 
|---|
 | 102 |   this->rebuild(); | 
|---|
| [9656] | 103 |  | 
|---|
 | 104 |   textureName_handle = registerVarId( new SynchronizeableString( &textureName, &textureName, "textureName", PERMISSION_MASTER_SERVER) ); | 
|---|
 | 105 |   size_handle = registerVarId( new SynchronizeableFloat( &size, &size, "size", PERMISSION_MASTER_SERVER ) ); | 
|---|
| [3411] | 106 | } | 
|---|
 | 107 |  | 
|---|
| [3507] | 108 |  | 
|---|
| [3416] | 109 | /** | 
|---|
| [4836] | 110 |  *  default destructor | 
|---|
| [3416] | 111 | */ | 
|---|
| [3796] | 112 | SkyBox::~SkyBox() | 
|---|
| [3411] | 113 | { | 
|---|
| [4136] | 114 |   PRINTF(5)("Deleting SkyBox\n"); | 
|---|
| [3801] | 115 |   for (int i = 0; i < 6; i++) | 
|---|
| [6523] | 116 |   { | 
|---|
| [7221] | 117 |     if (this->material[i]) | 
|---|
| [6863] | 118 |       delete this->material[i]; | 
|---|
| [6523] | 119 |   } | 
|---|
| [6307] | 120 | } | 
|---|
| [3411] | 121 |  | 
|---|
| [7221] | 122 | void SkyBox::setTexture(const std::string& name) | 
|---|
 | 123 | { | 
|---|
 | 124 |   this->textureName = name; | 
|---|
 | 125 |   this->setTextureAndType (name, "jpg"); | 
|---|
 | 126 | }; | 
|---|
 | 127 |  | 
|---|
 | 128 |  | 
|---|
| [3803] | 129 | /** | 
|---|
| [7328] | 130 |  * @brief sets A set of textures when just giving a Name and an extension: | 
|---|
 | 131 |  * @param name the prefix of the Name | 
|---|
 | 132 |  * @param extension the file extension (jpg by default) | 
|---|
 | 133 |  * usage: give this function an argument like | 
|---|
 | 134 |  * setTexture("skybox", "jpg"); | 
|---|
 | 135 |  * and it will convert this to | 
|---|
 | 136 |  * setTextures("skybox_negx.jpg", "skybox_posx.jpg", "skybox_negy.jpg", | 
|---|
 | 137 |  *             "skybox_posy.jpg", "skybox_negz.jpg", "skybox_posz.jpg"); | 
|---|
 | 138 |  */ | 
|---|
| [7221] | 139 | void SkyBox::setTextureAndType(const std::string& name, const std::string& extension) | 
|---|
| [3803] | 140 | { | 
|---|
| [7328] | 141 |   std::string negX = name + "_negx." + extension; | 
|---|
 | 142 |   std::string posX = name + "_posx." + extension; | 
|---|
| [3803] | 143 |  | 
|---|
| [7328] | 144 |   std::string negY = name + "_negy." + extension; | 
|---|
 | 145 |   std::string posY = name + "_posy." + extension; | 
|---|
 | 146 |  | 
|---|
 | 147 |   std::string negZ = name + "_negz." + extension; | 
|---|
 | 148 |   std::string posZ = name + "_posz." + extension; | 
|---|
 | 149 |  | 
|---|
 | 150 |   this->setTextures(negX, posX, negY, posY, negZ, posZ); | 
|---|
| [3803] | 151 | } | 
|---|
 | 152 |  | 
|---|
 | 153 | /** | 
|---|
| [7328] | 154 |  * @brief Defines which textures should be loaded onto the SkyBox. | 
|---|
 | 155 |  * @param negX the top texture. | 
|---|
 | 156 |  * @param posX the bottom texture. | 
|---|
 | 157 |  * @param negY the left texture. | 
|---|
 | 158 |  * @param posY the right texture. | 
|---|
 | 159 |  * @param negZ the front texture. | 
|---|
 | 160 |  * @param posZ the back texture. | 
|---|
| [3803] | 161 | */ | 
|---|
| [7328] | 162 | void SkyBox::setTextures(const std::string& negX, const std::string& posX, | 
|---|
 | 163 |                          const std::string& negY, const std::string& posY, | 
|---|
 | 164 |                          const std::string& negZ, const std::string& posZ) | 
|---|
| [3803] | 165 | { | 
|---|
| [7328] | 166 |   this->material[0]->setDiffuseMap(negX); | 
|---|
 | 167 |   this->material[1]->setDiffuseMap(posX); | 
|---|
 | 168 |   this->material[2]->setDiffuseMap(negY); | 
|---|
 | 169 |   this->material[3]->setDiffuseMap(posY); | 
|---|
 | 170 |   this->material[4]->setDiffuseMap(negZ); | 
|---|
 | 171 |   this->material[5]->setDiffuseMap(posZ); | 
|---|
| [6523] | 172 |   if (GLEW_EXT_texture_cube_map) | 
|---|
| [7328] | 173 |     this->loadCubeMapTextures(negX, posX, negY, posY, negZ, posZ); | 
|---|
| [3803] | 174 | } | 
|---|
 | 175 |  | 
|---|
| [7328] | 176 | void SkyBox::loadCubeMapTextures(const std::string& posY, const std::string& negY, const std::string& negZ, | 
|---|
 | 177 |                                   const std::string& posZ, const std::string& posX, const std::string& negX) | 
|---|
| [6470] | 178 | { | 
|---|
| [9869] | 179 |   this->cubeTexture[0] = ResourceTexture(negX, GL_TEXTURE_CUBE_MAP_NEGATIVE_X_EXT); | 
|---|
 | 180 |   this->cubeTexture[1] = ResourceTexture(posX, GL_TEXTURE_CUBE_MAP_POSITIVE_X_EXT); | 
|---|
| [7328] | 181 |  | 
|---|
| [9869] | 182 |   this->cubeTexture[2] = ResourceTexture(negY, GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_EXT); | 
|---|
 | 183 |   this->cubeTexture[3] = ResourceTexture(posY, GL_TEXTURE_CUBE_MAP_POSITIVE_Y_EXT); | 
|---|
| [7328] | 184 |  | 
|---|
| [9869] | 185 |   this->cubeTexture[4] = ResourceTexture(negZ, GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_EXT); | 
|---|
 | 186 |   this->cubeTexture[5] = ResourceTexture(posZ, GL_TEXTURE_CUBE_MAP_POSITIVE_Z_EXT); | 
|---|
| [6470] | 187 | } | 
|---|
 | 188 |  | 
|---|
 | 189 | void SkyBox::enableCubeMap() | 
|---|
 | 190 | { | 
|---|
| [6860] | 191 |   glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_REFLECTION_MAP); | 
|---|
 | 192 |   glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_REFLECTION_MAP); | 
|---|
 | 193 |   glTexGeni(GL_R, GL_TEXTURE_GEN_MODE, GL_REFLECTION_MAP); | 
|---|
 | 194 |  | 
|---|
| [6523] | 195 |   glEnable(GL_TEXTURE_CUBE_MAP_EXT); | 
|---|
| [6860] | 196 |  | 
|---|
| [6470] | 197 |   glEnable(GL_TEXTURE_GEN_S); | 
|---|
 | 198 |   glEnable(GL_TEXTURE_GEN_T); | 
|---|
 | 199 |   glEnable(GL_TEXTURE_GEN_R); | 
|---|
| [6523] | 200 |  | 
|---|
| [6470] | 201 | } | 
|---|
 | 202 |  | 
|---|
 | 203 | void SkyBox::disableCubeMap() | 
|---|
 | 204 | { | 
|---|
| [6860] | 205 |   glDisable(GL_TEXTURE_CUBE_MAP); | 
|---|
| [6519] | 206 |   glDisable(GL_TEXTURE_2D); | 
|---|
| [6470] | 207 |   glDisable(GL_TEXTURE_GEN_S); | 
|---|
 | 208 |   glDisable(GL_TEXTURE_GEN_T); | 
|---|
 | 209 |   glDisable(GL_TEXTURE_GEN_R); | 
|---|
| [6523] | 210 |  | 
|---|
 | 211 |   glDisable(GL_TEXTURE_GEN_S); | 
|---|
 | 212 |   glDisable(GL_TEXTURE_GEN_T); | 
|---|
 | 213 |   glDisable(GL_TEXTURE_GEN_R); | 
|---|
| [6470] | 214 | } | 
|---|
 | 215 |  | 
|---|
 | 216 |  | 
|---|
 | 217 |  | 
|---|
| [3803] | 218 | /** | 
|---|
| [4836] | 219 |  * @param size The new size of the SkyBox | 
|---|
| [4621] | 220 |  | 
|---|
 | 221 |  * do not forget to rebuild the SkyBox after this. | 
|---|
| [3803] | 222 | */ | 
|---|
 | 223 | void SkyBox::setSize(float size) | 
|---|
 | 224 | { | 
|---|
 | 225 |   this->size = size; | 
|---|
 | 226 | } | 
|---|
 | 227 |  | 
|---|
| [6634] | 228 |  | 
|---|
 | 229 |  | 
|---|
| [10259] | 230 | void SkyBox::tick(float dt) | 
|---|
 | 231 | { | 
|---|
 | 232 |   PRINTF(0)("tick\n"); | 
|---|
 | 233 |  | 
|---|
 | 234 |   Player* pl = State::getPlayer(); | 
|---|
 | 235 |   if( pl == NULL) | 
|---|
 | 236 |     return; | 
|---|
 | 237 |  | 
|---|
 | 238 |   Playable* playable = pl->getPlayable(); | 
|---|
 | 239 |   if( playable == NULL) | 
|---|
 | 240 |     return; | 
|---|
 | 241 |  | 
|---|
 | 242 |   if( this->getParent() != playable) | 
|---|
 | 243 |     this->setParent( playable); | 
|---|
 | 244 | } | 
|---|
 | 245 |  | 
|---|
 | 246 |  | 
|---|
 | 247 |  | 
|---|
| [6634] | 248 | void SkyBox::draw() | 
|---|
 | 249 | { | 
|---|
 | 250 |   glPushAttrib(GL_ENABLE_BIT); | 
|---|
 | 251 | //   glPushAttrib(GL_LIGHTING_BIT); | 
|---|
 | 252 |   glDisable(GL_LIGHTING); | 
|---|
 | 253 |  | 
|---|
| [6772] | 254 |   glDisable(GL_FOG); | 
|---|
 | 255 |  | 
|---|
| [6634] | 256 |   WorldEntity::draw(); | 
|---|
 | 257 |  | 
|---|
 | 258 |   glPopAttrib(); | 
|---|
 | 259 |  | 
|---|
 | 260 | } | 
|---|
 | 261 |  | 
|---|
 | 262 |  | 
|---|
| [3803] | 263 | /** | 
|---|
| [4836] | 264 |  *  rebuilds the SkyBox | 
|---|
| [4597] | 265 |  | 
|---|
| [3803] | 266 |    this must be done, when changing the Size of the Skybox (runtime-efficency) | 
|---|
 | 267 | */ | 
|---|
| [3801] | 268 | void SkyBox::rebuild() | 
|---|
 | 269 | { | 
|---|
| [6022] | 270 |   StaticModel* model = new StaticModel(); | 
|---|
| [3801] | 271 |  | 
|---|
| [5994] | 272 |   model->addVertex (-0.5*size, -0.5*size, 0.5*size); | 
|---|
 | 273 |   model->addVertex (0.5*size, -0.5*size, 0.5*size); | 
|---|
 | 274 |   model->addVertex (-0.5*size, 0.5*size, 0.5*size); | 
|---|
 | 275 |   model->addVertex (0.5*size, 0.5*size, 0.5*size); | 
|---|
 | 276 |   model->addVertex (-0.5*size, 0.5*size, -0.5*size); | 
|---|
 | 277 |   model->addVertex (0.5*size, 0.5*size, -0.5*size); | 
|---|
 | 278 |   model->addVertex (-0.5*size, -0.5*size, -0.5*size); | 
|---|
 | 279 |   model->addVertex (0.5*size, -0.5*size, -0.5*size); | 
|---|
| [3801] | 280 |  | 
|---|
| [6634] | 281 | //   model->addVertexTexture (0.0, 1.0); | 
|---|
 | 282 | //   model->addVertexTexture (1.0, 1.0); | 
|---|
 | 283 | //   model->addVertexTexture (1.0, 0.0); | 
|---|
 | 284 | //   model->addVertexTexture (0.0, 0.0); | 
|---|
| [3801] | 285 |  | 
|---|
| [6634] | 286 |   model->addVertexTexture (1.0/this->textureSize, (this->textureSize - 1.0)/this->textureSize); | 
|---|
 | 287 |   model->addVertexTexture ((this->textureSize - 1.0)/this->textureSize, (this->textureSize - 1.0)/this->textureSize); | 
|---|
 | 288 |   model->addVertexTexture ((this->textureSize - 1.0)/this->textureSize, 1.0/this->textureSize); | 
|---|
 | 289 |   model->addVertexTexture (1.0/this->textureSize, 1.0/this->textureSize); | 
|---|
 | 290 |  | 
|---|
 | 291 |  | 
|---|
| [5994] | 292 |   model->addVertexNormal (0.0, 0.0, 1.0); | 
|---|
 | 293 |   model->addVertexNormal (0.0, 1.0, 0.0); | 
|---|
 | 294 |   model->addVertexNormal (0.0, 0.0, -1.0); | 
|---|
 | 295 |   model->addVertexNormal (0.0, -1.0, 0.0); | 
|---|
 | 296 |   model->addVertexNormal (1.0, 0.0, 0.0); | 
|---|
 | 297 |   model->addVertexNormal (-1.0, 0.0, 0.0); | 
|---|
| [3801] | 298 |  | 
|---|
| [5994] | 299 |   model->setMaterial(material[0]); | 
|---|
| [7328] | 300 |   model->addFace (4, VERTEX_TEXCOORD_NORMAL, 6,0,4, 0,1,4, 2,2,4, 4,3,4); // back | 
|---|
| [5994] | 301 |   model->setMaterial(material[1]); | 
|---|
| [7328] | 302 |   model->addFace (4, VERTEX_TEXCOORD_NORMAL, 1,0,5, 7,1,5, 5,2,5, 3,3,5); // front | 
|---|
 | 303 |   model->setMaterial(material[2]); | 
|---|
| [5994] | 304 |   model->addFace (4, VERTEX_TEXCOORD_NORMAL, 6,0,1, 7,1,1, 1,2,1, 0,3,1); // bottom | 
|---|
 | 305 |   model->setMaterial(material[3]); | 
|---|
| [7328] | 306 |   model->addFace (4, VERTEX_TEXCOORD_NORMAL, 2,0,3, 3,1,3, 5,2,3, 4,3,3); // top | 
|---|
| [5994] | 307 |   model->setMaterial(material[4]); | 
|---|
| [7328] | 308 |   model->addFace (4, VERTEX_TEXCOORD_NORMAL, 4,2,2, 5,3,2, 7,0,2, 6,1,2); // left | 
|---|
| [5994] | 309 |   model->setMaterial(material[5]); | 
|---|
| [7328] | 310 |   model->addFace (4, VERTEX_TEXCOORD_NORMAL, 0,0,0, 1,1,0, 3,2,0, 2,3,0); // right | 
|---|
| [4597] | 311 |  | 
|---|
| [5994] | 312 |   model->finalize(); | 
|---|
 | 313 |  | 
|---|
 | 314 |   this->setModel(model); | 
|---|
| [3801] | 315 | } | 
|---|
| [6341] | 316 |  | 
|---|
| [7954] | 317 | void SkyBox::varChangeHandler( std::list< int > & id ) | 
|---|
| [6341] | 318 | { | 
|---|
| [7954] | 319 |   bool somethinChanged = false; | 
|---|
| [9656] | 320 |  | 
|---|
| [7954] | 321 |   if ( std::find( id.begin(), id.end(), textureName_handle ) != id.end() ) | 
|---|
| [6341] | 322 |   { | 
|---|
| [7954] | 323 |     somethinChanged = true; | 
|---|
 | 324 |     setTexture( textureName ); | 
|---|
| [6341] | 325 |   } | 
|---|
| [9656] | 326 |  | 
|---|
| [7954] | 327 |   if ( std::find( id.begin(), id.end(), size_handle ) != id.end() ) | 
|---|
| [6341] | 328 |   { | 
|---|
| [7954] | 329 |     somethinChanged = true; | 
|---|
| [6341] | 330 |   } | 
|---|
| [9656] | 331 |  | 
|---|
| [7954] | 332 |   rebuild(); | 
|---|
| [9656] | 333 |  | 
|---|
| [7954] | 334 |   WorldEntity::varChangeHandler( id ); | 
|---|
| [6341] | 335 | } | 
|---|