[3411] | 1 | |
---|
[3416] | 2 | /* |
---|
| 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 | /** |
---|
[3796] | 36 | \brief 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 | |
---|
[4261] | 47 | SkyBox::SkyBox(const TiXmlElement* root) : WorldEntity(root) |
---|
[4010] | 48 | { |
---|
[4012] | 49 | this->preInit(); |
---|
[4010] | 50 | |
---|
[4261] | 51 | this->loadParams(root); |
---|
[4010] | 52 | |
---|
[4012] | 53 | this->postInit(); |
---|
[4010] | 54 | } |
---|
| 55 | |
---|
[4261] | 56 | void SkyBox::loadParams(const TiXmlElement* root) |
---|
| 57 | { |
---|
| 58 | LoadParam<SkyBox>(root, "Materialset", this, &SkyBox::setTexture) |
---|
| 59 | .describe("Sets the material on the SkyBox. The string must be the path relative to the data-dir, and without a trailing .jpg"); |
---|
| 60 | } |
---|
| 61 | |
---|
[4012] | 62 | void SkyBox::preInit(void) |
---|
[4010] | 63 | { |
---|
[4320] | 64 | this->setClassID(CL_SKYBOX, "SkyBox"); |
---|
[4136] | 65 | this->skyModel = NULL; |
---|
[3801] | 66 | this->material = new Material*[6]; |
---|
[4136] | 67 | for (int i = 0; i < 6; i++) |
---|
[3801] | 68 | { |
---|
| 69 | this->material[i] = new Material(); |
---|
| 70 | this->material[i]->setIllum(3); |
---|
[3805] | 71 | this->material[i]->setDiffuse(0.0,0.0,0.0); |
---|
| 72 | this->material[i]->setSpecular(0.0,0.0,0.0); |
---|
| 73 | this->material[i]->setAmbient(2.0, 2.0, 2.0); |
---|
[3801] | 74 | } |
---|
[3803] | 75 | this->setMode(PNODE_MOVEMENT); |
---|
[4012] | 76 | } |
---|
[3803] | 77 | |
---|
[4012] | 78 | void SkyBox::postInit(void) |
---|
| 79 | { |
---|
[3803] | 80 | this->setSize(1900.0); |
---|
[4012] | 81 | this->rebuild(); |
---|
[3411] | 82 | } |
---|
| 83 | |
---|
[3507] | 84 | |
---|
[3416] | 85 | /** |
---|
| 86 | \brief default destructor |
---|
| 87 | */ |
---|
[3796] | 88 | SkyBox::~SkyBox() |
---|
[3411] | 89 | { |
---|
[4136] | 90 | PRINTF(5)("Deleting SkyBox\n"); |
---|
[3801] | 91 | for (int i = 0; i < 6; i++) |
---|
| 92 | delete this->material[i]; |
---|
| 93 | delete []this->material; |
---|
[3419] | 94 | } |
---|
[3411] | 95 | |
---|
[3803] | 96 | /** |
---|
| 97 | \brief sets A set of textures when just giving a Name and an extension: |
---|
[3763] | 98 | |
---|
[3803] | 99 | usage: give this function an argument like |
---|
| 100 | setTexture("skybox", "jpg"); |
---|
| 101 | and it will convert this to |
---|
| 102 | setTextures("skybox_top.jpg", "skybox_bottom.jpg", "skybox_left.jpg", |
---|
| 103 | "skybox_right.jpg", "skybox_front.jpg", "skybox_back.jpg"); |
---|
| 104 | */ |
---|
[4261] | 105 | void SkyBox::setTextureAndType(const char* name, const char* extension) |
---|
[3803] | 106 | { |
---|
[4012] | 107 | char* top = new char[strlen(name)+strlen(extension)+ 10]; |
---|
| 108 | char* bottom = new char[strlen(name)+strlen(extension)+ 10]; |
---|
| 109 | char* left = new char[strlen(name)+strlen(extension)+ 10]; |
---|
| 110 | char* right = new char[strlen(name)+strlen(extension)+ 10]; |
---|
| 111 | char* front = new char[strlen(name)+strlen(extension)+ 10]; |
---|
| 112 | char* back = new char[strlen(name)+strlen(extension)+ 10]; |
---|
[3803] | 113 | |
---|
| 114 | sprintf(top, "%s_top.%s", name, extension); |
---|
| 115 | sprintf(bottom, "%s_bottom.%s", name, extension); |
---|
| 116 | sprintf(left, "%s_left.%s", name, extension); |
---|
| 117 | sprintf(right, "%s_right.%s", name, extension); |
---|
| 118 | sprintf(front, "%s_front.%s", name, extension); |
---|
| 119 | sprintf(back, "%s_back.%s", name, extension); |
---|
| 120 | |
---|
| 121 | this->setTextures(top, bottom, left, right, front, back); |
---|
| 122 | |
---|
[4012] | 123 | // deleted alocated memory of this function |
---|
[3803] | 124 | delete []top; |
---|
| 125 | delete []bottom; |
---|
| 126 | delete []left; |
---|
| 127 | delete []right; |
---|
| 128 | delete []front; |
---|
| 129 | delete []back; |
---|
| 130 | } |
---|
| 131 | |
---|
| 132 | /** |
---|
| 133 | \brief Defines which textures should be loaded onto the SkyBox. |
---|
| 134 | \param top the top texture. |
---|
| 135 | \param bottom the bottom texture. |
---|
| 136 | \param left the left texture. |
---|
| 137 | \param right the right texture. |
---|
| 138 | \param front the front texture. |
---|
| 139 | \param back the back texture. |
---|
| 140 | */ |
---|
| 141 | void SkyBox::setTextures(const char* top, const char* bottom, const char* left, const char* right, const char* front, const char* back) |
---|
| 142 | { |
---|
| 143 | this->material[0]->setDiffuseMap(top); |
---|
| 144 | this->material[1]->setDiffuseMap(bottom); |
---|
| 145 | this->material[2]->setDiffuseMap(left); |
---|
| 146 | this->material[3]->setDiffuseMap(right); |
---|
| 147 | this->material[4]->setDiffuseMap(front); |
---|
| 148 | this->material[5]->setDiffuseMap(back); |
---|
| 149 | } |
---|
| 150 | |
---|
| 151 | /** |
---|
[3806] | 152 | \param size The new size of the SkyBox |
---|
[3803] | 153 | */ |
---|
| 154 | void SkyBox::setSize(float size) |
---|
| 155 | { |
---|
| 156 | this->size = size; |
---|
| 157 | } |
---|
| 158 | |
---|
| 159 | /** |
---|
[3807] | 160 | \brief draws the SkyBox |
---|
| 161 | */ |
---|
| 162 | void SkyBox::draw() |
---|
| 163 | { |
---|
| 164 | glPushMatrix(); |
---|
| 165 | glMatrixMode(GL_MODELVIEW); |
---|
| 166 | Vector r = this->getAbsCoor(); |
---|
| 167 | glTranslatef(r.x, r.y, r.z); |
---|
| 168 | |
---|
[4136] | 169 | this->skyModel->draw(); |
---|
[3807] | 170 | |
---|
| 171 | glPopMatrix(); |
---|
| 172 | } |
---|
| 173 | |
---|
| 174 | |
---|
| 175 | /** |
---|
[3803] | 176 | \brief rebuilds the SkyBox |
---|
| 177 | |
---|
| 178 | this must be done, when changing the Size of the Skybox (runtime-efficency) |
---|
| 179 | */ |
---|
[3801] | 180 | void SkyBox::rebuild() |
---|
| 181 | { |
---|
[4136] | 182 | if (this->skyModel) |
---|
| 183 | delete skyModel; |
---|
| 184 | skyModel = new Model(); |
---|
[3801] | 185 | |
---|
[4136] | 186 | this->skyModel->addVertex (-0.5*size, -0.5*size, 0.5*size); |
---|
| 187 | this->skyModel->addVertex (0.5*size, -0.5*size, 0.5*size); |
---|
| 188 | this->skyModel->addVertex (-0.5*size, 0.5*size, 0.5*size); |
---|
| 189 | this->skyModel->addVertex (0.5*size, 0.5*size, 0.5*size); |
---|
| 190 | this->skyModel->addVertex (-0.5*size, 0.5*size, -0.5*size); |
---|
| 191 | this->skyModel->addVertex (0.5*size, 0.5*size, -0.5*size); |
---|
| 192 | this->skyModel->addVertex (-0.5*size, -0.5*size, -0.5*size); |
---|
| 193 | this->skyModel->addVertex (0.5*size, -0.5*size, -0.5*size); |
---|
[3801] | 194 | |
---|
[4357] | 195 | this->skyModel->addVertexTexture (0.0, 1.0); |
---|
| 196 | this->skyModel->addVertexTexture (1.0, 1.0); |
---|
| 197 | this->skyModel->addVertexTexture (1.0, 0.0); |
---|
[4136] | 198 | this->skyModel->addVertexTexture (0.0, 0.0); |
---|
[3801] | 199 | |
---|
[4136] | 200 | this->skyModel->addVertexNormal (0.0, 0.0, 1.0); |
---|
| 201 | this->skyModel->addVertexNormal (0.0, 1.0, 0.0); |
---|
| 202 | this->skyModel->addVertexNormal (0.0, 0.0, -1.0); |
---|
| 203 | this->skyModel->addVertexNormal (0.0, -1.0, 0.0); |
---|
| 204 | this->skyModel->addVertexNormal (1.0, 0.0, 0.0); |
---|
| 205 | this->skyModel->addVertexNormal (-1.0, 0.0, 0.0); |
---|
[3801] | 206 | |
---|
[4136] | 207 | this->skyModel->setMaterial(material[0]); |
---|
[4261] | 208 | this->skyModel->addFace (4, VERTEX_TEXCOORD_NORMAL, 2,0,3, 3,1,3, 5,2,3, 4,3,3); // top |
---|
[4136] | 209 | this->skyModel->setMaterial(material[1]); |
---|
[4261] | 210 | this->skyModel->addFace (4, VERTEX_TEXCOORD_NORMAL, 6,0,1, 7,1,1, 1,2,1, 0,3,1); // bottom |
---|
[4136] | 211 | this->skyModel->setMaterial(material[2]); |
---|
[4261] | 212 | this->skyModel->addFace (4, VERTEX_TEXCOORD_NORMAL, 4,2,2, 5,3,2, 7,0,2, 6,1,2); // left |
---|
[4136] | 213 | this->skyModel->setMaterial(material[3]); |
---|
[4261] | 214 | this->skyModel->addFace (4, VERTEX_TEXCOORD_NORMAL, 0,0,0, 1,1,0, 3,2,0, 2,3,0); // right |
---|
[4136] | 215 | this->skyModel->setMaterial(material[4]); |
---|
[4261] | 216 | this->skyModel->addFace (4, VERTEX_TEXCOORD_NORMAL, 1,0,5, 7,1,5, 5,2,5, 3,3,5); // front |
---|
[4136] | 217 | this->skyModel->setMaterial(material[5]); |
---|
| 218 | this->skyModel->addFace (4, VERTEX_TEXCOORD_NORMAL, 6,0,4, 0,1,4, 2,2,4, 4,3,4); // back |
---|
[3803] | 219 | |
---|
[4136] | 220 | this->skyModel->finalize(); |
---|
[3801] | 221 | } |
---|