[3411] | 1 | |
---|
[4597] | 2 | /* |
---|
[3416] | 3 | orxonox - the future of 3D-vertical-scrollers |
---|
| 4 | |
---|
| 5 | Copyright (C) 2004 orx |
---|
| 6 | |
---|
| 7 | This program is free software; you can redistribute it and/or modify |
---|
| 8 | it under the terms of the GNU General Public License as published by |
---|
| 9 | the Free Software Foundation; either version 2, or (at your option) |
---|
| 10 | any later version. |
---|
| 11 | |
---|
| 12 | ### File Specific: |
---|
[4261] | 13 | main-programmer: Benjamin Grauer |
---|
| 14 | co-programmer: ... |
---|
[3411] | 15 | */ |
---|
| 16 | |
---|
[3590] | 17 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_WORLD_ENTITY |
---|
| 18 | |
---|
[3608] | 19 | |
---|
[3796] | 20 | #include "skybox.h" |
---|
[4010] | 21 | |
---|
[3411] | 22 | #include "stdincl.h" |
---|
[4010] | 23 | #include "factory.h" |
---|
[3608] | 24 | |
---|
[5155] | 25 | #include "load_param.h" |
---|
[3608] | 26 | #include "material.h" |
---|
[3411] | 27 | #include "vector.h" |
---|
[3796] | 28 | #include "resource_manager.h" |
---|
| 29 | #include "model.h" |
---|
[3608] | 30 | //#include "world_entity.h" |
---|
[3411] | 31 | |
---|
[4010] | 32 | CREATE_FACTORY(SkyBox); |
---|
[3608] | 33 | |
---|
[3411] | 34 | using namespace std; |
---|
| 35 | |
---|
[3416] | 36 | /** |
---|
[4836] | 37 | * Constructs a SkyBox and takes fileName as a map. |
---|
| 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 | |
---|
[4261] | 55 | this->loadParams(root); |
---|
[4010] | 56 | |
---|
[4012] | 57 | this->postInit(); |
---|
[4010] | 58 | } |
---|
| 59 | |
---|
[4261] | 60 | void SkyBox::loadParams(const TiXmlElement* root) |
---|
| 61 | { |
---|
[4436] | 62 | static_cast<WorldEntity*>(this)->loadParams(root); |
---|
| 63 | |
---|
[4261] | 64 | LoadParam<SkyBox>(root, "Materialset", this, &SkyBox::setTexture) |
---|
[4621] | 65 | .describe("Sets the material on the SkyBox. The string must be the path relative to the data-dir, and without a trailing .jpg"); |
---|
| 66 | |
---|
| 67 | LoadParam<SkyBox>(root, "Size", this, &SkyBox::setSize) |
---|
| 68 | .describe("Sets the Size of the SkyBox (normally this should be 90% of the maximal viewing Distance)."); |
---|
[4261] | 69 | } |
---|
| 70 | |
---|
[4746] | 71 | void SkyBox::preInit() |
---|
[4010] | 72 | { |
---|
[4320] | 73 | this->setClassID(CL_SKYBOX, "SkyBox"); |
---|
[4597] | 74 | |
---|
[4621] | 75 | this->size = 100.0; |
---|
[4620] | 76 | |
---|
[3801] | 77 | this->material = new Material*[6]; |
---|
[4597] | 78 | for (int i = 0; i < 6; i++) |
---|
[3801] | 79 | { |
---|
| 80 | this->material[i] = new Material(); |
---|
| 81 | this->material[i]->setIllum(3); |
---|
[3805] | 82 | this->material[i]->setDiffuse(0.0,0.0,0.0); |
---|
| 83 | this->material[i]->setSpecular(0.0,0.0,0.0); |
---|
| 84 | this->material[i]->setAmbient(2.0, 2.0, 2.0); |
---|
[3801] | 85 | } |
---|
[4444] | 86 | this->setParentMode(PNODE_MOVEMENT); |
---|
[4012] | 87 | } |
---|
[3803] | 88 | |
---|
[4746] | 89 | void SkyBox::postInit() |
---|
[4012] | 90 | { |
---|
| 91 | this->rebuild(); |
---|
[3411] | 92 | } |
---|
| 93 | |
---|
[3507] | 94 | |
---|
[3416] | 95 | /** |
---|
[4836] | 96 | * default destructor |
---|
[3416] | 97 | */ |
---|
[3796] | 98 | SkyBox::~SkyBox() |
---|
[3411] | 99 | { |
---|
[4136] | 100 | PRINTF(5)("Deleting SkyBox\n"); |
---|
[3801] | 101 | for (int i = 0; i < 6; i++) |
---|
| 102 | delete this->material[i]; |
---|
| 103 | delete []this->material; |
---|
[3419] | 104 | } |
---|
[3411] | 105 | |
---|
[3803] | 106 | /** |
---|
[4836] | 107 | * sets A set of textures when just giving a Name and an extension: |
---|
[3763] | 108 | |
---|
[3803] | 109 | usage: give this function an argument like |
---|
| 110 | setTexture("skybox", "jpg"); |
---|
[4597] | 111 | and it will convert this to |
---|
[3803] | 112 | setTextures("skybox_top.jpg", "skybox_bottom.jpg", "skybox_left.jpg", |
---|
| 113 | "skybox_right.jpg", "skybox_front.jpg", "skybox_back.jpg"); |
---|
| 114 | */ |
---|
[4261] | 115 | void SkyBox::setTextureAndType(const char* name, const char* extension) |
---|
[3803] | 116 | { |
---|
[4012] | 117 | char* top = new char[strlen(name)+strlen(extension)+ 10]; |
---|
| 118 | char* bottom = new char[strlen(name)+strlen(extension)+ 10]; |
---|
| 119 | char* left = new char[strlen(name)+strlen(extension)+ 10]; |
---|
| 120 | char* right = new char[strlen(name)+strlen(extension)+ 10]; |
---|
| 121 | char* front = new char[strlen(name)+strlen(extension)+ 10]; |
---|
| 122 | char* back = new char[strlen(name)+strlen(extension)+ 10]; |
---|
[3803] | 123 | |
---|
| 124 | sprintf(top, "%s_top.%s", name, extension); |
---|
| 125 | sprintf(bottom, "%s_bottom.%s", name, extension); |
---|
| 126 | sprintf(left, "%s_left.%s", name, extension); |
---|
| 127 | sprintf(right, "%s_right.%s", name, extension); |
---|
| 128 | sprintf(front, "%s_front.%s", name, extension); |
---|
| 129 | sprintf(back, "%s_back.%s", name, extension); |
---|
[4597] | 130 | |
---|
[3803] | 131 | this->setTextures(top, bottom, left, right, front, back); |
---|
| 132 | |
---|
[4012] | 133 | // deleted alocated memory of this function |
---|
[3803] | 134 | delete []top; |
---|
| 135 | delete []bottom; |
---|
| 136 | delete []left; |
---|
| 137 | delete []right; |
---|
| 138 | delete []front; |
---|
| 139 | delete []back; |
---|
| 140 | } |
---|
| 141 | |
---|
| 142 | /** |
---|
[4836] | 143 | * Defines which textures should be loaded onto the SkyBox. |
---|
| 144 | * @param top the top texture. |
---|
| 145 | * @param bottom the bottom texture. |
---|
| 146 | * @param left the left texture. |
---|
| 147 | * @param right the right texture. |
---|
| 148 | * @param front the front texture. |
---|
| 149 | * @param back the back texture. |
---|
[3803] | 150 | */ |
---|
| 151 | void SkyBox::setTextures(const char* top, const char* bottom, const char* left, const char* right, const char* front, const char* back) |
---|
| 152 | { |
---|
| 153 | this->material[0]->setDiffuseMap(top); |
---|
| 154 | this->material[1]->setDiffuseMap(bottom); |
---|
| 155 | this->material[2]->setDiffuseMap(left); |
---|
| 156 | this->material[3]->setDiffuseMap(right); |
---|
| 157 | this->material[4]->setDiffuseMap(front); |
---|
| 158 | this->material[5]->setDiffuseMap(back); |
---|
| 159 | } |
---|
| 160 | |
---|
| 161 | /** |
---|
[4836] | 162 | * @param size The new size of the SkyBox |
---|
[4621] | 163 | |
---|
| 164 | * do not forget to rebuild the SkyBox after this. |
---|
[3803] | 165 | */ |
---|
| 166 | void SkyBox::setSize(float size) |
---|
| 167 | { |
---|
| 168 | this->size = size; |
---|
| 169 | } |
---|
| 170 | |
---|
| 171 | /** |
---|
[4836] | 172 | * rebuilds the SkyBox |
---|
[4597] | 173 | |
---|
[3803] | 174 | this must be done, when changing the Size of the Skybox (runtime-efficency) |
---|
| 175 | */ |
---|
[3801] | 176 | void SkyBox::rebuild() |
---|
| 177 | { |
---|
[4680] | 178 | if (this->model) |
---|
| 179 | delete this->model; |
---|
| 180 | model = new Model(); |
---|
[3801] | 181 | |
---|
[4680] | 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); |
---|
| 185 | this->model->addVertex (0.5*size, 0.5*size, 0.5*size); |
---|
| 186 | this->model->addVertex (-0.5*size, 0.5*size, -0.5*size); |
---|
| 187 | this->model->addVertex (0.5*size, 0.5*size, -0.5*size); |
---|
| 188 | this->model->addVertex (-0.5*size, -0.5*size, -0.5*size); |
---|
| 189 | this->model->addVertex (0.5*size, -0.5*size, -0.5*size); |
---|
[3801] | 190 | |
---|
[4680] | 191 | this->model->addVertexTexture (0.0, 1.0); |
---|
| 192 | this->model->addVertexTexture (1.0, 1.0); |
---|
| 193 | this->model->addVertexTexture (1.0, 0.0); |
---|
| 194 | this->model->addVertexTexture (0.0, 0.0); |
---|
[3801] | 195 | |
---|
[4680] | 196 | this->model->addVertexNormal (0.0, 0.0, 1.0); |
---|
| 197 | this->model->addVertexNormal (0.0, 1.0, 0.0); |
---|
| 198 | this->model->addVertexNormal (0.0, 0.0, -1.0); |
---|
| 199 | this->model->addVertexNormal (0.0, -1.0, 0.0); |
---|
| 200 | this->model->addVertexNormal (1.0, 0.0, 0.0); |
---|
| 201 | this->model->addVertexNormal (-1.0, 0.0, 0.0); |
---|
[3801] | 202 | |
---|
[4680] | 203 | this->model->setMaterial(material[0]); |
---|
| 204 | this->model->addFace (4, VERTEX_TEXCOORD_NORMAL, 2,0,3, 3,1,3, 5,2,3, 4,3,3); // top |
---|
| 205 | this->model->setMaterial(material[1]); |
---|
| 206 | this->model->addFace (4, VERTEX_TEXCOORD_NORMAL, 6,0,1, 7,1,1, 1,2,1, 0,3,1); // bottom |
---|
| 207 | this->model->setMaterial(material[2]); |
---|
| 208 | this->model->addFace (4, VERTEX_TEXCOORD_NORMAL, 4,2,2, 5,3,2, 7,0,2, 6,1,2); // left |
---|
| 209 | this->model->setMaterial(material[3]); |
---|
| 210 | this->model->addFace (4, VERTEX_TEXCOORD_NORMAL, 0,0,0, 1,1,0, 3,2,0, 2,3,0); // right |
---|
| 211 | this->model->setMaterial(material[4]); |
---|
| 212 | this->model->addFace (4, VERTEX_TEXCOORD_NORMAL, 1,0,5, 7,1,5, 5,2,5, 3,3,5); // front |
---|
| 213 | this->model->setMaterial(material[5]); |
---|
| 214 | this->model->addFace (4, VERTEX_TEXCOORD_NORMAL, 6,0,4, 0,1,4, 2,2,4, 4,3,4); // back |
---|
[4597] | 215 | |
---|
[4680] | 216 | this->model->finalize(); |
---|
[3801] | 217 | } |
---|