Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: the Sky is farther now

File size: 6.4 KB
Line 
1
2/*
3   orxonox - the future of 3D-vertical-scrollers
4
5   Copyright (C) 2004 orx
6
7   This program is free software; you can redistribute it and/or modify
8   it under the terms of the GNU General Public License as published by
9   the Free Software Foundation; either version 2, or (at your option)
10   any later version.
11
12   ### File Specific:
13   main-programmer: Benjamin Grauer
14   co-programmer: ...
15*/
16
17#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_WORLD_ENTITY
18
19
20#include "skybox.h"
21
22#include "stdincl.h"
23#include "factory.h"
24
25#include "material.h"
26#include "vector.h"
27#include "resource_manager.h"
28#include "model.h"
29//#include "world_entity.h"
30
31CREATE_FACTORY(SkyBox);
32
33using namespace std;
34
35/**
36   \brief Constructs a SkyBox and takes fileName as a map.
37   \param fileName the file to take as input for the SkyBox
38*/
39SkyBox::SkyBox(const char* fileName)
40{
41  this->preInit();
42  if (fileName)
43    this->setTextureAndType(fileName, ".jpg");
44  this->postInit();
45}
46
47/**
48   \brief initializes a skybox from a XmlElement
49*/
50SkyBox::SkyBox(const TiXmlElement* root)
51{
52  this->preInit();
53
54  this->loadParams(root);
55
56  this->postInit();
57}
58
59void SkyBox::loadParams(const TiXmlElement* root)
60{
61  static_cast<WorldEntity*>(this)->loadParams(root);
62
63  LoadParam<SkyBox>(root, "Materialset", this, &SkyBox::setTexture)
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}
66
67void SkyBox::preInit(void)
68{
69  this->setClassID(CL_SKYBOX, "SkyBox");
70
71  this->size = 1900.0;
72
73  this->skyModel = NULL;
74  this->material = new Material*[6];
75  for (int i = 0; i < 6; i++)
76    {
77      this->material[i] = new Material();
78      this->material[i]->setIllum(3);
79      this->material[i]->setDiffuse(0.0,0.0,0.0);
80      this->material[i]->setSpecular(0.0,0.0,0.0);
81      this->material[i]->setAmbient(2.0, 2.0, 2.0);
82    }
83  this->setParentMode(PNODE_MOVEMENT);
84}
85
86void SkyBox::postInit(void)
87{
88  this->rebuild();
89}
90
91
92/**
93   \brief default destructor
94*/
95SkyBox::~SkyBox()
96{
97  PRINTF(5)("Deleting SkyBox\n");
98  for (int i = 0; i < 6; i++)
99    delete this->material[i];
100  delete []this->material;
101}
102
103/**
104   \brief sets A set of textures when just giving a Name and an extension:
105
106   usage: give this function an argument like
107   setTexture("skybox", "jpg");
108   and it will convert this to
109   setTextures("skybox_top.jpg", "skybox_bottom.jpg", "skybox_left.jpg",
110               "skybox_right.jpg", "skybox_front.jpg", "skybox_back.jpg");
111*/
112void SkyBox::setTextureAndType(const char* name, const char* extension)
113{
114  char* top    = new char[strlen(name)+strlen(extension)+ 10];
115  char* bottom = new char[strlen(name)+strlen(extension)+ 10];
116  char* left   = new char[strlen(name)+strlen(extension)+ 10];
117  char* right  = new char[strlen(name)+strlen(extension)+ 10];
118  char* front  = new char[strlen(name)+strlen(extension)+ 10];
119  char* back   = new char[strlen(name)+strlen(extension)+ 10];
120
121  sprintf(top, "%s_top.%s", name, extension);
122  sprintf(bottom, "%s_bottom.%s", name, extension);
123  sprintf(left, "%s_left.%s", name, extension);
124  sprintf(right, "%s_right.%s", name, extension);
125  sprintf(front, "%s_front.%s", name, extension);
126  sprintf(back, "%s_back.%s", name, extension);
127
128  this->setTextures(top, bottom, left, right, front, back);
129
130  // deleted alocated memory of this function
131  delete []top;
132  delete []bottom;
133  delete []left;
134  delete []right;
135  delete []front;
136  delete []back;
137}
138
139/**
140   \brief Defines which textures should be loaded onto the SkyBox.
141   \param top the top texture.
142   \param bottom the bottom texture.
143   \param left the left texture.
144   \param right the right texture.
145   \param front the front texture.
146   \param back the back texture.
147*/
148void SkyBox::setTextures(const char* top, const char* bottom, const char* left, const char* right, const char* front, const char* back)
149{
150  this->material[0]->setDiffuseMap(top);
151  this->material[1]->setDiffuseMap(bottom);
152  this->material[2]->setDiffuseMap(left);
153  this->material[3]->setDiffuseMap(right);
154  this->material[4]->setDiffuseMap(front);
155  this->material[5]->setDiffuseMap(back);
156}
157
158/**
159   \param size The new size of the SkyBox
160*/
161void SkyBox::setSize(float size)
162{
163  this->size = size;
164  this->rebuild();
165}
166
167/**
168   \brief draws the SkyBox
169*/
170void SkyBox::draw()
171{
172  glPushMatrix();
173  glMatrixMode(GL_MODELVIEW);
174  Vector r = this->getAbsCoor();
175  glTranslatef(r.x, r.y, r.z);
176
177  this->skyModel->draw();
178
179  glPopMatrix();
180}
181
182
183/**
184   \brief rebuilds the SkyBox
185
186   this must be done, when changing the Size of the Skybox (runtime-efficency)
187*/
188void SkyBox::rebuild()
189{
190  if (this->skyModel)
191    delete skyModel;
192  skyModel = new Model();
193
194  this->skyModel->addVertex (-0.5*size, -0.5*size, 0.5*size);
195  this->skyModel->addVertex (0.5*size, -0.5*size, 0.5*size);
196  this->skyModel->addVertex (-0.5*size, 0.5*size, 0.5*size);
197  this->skyModel->addVertex (0.5*size, 0.5*size, 0.5*size);
198  this->skyModel->addVertex (-0.5*size, 0.5*size, -0.5*size);
199  this->skyModel->addVertex (0.5*size, 0.5*size, -0.5*size);
200  this->skyModel->addVertex (-0.5*size, -0.5*size, -0.5*size);
201  this->skyModel->addVertex (0.5*size, -0.5*size, -0.5*size);
202
203  this->skyModel->addVertexTexture (0.0, 1.0);
204  this->skyModel->addVertexTexture (1.0, 1.0);
205  this->skyModel->addVertexTexture (1.0, 0.0);
206  this->skyModel->addVertexTexture (0.0, 0.0);
207
208  this->skyModel->addVertexNormal (0.0, 0.0, 1.0);
209  this->skyModel->addVertexNormal (0.0, 1.0, 0.0);
210  this->skyModel->addVertexNormal (0.0, 0.0, -1.0);
211  this->skyModel->addVertexNormal (0.0, -1.0, 0.0);
212  this->skyModel->addVertexNormal (1.0, 0.0, 0.0);
213  this->skyModel->addVertexNormal (-1.0, 0.0, 0.0);
214
215  this->skyModel->setMaterial(material[0]);
216  this->skyModel->addFace (4, VERTEX_TEXCOORD_NORMAL, 2,0,3, 3,1,3, 5,2,3, 4,3,3); // top
217  this->skyModel->setMaterial(material[1]);
218  this->skyModel->addFace (4, VERTEX_TEXCOORD_NORMAL, 6,0,1, 7,1,1, 1,2,1, 0,3,1); // bottom
219  this->skyModel->setMaterial(material[2]);
220  this->skyModel->addFace (4, VERTEX_TEXCOORD_NORMAL, 4,2,2, 5,3,2, 7,0,2, 6,1,2); // left
221  this->skyModel->setMaterial(material[3]);
222  this->skyModel->addFace (4, VERTEX_TEXCOORD_NORMAL, 0,0,0, 1,1,0, 3,2,0, 2,3,0); // right
223  this->skyModel->setMaterial(material[4]);
224  this->skyModel->addFace (4, VERTEX_TEXCOORD_NORMAL, 1,0,5, 7,1,5, 5,2,5, 3,3,5); // front
225  this->skyModel->setMaterial(material[5]);
226  this->skyModel->addFace (4, VERTEX_TEXCOORD_NORMAL, 6,0,4, 0,1,4, 2,2,4, 4,3,4); // back
227
228  this->skyModel->finalize();
229}
Note: See TracBrowser for help on using the repository browser.