Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: some cleanup before the new tag

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