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