Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/powerups/src/world_entities/skybox.cc @ 6946

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

trunk: fixed the segfault problem, when restarting world

File size: 9.8 KB
Line 
1/*
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:
12   main-programmer: Benjamin Grauer
13   co-programmer: ...
14*/
15
16#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_WORLD_ENTITY
17
18#include "skybox.h"
19
20#include "load_param.h"
21#include "factory.h"
22#include "static_model.h"
23
24#include "material.h"
25#include "texture.h"
26
27#include "network_game_manager.h"
28#include "converter.h"
29
30using namespace std;
31
32CREATE_FACTORY(SkyBox, CL_SKYBOX);
33
34/**
35 * Constructs a SkyBox and takes fileName as a map.
36 * @param fileName the file to take as input for the SkyBox
37*/
38SkyBox::SkyBox(const char* fileName)
39{
40  this->preInit();
41  if (fileName)
42    this->setTextureAndType(fileName, ".jpg");
43  this->postInit();
44}
45
46/**
47 *  initializes a skybox from a XmlElement
48*/
49SkyBox::SkyBox(const TiXmlElement* root)
50{
51  this->preInit();
52
53  if( root != NULL)
54    this->loadParams(root);
55
56  this->postInit();
57}
58
59void SkyBox::loadParams(const TiXmlElement* root)
60{
61  WorldEntity::loadParams(root);
62
63  LoadParam(root, "Materialset", this, SkyBox, setTexture)
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
66  LoadParam(root, "Size", this, SkyBox, setSize)
67      .describe("Sets the Size of the SkyBox (normally this should be 90% of the maximal viewing Distance).");
68}
69
70void SkyBox::preInit()
71{
72  this->setClassID(CL_SKYBOX, "SkyBox");
73  this->toList(OM_ENVIRON_NOTICK);
74  //this->size = 100.0;
75  this->textureSize = 1024.0f;
76
77  for (int i = 0; i < 6; i++)
78    {
79      this->material[i] = new Material();
80      this->material[i]->setIllum(3);
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);
84
85      this->cubeTexture[i] = NULL;
86    }
87  this->setParentMode(PNODE_MOVEMENT);
88
89  this->textureName = NULL;
90}
91
92void SkyBox::postInit()
93{
94  this->rebuild();
95}
96
97
98/**
99 *  default destructor
100*/
101SkyBox::~SkyBox()
102{
103  PRINTF(5)("Deleting SkyBox\n");
104  this->setModel(NULL); //< so that WorldEntity does not try to delete it again.
105  for (int i = 0; i < 6; i++)
106  {
107    if( this->material[i])
108      delete this->material[i];
109//     if( this->cubeTexture[i])
110//       delete this->cubeTexture[i];
111  }
112}
113
114/**
115 *  sets A set of textures when just giving a Name and an extension:
116
117   usage: give this function an argument like
118   setTexture("skybox", "jpg");
119   and it will convert this to
120   setTextures("skybox_top.jpg", "skybox_bottom.jpg", "skybox_left.jpg",
121               "skybox_right.jpg", "skybox_front.jpg", "skybox_back.jpg");
122*/
123void SkyBox::setTextureAndType(const char* name, const char* extension)
124{
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];
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);
138
139  this->setTextures(top, bottom, left, right, front, back);
140
141  // deleted alocated memory of this function
142  delete []top;
143  delete []bottom;
144  delete []left;
145  delete []right;
146  delete []front;
147  delete []back;
148}
149
150/**
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.
158*/
159void SkyBox::setTextures(const char* top, const char* bottom, const char* left,
160                          const char* right, const char* front, const char* back)
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);
168  if (GLEW_EXT_texture_cube_map)
169    this->loadCubeMapTextures(top, bottom, left, right, front, back);
170}
171
172
173#include "resource_manager.h"
174
175void SkyBox::loadCubeMapTextures(const char* top, const char* bottom, const char* left,
176                                  const char* right, const char* front, const char* back)
177{
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);
184}
185
186void SkyBox::enableCubeMap()
187{
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
192  glEnable(GL_TEXTURE_CUBE_MAP_EXT);
193
194  glEnable(GL_TEXTURE_GEN_S);
195  glEnable(GL_TEXTURE_GEN_T);
196  glEnable(GL_TEXTURE_GEN_R);
197
198}
199
200void SkyBox::disableCubeMap()
201{
202  glDisable(GL_TEXTURE_CUBE_MAP);
203  glDisable(GL_TEXTURE_2D);
204  glDisable(GL_TEXTURE_GEN_S);
205  glDisable(GL_TEXTURE_GEN_T);
206  glDisable(GL_TEXTURE_GEN_R);
207
208  glDisable(GL_TEXTURE_GEN_S);
209  glDisable(GL_TEXTURE_GEN_T);
210  glDisable(GL_TEXTURE_GEN_R);
211}
212
213
214
215/**
216 * @param size The new size of the SkyBox
217
218 * do not forget to rebuild the SkyBox after this.
219*/
220void SkyBox::setSize(float size)
221{
222  this->size = size;
223}
224
225
226
227void SkyBox::draw()
228{
229  glPushAttrib(GL_ENABLE_BIT);
230//   glPushAttrib(GL_LIGHTING_BIT);
231  glDisable(GL_LIGHTING);
232
233  glPushAttrib(GL_ENABLE_BIT);
234  glDisable(GL_FOG);
235
236  WorldEntity::draw();
237
238  glPopAttrib();
239  glPopAttrib();
240
241}
242
243
244/**
245 *  rebuilds the SkyBox
246
247   this must be done, when changing the Size of the Skybox (runtime-efficency)
248*/
249void SkyBox::rebuild()
250{
251  StaticModel* model = new StaticModel();
252
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);
261
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);
266
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
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);
279
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
292
293  model->finalize();
294
295  this->setModel(model);
296}
297
298int SkyBox::writeBytes( const byte * data, int length, int sender )
299{
300  setRequestedSync( false );
301  setIsOutOfSync( false );
302
303  SYNCHELP_READ_BEGIN();
304
305  SYNCHELP_READ_FKT( WorldEntity::writeState, NWT_SB_WE_STATE );
306
307  SYNCHELP_READ_FLOAT( size, NWT_SB_SIZE );
308  if ( textureName )
309  {
310    delete[] textureName;
311    textureName = NULL;
312  }
313  SYNCHELP_READ_STRINGM( textureName, NWT_SB_TEXTURENAME );
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
339    SYNCHELP_WRITE_FKT( WorldEntity::readState, NWT_SB_WE_STATE );
340
341    SYNCHELP_WRITE_FLOAT(this->size, NWT_SB_SIZE);
342    SYNCHELP_WRITE_STRING(this->textureName, NWT_SB_TEXTURENAME);
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.