Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/terrain.old/src/world_entities/skybox.cc @ 10642

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

merged back here the terrain.old

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