Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

trunk: better smoother water

File size: 8.6 KB
RevLine 
[4597]1/*
[3416]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:
[4261]12   main-programmer: Benjamin Grauer
13   co-programmer: ...
[3411]14*/
15
[3590]16#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_WORLD_ENTITY
17
[3796]18#include "skybox.h"
[4010]19
[5356]20#include "load_param.h"
[4010]21#include "factory.h"
[6022]22#include "static_model.h"
[6470]23
[6022]24#include "material.h"
[6470]25#include "texture.h"
26
[6341]27#include "network_game_manager.h"
28#include "converter.h"
[3608]29
[5356]30using namespace std;
[5357]31
[5750]32CREATE_FACTORY(SkyBox, CL_SKYBOX);
[3608]33
[3416]34/**
[5357]35 * Constructs a SkyBox and takes fileName as a map.
[4836]36 * @param fileName the file to take as input for the SkyBox
[3419]37*/
[4261]38SkyBox::SkyBox(const char* fileName)
[3419]39{
[4012]40  this->preInit();
[4261]41  if (fileName)
42    this->setTextureAndType(fileName, ".jpg");
[4012]43  this->postInit();
[4010]44}
45
[4444]46/**
[4836]47 *  initializes a skybox from a XmlElement
[4444]48*/
[4436]49SkyBox::SkyBox(const TiXmlElement* root)
[4010]50{
[4012]51  this->preInit();
[4010]52
[4261]53  this->loadParams(root);
[4010]54
[4012]55  this->postInit();
[4010]56}
57
[4261]58void SkyBox::loadParams(const TiXmlElement* root)
59{
[6512]60  WorldEntity::loadParams(root);
[4436]61
[5671]62  LoadParam(root, "Materialset", this, SkyBox, setTexture)
[4621]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
[5671]65  LoadParam(root, "Size", this, SkyBox, setSize)
[4621]66      .describe("Sets the Size of the SkyBox (normally this should be 90% of the maximal viewing Distance).");
[4261]67}
68
[4746]69void SkyBox::preInit()
[4010]70{
[4320]71  this->setClassID(CL_SKYBOX, "SkyBox");
[6142]72  this->toList(OM_ENVIRON_NOTICK);
[4621]73  this->size = 100.0;
[4620]74
[4597]75  for (int i = 0; i < 6; i++)
[3801]76    {
77      this->material[i] = new Material();
78      this->material[i]->setIllum(3);
[3805]79      this->material[i]->setDiffuse(0.0,0.0,0.0);
80      this->material[i]->setSpecular(0.0,0.0,0.0);
81      this->material[i]->setAmbient(2.0, 2.0, 2.0);
[6470]82
[6519]83      this->cubeTexture[i] = NULL;
[3801]84    }
[4444]85  this->setParentMode(PNODE_MOVEMENT);
[6341]86
87  this->textureName = NULL;
[4012]88}
[3803]89
[4746]90void SkyBox::postInit()
[4012]91{
92  this->rebuild();
[3411]93}
94
[3507]95
[3416]96/**
[4836]97 *  default destructor
[3416]98*/
[3796]99SkyBox::~SkyBox()
[3411]100{
[4136]101  PRINTF(5)("Deleting SkyBox\n");
[5994]102  this->setModel(NULL); //< so that WorldEntity does not try to delete it again.
[3801]103  for (int i = 0; i < 6; i++)
104    delete this->material[i];
[6307]105}
[3411]106
[3803]107/**
[4836]108 *  sets A set of textures when just giving a Name and an extension:
[3763]109
[3803]110   usage: give this function an argument like
111   setTexture("skybox", "jpg");
[4597]112   and it will convert this to
[3803]113   setTextures("skybox_top.jpg", "skybox_bottom.jpg", "skybox_left.jpg",
114               "skybox_right.jpg", "skybox_front.jpg", "skybox_back.jpg");
115*/
[4261]116void SkyBox::setTextureAndType(const char* name, const char* extension)
[3803]117{
[4012]118  char* top    = new char[strlen(name)+strlen(extension)+ 10];
119  char* bottom = new char[strlen(name)+strlen(extension)+ 10];
120  char* left   = new char[strlen(name)+strlen(extension)+ 10];
121  char* right  = new char[strlen(name)+strlen(extension)+ 10];
122  char* front  = new char[strlen(name)+strlen(extension)+ 10];
123  char* back   = new char[strlen(name)+strlen(extension)+ 10];
[3803]124
125  sprintf(top, "%s_top.%s", name, extension);
126  sprintf(bottom, "%s_bottom.%s", name, extension);
127  sprintf(left, "%s_left.%s", name, extension);
128  sprintf(right, "%s_right.%s", name, extension);
129  sprintf(front, "%s_front.%s", name, extension);
130  sprintf(back, "%s_back.%s", name, extension);
[4597]131
[3803]132  this->setTextures(top, bottom, left, right, front, back);
133
[4012]134  // deleted alocated memory of this function
[3803]135  delete []top;
136  delete []bottom;
137  delete []left;
138  delete []right;
139  delete []front;
140  delete []back;
141}
142
143/**
[4836]144 *  Defines which textures should be loaded onto the SkyBox.
145 * @param top the top texture.
146 * @param bottom the bottom texture.
147 * @param left the left texture.
148 * @param right the right texture.
149 * @param front the front texture.
150 * @param back the back texture.
[3803]151*/
[6470]152void SkyBox::setTextures(const char* top, const char* bottom, const char* left,
153                          const char* right, const char* front, const char* back)
[3803]154{
155  this->material[0]->setDiffuseMap(top);
156  this->material[1]->setDiffuseMap(bottom);
157  this->material[2]->setDiffuseMap(left);
158  this->material[3]->setDiffuseMap(right);
159  this->material[4]->setDiffuseMap(front);
160  this->material[5]->setDiffuseMap(back);
[6470]161  this->loadCubeMapTextures(top, bottom, left, right, front, back);
162  //this->enableCubeMap();
[3803]163}
164
[6470]165void SkyBox::loadCubeMapTextures(const char* top, const char* bottom, const char* left,
166                                  const char* right, const char* front, const char* back)
167{
[6519]168  this->cubeTexture[0] = new Texture (top, GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB);
169  this->cubeTexture[1] = new Texture (bottom, GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB);
170  this->cubeTexture[2] = new Texture (left, GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB);
171  this->cubeTexture[3] = new Texture (right, GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB);
172  this->cubeTexture[4] = new Texture (front, GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB);
173  this->cubeTexture[5] = new Texture (back, GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB);
[6470]174}
175
176void SkyBox::enableCubeMap()
177{
178  glEnable(GL_TEXTURE_CUBE_MAP_ARB);
[6519]179  glEnable(GL_TEXTURE_2D);
[6470]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);
[6519]191  glDisable(GL_TEXTURE_2D);
[6470]192  glDisable(GL_TEXTURE_GEN_S);
193  glDisable(GL_TEXTURE_GEN_T);
194  glDisable(GL_TEXTURE_GEN_R);
195}
196
197
198
[3803]199/**
[4836]200 * @param size The new size of the SkyBox
[4621]201
202 * do not forget to rebuild the SkyBox after this.
[3803]203*/
204void SkyBox::setSize(float size)
205{
206  this->size = size;
207}
208
209/**
[4836]210 *  rebuilds the SkyBox
[4597]211
[3803]212   this must be done, when changing the Size of the Skybox (runtime-efficency)
213*/
[3801]214void SkyBox::rebuild()
215{
[6022]216  StaticModel* model = new StaticModel();
[3801]217
[5994]218  model->addVertex (-0.5*size, -0.5*size, 0.5*size);
219  model->addVertex (0.5*size, -0.5*size, 0.5*size);
220  model->addVertex (-0.5*size, 0.5*size, 0.5*size);
221  model->addVertex (0.5*size, 0.5*size, 0.5*size);
222  model->addVertex (-0.5*size, 0.5*size, -0.5*size);
223  model->addVertex (0.5*size, 0.5*size, -0.5*size);
224  model->addVertex (-0.5*size, -0.5*size, -0.5*size);
225  model->addVertex (0.5*size, -0.5*size, -0.5*size);
[3801]226
[5994]227  model->addVertexTexture (0.0, 1.0);
228  model->addVertexTexture (1.0, 1.0);
229  model->addVertexTexture (1.0, 0.0);
230  model->addVertexTexture (0.0, 0.0);
[3801]231
[5994]232  model->addVertexNormal (0.0, 0.0, 1.0);
233  model->addVertexNormal (0.0, 1.0, 0.0);
234  model->addVertexNormal (0.0, 0.0, -1.0);
235  model->addVertexNormal (0.0, -1.0, 0.0);
236  model->addVertexNormal (1.0, 0.0, 0.0);
237  model->addVertexNormal (-1.0, 0.0, 0.0);
[3801]238
[5994]239  model->setMaterial(material[0]);
240  model->addFace (4, VERTEX_TEXCOORD_NORMAL, 2,0,3, 3,1,3, 5,2,3, 4,3,3); // top
241  model->setMaterial(material[1]);
242  model->addFace (4, VERTEX_TEXCOORD_NORMAL, 6,0,1, 7,1,1, 1,2,1, 0,3,1); // bottom
243  model->setMaterial(material[2]);
244  model->addFace (4, VERTEX_TEXCOORD_NORMAL, 4,2,2, 5,3,2, 7,0,2, 6,1,2); // left
245  model->setMaterial(material[3]);
246  model->addFace (4, VERTEX_TEXCOORD_NORMAL, 0,0,0, 1,1,0, 3,2,0, 2,3,0); // right
247  model->setMaterial(material[4]);
248  model->addFace (4, VERTEX_TEXCOORD_NORMAL, 1,0,5, 7,1,5, 5,2,5, 3,3,5); // front
249  model->setMaterial(material[5]);
250  model->addFace (4, VERTEX_TEXCOORD_NORMAL, 6,0,4, 0,1,4, 2,2,4, 4,3,4); // back
[4597]251
[5994]252  model->finalize();
253
254  this->setModel(model);
[3801]255}
[6341]256
257int SkyBox::writeBytes( const byte * data, int length, int sender )
258{
259  setRequestedSync( false );
260  setIsOutOfSync( false );
261
262  SYNCHELP_READ_BEGIN();
263
264  SYNCHELP_READ_FKT( WorldEntity::writeState );
265
266  SYNCHELP_READ_FLOAT( size );
267  if ( textureName )
268  {
269    delete[] textureName;
270    textureName = NULL;
271  }
272  SYNCHELP_READ_STRINGM( textureName );
273
274  this->setSize( size );
275  this->setTextureAndType( textureName, "jpg" );
276  this->rebuild();
277
278  return SYNCHELP_READ_N;
279}
280
281
282
283int SkyBox::readBytes( byte * data, int maxLength, int * reciever )
284{
285  if ( isOutOfSync() && !requestedSync() && this->getHostID()!=this->getOwner() )
286  {
287    (NetworkGameManager::getInstance())->sync( this->getUniqueID(), this->getOwner() );
288    setRequestedSync( true );
289  }
290
291  int rec = this->getRequestSync();
292  if ( rec > 0 )
293  {
294    *reciever = rec;
295
296    SYNCHELP_WRITE_BEGIN();
297
298    SYNCHELP_WRITE_FKT( WorldEntity::readState );
299
300    SYNCHELP_WRITE_FLOAT(this->size);
301    SYNCHELP_WRITE_STRING(this->textureName);
302
303    return SYNCHELP_WRITE_N;
304  }
305
306  *reciever = 0;
307  return 0;
308}
309
310void SkyBox::writeDebug( ) const
311{
312}
313
314void SkyBox::readDebug( ) const
315{
316}
Note: See TracBrowser for help on using the repository browser.