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