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