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