Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/avi_play/src/world_entities/skybox.cc @ 6785

Last change on this file since 6785 was 6695, checked in by patrick, 19 years ago

merged the network branche to the trunk

File size: 9.3 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    delete this->material[i];
108    delete this->cubeTexture[i];
109  }
110}
111
112/**
113 *  sets A set of textures when just giving a Name and an extension:
114
115   usage: give this function an argument like
116   setTexture("skybox", "jpg");
117   and it will convert this to
118   setTextures("skybox_top.jpg", "skybox_bottom.jpg", "skybox_left.jpg",
119               "skybox_right.jpg", "skybox_front.jpg", "skybox_back.jpg");
120*/
121void SkyBox::setTextureAndType(const char* name, const char* extension)
122{
123  char* top    = new char[strlen(name)+strlen(extension)+ 10];
124  char* bottom = new char[strlen(name)+strlen(extension)+ 10];
125  char* left   = new char[strlen(name)+strlen(extension)+ 10];
126  char* right  = new char[strlen(name)+strlen(extension)+ 10];
127  char* front  = new char[strlen(name)+strlen(extension)+ 10];
128  char* back   = new char[strlen(name)+strlen(extension)+ 10];
129
130  sprintf(top, "%s_top.%s", name, extension);
131  sprintf(bottom, "%s_bottom.%s", name, extension);
132  sprintf(left, "%s_left.%s", name, extension);
133  sprintf(right, "%s_right.%s", name, extension);
134  sprintf(front, "%s_front.%s", name, extension);
135  sprintf(back, "%s_back.%s", name, extension);
136
137  this->setTextures(top, bottom, left, right, front, back);
138
139  // deleted alocated memory of this function
140  delete []top;
141  delete []bottom;
142  delete []left;
143  delete []right;
144  delete []front;
145  delete []back;
146}
147
148/**
149 *  Defines which textures should be loaded onto the SkyBox.
150 * @param top the top texture.
151 * @param bottom the bottom texture.
152 * @param left the left texture.
153 * @param right the right texture.
154 * @param front the front texture.
155 * @param back the back texture.
156*/
157void SkyBox::setTextures(const char* top, const char* bottom, const char* left,
158                          const char* right, const char* front, const char* back)
159{
160  this->material[0]->setDiffuseMap(top);
161  this->material[1]->setDiffuseMap(bottom);
162  this->material[2]->setDiffuseMap(left);
163  this->material[3]->setDiffuseMap(right);
164  this->material[4]->setDiffuseMap(front);
165  this->material[5]->setDiffuseMap(back);
166  if (GLEW_EXT_texture_cube_map)
167    this->loadCubeMapTextures(top, bottom, left, right, front, back);
168}
169
170void SkyBox::loadCubeMapTextures(const char* top, const char* bottom, const char* left,
171                                  const char* right, const char* front, const char* back)
172{
173  this->cubeTexture[0] = new Texture (top, GL_TEXTURE_CUBE_MAP_POSITIVE_Y_EXT);
174  this->cubeTexture[1] = new Texture (bottom, GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_EXT);
175  this->cubeTexture[2] = new Texture (left, GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_EXT);
176  this->cubeTexture[3] = new Texture (right, GL_TEXTURE_CUBE_MAP_POSITIVE_Z_EXT);
177  this->cubeTexture[4] = new Texture (front, GL_TEXTURE_CUBE_MAP_POSITIVE_X_EXT);
178  this->cubeTexture[5] = new Texture (back, GL_TEXTURE_CUBE_MAP_NEGATIVE_X_EXT);
179}
180
181void SkyBox::enableCubeMap()
182{
183  glEnable(GL_TEXTURE_2D);
184  glEnable(GL_TEXTURE_CUBE_MAP_EXT);
185  glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_REFLECTION_MAP_EXT);
186  glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_REFLECTION_MAP_EXT);
187  glTexGeni(GL_R, GL_TEXTURE_GEN_MODE, GL_REFLECTION_MAP_EXT);
188  glEnable(GL_TEXTURE_GEN_S);
189  glEnable(GL_TEXTURE_GEN_T);
190  glEnable(GL_TEXTURE_GEN_R);
191
192}
193
194void SkyBox::disableCubeMap()
195{
196  glDisable(GL_TEXTURE_CUBE_MAP_EXT);
197  glDisable(GL_TEXTURE_2D);
198  glDisable(GL_TEXTURE_GEN_S);
199  glDisable(GL_TEXTURE_GEN_T);
200  glDisable(GL_TEXTURE_GEN_R);
201
202  glDisable(GL_TEXTURE_GEN_S);
203  glDisable(GL_TEXTURE_GEN_T);
204  glDisable(GL_TEXTURE_GEN_R);
205}
206
207
208
209/**
210 * @param size The new size of the SkyBox
211
212 * do not forget to rebuild the SkyBox after this.
213*/
214void SkyBox::setSize(float size)
215{
216  this->size = size;
217}
218
219
220
221void SkyBox::draw()
222{
223  glPushAttrib(GL_ENABLE_BIT);
224//   glPushAttrib(GL_LIGHTING_BIT);
225  glDisable(GL_LIGHTING);
226
227  WorldEntity::draw();
228
229  glPopAttrib();
230
231}
232
233
234/**
235 *  rebuilds the SkyBox
236
237   this must be done, when changing the Size of the Skybox (runtime-efficency)
238*/
239void SkyBox::rebuild()
240{
241  StaticModel* model = new StaticModel();
242
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);
251
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);
256
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
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);
269
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
282
283  model->finalize();
284
285  this->setModel(model);
286}
287
288int SkyBox::writeBytes( const byte * data, int length, int sender )
289{
290  setRequestedSync( false );
291  setIsOutOfSync( false );
292
293  SYNCHELP_READ_BEGIN();
294
295  SYNCHELP_READ_FKT( WorldEntity::writeState );
296
297  SYNCHELP_READ_FLOAT( size );
298  if ( textureName )
299  {
300    delete[] textureName;
301    textureName = NULL;
302  }
303  SYNCHELP_READ_STRINGM( textureName );
304
305  this->setSize( size );
306  this->setTextureAndType( textureName, "jpg" );
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
329    SYNCHELP_WRITE_FKT( WorldEntity::readState );
330
331    SYNCHELP_WRITE_FLOAT(this->size);
332    SYNCHELP_WRITE_STRING(this->textureName);
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.