Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/blink/src/world_entities/skybox.cc @ 10663

Last change on this file since 10663 was 10114, checked in by patrick, 18 years ago

merged network back to trunk

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