Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 6511 was 6511, checked in by patrick, 20 years ago

network: skybox is now fixed, there is no seam on the border anymore, some more texture work for the stars background

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_LIGHTING_BIT);
213  glDisable(GL_LIGHTING);
214  WorldEntity::draw();
215//   glEnable(GL_LIGHTING);
216  glPopAttrib();
217
218}
219
220
221/**
222 *  rebuilds the SkyBox
223
224   this must be done, when changing the Size of the Skybox (runtime-efficency)
225*/
226void SkyBox::rebuild()
227{
228  StaticModel* model = new StaticModel();
229
230  model->addVertex (-0.5*size, -0.5*size, 0.5*size);
231  model->addVertex (0.5*size, -0.5*size, 0.5*size);
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
239//   model->addVertexTexture (0.0, 1.0);
240//   model->addVertexTexture (1.0, 1.0);
241//   model->addVertexTexture (1.0, 0.0);
242//   model->addVertexTexture (0.0, 0.0);
243
244  model->addVertexTexture (1.0/this->textureSize, (this->textureSize - 1.0)/this->textureSize);
245  model->addVertexTexture ((this->textureSize - 1.0)/this->textureSize, (this->textureSize - 1.0)/this->textureSize);
246  model->addVertexTexture ((this->textureSize - 1.0)/this->textureSize, 1.0/this->textureSize);
247  model->addVertexTexture (1.0/this->textureSize, 1.0/this->textureSize);
248
249
250  model->addVertexNormal (0.0, 0.0, 1.0);
251  model->addVertexNormal (0.0, 1.0, 0.0);
252  model->addVertexNormal (0.0, 0.0, -1.0);
253  model->addVertexNormal (0.0, -1.0, 0.0);
254  model->addVertexNormal (1.0, 0.0, 0.0);
255  model->addVertexNormal (-1.0, 0.0, 0.0);
256
257  model->setMaterial(material[0]);
258  model->addFace (4, VERTEX_TEXCOORD_NORMAL, 2,0,3, 3,1,3, 5,2,3, 4,3,3); // top
259  model->setMaterial(material[1]);
260  model->addFace (4, VERTEX_TEXCOORD_NORMAL, 6,0,1, 7,1,1, 1,2,1, 0,3,1); // bottom
261  model->setMaterial(material[2]);
262  model->addFace (4, VERTEX_TEXCOORD_NORMAL, 4,2,2, 5,3,2, 7,0,2, 6,1,2); // left
263  model->setMaterial(material[3]);
264  model->addFace (4, VERTEX_TEXCOORD_NORMAL, 0,0,0, 1,1,0, 3,2,0, 2,3,0); // right
265  model->setMaterial(material[4]);
266  model->addFace (4, VERTEX_TEXCOORD_NORMAL, 1,0,5, 7,1,5, 5,2,5, 3,3,5); // front
267  model->setMaterial(material[5]);
268  model->addFace (4, VERTEX_TEXCOORD_NORMAL, 6,0,4, 0,1,4, 2,2,4, 4,3,4); // back
269
270  model->finalize();
271
272  this->setModel(model);
273}
274
275int SkyBox::writeBytes( const byte * data, int length, int sender )
276{
277  setRequestedSync( false );
278  setIsOutOfSync( false );
279
280  SYNCHELP_READ_BEGIN();
281
282  SYNCHELP_READ_FKT( WorldEntity::writeState );
283
284  SYNCHELP_READ_FLOAT( size );
285  if ( textureName )
286  {
287    delete[] textureName;
288    textureName = NULL;
289  }
290  SYNCHELP_READ_STRINGM( textureName );
291
292  this->setSize( size );
293  this->setTextureAndType( textureName, "jpg" );
294  this->rebuild();
295
296  return SYNCHELP_READ_N;
297}
298
299
300
301int SkyBox::readBytes( byte * data, int maxLength, int * reciever )
302{
303  if ( isOutOfSync() && !requestedSync() && this->getHostID()!=this->getOwner() )
304  {
305    (NetworkGameManager::getInstance())->sync( this->getUniqueID(), this->getOwner() );
306    setRequestedSync( true );
307  }
308
309  int rec = this->getRequestSync();
310  if ( rec > 0 )
311  {
312    *reciever = rec;
313
314    SYNCHELP_WRITE_BEGIN();
315
316    SYNCHELP_WRITE_FKT( WorldEntity::readState );
317
318    SYNCHELP_WRITE_FLOAT(this->size);
319    SYNCHELP_WRITE_STRING(this->textureName);
320
321    return SYNCHELP_WRITE_N;
322  }
323
324  *reciever = 0;
325  return 0;
326}
327
328void SkyBox::writeDebug( ) const
329{
330}
331
332void SkyBox::readDebug( ) const
333{
334}
Note: See TracBrowser for help on using the repository browser.