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