Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/camera/src/world_entities/skybox.cc @ 10259

Last change on this file since 10259 was 10259, checked in by gfilip, 17 years ago

the skybox problem

File size: 9.0 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 "util/loading/load_param.h"
21#include "util/loading/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#include "resource_texture.h"
30
31#include "debug.h"
32
33#include "state.h"
34#include "playable.h"
35#include "player.h"
36
37#include "class_id_DEPRECATED.h"
38ObjectListDefinitionID(SkyBox, CL_SKYBOX);
39CREATE_FACTORY(SkyBox);
40
41/**
42 * Constructs a SkyBox and takes fileName as a map.
43 * @param fileName the file to take as input for the SkyBox
44*/
45SkyBox::SkyBox(const std::string& fileName)
46{
47  this->preInit();
48  if (!fileName.empty())
49    this->setTextureAndType(fileName, ".jpg");
50  this->postInit();
51}
52
53/**
54 *  initializes a skybox from a XmlElement
55*/
56SkyBox::SkyBox(const TiXmlElement* root)
57{
58  this->preInit();
59
60  if( root != NULL)
61    this->loadParams(root);
62
63  this->postInit();
64}
65
66void SkyBox::loadParams(const TiXmlElement* root)
67{
68  WorldEntity::loadParams(root);
69
70  LoadParam(root, "Materialset", this, SkyBox, setTexture)
71      .describe("Sets the material on the SkyBox. The string must be the path relative to the data-dir, and without a trailing .jpg");
72
73  LoadParam(root, "Size", this, SkyBox, setSize)
74      .describe("Sets the Size of the SkyBox (normally this should be 90% of the maximal viewing Distance).");
75}
76
77void SkyBox::preInit()
78{
79  this->registerObject(this, SkyBox::_objectList);
80  this->toList(OM_BACKGROUND);
81  this->toReflectionList();
82  //this->size = 100.0;
83  this->textureSize = 1024.0f;
84
85  for (int i = 0; i < 6; i++)
86    {
87      this->material[i] = new Material();
88      this->material[i]->setIllum(3);
89      this->material[i]->setDiffuse(0.0,0.0,0.0);
90      this->material[i]->setSpecular(0.0,0.0,0.0);
91      this->material[i]->setAmbient(2.0, 2.0, 2.0);
92
93      this->cubeTexture[i] = NULL;
94    }
95  this->setParentMode(PNODE_MOVEMENT);
96
97  this->textureName = "";
98}
99
100void SkyBox::postInit()
101{
102  this->rebuild();
103
104  textureName_handle = registerVarId( new SynchronizeableString( &textureName, &textureName, "textureName", PERMISSION_MASTER_SERVER) );
105  size_handle = registerVarId( new SynchronizeableFloat( &size, &size, "size", PERMISSION_MASTER_SERVER ) );
106}
107
108
109/**
110 *  default destructor
111*/
112SkyBox::~SkyBox()
113{
114  PRINTF(5)("Deleting SkyBox\n");
115  for (int i = 0; i < 6; i++)
116  {
117    if (this->material[i])
118      delete this->material[i];
119  }
120}
121
122void SkyBox::setTexture(const std::string& name)
123{
124  this->textureName = name;
125  this->setTextureAndType (name, "jpg");
126};
127
128
129/**
130 * @brief sets A set of textures when just giving a Name and an extension:
131 * @param name the prefix of the Name
132 * @param extension the file extension (jpg by default)
133 * usage: give this function an argument like
134 * setTexture("skybox", "jpg");
135 * and it will convert this to
136 * setTextures("skybox_negx.jpg", "skybox_posx.jpg", "skybox_negy.jpg",
137 *             "skybox_posy.jpg", "skybox_negz.jpg", "skybox_posz.jpg");
138 */
139void SkyBox::setTextureAndType(const std::string& name, const std::string& extension)
140{
141  std::string negX = name + "_negx." + extension;
142  std::string posX = name + "_posx." + extension;
143
144  std::string negY = name + "_negy." + extension;
145  std::string posY = name + "_posy." + extension;
146
147  std::string negZ = name + "_negz." + extension;
148  std::string posZ = name + "_posz." + extension;
149
150  this->setTextures(negX, posX, negY, posY, negZ, posZ);
151}
152
153/**
154 * @brief Defines which textures should be loaded onto the SkyBox.
155 * @param negX the top texture.
156 * @param posX the bottom texture.
157 * @param negY the left texture.
158 * @param posY the right texture.
159 * @param negZ the front texture.
160 * @param posZ the back texture.
161*/
162void SkyBox::setTextures(const std::string& negX, const std::string& posX,
163                         const std::string& negY, const std::string& posY,
164                         const std::string& negZ, const std::string& posZ)
165{
166  this->material[0]->setDiffuseMap(negX);
167  this->material[1]->setDiffuseMap(posX);
168  this->material[2]->setDiffuseMap(negY);
169  this->material[3]->setDiffuseMap(posY);
170  this->material[4]->setDiffuseMap(negZ);
171  this->material[5]->setDiffuseMap(posZ);
172  if (GLEW_EXT_texture_cube_map)
173    this->loadCubeMapTextures(negX, posX, negY, posY, negZ, posZ);
174}
175
176void SkyBox::loadCubeMapTextures(const std::string& posY, const std::string& negY, const std::string& negZ,
177                                  const std::string& posZ, const std::string& posX, const std::string& negX)
178{
179  this->cubeTexture[0] = ResourceTexture(negX, GL_TEXTURE_CUBE_MAP_NEGATIVE_X_EXT);
180  this->cubeTexture[1] = ResourceTexture(posX, GL_TEXTURE_CUBE_MAP_POSITIVE_X_EXT);
181
182  this->cubeTexture[2] = ResourceTexture(negY, GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_EXT);
183  this->cubeTexture[3] = ResourceTexture(posY, GL_TEXTURE_CUBE_MAP_POSITIVE_Y_EXT);
184
185  this->cubeTexture[4] = ResourceTexture(negZ, GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_EXT);
186  this->cubeTexture[5] = ResourceTexture(posZ, GL_TEXTURE_CUBE_MAP_POSITIVE_Z_EXT);
187}
188
189void SkyBox::enableCubeMap()
190{
191  glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_REFLECTION_MAP);
192  glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_REFLECTION_MAP);
193  glTexGeni(GL_R, GL_TEXTURE_GEN_MODE, GL_REFLECTION_MAP);
194
195  glEnable(GL_TEXTURE_CUBE_MAP_EXT);
196
197  glEnable(GL_TEXTURE_GEN_S);
198  glEnable(GL_TEXTURE_GEN_T);
199  glEnable(GL_TEXTURE_GEN_R);
200
201}
202
203void SkyBox::disableCubeMap()
204{
205  glDisable(GL_TEXTURE_CUBE_MAP);
206  glDisable(GL_TEXTURE_2D);
207  glDisable(GL_TEXTURE_GEN_S);
208  glDisable(GL_TEXTURE_GEN_T);
209  glDisable(GL_TEXTURE_GEN_R);
210
211  glDisable(GL_TEXTURE_GEN_S);
212  glDisable(GL_TEXTURE_GEN_T);
213  glDisable(GL_TEXTURE_GEN_R);
214}
215
216
217
218/**
219 * @param size The new size of the SkyBox
220
221 * do not forget to rebuild the SkyBox after this.
222*/
223void SkyBox::setSize(float size)
224{
225  this->size = size;
226}
227
228
229
230void SkyBox::tick(float dt)
231{
232  PRINTF(0)("tick\n");
233
234  Player* pl = State::getPlayer();
235  if( pl == NULL)
236    return;
237
238  Playable* playable = pl->getPlayable();
239  if( playable == NULL)
240    return;
241
242  if( this->getParent() != playable)
243    this->setParent( playable);
244}
245
246
247
248void SkyBox::draw()
249{
250  glPushAttrib(GL_ENABLE_BIT);
251//   glPushAttrib(GL_LIGHTING_BIT);
252  glDisable(GL_LIGHTING);
253
254  glDisable(GL_FOG);
255
256  WorldEntity::draw();
257
258  glPopAttrib();
259
260}
261
262
263/**
264 *  rebuilds the SkyBox
265
266   this must be done, when changing the Size of the Skybox (runtime-efficency)
267*/
268void SkyBox::rebuild()
269{
270  StaticModel* model = new StaticModel();
271
272  model->addVertex (-0.5*size, -0.5*size, 0.5*size);
273  model->addVertex (0.5*size, -0.5*size, 0.5*size);
274  model->addVertex (-0.5*size, 0.5*size, 0.5*size);
275  model->addVertex (0.5*size, 0.5*size, 0.5*size);
276  model->addVertex (-0.5*size, 0.5*size, -0.5*size);
277  model->addVertex (0.5*size, 0.5*size, -0.5*size);
278  model->addVertex (-0.5*size, -0.5*size, -0.5*size);
279  model->addVertex (0.5*size, -0.5*size, -0.5*size);
280
281//   model->addVertexTexture (0.0, 1.0);
282//   model->addVertexTexture (1.0, 1.0);
283//   model->addVertexTexture (1.0, 0.0);
284//   model->addVertexTexture (0.0, 0.0);
285
286  model->addVertexTexture (1.0/this->textureSize, (this->textureSize - 1.0)/this->textureSize);
287  model->addVertexTexture ((this->textureSize - 1.0)/this->textureSize, (this->textureSize - 1.0)/this->textureSize);
288  model->addVertexTexture ((this->textureSize - 1.0)/this->textureSize, 1.0/this->textureSize);
289  model->addVertexTexture (1.0/this->textureSize, 1.0/this->textureSize);
290
291
292  model->addVertexNormal (0.0, 0.0, 1.0);
293  model->addVertexNormal (0.0, 1.0, 0.0);
294  model->addVertexNormal (0.0, 0.0, -1.0);
295  model->addVertexNormal (0.0, -1.0, 0.0);
296  model->addVertexNormal (1.0, 0.0, 0.0);
297  model->addVertexNormal (-1.0, 0.0, 0.0);
298
299  model->setMaterial(material[0]);
300  model->addFace (4, VERTEX_TEXCOORD_NORMAL, 6,0,4, 0,1,4, 2,2,4, 4,3,4); // back
301  model->setMaterial(material[1]);
302  model->addFace (4, VERTEX_TEXCOORD_NORMAL, 1,0,5, 7,1,5, 5,2,5, 3,3,5); // front
303  model->setMaterial(material[2]);
304  model->addFace (4, VERTEX_TEXCOORD_NORMAL, 6,0,1, 7,1,1, 1,2,1, 0,3,1); // bottom
305  model->setMaterial(material[3]);
306  model->addFace (4, VERTEX_TEXCOORD_NORMAL, 2,0,3, 3,1,3, 5,2,3, 4,3,3); // top
307  model->setMaterial(material[4]);
308  model->addFace (4, VERTEX_TEXCOORD_NORMAL, 4,2,2, 5,3,2, 7,0,2, 6,1,2); // left
309  model->setMaterial(material[5]);
310  model->addFace (4, VERTEX_TEXCOORD_NORMAL, 0,0,0, 1,1,0, 3,2,0, 2,3,0); // right
311
312  model->finalize();
313
314  this->setModel(model);
315}
316
317void SkyBox::varChangeHandler( std::list< int > & id )
318{
319  bool somethinChanged = false;
320
321  if ( std::find( id.begin(), id.end(), textureName_handle ) != id.end() )
322  {
323    somethinChanged = true;
324    setTexture( textureName );
325  }
326
327  if ( std::find( id.begin(), id.end(), size_handle ) != id.end() )
328  {
329    somethinChanged = true;
330  }
331
332  rebuild();
333
334  WorldEntity::varChangeHandler( id );
335}
Note: See TracBrowser for help on using the repository browser.