Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/new_class_id/src/world_entities/skybox.cc @ 9762

Last change on this file since 9762 was 9727, checked in by bensch, 18 years ago

orxonox/new_class_id: new Executor construct, that is much more typesafe, faster, and easier to extend…

Also changed the LoadParam process, and adapted ScriptEngine calls

Then at the end, some missing headers appeared, and appended them to all the cc-files again.

File size: 9.1 KB
RevLine 
[4597]1/*
[3416]2   orxonox - the future of 3D-vertical-scrollers
3
4   Copyright (C) 2004 orx
5
6   This program is free software; you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation; either version 2, or (at your option)
9   any later version.
10
11   ### File Specific:
[4261]12   main-programmer: Benjamin Grauer
13   co-programmer: ...
[3411]14*/
15
[3590]16#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_WORLD_ENTITY
17
[3796]18#include "skybox.h"
[4010]19
[7193]20#include "util/loading/load_param.h"
21#include "util/loading/factory.h"
[6022]22#include "static_model.h"
[6470]23
[6022]24#include "material.h"
[6470]25#include "texture.h"
26
[6341]27#include "network_game_manager.h"
28#include "converter.h"
[7193]29#include "util/loading/resource_manager.h"
[3608]30
[9727]31#include "debug.h"
[7123]32
[9716]33#include "class_id_DEPRECATED.h"
[9715]34ObjectListDefinitionID(SkyBox, CL_SKYBOX);
[9709]35CREATE_FACTORY(SkyBox);
[9406]36
[3416]37/**
[5357]38 * Constructs a SkyBox and takes fileName as a map.
[4836]39 * @param fileName the file to take as input for the SkyBox
[3419]40*/
[7221]41SkyBox::SkyBox(const std::string& fileName)
[3419]42{
[4012]43  this->preInit();
[7221]44  if (!fileName.empty())
[4261]45    this->setTextureAndType(fileName, ".jpg");
[4012]46  this->postInit();
[4010]47}
48
[4444]49/**
[4836]50 *  initializes a skybox from a XmlElement
[4444]51*/
[4436]52SkyBox::SkyBox(const TiXmlElement* root)
[4010]53{
[4012]54  this->preInit();
[4010]55
[6695]56  if( root != NULL)
57    this->loadParams(root);
[4010]58
[4012]59  this->postInit();
[4010]60}
61
[4261]62void SkyBox::loadParams(const TiXmlElement* root)
63{
[6512]64  WorldEntity::loadParams(root);
[4436]65
[5671]66  LoadParam(root, "Materialset", this, SkyBox, setTexture)
[4621]67      .describe("Sets the material on the SkyBox. The string must be the path relative to the data-dir, and without a trailing .jpg");
68
[5671]69  LoadParam(root, "Size", this, SkyBox, setSize)
[4621]70      .describe("Sets the Size of the SkyBox (normally this should be 90% of the maximal viewing Distance).");
[4261]71}
72
[4746]73void SkyBox::preInit()
[4010]74{
[9709]75  this->registerObject(this, SkyBox::_objectList);
[7840]76  this->toList(OM_BACKGROUND);
[8037]77  this->toReflectionList();
[6634]78  //this->size = 100.0;
79  this->textureSize = 1024.0f;
[4620]80
[4597]81  for (int i = 0; i < 6; i++)
[3801]82    {
83      this->material[i] = new Material();
84      this->material[i]->setIllum(3);
[3805]85      this->material[i]->setDiffuse(0.0,0.0,0.0);
86      this->material[i]->setSpecular(0.0,0.0,0.0);
87      this->material[i]->setAmbient(2.0, 2.0, 2.0);
[6470]88
[6519]89      this->cubeTexture[i] = NULL;
[3801]90    }
[4444]91  this->setParentMode(PNODE_MOVEMENT);
[6341]92
[7221]93  this->textureName = "";
[4012]94}
[3803]95
[4746]96void SkyBox::postInit()
[4012]97{
98  this->rebuild();
[9656]99
100  textureName_handle = registerVarId( new SynchronizeableString( &textureName, &textureName, "textureName", PERMISSION_MASTER_SERVER) );
101  size_handle = registerVarId( new SynchronizeableFloat( &size, &size, "size", PERMISSION_MASTER_SERVER ) );
[3411]102}
103
[3507]104
[3416]105/**
[4836]106 *  default destructor
[3416]107*/
[3796]108SkyBox::~SkyBox()
[3411]109{
[4136]110  PRINTF(5)("Deleting SkyBox\n");
[3801]111  for (int i = 0; i < 6; i++)
[6523]112  {
[7221]113    if (this->material[i])
[6863]114      delete this->material[i];
[7221]115    if (this->cubeTexture[i])
[7123]116      ResourceManager::getInstance()->unload(this->cubeTexture[i]);
[6523]117  }
[6307]118}
[3411]119
[7221]120void SkyBox::setTexture(const std::string& name)
121{
122  this->textureName = name;
123  this->setTextureAndType (name, "jpg");
124};
125
126
[3803]127/**
[7328]128 * @brief sets A set of textures when just giving a Name and an extension:
129 * @param name the prefix of the Name
130 * @param extension the file extension (jpg by default)
131 * usage: give this function an argument like
132 * setTexture("skybox", "jpg");
133 * and it will convert this to
134 * setTextures("skybox_negx.jpg", "skybox_posx.jpg", "skybox_negy.jpg",
135 *             "skybox_posy.jpg", "skybox_negz.jpg", "skybox_posz.jpg");
136 */
[7221]137void SkyBox::setTextureAndType(const std::string& name, const std::string& extension)
[3803]138{
[7328]139  std::string negX = name + "_negx." + extension;
140  std::string posX = name + "_posx." + extension;
[3803]141
[7328]142  std::string negY = name + "_negy." + extension;
143  std::string posY = name + "_posy." + extension;
144
145  std::string negZ = name + "_negz." + extension;
146  std::string posZ = name + "_posz." + extension;
147
148  this->setTextures(negX, posX, negY, posY, negZ, posZ);
[3803]149}
150
151/**
[7328]152 * @brief Defines which textures should be loaded onto the SkyBox.
153 * @param negX the top texture.
154 * @param posX the bottom texture.
155 * @param negY the left texture.
156 * @param posY the right texture.
157 * @param negZ the front texture.
158 * @param posZ the back texture.
[3803]159*/
[7328]160void SkyBox::setTextures(const std::string& negX, const std::string& posX,
161                         const std::string& negY, const std::string& posY,
162                         const std::string& negZ, const std::string& posZ)
[3803]163{
[7328]164  this->material[0]->setDiffuseMap(negX);
165  this->material[1]->setDiffuseMap(posX);
166  this->material[2]->setDiffuseMap(negY);
167  this->material[3]->setDiffuseMap(posY);
168  this->material[4]->setDiffuseMap(negZ);
169  this->material[5]->setDiffuseMap(posZ);
[6523]170  if (GLEW_EXT_texture_cube_map)
[7328]171    this->loadCubeMapTextures(negX, posX, negY, posY, negZ, posZ);
[3803]172}
173
[7328]174void SkyBox::loadCubeMapTextures(const std::string& posY, const std::string& negY, const std::string& negZ,
175                                  const std::string& posZ, const std::string& posX, const std::string& negX)
[6470]176{
[7328]177  this->cubeTexture[0] = (Texture*)ResourceManager::getInstance()->load(negX, RP_LEVEL, IMAGE, GL_TEXTURE_CUBE_MAP_NEGATIVE_X_EXT);
178  this->cubeTexture[1] = (Texture*)ResourceManager::getInstance()->load(posX, RP_LEVEL, IMAGE, GL_TEXTURE_CUBE_MAP_POSITIVE_X_EXT);
179
180  this->cubeTexture[2] = (Texture*)ResourceManager::getInstance()->load(negY, RP_LEVEL, IMAGE, GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_EXT);
181  this->cubeTexture[3] = (Texture*)ResourceManager::getInstance()->load(posY, RP_LEVEL, IMAGE, GL_TEXTURE_CUBE_MAP_POSITIVE_Y_EXT);
182
183  this->cubeTexture[4] = (Texture*)ResourceManager::getInstance()->load(negZ, RP_LEVEL, IMAGE, GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_EXT);
184  this->cubeTexture[5] = (Texture*)ResourceManager::getInstance()->load(posZ, RP_LEVEL, IMAGE, GL_TEXTURE_CUBE_MAP_POSITIVE_Z_EXT);
[6470]185}
186
187void SkyBox::enableCubeMap()
188{
[6860]189  glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_REFLECTION_MAP);
190  glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_REFLECTION_MAP);
191  glTexGeni(GL_R, GL_TEXTURE_GEN_MODE, GL_REFLECTION_MAP);
192
[6523]193  glEnable(GL_TEXTURE_CUBE_MAP_EXT);
[6860]194
[6470]195  glEnable(GL_TEXTURE_GEN_S);
196  glEnable(GL_TEXTURE_GEN_T);
197  glEnable(GL_TEXTURE_GEN_R);
[6523]198
[6470]199}
200
201void SkyBox::disableCubeMap()
202{
[6860]203  glDisable(GL_TEXTURE_CUBE_MAP);
[6519]204  glDisable(GL_TEXTURE_2D);
[6470]205  glDisable(GL_TEXTURE_GEN_S);
206  glDisable(GL_TEXTURE_GEN_T);
207  glDisable(GL_TEXTURE_GEN_R);
[6523]208
209  glDisable(GL_TEXTURE_GEN_S);
210  glDisable(GL_TEXTURE_GEN_T);
211  glDisable(GL_TEXTURE_GEN_R);
[6470]212}
213
214
215
[3803]216/**
[4836]217 * @param size The new size of the SkyBox
[4621]218
219 * do not forget to rebuild the SkyBox after this.
[3803]220*/
221void SkyBox::setSize(float size)
222{
223  this->size = size;
224}
225
[6634]226
227
228void SkyBox::draw()
229{
230  glPushAttrib(GL_ENABLE_BIT);
231//   glPushAttrib(GL_LIGHTING_BIT);
232  glDisable(GL_LIGHTING);
233
[6772]234  glDisable(GL_FOG);
235
[6634]236  WorldEntity::draw();
237
238  glPopAttrib();
239
240}
241
242
[3803]243/**
[4836]244 *  rebuilds the SkyBox
[4597]245
[3803]246   this must be done, when changing the Size of the Skybox (runtime-efficency)
247*/
[3801]248void SkyBox::rebuild()
249{
[6022]250  StaticModel* model = new StaticModel();
[3801]251
[5994]252  model->addVertex (-0.5*size, -0.5*size, 0.5*size);
253  model->addVertex (0.5*size, -0.5*size, 0.5*size);
254  model->addVertex (-0.5*size, 0.5*size, 0.5*size);
255  model->addVertex (0.5*size, 0.5*size, 0.5*size);
256  model->addVertex (-0.5*size, 0.5*size, -0.5*size);
257  model->addVertex (0.5*size, 0.5*size, -0.5*size);
258  model->addVertex (-0.5*size, -0.5*size, -0.5*size);
259  model->addVertex (0.5*size, -0.5*size, -0.5*size);
[3801]260
[6634]261//   model->addVertexTexture (0.0, 1.0);
262//   model->addVertexTexture (1.0, 1.0);
263//   model->addVertexTexture (1.0, 0.0);
264//   model->addVertexTexture (0.0, 0.0);
[3801]265
[6634]266  model->addVertexTexture (1.0/this->textureSize, (this->textureSize - 1.0)/this->textureSize);
267  model->addVertexTexture ((this->textureSize - 1.0)/this->textureSize, (this->textureSize - 1.0)/this->textureSize);
268  model->addVertexTexture ((this->textureSize - 1.0)/this->textureSize, 1.0/this->textureSize);
269  model->addVertexTexture (1.0/this->textureSize, 1.0/this->textureSize);
270
271
[5994]272  model->addVertexNormal (0.0, 0.0, 1.0);
273  model->addVertexNormal (0.0, 1.0, 0.0);
274  model->addVertexNormal (0.0, 0.0, -1.0);
275  model->addVertexNormal (0.0, -1.0, 0.0);
276  model->addVertexNormal (1.0, 0.0, 0.0);
277  model->addVertexNormal (-1.0, 0.0, 0.0);
[3801]278
[5994]279  model->setMaterial(material[0]);
[7328]280  model->addFace (4, VERTEX_TEXCOORD_NORMAL, 6,0,4, 0,1,4, 2,2,4, 4,3,4); // back
[5994]281  model->setMaterial(material[1]);
[7328]282  model->addFace (4, VERTEX_TEXCOORD_NORMAL, 1,0,5, 7,1,5, 5,2,5, 3,3,5); // front
283  model->setMaterial(material[2]);
[5994]284  model->addFace (4, VERTEX_TEXCOORD_NORMAL, 6,0,1, 7,1,1, 1,2,1, 0,3,1); // bottom
285  model->setMaterial(material[3]);
[7328]286  model->addFace (4, VERTEX_TEXCOORD_NORMAL, 2,0,3, 3,1,3, 5,2,3, 4,3,3); // top
[5994]287  model->setMaterial(material[4]);
[7328]288  model->addFace (4, VERTEX_TEXCOORD_NORMAL, 4,2,2, 5,3,2, 7,0,2, 6,1,2); // left
[5994]289  model->setMaterial(material[5]);
[7328]290  model->addFace (4, VERTEX_TEXCOORD_NORMAL, 0,0,0, 1,1,0, 3,2,0, 2,3,0); // right
[4597]291
[5994]292  model->finalize();
293
294  this->setModel(model);
[3801]295}
[6341]296
[7954]297void SkyBox::varChangeHandler( std::list< int > & id )
[6341]298{
[7954]299  bool somethinChanged = false;
[9656]300
[7954]301  if ( std::find( id.begin(), id.end(), textureName_handle ) != id.end() )
[6341]302  {
[7954]303    somethinChanged = true;
304    setTexture( textureName );
[6341]305  }
[9656]306
[7954]307  if ( std::find( id.begin(), id.end(), size_handle ) != id.end() )
[6341]308  {
[7954]309    somethinChanged = true;
[6341]310  }
[9656]311
[7954]312  rebuild();
[9656]313
[7954]314  WorldEntity::varChangeHandler( id );
[6341]315}
Note: See TracBrowser for help on using the repository browser.