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