Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 6686 was 6634, checked in by bensch, 19 years ago

orxonox/trunk: merged the network-branche back to the trunk

merged with command:
svn merge https://svn.orxonox.net/orxonox/branches/network . -r6500:HEAD
minor conflicts in texture and one Makefile resolved to the trunk

also made a small patch to texture, so it Modulates with GL_REPEAT

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