Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/world_entities/skybox.cc @ 7091

Last change on this file since 7091 was 6863, checked in by patrick, 19 years ago

trunk: fixed the segfault problem, when restarting world

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