[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: |
---|
| 13 | main-programmer: David Gruetter |
---|
[3421] | 14 | co-programmer: Benjamin Grauer |
---|
[3484] | 15 | |
---|
| 16 | Created by Dave: this file is actually quite similar to player.cc and so is |
---|
| 17 | skybox.h similar to player.h |
---|
| 18 | With that said, things should be clear:) |
---|
| 19 | |
---|
| 20 | Edited: |
---|
| 21 | Bensch: more constructors, changeability, comments... |
---|
| 22 | Patrick: giving it the common orxonox style, not much to do... good work Dave! |
---|
[3416] | 23 | |
---|
[3411] | 24 | */ |
---|
| 25 | |
---|
[3590] | 26 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_WORLD_ENTITY |
---|
| 27 | |
---|
[3608] | 28 | |
---|
[3796] | 29 | #include "skybox.h" |
---|
[4010] | 30 | |
---|
[3411] | 31 | #include "stdincl.h" |
---|
[4010] | 32 | #include "factory.h" |
---|
[3608] | 33 | |
---|
| 34 | #include "material.h" |
---|
[3411] | 35 | #include "vector.h" |
---|
[3796] | 36 | #include "resource_manager.h" |
---|
| 37 | #include "model.h" |
---|
[3608] | 38 | //#include "world_entity.h" |
---|
[3411] | 39 | |
---|
[4010] | 40 | CREATE_FACTORY(SkyBox); |
---|
[3608] | 41 | |
---|
[3411] | 42 | using namespace std; |
---|
| 43 | |
---|
[3416] | 44 | /** |
---|
[3796] | 45 | \brief Constructs a SkyBox and takes fileName as a map. |
---|
| 46 | \param fileName the file to take as input for the SkyBox |
---|
[3419] | 47 | */ |
---|
[3796] | 48 | SkyBox::SkyBox(char* fileName) |
---|
[3419] | 49 | { |
---|
[4012] | 50 | this->preInit(); |
---|
| 51 | this->postInit(); |
---|
[4010] | 52 | } |
---|
| 53 | |
---|
| 54 | SkyBox::SkyBox(TiXmlElement* root) : WorldEntity(root) |
---|
| 55 | { |
---|
[4012] | 56 | this->preInit(); |
---|
[4010] | 57 | |
---|
| 58 | const char* string; |
---|
| 59 | |
---|
| 60 | // Model Loading |
---|
| 61 | string = grabParameter( root, "materialset"); |
---|
| 62 | if( string != NULL) |
---|
| 63 | this->setTexture(string, "jpg"); |
---|
| 64 | else |
---|
| 65 | { |
---|
| 66 | PRINTF(0)("SkyBox is missing a proper 'MaterialSet'\n"); |
---|
| 67 | } |
---|
| 68 | if( this->model == NULL) |
---|
| 69 | { |
---|
| 70 | PRINTF(0)("SkyBox model '%s' could not be loaded\n", string); |
---|
| 71 | } |
---|
[4012] | 72 | this->postInit(); |
---|
[4010] | 73 | } |
---|
| 74 | |
---|
[4012] | 75 | void SkyBox::preInit(void) |
---|
[4010] | 76 | { |
---|
[3796] | 77 | this->setClassName("SkyBox"); |
---|
[3801] | 78 | this->material = new Material*[6]; |
---|
| 79 | for (int i = 0; i <6; i++) |
---|
| 80 | { |
---|
| 81 | this->material[i] = new Material(); |
---|
| 82 | this->material[i]->setIllum(3); |
---|
[3805] | 83 | this->material[i]->setDiffuse(0.0,0.0,0.0); |
---|
| 84 | this->material[i]->setSpecular(0.0,0.0,0.0); |
---|
| 85 | this->material[i]->setAmbient(2.0, 2.0, 2.0); |
---|
[3801] | 86 | } |
---|
[3803] | 87 | this->setMode(PNODE_MOVEMENT); |
---|
[4012] | 88 | } |
---|
[3803] | 89 | |
---|
[4012] | 90 | void SkyBox::postInit(void) |
---|
| 91 | { |
---|
[3803] | 92 | this->setSize(1900.0); |
---|
[4012] | 93 | this->rebuild(); |
---|
[3411] | 94 | } |
---|
| 95 | |
---|
[3507] | 96 | |
---|
[3416] | 97 | /** |
---|
| 98 | \brief default destructor |
---|
| 99 | */ |
---|
[3796] | 100 | SkyBox::~SkyBox() |
---|
[3411] | 101 | { |
---|
[3801] | 102 | PRINTF(5)("Deleting the SkyBox\n"); |
---|
| 103 | for (int i = 0; i < 6; i++) |
---|
| 104 | delete this->material[i]; |
---|
| 105 | delete []this->material; |
---|
[3419] | 106 | } |
---|
[3411] | 107 | |
---|
[3803] | 108 | /** |
---|
| 109 | \brief sets A set of textures when just giving a Name and an extension: |
---|
[3763] | 110 | |
---|
[3803] | 111 | usage: give this function an argument like |
---|
| 112 | setTexture("skybox", "jpg"); |
---|
| 113 | and it will convert this to |
---|
| 114 | setTextures("skybox_top.jpg", "skybox_bottom.jpg", "skybox_left.jpg", |
---|
| 115 | "skybox_right.jpg", "skybox_front.jpg", "skybox_back.jpg"); |
---|
| 116 | */ |
---|
| 117 | void SkyBox::setTexture(const char* name, const char* extension) |
---|
| 118 | { |
---|
[4012] | 119 | char* top = new char[strlen(name)+strlen(extension)+ 10]; |
---|
| 120 | char* bottom = new char[strlen(name)+strlen(extension)+ 10]; |
---|
| 121 | char* left = new char[strlen(name)+strlen(extension)+ 10]; |
---|
| 122 | char* right = new char[strlen(name)+strlen(extension)+ 10]; |
---|
| 123 | char* front = new char[strlen(name)+strlen(extension)+ 10]; |
---|
| 124 | char* back = new char[strlen(name)+strlen(extension)+ 10]; |
---|
[3803] | 125 | |
---|
| 126 | sprintf(top, "%s_top.%s", name, extension); |
---|
| 127 | sprintf(bottom, "%s_bottom.%s", name, extension); |
---|
| 128 | sprintf(left, "%s_left.%s", name, extension); |
---|
| 129 | sprintf(right, "%s_right.%s", name, extension); |
---|
| 130 | sprintf(front, "%s_front.%s", name, extension); |
---|
| 131 | sprintf(back, "%s_back.%s", name, extension); |
---|
| 132 | |
---|
| 133 | this->setTextures(top, bottom, left, right, front, back); |
---|
| 134 | |
---|
[4012] | 135 | // deleted alocated memory of this function |
---|
[3803] | 136 | delete []top; |
---|
| 137 | delete []bottom; |
---|
| 138 | delete []left; |
---|
| 139 | delete []right; |
---|
| 140 | delete []front; |
---|
| 141 | delete []back; |
---|
| 142 | } |
---|
| 143 | |
---|
| 144 | /** |
---|
| 145 | \brief Defines which textures should be loaded onto the SkyBox. |
---|
| 146 | \param top the top texture. |
---|
| 147 | \param bottom the bottom texture. |
---|
| 148 | \param left the left texture. |
---|
| 149 | \param right the right texture. |
---|
| 150 | \param front the front texture. |
---|
| 151 | \param back the back texture. |
---|
| 152 | */ |
---|
| 153 | void SkyBox::setTextures(const char* top, const char* bottom, const char* left, const char* right, const char* front, const char* back) |
---|
| 154 | { |
---|
| 155 | this->material[0]->setDiffuseMap(top); |
---|
| 156 | this->material[1]->setDiffuseMap(bottom); |
---|
| 157 | this->material[2]->setDiffuseMap(left); |
---|
| 158 | this->material[3]->setDiffuseMap(right); |
---|
| 159 | this->material[4]->setDiffuseMap(front); |
---|
| 160 | this->material[5]->setDiffuseMap(back); |
---|
| 161 | } |
---|
| 162 | |
---|
| 163 | /** |
---|
[3806] | 164 | \param size The new size of the SkyBox |
---|
[3803] | 165 | */ |
---|
| 166 | void SkyBox::setSize(float size) |
---|
| 167 | { |
---|
| 168 | this->size = size; |
---|
| 169 | } |
---|
| 170 | |
---|
| 171 | /** |
---|
[3807] | 172 | \brief draws the SkyBox |
---|
| 173 | */ |
---|
| 174 | void SkyBox::draw() |
---|
| 175 | { |
---|
| 176 | glPushMatrix(); |
---|
| 177 | glMatrixMode(GL_MODELVIEW); |
---|
| 178 | Vector r = this->getAbsCoor(); |
---|
| 179 | glTranslatef(r.x, r.y, r.z); |
---|
| 180 | |
---|
| 181 | this->model->draw(); |
---|
| 182 | |
---|
| 183 | glPopMatrix(); |
---|
| 184 | } |
---|
| 185 | |
---|
| 186 | |
---|
| 187 | /** |
---|
[3803] | 188 | \brief rebuilds the SkyBox |
---|
| 189 | |
---|
| 190 | this must be done, when changing the Size of the Skybox (runtime-efficency) |
---|
| 191 | */ |
---|
[3801] | 192 | void SkyBox::rebuild() |
---|
| 193 | { |
---|
| 194 | if (this->model) |
---|
| 195 | delete model; |
---|
| 196 | model = new Model(); |
---|
| 197 | |
---|
[3805] | 198 | model->addVertex (-0.5*size, -0.5*size, 0.5*size); |
---|
[3801] | 199 | model->addVertex (0.5*size, -0.5*size, 0.5*size); |
---|
| 200 | model->addVertex (-0.5*size, 0.5*size, 0.5*size); |
---|
| 201 | model->addVertex (0.5*size, 0.5*size, 0.5*size); |
---|
| 202 | model->addVertex (-0.5*size, 0.5*size, -0.5*size); |
---|
| 203 | model->addVertex (0.5*size, 0.5*size, -0.5*size); |
---|
| 204 | model->addVertex (-0.5*size, -0.5*size, -0.5*size); |
---|
| 205 | model->addVertex (0.5*size, -0.5*size, -0.5*size); |
---|
| 206 | |
---|
| 207 | model->addVertexTexture (0.0, 0.0); |
---|
| 208 | model->addVertexTexture (1.0, 0.0); |
---|
[3805] | 209 | model->addVertexTexture (1.0, 1.0); |
---|
[3801] | 210 | model->addVertexTexture (0.0, 1.0); |
---|
| 211 | |
---|
| 212 | model->addVertexNormal (0.0, 0.0, 1.0); |
---|
| 213 | model->addVertexNormal (0.0, 1.0, 0.0); |
---|
| 214 | model->addVertexNormal (0.0, 0.0, -1.0); |
---|
| 215 | model->addVertexNormal (0.0, -1.0, 0.0); |
---|
| 216 | model->addVertexNormal (1.0, 0.0, 0.0); |
---|
| 217 | model->addVertexNormal (-1.0, 0.0, 0.0); |
---|
| 218 | |
---|
[3913] | 219 | model->setMaterial(material[0]); |
---|
[3895] | 220 | model->addFace (4, VERTEX_TEXCOORD_NORMAL, 3,2,4, 4,3,4, 6,4,4, 5,1,4); // top |
---|
[3913] | 221 | model->setMaterial(material[1]); |
---|
[3895] | 222 | model->addFace (4, VERTEX_TEXCOORD_NORMAL, 7,4,2, 8,1,2, 2,2,2, 1,3,2); // bottom |
---|
[3913] | 223 | model->setMaterial(material[2]); |
---|
[3895] | 224 | model->addFace (4, VERTEX_TEXCOORD_NORMAL, 1,1,3, 2,2,3, 4,3,3, 3,4,3); // left |
---|
[3913] | 225 | model->setMaterial(material[3]); |
---|
[3895] | 226 | model->addFace (4, VERTEX_TEXCOORD_NORMAL, 5,3,1, 6,4,1, 8,1,1, 7,2,1); // right |
---|
[3913] | 227 | model->setMaterial(material[4]); |
---|
[3895] | 228 | model->addFace (4, VERTEX_TEXCOORD_NORMAL, 2,1,6, 8,2,6, 6,3,6, 4,4,6); // front |
---|
[3913] | 229 | model->setMaterial(material[5]); |
---|
[3895] | 230 | model->addFace (4, VERTEX_TEXCOORD_NORMAL, 7,1,5, 1,2,5, 3,3,5, 5,4,5); // back |
---|
[3803] | 231 | |
---|
| 232 | model->finalize(); |
---|
[3801] | 233 | } |
---|