Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/resources/src/world_entities/skybox.cc @ 7895

Last change on this file since 7895 was 7229, checked in by bensch, 19 years ago

resources: some minor implementations

File size: 9.6 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
[7123]31
[5356]32using namespace std;
[5357]33
[5750]34CREATE_FACTORY(SkyBox, CL_SKYBOX);
[3608]35
[3416]36/**
[5357]37 * Constructs a SkyBox and takes fileName as a map.
[4836]38 * @param fileName the file to take as input for the SkyBox
[3419]39*/
[7221]40SkyBox::SkyBox(const std::string& fileName)
[3419]41{
[4012]42  this->preInit();
[7221]43  if (!fileName.empty())
[4261]44    this->setTextureAndType(fileName, ".jpg");
[4012]45  this->postInit();
[4010]46}
47
[4444]48/**
[4836]49 *  initializes a skybox from a XmlElement
[4444]50*/
[4436]51SkyBox::SkyBox(const TiXmlElement* root)
[4010]52{
[4012]53  this->preInit();
[4010]54
[6695]55  if( root != NULL)
56    this->loadParams(root);
[4010]57
[4012]58  this->postInit();
[4010]59}
60
[4261]61void SkyBox::loadParams(const TiXmlElement* root)
62{
[6512]63  WorldEntity::loadParams(root);
[4436]64
[5671]65  LoadParam(root, "Materialset", this, SkyBox, setTexture)
[4621]66      .describe("Sets the material on the SkyBox. The string must be the path relative to the data-dir, and without a trailing .jpg");
67
[5671]68  LoadParam(root, "Size", this, SkyBox, setSize)
[4621]69      .describe("Sets the Size of the SkyBox (normally this should be 90% of the maximal viewing Distance).");
[4261]70}
71
[4746]72void SkyBox::preInit()
[4010]73{
[4320]74  this->setClassID(CL_SKYBOX, "SkyBox");
[6142]75  this->toList(OM_ENVIRON_NOTICK);
[6634]76  //this->size = 100.0;
77  this->textureSize = 1024.0f;
[4620]78
[4597]79  for (int i = 0; i < 6; i++)
[3801]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);
[6470]86
[6519]87      this->cubeTexture[i] = NULL;
[3801]88    }
[4444]89  this->setParentMode(PNODE_MOVEMENT);
[6341]90
[7221]91  this->textureName = "";
[4012]92}
[3803]93
[4746]94void SkyBox::postInit()
[4012]95{
96  this->rebuild();
[3411]97}
98
[3507]99
[3416]100/**
[4836]101 *  default destructor
[3416]102*/
[3796]103SkyBox::~SkyBox()
[3411]104{
[4136]105  PRINTF(5)("Deleting SkyBox\n");
[3801]106  for (int i = 0; i < 6; i++)
[6523]107  {
[7221]108    if (this->material[i])
[6863]109      delete this->material[i];
[7221]110    if (this->cubeTexture[i])
[7123]111      ResourceManager::getInstance()->unload(this->cubeTexture[i]);
[6523]112  }
[6307]113}
[3411]114
[7221]115void SkyBox::setTexture(const std::string& name)
116{
117  this->textureName = name;
118  this->setTextureAndType (name, "jpg");
119};
120
121
[3803]122/**
[4836]123 *  sets A set of textures when just giving a Name and an extension:
[3763]124
[3803]125   usage: give this function an argument like
126   setTexture("skybox", "jpg");
[4597]127   and it will convert this to
[3803]128   setTextures("skybox_top.jpg", "skybox_bottom.jpg", "skybox_left.jpg",
129               "skybox_right.jpg", "skybox_front.jpg", "skybox_back.jpg");
130*/
[7221]131void SkyBox::setTextureAndType(const std::string& name, const std::string& extension)
[3803]132{
[7221]133  std::string top = name + "_top." + extension;
134  std::string bottom = name + "_bottom." + extension;
135  std::string left = name + "_left." + extension;
136  std::string right = name + "_right." + extension;
137  std::string front = name + "_front." + extension;
138  std::string back = name + "_back." + extension;
[3803]139
140  this->setTextures(top, bottom, left, right, front, back);
141}
142
143/**
[4836]144 *  Defines which textures should be loaded onto the SkyBox.
145 * @param top the top texture.
146 * @param bottom the bottom texture.
147 * @param left the left texture.
148 * @param right the right texture.
149 * @param front the front texture.
150 * @param back the back texture.
[3803]151*/
[7221]152void SkyBox::setTextures(const std::string& top, const std::string& bottom, const std::string& left,
153                          const std::string& right, const std::string& front, const std::string& back)
[3803]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);
[6523]161  if (GLEW_EXT_texture_cube_map)
162    this->loadCubeMapTextures(top, bottom, left, right, front, back);
[3803]163}
164
[7221]165void SkyBox::loadCubeMapTextures(const std::string& top, const std::string& bottom, const std::string& left,
166                                  const std::string& right, const std::string& front, const std::string& back)
[6470]167{
[7229]168  this->cubeTexture[0] = dynamic_cast<Texture*>(ResourceManager::getInstance()->load(top, RP_LEVEL, IMAGE, GL_TEXTURE_CUBE_MAP_POSITIVE_Y_EXT));
169  this->cubeTexture[1] = dynamic_cast<Texture*>(ResourceManager::getInstance()->load(bottom, RP_LEVEL, IMAGE, GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_EXT));
170  this->cubeTexture[2] = dynamic_cast<Texture*>(ResourceManager::getInstance()->load(left, RP_LEVEL, IMAGE, GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_EXT));
171  this->cubeTexture[3] = dynamic_cast<Texture*>(ResourceManager::getInstance()->load(right, RP_LEVEL, IMAGE, GL_TEXTURE_CUBE_MAP_POSITIVE_Z_EXT));
172  this->cubeTexture[4] = dynamic_cast<Texture*>(ResourceManager::getInstance()->load(front, RP_LEVEL, IMAGE, GL_TEXTURE_CUBE_MAP_POSITIVE_X_EXT));
173  this->cubeTexture[5] = dynamic_cast<Texture*>(ResourceManager::getInstance()->load(back, RP_LEVEL, IMAGE, GL_TEXTURE_CUBE_MAP_NEGATIVE_X_EXT));
[6470]174}
175
176void SkyBox::enableCubeMap()
177{
[6860]178  glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_REFLECTION_MAP);
179  glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_REFLECTION_MAP);
180  glTexGeni(GL_R, GL_TEXTURE_GEN_MODE, GL_REFLECTION_MAP);
181
[6523]182  glEnable(GL_TEXTURE_CUBE_MAP_EXT);
[6860]183
[6470]184  glEnable(GL_TEXTURE_GEN_S);
185  glEnable(GL_TEXTURE_GEN_T);
186  glEnable(GL_TEXTURE_GEN_R);
[6523]187
[6470]188}
189
190void SkyBox::disableCubeMap()
191{
[6860]192  glDisable(GL_TEXTURE_CUBE_MAP);
[6519]193  glDisable(GL_TEXTURE_2D);
[6470]194  glDisable(GL_TEXTURE_GEN_S);
195  glDisable(GL_TEXTURE_GEN_T);
196  glDisable(GL_TEXTURE_GEN_R);
[6523]197
198  glDisable(GL_TEXTURE_GEN_S);
199  glDisable(GL_TEXTURE_GEN_T);
200  glDisable(GL_TEXTURE_GEN_R);
[6470]201}
202
203
204
[3803]205/**
[4836]206 * @param size The new size of the SkyBox
[4621]207
208 * do not forget to rebuild the SkyBox after this.
[3803]209*/
210void SkyBox::setSize(float size)
211{
212  this->size = size;
213}
214
[6634]215
216
217void SkyBox::draw()
218{
219  glPushAttrib(GL_ENABLE_BIT);
220//   glPushAttrib(GL_LIGHTING_BIT);
221  glDisable(GL_LIGHTING);
222
[6772]223  glPushAttrib(GL_ENABLE_BIT);
224  glDisable(GL_FOG);
225
[6634]226  WorldEntity::draw();
227
228  glPopAttrib();
[6772]229  glPopAttrib();
[6634]230
231}
232
233
[3803]234/**
[4836]235 *  rebuilds the SkyBox
[4597]236
[3803]237   this must be done, when changing the Size of the Skybox (runtime-efficency)
238*/
[3801]239void SkyBox::rebuild()
240{
[6022]241  StaticModel* model = new StaticModel();
[3801]242
[5994]243  model->addVertex (-0.5*size, -0.5*size, 0.5*size);
244  model->addVertex (0.5*size, -0.5*size, 0.5*size);
245  model->addVertex (-0.5*size, 0.5*size, 0.5*size);
246  model->addVertex (0.5*size, 0.5*size, 0.5*size);
247  model->addVertex (-0.5*size, 0.5*size, -0.5*size);
248  model->addVertex (0.5*size, 0.5*size, -0.5*size);
249  model->addVertex (-0.5*size, -0.5*size, -0.5*size);
250  model->addVertex (0.5*size, -0.5*size, -0.5*size);
[3801]251
[6634]252//   model->addVertexTexture (0.0, 1.0);
253//   model->addVertexTexture (1.0, 1.0);
254//   model->addVertexTexture (1.0, 0.0);
255//   model->addVertexTexture (0.0, 0.0);
[3801]256
[6634]257  model->addVertexTexture (1.0/this->textureSize, (this->textureSize - 1.0)/this->textureSize);
258  model->addVertexTexture ((this->textureSize - 1.0)/this->textureSize, (this->textureSize - 1.0)/this->textureSize);
259  model->addVertexTexture ((this->textureSize - 1.0)/this->textureSize, 1.0/this->textureSize);
260  model->addVertexTexture (1.0/this->textureSize, 1.0/this->textureSize);
261
262
[5994]263  model->addVertexNormal (0.0, 0.0, 1.0);
264  model->addVertexNormal (0.0, 1.0, 0.0);
265  model->addVertexNormal (0.0, 0.0, -1.0);
266  model->addVertexNormal (0.0, -1.0, 0.0);
267  model->addVertexNormal (1.0, 0.0, 0.0);
268  model->addVertexNormal (-1.0, 0.0, 0.0);
[3801]269
[5994]270  model->setMaterial(material[0]);
271  model->addFace (4, VERTEX_TEXCOORD_NORMAL, 2,0,3, 3,1,3, 5,2,3, 4,3,3); // top
272  model->setMaterial(material[1]);
273  model->addFace (4, VERTEX_TEXCOORD_NORMAL, 6,0,1, 7,1,1, 1,2,1, 0,3,1); // bottom
274  model->setMaterial(material[2]);
275  model->addFace (4, VERTEX_TEXCOORD_NORMAL, 4,2,2, 5,3,2, 7,0,2, 6,1,2); // left
276  model->setMaterial(material[3]);
277  model->addFace (4, VERTEX_TEXCOORD_NORMAL, 0,0,0, 1,1,0, 3,2,0, 2,3,0); // right
278  model->setMaterial(material[4]);
279  model->addFace (4, VERTEX_TEXCOORD_NORMAL, 1,0,5, 7,1,5, 5,2,5, 3,3,5); // front
280  model->setMaterial(material[5]);
281  model->addFace (4, VERTEX_TEXCOORD_NORMAL, 6,0,4, 0,1,4, 2,2,4, 4,3,4); // back
[4597]282
[5994]283  model->finalize();
284
285  this->setModel(model);
[3801]286}
[6341]287
288int SkyBox::writeBytes( const byte * data, int length, int sender )
289{
290  setRequestedSync( false );
291  setIsOutOfSync( false );
292
293  SYNCHELP_READ_BEGIN();
294
[6815]295  SYNCHELP_READ_FKT( WorldEntity::writeState, NWT_SB_WE_STATE );
[6341]296
[6815]297  SYNCHELP_READ_FLOAT( size, NWT_SB_SIZE );
[7221]298  if ( !this->textureName.empty() )
[6341]299  {
[7221]300    textureName = "";
[6341]301  }
[7221]302  char* texName;
303  SYNCHELP_READ_STRINGM( texName, NWT_SB_TEXTURENAME );
[6341]304
305  this->setSize( size );
[7221]306  this->setTextureAndType( texName, "jpg" );
[6341]307  this->rebuild();
308
309  return SYNCHELP_READ_N;
310}
311
312
313
314int SkyBox::readBytes( byte * data, int maxLength, int * reciever )
315{
316  if ( isOutOfSync() && !requestedSync() && this->getHostID()!=this->getOwner() )
317  {
318    (NetworkGameManager::getInstance())->sync( this->getUniqueID(), this->getOwner() );
319    setRequestedSync( true );
320  }
321
322  int rec = this->getRequestSync();
323  if ( rec > 0 )
324  {
325    *reciever = rec;
326
327    SYNCHELP_WRITE_BEGIN();
328
[6815]329    SYNCHELP_WRITE_FKT( WorldEntity::readState, NWT_SB_WE_STATE );
[6341]330
[6815]331    SYNCHELP_WRITE_FLOAT(this->size, NWT_SB_SIZE);
[7221]332    SYNCHELP_WRITE_STRING(this->textureName.c_str(), NWT_SB_TEXTURENAME);
[6341]333
334    return SYNCHELP_WRITE_N;
335  }
336
337  *reciever = 0;
338  return 0;
339}
340
341void SkyBox::writeDebug( ) const
342{
343}
344
345void SkyBox::readDebug( ) const
346{
347}
Note: See TracBrowser for help on using the repository browser.