[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 | |
---|
[5356] | 20 | #include "load_param.h" |
---|
[4010] | 21 | #include "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" |
---|
[7123] | 29 | #include "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 | */ |
---|
[4261] | 40 | SkyBox::SkyBox(const char* fileName) |
---|
[3419] | 41 | { |
---|
[4012] | 42 | this->preInit(); |
---|
[4261] | 43 | if (fileName) |
---|
| 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); |
---|
[6634] | 76 | //this->size = 100.0; |
---|
| 77 | this->textureSize = 1024.0f; |
---|
[4620] | 78 | |
---|
[4597] | 79 | for (int i = 0; i < 6; i++) |
---|
[3801] | 80 | { |
---|
| 81 | this->material[i] = new Material(); |
---|
| 82 | this->material[i]->setIllum(3); |
---|
[3805] | 83 | this->material[i]->setDiffuse(0.0,0.0,0.0); |
---|
| 84 | this->material[i]->setSpecular(0.0,0.0,0.0); |
---|
| 85 | this->material[i]->setAmbient(2.0, 2.0, 2.0); |
---|
[6470] | 86 | |
---|
[6519] | 87 | this->cubeTexture[i] = NULL; |
---|
[3801] | 88 | } |
---|
[4444] | 89 | this->setParentMode(PNODE_MOVEMENT); |
---|
[6341] | 90 | |
---|
| 91 | this->textureName = NULL; |
---|
[4012] | 92 | } |
---|
[3803] | 93 | |
---|
[4746] | 94 | void SkyBox::postInit() |
---|
[4012] | 95 | { |
---|
| 96 | this->rebuild(); |
---|
[3411] | 97 | } |
---|
| 98 | |
---|
[3507] | 99 | |
---|
[3416] | 100 | /** |
---|
[4836] | 101 | * default destructor |
---|
[3416] | 102 | */ |
---|
[3796] | 103 | SkyBox::~SkyBox() |
---|
[3411] | 104 | { |
---|
[4136] | 105 | PRINTF(5)("Deleting SkyBox\n"); |
---|
[3801] | 106 | for (int i = 0; i < 6; i++) |
---|
[6523] | 107 | { |
---|
[6863] | 108 | if( this->material[i]) |
---|
| 109 | delete this->material[i]; |
---|
[7123] | 110 | if( this->cubeTexture[i]) |
---|
| 111 | ResourceManager::getInstance()->unload(this->cubeTexture[i]); |
---|
[6523] | 112 | } |
---|
[6307] | 113 | } |
---|
[3411] | 114 | |
---|
[3803] | 115 | /** |
---|
[4836] | 116 | * sets A set of textures when just giving a Name and an extension: |
---|
[3763] | 117 | |
---|
[3803] | 118 | usage: give this function an argument like |
---|
| 119 | setTexture("skybox", "jpg"); |
---|
[4597] | 120 | and it will convert this to |
---|
[3803] | 121 | setTextures("skybox_top.jpg", "skybox_bottom.jpg", "skybox_left.jpg", |
---|
| 122 | "skybox_right.jpg", "skybox_front.jpg", "skybox_back.jpg"); |
---|
| 123 | */ |
---|
[4261] | 124 | void SkyBox::setTextureAndType(const char* name, const char* extension) |
---|
[3803] | 125 | { |
---|
[4012] | 126 | char* top = new char[strlen(name)+strlen(extension)+ 10]; |
---|
| 127 | char* bottom = new char[strlen(name)+strlen(extension)+ 10]; |
---|
| 128 | char* left = new char[strlen(name)+strlen(extension)+ 10]; |
---|
| 129 | char* right = new char[strlen(name)+strlen(extension)+ 10]; |
---|
| 130 | char* front = new char[strlen(name)+strlen(extension)+ 10]; |
---|
| 131 | char* back = new char[strlen(name)+strlen(extension)+ 10]; |
---|
[3803] | 132 | |
---|
| 133 | sprintf(top, "%s_top.%s", name, extension); |
---|
| 134 | sprintf(bottom, "%s_bottom.%s", name, extension); |
---|
| 135 | sprintf(left, "%s_left.%s", name, extension); |
---|
| 136 | sprintf(right, "%s_right.%s", name, extension); |
---|
| 137 | sprintf(front, "%s_front.%s", name, extension); |
---|
| 138 | sprintf(back, "%s_back.%s", name, extension); |
---|
[4597] | 139 | |
---|
[3803] | 140 | this->setTextures(top, bottom, left, right, front, back); |
---|
| 141 | |
---|
[4012] | 142 | // deleted alocated memory of this function |
---|
[3803] | 143 | delete []top; |
---|
| 144 | delete []bottom; |
---|
| 145 | delete []left; |
---|
| 146 | delete []right; |
---|
| 147 | delete []front; |
---|
| 148 | delete []back; |
---|
| 149 | } |
---|
| 150 | |
---|
| 151 | /** |
---|
[4836] | 152 | * Defines which textures should be loaded onto the SkyBox. |
---|
| 153 | * @param top the top texture. |
---|
| 154 | * @param bottom the bottom texture. |
---|
| 155 | * @param left the left texture. |
---|
| 156 | * @param right the right texture. |
---|
| 157 | * @param front the front texture. |
---|
| 158 | * @param back the back texture. |
---|
[3803] | 159 | */ |
---|
[6470] | 160 | void SkyBox::setTextures(const char* top, const char* bottom, const char* left, |
---|
| 161 | const char* right, const char* front, const char* back) |
---|
[3803] | 162 | { |
---|
| 163 | this->material[0]->setDiffuseMap(top); |
---|
| 164 | this->material[1]->setDiffuseMap(bottom); |
---|
| 165 | this->material[2]->setDiffuseMap(left); |
---|
| 166 | this->material[3]->setDiffuseMap(right); |
---|
| 167 | this->material[4]->setDiffuseMap(front); |
---|
| 168 | this->material[5]->setDiffuseMap(back); |
---|
[6523] | 169 | if (GLEW_EXT_texture_cube_map) |
---|
| 170 | this->loadCubeMapTextures(top, bottom, left, right, front, back); |
---|
[3803] | 171 | } |
---|
| 172 | |
---|
[6470] | 173 | void SkyBox::loadCubeMapTextures(const char* top, const char* bottom, const char* left, |
---|
| 174 | const char* right, const char* front, const char* back) |
---|
| 175 | { |
---|
[6860] | 176 | this->cubeTexture[0] = (Texture*)ResourceManager::getInstance()->load(top, RP_LEVEL, IMAGE, GL_TEXTURE_CUBE_MAP_POSITIVE_Y_EXT); |
---|
| 177 | this->cubeTexture[1] = (Texture*)ResourceManager::getInstance()->load(bottom, RP_LEVEL, IMAGE, GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_EXT); |
---|
| 178 | this->cubeTexture[2] = (Texture*)ResourceManager::getInstance()->load(left, RP_LEVEL, IMAGE, GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_EXT); |
---|
| 179 | this->cubeTexture[3] = (Texture*)ResourceManager::getInstance()->load(right, RP_LEVEL, IMAGE, GL_TEXTURE_CUBE_MAP_POSITIVE_Z_EXT); |
---|
| 180 | this->cubeTexture[4] = (Texture*)ResourceManager::getInstance()->load(front, RP_LEVEL, IMAGE, GL_TEXTURE_CUBE_MAP_POSITIVE_X_EXT); |
---|
| 181 | this->cubeTexture[5] = (Texture*)ResourceManager::getInstance()->load(back, RP_LEVEL, IMAGE, GL_TEXTURE_CUBE_MAP_NEGATIVE_X_EXT); |
---|
[6470] | 182 | } |
---|
| 183 | |
---|
| 184 | void SkyBox::enableCubeMap() |
---|
| 185 | { |
---|
[6860] | 186 | glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_REFLECTION_MAP); |
---|
| 187 | glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_REFLECTION_MAP); |
---|
| 188 | glTexGeni(GL_R, GL_TEXTURE_GEN_MODE, GL_REFLECTION_MAP); |
---|
| 189 | |
---|
[6523] | 190 | glEnable(GL_TEXTURE_CUBE_MAP_EXT); |
---|
[6860] | 191 | |
---|
[6470] | 192 | glEnable(GL_TEXTURE_GEN_S); |
---|
| 193 | glEnable(GL_TEXTURE_GEN_T); |
---|
| 194 | glEnable(GL_TEXTURE_GEN_R); |
---|
[6523] | 195 | |
---|
[6470] | 196 | } |
---|
| 197 | |
---|
| 198 | void SkyBox::disableCubeMap() |
---|
| 199 | { |
---|
[6860] | 200 | glDisable(GL_TEXTURE_CUBE_MAP); |
---|
[6519] | 201 | glDisable(GL_TEXTURE_2D); |
---|
[6470] | 202 | glDisable(GL_TEXTURE_GEN_S); |
---|
| 203 | glDisable(GL_TEXTURE_GEN_T); |
---|
| 204 | glDisable(GL_TEXTURE_GEN_R); |
---|
[6523] | 205 | |
---|
| 206 | glDisable(GL_TEXTURE_GEN_S); |
---|
| 207 | glDisable(GL_TEXTURE_GEN_T); |
---|
| 208 | glDisable(GL_TEXTURE_GEN_R); |
---|
[6470] | 209 | } |
---|
| 210 | |
---|
| 211 | |
---|
| 212 | |
---|
[3803] | 213 | /** |
---|
[4836] | 214 | * @param size The new size of the SkyBox |
---|
[4621] | 215 | |
---|
| 216 | * do not forget to rebuild the SkyBox after this. |
---|
[3803] | 217 | */ |
---|
| 218 | void SkyBox::setSize(float size) |
---|
| 219 | { |
---|
| 220 | this->size = size; |
---|
| 221 | } |
---|
| 222 | |
---|
[6634] | 223 | |
---|
| 224 | |
---|
| 225 | void SkyBox::draw() |
---|
| 226 | { |
---|
| 227 | glPushAttrib(GL_ENABLE_BIT); |
---|
| 228 | // glPushAttrib(GL_LIGHTING_BIT); |
---|
| 229 | glDisable(GL_LIGHTING); |
---|
| 230 | |
---|
[6772] | 231 | glPushAttrib(GL_ENABLE_BIT); |
---|
| 232 | glDisable(GL_FOG); |
---|
| 233 | |
---|
[6634] | 234 | WorldEntity::draw(); |
---|
| 235 | |
---|
| 236 | glPopAttrib(); |
---|
[6772] | 237 | glPopAttrib(); |
---|
[6634] | 238 | |
---|
| 239 | } |
---|
| 240 | |
---|
| 241 | |
---|
[3803] | 242 | /** |
---|
[4836] | 243 | * rebuilds the SkyBox |
---|
[4597] | 244 | |
---|
[3803] | 245 | this must be done, when changing the Size of the Skybox (runtime-efficency) |
---|
| 246 | */ |
---|
[3801] | 247 | void SkyBox::rebuild() |
---|
| 248 | { |
---|
[6022] | 249 | StaticModel* model = new StaticModel(); |
---|
[3801] | 250 | |
---|
[5994] | 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); |
---|
| 258 | model->addVertex (0.5*size, -0.5*size, -0.5*size); |
---|
[3801] | 259 | |
---|
[6634] | 260 | // model->addVertexTexture (0.0, 1.0); |
---|
| 261 | // model->addVertexTexture (1.0, 1.0); |
---|
| 262 | // model->addVertexTexture (1.0, 0.0); |
---|
| 263 | // model->addVertexTexture (0.0, 0.0); |
---|
[3801] | 264 | |
---|
[6634] | 265 | model->addVertexTexture (1.0/this->textureSize, (this->textureSize - 1.0)/this->textureSize); |
---|
| 266 | model->addVertexTexture ((this->textureSize - 1.0)/this->textureSize, (this->textureSize - 1.0)/this->textureSize); |
---|
| 267 | model->addVertexTexture ((this->textureSize - 1.0)/this->textureSize, 1.0/this->textureSize); |
---|
| 268 | model->addVertexTexture (1.0/this->textureSize, 1.0/this->textureSize); |
---|
| 269 | |
---|
| 270 | |
---|
[5994] | 271 | model->addVertexNormal (0.0, 0.0, 1.0); |
---|
| 272 | model->addVertexNormal (0.0, 1.0, 0.0); |
---|
| 273 | model->addVertexNormal (0.0, 0.0, -1.0); |
---|
| 274 | model->addVertexNormal (0.0, -1.0, 0.0); |
---|
| 275 | model->addVertexNormal (1.0, 0.0, 0.0); |
---|
| 276 | model->addVertexNormal (-1.0, 0.0, 0.0); |
---|
[3801] | 277 | |
---|
[5994] | 278 | model->setMaterial(material[0]); |
---|
| 279 | model->addFace (4, VERTEX_TEXCOORD_NORMAL, 2,0,3, 3,1,3, 5,2,3, 4,3,3); // top |
---|
| 280 | model->setMaterial(material[1]); |
---|
| 281 | model->addFace (4, VERTEX_TEXCOORD_NORMAL, 6,0,1, 7,1,1, 1,2,1, 0,3,1); // bottom |
---|
| 282 | model->setMaterial(material[2]); |
---|
| 283 | model->addFace (4, VERTEX_TEXCOORD_NORMAL, 4,2,2, 5,3,2, 7,0,2, 6,1,2); // left |
---|
| 284 | model->setMaterial(material[3]); |
---|
| 285 | model->addFace (4, VERTEX_TEXCOORD_NORMAL, 0,0,0, 1,1,0, 3,2,0, 2,3,0); // right |
---|
| 286 | model->setMaterial(material[4]); |
---|
| 287 | model->addFace (4, VERTEX_TEXCOORD_NORMAL, 1,0,5, 7,1,5, 5,2,5, 3,3,5); // front |
---|
| 288 | model->setMaterial(material[5]); |
---|
| 289 | model->addFace (4, VERTEX_TEXCOORD_NORMAL, 6,0,4, 0,1,4, 2,2,4, 4,3,4); // back |
---|
[4597] | 290 | |
---|
[5994] | 291 | model->finalize(); |
---|
| 292 | |
---|
| 293 | this->setModel(model); |
---|
[3801] | 294 | } |
---|
[6341] | 295 | |
---|
| 296 | int SkyBox::writeBytes( const byte * data, int length, int sender ) |
---|
| 297 | { |
---|
| 298 | setRequestedSync( false ); |
---|
| 299 | setIsOutOfSync( false ); |
---|
| 300 | |
---|
| 301 | SYNCHELP_READ_BEGIN(); |
---|
| 302 | |
---|
[6815] | 303 | SYNCHELP_READ_FKT( WorldEntity::writeState, NWT_SB_WE_STATE ); |
---|
[6341] | 304 | |
---|
[6815] | 305 | SYNCHELP_READ_FLOAT( size, NWT_SB_SIZE ); |
---|
[6341] | 306 | if ( textureName ) |
---|
| 307 | { |
---|
| 308 | delete[] textureName; |
---|
| 309 | textureName = NULL; |
---|
| 310 | } |
---|
[6815] | 311 | SYNCHELP_READ_STRINGM( textureName, NWT_SB_TEXTURENAME ); |
---|
[6341] | 312 | |
---|
| 313 | this->setSize( size ); |
---|
| 314 | this->setTextureAndType( textureName, "jpg" ); |
---|
| 315 | this->rebuild(); |
---|
| 316 | |
---|
| 317 | return SYNCHELP_READ_N; |
---|
| 318 | } |
---|
| 319 | |
---|
| 320 | |
---|
| 321 | |
---|
| 322 | int SkyBox::readBytes( byte * data, int maxLength, int * reciever ) |
---|
| 323 | { |
---|
| 324 | if ( isOutOfSync() && !requestedSync() && this->getHostID()!=this->getOwner() ) |
---|
| 325 | { |
---|
| 326 | (NetworkGameManager::getInstance())->sync( this->getUniqueID(), this->getOwner() ); |
---|
| 327 | setRequestedSync( true ); |
---|
| 328 | } |
---|
| 329 | |
---|
| 330 | int rec = this->getRequestSync(); |
---|
| 331 | if ( rec > 0 ) |
---|
| 332 | { |
---|
| 333 | *reciever = rec; |
---|
| 334 | |
---|
| 335 | SYNCHELP_WRITE_BEGIN(); |
---|
| 336 | |
---|
[6815] | 337 | SYNCHELP_WRITE_FKT( WorldEntity::readState, NWT_SB_WE_STATE ); |
---|
[6341] | 338 | |
---|
[6815] | 339 | SYNCHELP_WRITE_FLOAT(this->size, NWT_SB_SIZE); |
---|
| 340 | SYNCHELP_WRITE_STRING(this->textureName, NWT_SB_TEXTURENAME); |
---|
[6341] | 341 | |
---|
| 342 | return SYNCHELP_WRITE_N; |
---|
| 343 | } |
---|
| 344 | |
---|
| 345 | *reciever = 0; |
---|
| 346 | return 0; |
---|
| 347 | } |
---|
| 348 | |
---|
| 349 | void SkyBox::writeDebug( ) const |
---|
| 350 | { |
---|
| 351 | } |
---|
| 352 | |
---|
| 353 | void SkyBox::readDebug( ) const |
---|
| 354 | { |
---|
| 355 | } |
---|