| [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" | 
|---|
| [5511] | 22 | #include "model.h" | 
|---|
| [3608] | 23 |  | 
|---|
| [5511] | 24 |  | 
|---|
| [5356] | 25 | using namespace std; | 
|---|
| [5357] | 26 |  | 
|---|
| [4010] | 27 | CREATE_FACTORY(SkyBox); | 
|---|
| [3608] | 28 |  | 
|---|
| [3416] | 29 | /** | 
|---|
| [5357] | 30 |  * Constructs a SkyBox and takes fileName as a map. | 
|---|
| [4836] | 31 |  * @param fileName the file to take as input for the SkyBox | 
|---|
| [3419] | 32 | */ | 
|---|
| [4261] | 33 | SkyBox::SkyBox(const char* fileName) | 
|---|
| [3419] | 34 | { | 
|---|
| [4012] | 35 |   this->preInit(); | 
|---|
| [4261] | 36 |   if (fileName) | 
|---|
 | 37 |     this->setTextureAndType(fileName, ".jpg"); | 
|---|
| [4012] | 38 |   this->postInit(); | 
|---|
| [4010] | 39 | } | 
|---|
 | 40 |  | 
|---|
| [4444] | 41 | /** | 
|---|
| [4836] | 42 |  *  initializes a skybox from a XmlElement | 
|---|
| [4444] | 43 | */ | 
|---|
| [4436] | 44 | SkyBox::SkyBox(const TiXmlElement* root) | 
|---|
| [4010] | 45 | { | 
|---|
| [4012] | 46 |   this->preInit(); | 
|---|
| [4010] | 47 |  | 
|---|
| [4261] | 48 |   this->loadParams(root); | 
|---|
| [4010] | 49 |  | 
|---|
| [4012] | 50 |   this->postInit(); | 
|---|
| [4010] | 51 | } | 
|---|
 | 52 |  | 
|---|
| [4261] | 53 | void SkyBox::loadParams(const TiXmlElement* root) | 
|---|
 | 54 | { | 
|---|
| [4436] | 55 |   static_cast<WorldEntity*>(this)->loadParams(root); | 
|---|
 | 56 |  | 
|---|
| [4261] | 57 |   LoadParam<SkyBox>(root, "Materialset", this, &SkyBox::setTexture) | 
|---|
| [4621] | 58 |       .describe("Sets the material on the SkyBox. The string must be the path relative to the data-dir, and without a trailing .jpg"); | 
|---|
 | 59 |  | 
|---|
 | 60 |   LoadParam<SkyBox>(root, "Size", this, &SkyBox::setSize) | 
|---|
 | 61 |       .describe("Sets the Size of the SkyBox (normally this should be 90% of the maximal viewing Distance)."); | 
|---|
| [4261] | 62 | } | 
|---|
 | 63 |  | 
|---|
| [4746] | 64 | void SkyBox::preInit() | 
|---|
| [4010] | 65 | { | 
|---|
| [4320] | 66 |   this->setClassID(CL_SKYBOX, "SkyBox"); | 
|---|
| [4597] | 67 |  | 
|---|
| [4621] | 68 |   this->size = 100.0; | 
|---|
| [4620] | 69 |  | 
|---|
| [3801] | 70 |   this->material = new Material*[6]; | 
|---|
| [4597] | 71 |   for (int i = 0; i < 6; i++) | 
|---|
| [3801] | 72 |     { | 
|---|
 | 73 |       this->material[i] = new Material(); | 
|---|
 | 74 |       this->material[i]->setIllum(3); | 
|---|
| [3805] | 75 |       this->material[i]->setDiffuse(0.0,0.0,0.0); | 
|---|
 | 76 |       this->material[i]->setSpecular(0.0,0.0,0.0); | 
|---|
 | 77 |       this->material[i]->setAmbient(2.0, 2.0, 2.0); | 
|---|
| [3801] | 78 |     } | 
|---|
| [4444] | 79 |   this->setParentMode(PNODE_MOVEMENT); | 
|---|
| [4012] | 80 | } | 
|---|
| [3803] | 81 |  | 
|---|
| [4746] | 82 | void SkyBox::postInit() | 
|---|
| [4012] | 83 | { | 
|---|
 | 84 |   this->rebuild(); | 
|---|
| [3411] | 85 | } | 
|---|
 | 86 |  | 
|---|
| [3507] | 87 |  | 
|---|
| [3416] | 88 | /** | 
|---|
| [4836] | 89 |  *  default destructor | 
|---|
| [3416] | 90 | */ | 
|---|
| [3796] | 91 | SkyBox::~SkyBox() | 
|---|
| [3411] | 92 | { | 
|---|
| [4136] | 93 |   PRINTF(5)("Deleting SkyBox\n"); | 
|---|
| [3801] | 94 |   for (int i = 0; i < 6; i++) | 
|---|
 | 95 |     delete this->material[i]; | 
|---|
| [5308] | 96 |   delete[] this->material; | 
|---|
| [5212] | 97 |   delete this->model; | 
|---|
| [5302] | 98 |   this->model = NULL; //< so that WorldEntity does not try to delete it again. | 
|---|
| [3419] | 99 | } | 
|---|
| [3411] | 100 |  | 
|---|
| [3803] | 101 | /** | 
|---|
| [4836] | 102 |  *  sets A set of textures when just giving a Name and an extension: | 
|---|
| [3763] | 103 |  | 
|---|
| [3803] | 104 |    usage: give this function an argument like | 
|---|
 | 105 |    setTexture("skybox", "jpg"); | 
|---|
| [4597] | 106 |    and it will convert this to | 
|---|
| [3803] | 107 |    setTextures("skybox_top.jpg", "skybox_bottom.jpg", "skybox_left.jpg", | 
|---|
 | 108 |                "skybox_right.jpg", "skybox_front.jpg", "skybox_back.jpg"); | 
|---|
 | 109 | */ | 
|---|
| [4261] | 110 | void SkyBox::setTextureAndType(const char* name, const char* extension) | 
|---|
| [3803] | 111 | { | 
|---|
| [4012] | 112 |   char* top    = new char[strlen(name)+strlen(extension)+ 10]; | 
|---|
 | 113 |   char* bottom = new char[strlen(name)+strlen(extension)+ 10]; | 
|---|
 | 114 |   char* left   = new char[strlen(name)+strlen(extension)+ 10]; | 
|---|
 | 115 |   char* right  = new char[strlen(name)+strlen(extension)+ 10]; | 
|---|
 | 116 |   char* front  = new char[strlen(name)+strlen(extension)+ 10]; | 
|---|
 | 117 |   char* back   = new char[strlen(name)+strlen(extension)+ 10]; | 
|---|
| [3803] | 118 |  | 
|---|
 | 119 |   sprintf(top, "%s_top.%s", name, extension); | 
|---|
 | 120 |   sprintf(bottom, "%s_bottom.%s", name, extension); | 
|---|
 | 121 |   sprintf(left, "%s_left.%s", name, extension); | 
|---|
 | 122 |   sprintf(right, "%s_right.%s", name, extension); | 
|---|
 | 123 |   sprintf(front, "%s_front.%s", name, extension); | 
|---|
 | 124 |   sprintf(back, "%s_back.%s", name, extension); | 
|---|
| [4597] | 125 |  | 
|---|
| [3803] | 126 |   this->setTextures(top, bottom, left, right, front, back); | 
|---|
 | 127 |  | 
|---|
| [4012] | 128 |   // deleted alocated memory of this function | 
|---|
| [3803] | 129 |   delete []top; | 
|---|
 | 130 |   delete []bottom; | 
|---|
 | 131 |   delete []left; | 
|---|
 | 132 |   delete []right; | 
|---|
 | 133 |   delete []front; | 
|---|
 | 134 |   delete []back; | 
|---|
 | 135 | } | 
|---|
 | 136 |  | 
|---|
 | 137 | /** | 
|---|
| [4836] | 138 |  *  Defines which textures should be loaded onto the SkyBox. | 
|---|
 | 139 |  * @param top the top texture. | 
|---|
 | 140 |  * @param bottom the bottom texture. | 
|---|
 | 141 |  * @param left the left texture. | 
|---|
 | 142 |  * @param right the right texture. | 
|---|
 | 143 |  * @param front the front texture. | 
|---|
 | 144 |  * @param back the back texture. | 
|---|
| [3803] | 145 | */ | 
|---|
 | 146 | void SkyBox::setTextures(const char* top, const char* bottom, const char* left, const char* right, const char* front, const char* back) | 
|---|
 | 147 | { | 
|---|
 | 148 |   this->material[0]->setDiffuseMap(top); | 
|---|
 | 149 |   this->material[1]->setDiffuseMap(bottom); | 
|---|
 | 150 |   this->material[2]->setDiffuseMap(left); | 
|---|
 | 151 |   this->material[3]->setDiffuseMap(right); | 
|---|
 | 152 |   this->material[4]->setDiffuseMap(front); | 
|---|
 | 153 |   this->material[5]->setDiffuseMap(back); | 
|---|
 | 154 | } | 
|---|
 | 155 |  | 
|---|
 | 156 | /** | 
|---|
| [4836] | 157 |  * @param size The new size of the SkyBox | 
|---|
| [4621] | 158 |  | 
|---|
 | 159 |  * do not forget to rebuild the SkyBox after this. | 
|---|
| [3803] | 160 | */ | 
|---|
 | 161 | void SkyBox::setSize(float size) | 
|---|
 | 162 | { | 
|---|
 | 163 |   this->size = size; | 
|---|
 | 164 | } | 
|---|
 | 165 |  | 
|---|
 | 166 | /** | 
|---|
| [4836] | 167 |  *  rebuilds the SkyBox | 
|---|
| [4597] | 168 |  | 
|---|
| [3803] | 169 |    this must be done, when changing the Size of the Skybox (runtime-efficency) | 
|---|
 | 170 | */ | 
|---|
| [3801] | 171 | void SkyBox::rebuild() | 
|---|
 | 172 | { | 
|---|
| [4680] | 173 |   if (this->model) | 
|---|
 | 174 |     delete this->model; | 
|---|
 | 175 |   model = new Model(); | 
|---|
| [3801] | 176 |  | 
|---|
| [4680] | 177 |   this->model->addVertex (-0.5*size, -0.5*size, 0.5*size); | 
|---|
 | 178 |   this->model->addVertex (0.5*size, -0.5*size, 0.5*size); | 
|---|
 | 179 |   this->model->addVertex (-0.5*size, 0.5*size, 0.5*size); | 
|---|
 | 180 |   this->model->addVertex (0.5*size, 0.5*size, 0.5*size); | 
|---|
 | 181 |   this->model->addVertex (-0.5*size, 0.5*size, -0.5*size); | 
|---|
 | 182 |   this->model->addVertex (0.5*size, 0.5*size, -0.5*size); | 
|---|
 | 183 |   this->model->addVertex (-0.5*size, -0.5*size, -0.5*size); | 
|---|
 | 184 |   this->model->addVertex (0.5*size, -0.5*size, -0.5*size); | 
|---|
| [3801] | 185 |  | 
|---|
| [4680] | 186 |   this->model->addVertexTexture (0.0, 1.0); | 
|---|
 | 187 |   this->model->addVertexTexture (1.0, 1.0); | 
|---|
 | 188 |   this->model->addVertexTexture (1.0, 0.0); | 
|---|
 | 189 |   this->model->addVertexTexture (0.0, 0.0); | 
|---|
| [3801] | 190 |  | 
|---|
| [4680] | 191 |   this->model->addVertexNormal (0.0, 0.0, 1.0); | 
|---|
 | 192 |   this->model->addVertexNormal (0.0, 1.0, 0.0); | 
|---|
 | 193 |   this->model->addVertexNormal (0.0, 0.0, -1.0); | 
|---|
 | 194 |   this->model->addVertexNormal (0.0, -1.0, 0.0); | 
|---|
 | 195 |   this->model->addVertexNormal (1.0, 0.0, 0.0); | 
|---|
 | 196 |   this->model->addVertexNormal (-1.0, 0.0, 0.0); | 
|---|
| [3801] | 197 |  | 
|---|
| [4680] | 198 |   this->model->setMaterial(material[0]); | 
|---|
 | 199 |   this->model->addFace (4, VERTEX_TEXCOORD_NORMAL, 2,0,3, 3,1,3, 5,2,3, 4,3,3); // top | 
|---|
 | 200 |   this->model->setMaterial(material[1]); | 
|---|
 | 201 |   this->model->addFace (4, VERTEX_TEXCOORD_NORMAL, 6,0,1, 7,1,1, 1,2,1, 0,3,1); // bottom | 
|---|
 | 202 |   this->model->setMaterial(material[2]); | 
|---|
 | 203 |   this->model->addFace (4, VERTEX_TEXCOORD_NORMAL, 4,2,2, 5,3,2, 7,0,2, 6,1,2); // left | 
|---|
 | 204 |   this->model->setMaterial(material[3]); | 
|---|
 | 205 |   this->model->addFace (4, VERTEX_TEXCOORD_NORMAL, 0,0,0, 1,1,0, 3,2,0, 2,3,0); // right | 
|---|
 | 206 |   this->model->setMaterial(material[4]); | 
|---|
 | 207 |   this->model->addFace (4, VERTEX_TEXCOORD_NORMAL, 1,0,5, 7,1,5, 5,2,5, 3,3,5); // front | 
|---|
 | 208 |   this->model->setMaterial(material[5]); | 
|---|
 | 209 |   this->model->addFace (4, VERTEX_TEXCOORD_NORMAL, 6,0,4, 0,1,4, 2,2,4, 4,3,4); // back | 
|---|
| [4597] | 210 |  | 
|---|
| [4680] | 211 |   this->model->finalize(); | 
|---|
| [3801] | 212 | } | 
|---|