Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 6797 was 6772, checked in by bensch, 19 years ago

orxonox/trunk: merged the branche network back
merged with command
svn merge https://svn.orxonox.net/orxonox/branches/network . -r6755:HEAD

absolutely no conficts

File size: 9.4 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
[6695]53  if( root != NULL)
54    this->loadParams(root);
[4010]55
[4012]56  this->postInit();
[4010]57}
58
[4261]59void SkyBox::loadParams(const TiXmlElement* root)
60{
[6512]61  WorldEntity::loadParams(root);
[4436]62
[5671]63  LoadParam(root, "Materialset", this, SkyBox, setTexture)
[4621]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
[5671]66  LoadParam(root, "Size", this, SkyBox, setSize)
[4621]67      .describe("Sets the Size of the SkyBox (normally this should be 90% of the maximal viewing Distance).");
[4261]68}
69
[4746]70void SkyBox::preInit()
[4010]71{
[4320]72  this->setClassID(CL_SKYBOX, "SkyBox");
[6142]73  this->toList(OM_ENVIRON_NOTICK);
[6634]74  //this->size = 100.0;
75  this->textureSize = 1024.0f;
[4620]76
[4597]77  for (int i = 0; i < 6; i++)
[3801]78    {
79      this->material[i] = new Material();
80      this->material[i]->setIllum(3);
[3805]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);
[6470]84
[6519]85      this->cubeTexture[i] = NULL;
[3801]86    }
[4444]87  this->setParentMode(PNODE_MOVEMENT);
[6341]88
89  this->textureName = NULL;
[4012]90}
[3803]91
[4746]92void SkyBox::postInit()
[4012]93{
94  this->rebuild();
[3411]95}
96
[3507]97
[3416]98/**
[4836]99 *  default destructor
[3416]100*/
[3796]101SkyBox::~SkyBox()
[3411]102{
[4136]103  PRINTF(5)("Deleting SkyBox\n");
[5994]104  this->setModel(NULL); //< so that WorldEntity does not try to delete it again.
[3801]105  for (int i = 0; i < 6; i++)
[6523]106  {
[3801]107    delete this->material[i];
[6523]108    delete this->cubeTexture[i];
109  }
[6307]110}
[3411]111
[3803]112/**
[4836]113 *  sets A set of textures when just giving a Name and an extension:
[3763]114
[3803]115   usage: give this function an argument like
116   setTexture("skybox", "jpg");
[4597]117   and it will convert this to
[3803]118   setTextures("skybox_top.jpg", "skybox_bottom.jpg", "skybox_left.jpg",
119               "skybox_right.jpg", "skybox_front.jpg", "skybox_back.jpg");
120*/
[4261]121void SkyBox::setTextureAndType(const char* name, const char* extension)
[3803]122{
[4012]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];
[3803]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);
[4597]136
[3803]137  this->setTextures(top, bottom, left, right, front, back);
138
[4012]139  // deleted alocated memory of this function
[3803]140  delete []top;
141  delete []bottom;
142  delete []left;
143  delete []right;
144  delete []front;
145  delete []back;
146}
147
148/**
[4836]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.
[3803]156*/
[6470]157void SkyBox::setTextures(const char* top, const char* bottom, const char* left,
158                          const char* right, const char* front, const char* back)
[3803]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);
[6523]166  if (GLEW_EXT_texture_cube_map)
167    this->loadCubeMapTextures(top, bottom, left, right, front, back);
[3803]168}
169
[6470]170void SkyBox::loadCubeMapTextures(const char* top, const char* bottom, const char* left,
171                                  const char* right, const char* front, const char* back)
172{
[6523]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);
[6470]179}
180
181void SkyBox::enableCubeMap()
182{
[6519]183  glEnable(GL_TEXTURE_2D);
[6523]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);
[6470]188  glEnable(GL_TEXTURE_GEN_S);
189  glEnable(GL_TEXTURE_GEN_T);
190  glEnable(GL_TEXTURE_GEN_R);
[6523]191
[6470]192}
193
194void SkyBox::disableCubeMap()
195{
[6523]196  glDisable(GL_TEXTURE_CUBE_MAP_EXT);
[6519]197  glDisable(GL_TEXTURE_2D);
[6470]198  glDisable(GL_TEXTURE_GEN_S);
199  glDisable(GL_TEXTURE_GEN_T);
200  glDisable(GL_TEXTURE_GEN_R);
[6523]201
202  glDisable(GL_TEXTURE_GEN_S);
203  glDisable(GL_TEXTURE_GEN_T);
204  glDisable(GL_TEXTURE_GEN_R);
[6470]205}
206
207
208
[3803]209/**
[4836]210 * @param size The new size of the SkyBox
[4621]211
212 * do not forget to rebuild the SkyBox after this.
[3803]213*/
214void SkyBox::setSize(float size)
215{
216  this->size = size;
217}
218
[6634]219
220
221void SkyBox::draw()
222{
223  glPushAttrib(GL_ENABLE_BIT);
224//   glPushAttrib(GL_LIGHTING_BIT);
225  glDisable(GL_LIGHTING);
226
[6772]227  glPushAttrib(GL_ENABLE_BIT);
228  glDisable(GL_FOG);
229
[6634]230  WorldEntity::draw();
231
232  glPopAttrib();
[6772]233  glPopAttrib();
[6634]234
235}
236
237
[3803]238/**
[4836]239 *  rebuilds the SkyBox
[4597]240
[3803]241   this must be done, when changing the Size of the Skybox (runtime-efficency)
242*/
[3801]243void SkyBox::rebuild()
244{
[6022]245  StaticModel* model = new StaticModel();
[3801]246
[5994]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  model->addVertex (-0.5*size, 0.5*size, -0.5*size);
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);
[3801]255
[6634]256//   model->addVertexTexture (0.0, 1.0);
257//   model->addVertexTexture (1.0, 1.0);
258//   model->addVertexTexture (1.0, 0.0);
259//   model->addVertexTexture (0.0, 0.0);
[3801]260
[6634]261  model->addVertexTexture (1.0/this->textureSize, (this->textureSize - 1.0)/this->textureSize);
262  model->addVertexTexture ((this->textureSize - 1.0)/this->textureSize, (this->textureSize - 1.0)/this->textureSize);
263  model->addVertexTexture ((this->textureSize - 1.0)/this->textureSize, 1.0/this->textureSize);
264  model->addVertexTexture (1.0/this->textureSize, 1.0/this->textureSize);
265
266
[5994]267  model->addVertexNormal (0.0, 0.0, 1.0);
268  model->addVertexNormal (0.0, 1.0, 0.0);
269  model->addVertexNormal (0.0, 0.0, -1.0);
270  model->addVertexNormal (0.0, -1.0, 0.0);
271  model->addVertexNormal (1.0, 0.0, 0.0);
272  model->addVertexNormal (-1.0, 0.0, 0.0);
[3801]273
[5994]274  model->setMaterial(material[0]);
275  model->addFace (4, VERTEX_TEXCOORD_NORMAL, 2,0,3, 3,1,3, 5,2,3, 4,3,3); // top
276  model->setMaterial(material[1]);
277  model->addFace (4, VERTEX_TEXCOORD_NORMAL, 6,0,1, 7,1,1, 1,2,1, 0,3,1); // bottom
278  model->setMaterial(material[2]);
279  model->addFace (4, VERTEX_TEXCOORD_NORMAL, 4,2,2, 5,3,2, 7,0,2, 6,1,2); // left
280  model->setMaterial(material[3]);
281  model->addFace (4, VERTEX_TEXCOORD_NORMAL, 0,0,0, 1,1,0, 3,2,0, 2,3,0); // right
282  model->setMaterial(material[4]);
283  model->addFace (4, VERTEX_TEXCOORD_NORMAL, 1,0,5, 7,1,5, 5,2,5, 3,3,5); // front
284  model->setMaterial(material[5]);
285  model->addFace (4, VERTEX_TEXCOORD_NORMAL, 6,0,4, 0,1,4, 2,2,4, 4,3,4); // back
[4597]286
[5994]287  model->finalize();
288
289  this->setModel(model);
[3801]290}
[6341]291
292int SkyBox::writeBytes( const byte * data, int length, int sender )
293{
294  setRequestedSync( false );
295  setIsOutOfSync( false );
296
297  SYNCHELP_READ_BEGIN();
298
299  SYNCHELP_READ_FKT( WorldEntity::writeState );
300
301  SYNCHELP_READ_FLOAT( size );
302  if ( textureName )
303  {
304    delete[] textureName;
305    textureName = NULL;
306  }
307  SYNCHELP_READ_STRINGM( textureName );
308
309  this->setSize( size );
310  this->setTextureAndType( textureName, "jpg" );
311  this->rebuild();
312
313  return SYNCHELP_READ_N;
314}
315
316
317
318int SkyBox::readBytes( byte * data, int maxLength, int * reciever )
319{
320  if ( isOutOfSync() && !requestedSync() && this->getHostID()!=this->getOwner() )
321  {
322    (NetworkGameManager::getInstance())->sync( this->getUniqueID(), this->getOwner() );
323    setRequestedSync( true );
324  }
325
326  int rec = this->getRequestSync();
327  if ( rec > 0 )
328  {
329    *reciever = rec;
330
331    SYNCHELP_WRITE_BEGIN();
332
333    SYNCHELP_WRITE_FKT( WorldEntity::readState );
334
335    SYNCHELP_WRITE_FLOAT(this->size);
336    SYNCHELP_WRITE_STRING(this->textureName);
337
338    return SYNCHELP_WRITE_N;
339  }
340
341  *reciever = 0;
342  return 0;
343}
344
345void SkyBox::writeDebug( ) const
346{
347}
348
349void SkyBox::readDebug( ) const
350{
351}
Note: See TracBrowser for help on using the repository browser.