Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/network/src/world_entities/skybox.cc @ 6566

Last change on this file since 6566 was 6524, checked in by patrick, 19 years ago

network: added the planet to the menu

File size: 9.1 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  this->loadParams(root);
54
55  this->postInit();
56}
57
58void SkyBox::loadParams(const TiXmlElement* root)
59{
60  static_cast<WorldEntity*>(this)->loadParams(root);
61
62  LoadParam(root, "Materialset", this, SkyBox, setTexture)
63      .describe("Sets the material on the SkyBox. The string must be the path relative to the data-dir, and without a trailing .jpg");
64
65  LoadParam(root, "Size", this, SkyBox, setSize)
66      .describe("Sets the Size of the SkyBox (normally this should be 90% of the maximal viewing Distance).");
67}
68
69void SkyBox::preInit()
70{
71  this->setClassID(CL_SKYBOX, "SkyBox");
72  this->toList(OM_ENVIRON_NOTICK);
73  //this->size = 100.0;
74  this->textureSize = 1024.0f;
75
76  for (int i = 0; i < 6; i++)
77    {
78      this->material[i] = new Material();
79      this->material[i]->setIllum(3);
80      this->material[i]->setDiffuse(0.0,0.0,0.0);
81      this->material[i]->setSpecular(0.0,0.0,0.0);
82      this->material[i]->setAmbient(2.0, 2.0, 2.0);
83
84      this->texture[i] = NULL;
85    }
86  this->setParentMode(PNODE_MOVEMENT);
87
88  this->textureName = NULL;
89}
90
91void SkyBox::postInit()
92{
93  this->rebuild();
94}
95
96
97/**
98 *  default destructor
99*/
100SkyBox::~SkyBox()
101{
102  PRINTF(5)("Deleting SkyBox\n");
103  this->setModel(NULL); //< so that WorldEntity does not try to delete it again.
104  for (int i = 0; i < 6; i++)
105    delete this->material[i];
106}
107
108/**
109 *  sets A set of textures when just giving a Name and an extension:
110
111   usage: give this function an argument like
112   setTexture("skybox", "jpg");
113   and it will convert this to
114   setTextures("skybox_top.jpg", "skybox_bottom.jpg", "skybox_left.jpg",
115               "skybox_right.jpg", "skybox_front.jpg", "skybox_back.jpg");
116*/
117void SkyBox::setTextureAndType(const char* name, const char* extension)
118{
119  char* top    = new char[strlen(name)+strlen(extension)+ 10];
120  char* bottom = new char[strlen(name)+strlen(extension)+ 10];
121  char* left   = new char[strlen(name)+strlen(extension)+ 10];
122  char* right  = new char[strlen(name)+strlen(extension)+ 10];
123  char* front  = new char[strlen(name)+strlen(extension)+ 10];
124  char* back   = new char[strlen(name)+strlen(extension)+ 10];
125
126  sprintf(top, "%s_top.%s", name, extension);
127  sprintf(bottom, "%s_bottom.%s", name, extension);
128  sprintf(left, "%s_left.%s", name, extension);
129  sprintf(right, "%s_right.%s", name, extension);
130  sprintf(front, "%s_front.%s", name, extension);
131  sprintf(back, "%s_back.%s", name, extension);
132
133  this->setTextures(top, bottom, left, right, front, back);
134
135  // deleted alocated memory of this function
136  delete []top;
137  delete []bottom;
138  delete []left;
139  delete []right;
140  delete []front;
141  delete []back;
142}
143
144/**
145 *  Defines which textures should be loaded onto the SkyBox.
146 * @param top the top texture.
147 * @param bottom the bottom texture.
148 * @param left the left texture.
149 * @param right the right texture.
150 * @param front the front texture.
151 * @param back the back texture.
152*/
153void SkyBox::setTextures(const char* top, const char* bottom, const char* left,
154                          const char* right, const char* front, const char* back)
155{
156  this->material[0]->setDiffuseMap(top);
157  this->material[1]->setDiffuseMap(bottom);
158  this->material[2]->setDiffuseMap(left);
159  this->material[3]->setDiffuseMap(right);
160  this->material[4]->setDiffuseMap(front);
161  this->material[5]->setDiffuseMap(back);
162  this->loadCubeMapTextures(top, bottom, left, right, front, back);
163  //this->enableCubeMap();
164}
165
166void SkyBox::loadCubeMapTextures(const char* top, const char* bottom, const char* left,
167                                  const char* right, const char* front, const char* back)
168{
169  this->texture[0] = new Texture (top, GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB);
170  this->texture[1] = new Texture (bottom, GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB);
171  this->texture[2] = new Texture (left, GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB);
172  this->texture[3] = new Texture (right, GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB);
173  this->texture[4] = new Texture (front, GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB);
174  this->texture[5] = new Texture (back, GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB);
175}
176
177void SkyBox::enableCubeMap()
178{
179  glEnable(GL_TEXTURE_CUBE_MAP_ARB);
180  glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_REFLECTION_MAP_ARB);
181  glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_REFLECTION_MAP_ARB);
182  glTexGeni(GL_R, GL_TEXTURE_GEN_MODE, GL_REFLECTION_MAP_ARB);
183  glEnable(GL_TEXTURE_GEN_S);
184  glEnable(GL_TEXTURE_GEN_T);
185  glEnable(GL_TEXTURE_GEN_R);
186}
187
188void SkyBox::disableCubeMap()
189{
190  glDisable(GL_TEXTURE_CUBE_MAP_ARB);
191  glDisable(GL_TEXTURE_GEN_S);
192  glDisable(GL_TEXTURE_GEN_T);
193  glDisable(GL_TEXTURE_GEN_R);
194}
195
196
197
198/**
199 * @param size The new size of the SkyBox
200
201 * do not forget to rebuild the SkyBox after this.
202*/
203void SkyBox::setSize(float size)
204{
205  this->size = size;
206}
207
208
209
210void SkyBox::draw()
211{
212  glPushAttrib(GL_ENABLE_BIT);
213//   glPushAttrib(GL_LIGHTING_BIT);
214  glDisable(GL_LIGHTING);
215
216  WorldEntity::draw();
217
218  glPopAttrib();
219
220}
221
222
223/**
224 *  rebuilds the SkyBox
225
226   this must be done, when changing the Size of the Skybox (runtime-efficency)
227*/
228void SkyBox::rebuild()
229{
230  StaticModel* model = new StaticModel();
231
232  model->addVertex (-0.5*size, -0.5*size, 0.5*size);
233  model->addVertex (0.5*size, -0.5*size, 0.5*size);
234  model->addVertex (-0.5*size, 0.5*size, 0.5*size);
235  model->addVertex (0.5*size, 0.5*size, 0.5*size);
236  model->addVertex (-0.5*size, 0.5*size, -0.5*size);
237  model->addVertex (0.5*size, 0.5*size, -0.5*size);
238  model->addVertex (-0.5*size, -0.5*size, -0.5*size);
239  model->addVertex (0.5*size, -0.5*size, -0.5*size);
240
241//   model->addVertexTexture (0.0, 1.0);
242//   model->addVertexTexture (1.0, 1.0);
243//   model->addVertexTexture (1.0, 0.0);
244//   model->addVertexTexture (0.0, 0.0);
245
246  model->addVertexTexture (1.0/this->textureSize, (this->textureSize - 1.0)/this->textureSize);
247  model->addVertexTexture ((this->textureSize - 1.0)/this->textureSize, (this->textureSize - 1.0)/this->textureSize);
248  model->addVertexTexture ((this->textureSize - 1.0)/this->textureSize, 1.0/this->textureSize);
249  model->addVertexTexture (1.0/this->textureSize, 1.0/this->textureSize);
250
251
252  model->addVertexNormal (0.0, 0.0, 1.0);
253  model->addVertexNormal (0.0, 1.0, 0.0);
254  model->addVertexNormal (0.0, 0.0, -1.0);
255  model->addVertexNormal (0.0, -1.0, 0.0);
256  model->addVertexNormal (1.0, 0.0, 0.0);
257  model->addVertexNormal (-1.0, 0.0, 0.0);
258
259  model->setMaterial(material[0]);
260  model->addFace (4, VERTEX_TEXCOORD_NORMAL, 2,0,3, 3,1,3, 5,2,3, 4,3,3); // top
261  model->setMaterial(material[1]);
262  model->addFace (4, VERTEX_TEXCOORD_NORMAL, 6,0,1, 7,1,1, 1,2,1, 0,3,1); // bottom
263  model->setMaterial(material[2]);
264  model->addFace (4, VERTEX_TEXCOORD_NORMAL, 4,2,2, 5,3,2, 7,0,2, 6,1,2); // left
265  model->setMaterial(material[3]);
266  model->addFace (4, VERTEX_TEXCOORD_NORMAL, 0,0,0, 1,1,0, 3,2,0, 2,3,0); // right
267  model->setMaterial(material[4]);
268  model->addFace (4, VERTEX_TEXCOORD_NORMAL, 1,0,5, 7,1,5, 5,2,5, 3,3,5); // front
269  model->setMaterial(material[5]);
270  model->addFace (4, VERTEX_TEXCOORD_NORMAL, 6,0,4, 0,1,4, 2,2,4, 4,3,4); // back
271
272  model->finalize();
273
274  this->setModel(model);
275}
276
277int SkyBox::writeBytes( const byte * data, int length, int sender )
278{
279  setRequestedSync( false );
280  setIsOutOfSync( false );
281
282  SYNCHELP_READ_BEGIN();
283
284  SYNCHELP_READ_FKT( WorldEntity::writeState );
285
286  SYNCHELP_READ_FLOAT( size );
287  if ( textureName )
288  {
289    delete[] textureName;
290    textureName = NULL;
291  }
292  SYNCHELP_READ_STRINGM( textureName );
293
294  this->setSize( size );
295  this->setTextureAndType( textureName, "jpg" );
296  this->rebuild();
297
298  return SYNCHELP_READ_N;
299}
300
301
302
303int SkyBox::readBytes( byte * data, int maxLength, int * reciever )
304{
305  if ( isOutOfSync() && !requestedSync() && this->getHostID()!=this->getOwner() )
306  {
307    (NetworkGameManager::getInstance())->sync( this->getUniqueID(), this->getOwner() );
308    setRequestedSync( true );
309  }
310
311  int rec = this->getRequestSync();
312  if ( rec > 0 )
313  {
314    *reciever = rec;
315
316    SYNCHELP_WRITE_BEGIN();
317
318    SYNCHELP_WRITE_FKT( WorldEntity::readState );
319
320    SYNCHELP_WRITE_FLOAT(this->size);
321    SYNCHELP_WRITE_STRING(this->textureName);
322
323    return SYNCHELP_WRITE_N;
324  }
325
326  *reciever = 0;
327  return 0;
328}
329
330void SkyBox::writeDebug( ) const
331{
332}
333
334void SkyBox::readDebug( ) const
335{
336}
Note: See TracBrowser for help on using the repository browser.