Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/branches/levelLoader/src/world_entities/skybox.cc @ 4255

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

orxonox/branches/levelLoader: description works

File size: 6.3 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
47SkyBox::SkyBox(const TiXmlElement* root) : WorldEntity(root)
48{
49  this->preInit();
50
51  this->loadParams(root);
52
53  this->postInit();
54}
55
56void SkyBox::loadParams(const TiXmlElement* root)
57{
58  LoadParam<SkyBox>(root, "Materialset", this, &SkyBox::setTexture).describe("Sets the material on the skysphere. The string must be the path relative to the data-dir, and without a trailing .jpg");
59}
60
61void SkyBox::preInit(void)
62{
63  this->setClassName("SkyBox");
64  this->skyModel = NULL;
65  this->material = new Material*[6];
66  for (int i = 0; i < 6; i++) 
67    {
68      this->material[i] = new Material();
69      this->material[i]->setIllum(3);
70      this->material[i]->setDiffuse(0.0,0.0,0.0);
71      this->material[i]->setSpecular(0.0,0.0,0.0);
72      this->material[i]->setAmbient(2.0, 2.0, 2.0);
73    }
74  this->setMode(PNODE_MOVEMENT);
75}
76
77void SkyBox::postInit(void)
78{
79  this->setSize(1900.0);
80  this->rebuild();
81}
82
83
84/**
85   \brief default destructor
86*/
87SkyBox::~SkyBox()
88{
89  PRINTF(5)("Deleting SkyBox\n");
90  for (int i = 0; i < 6; i++)
91    delete this->material[i];
92  delete []this->material;
93}
94
95/**
96   \brief sets A set of textures when just giving a Name and an extension:
97
98   usage: give this function an argument like
99   setTexture("skybox", "jpg");
100   and it will convert this to
101   setTextures("skybox_top.jpg", "skybox_bottom.jpg", "skybox_left.jpg",
102               "skybox_right.jpg", "skybox_front.jpg", "skybox_back.jpg");
103*/
104void SkyBox::setTextureAndType(const char* name, const char* extension)
105{
106  char* top    = new char[strlen(name)+strlen(extension)+ 10];
107  char* bottom = new char[strlen(name)+strlen(extension)+ 10];
108  char* left   = new char[strlen(name)+strlen(extension)+ 10];
109  char* right  = new char[strlen(name)+strlen(extension)+ 10];
110  char* front  = new char[strlen(name)+strlen(extension)+ 10];
111  char* back   = new char[strlen(name)+strlen(extension)+ 10];
112
113  sprintf(top, "%s_top.%s", name, extension);
114  sprintf(bottom, "%s_bottom.%s", name, extension);
115  sprintf(left, "%s_left.%s", name, extension);
116  sprintf(right, "%s_right.%s", name, extension);
117  sprintf(front, "%s_front.%s", name, extension);
118  sprintf(back, "%s_back.%s", name, extension);
119 
120  this->setTextures(top, bottom, left, right, front, back);
121
122  // deleted alocated memory of this function
123  delete []top;
124  delete []bottom;
125  delete []left;
126  delete []right;
127  delete []front;
128  delete []back;
129}
130
131/**
132   \brief Defines which textures should be loaded onto the SkyBox.
133   \param top the top texture.
134   \param bottom the bottom texture.
135   \param left the left texture.
136   \param right the right texture.
137   \param front the front texture.
138   \param back the back texture.
139*/
140void SkyBox::setTextures(const char* top, const char* bottom, const char* left, const char* right, const char* front, const char* back)
141{
142  this->material[0]->setDiffuseMap(top);
143  this->material[1]->setDiffuseMap(bottom);
144  this->material[2]->setDiffuseMap(left);
145  this->material[3]->setDiffuseMap(right);
146  this->material[4]->setDiffuseMap(front);
147  this->material[5]->setDiffuseMap(back);
148}
149
150/**
151   \param size The new size of the SkyBox
152*/
153void SkyBox::setSize(float size)
154{
155  this->size = size;
156}
157
158/**
159   \brief draws the SkyBox
160*/
161void SkyBox::draw()
162{
163  glPushMatrix();
164  glMatrixMode(GL_MODELVIEW);
165  Vector r = this->getAbsCoor();
166  glTranslatef(r.x, r.y, r.z);
167
168  this->skyModel->draw();
169
170  glPopMatrix();
171}
172
173
174/**
175   \brief rebuilds the SkyBox
176   
177   this must be done, when changing the Size of the Skybox (runtime-efficency)
178*/
179void SkyBox::rebuild()
180{
181  if (this->skyModel)
182    delete skyModel;
183  skyModel = new Model();
184
185  this->skyModel->addVertex (-0.5*size, -0.5*size, 0.5*size); 
186  this->skyModel->addVertex (0.5*size, -0.5*size, 0.5*size);
187  this->skyModel->addVertex (-0.5*size, 0.5*size, 0.5*size);
188  this->skyModel->addVertex (0.5*size, 0.5*size, 0.5*size);
189  this->skyModel->addVertex (-0.5*size, 0.5*size, -0.5*size);
190  this->skyModel->addVertex (0.5*size, 0.5*size, -0.5*size);
191  this->skyModel->addVertex (-0.5*size, -0.5*size, -0.5*size);
192  this->skyModel->addVertex (0.5*size, -0.5*size, -0.5*size);
193
194  this->skyModel->addVertexTexture (0.0, 0.0);
195  this->skyModel->addVertexTexture (1.0, 0.0);
196  this->skyModel->addVertexTexture (1.0, 1.0);
197  this->skyModel->addVertexTexture (0.0, 1.0);
198
199  this->skyModel->addVertexNormal (0.0, 0.0, 1.0);
200  this->skyModel->addVertexNormal (0.0, 1.0, 0.0);
201  this->skyModel->addVertexNormal (0.0, 0.0, -1.0);
202  this->skyModel->addVertexNormal (0.0, -1.0, 0.0);
203  this->skyModel->addVertexNormal (1.0, 0.0, 0.0);
204  this->skyModel->addVertexNormal (-1.0, 0.0, 0.0);
205
206  this->skyModel->setMaterial(material[0]);
207  this->skyModel->addFace (4, VERTEX_TEXCOORD_NORMAL, 2,0,3, 3,1,3, 5,2,3, 4,3,3); // top
208  this->skyModel->setMaterial(material[1]);
209  this->skyModel->addFace (4, VERTEX_TEXCOORD_NORMAL, 6,0,1, 7,1,1, 1,2,1, 0,3,1); // bottom
210  this->skyModel->setMaterial(material[2]);
211  this->skyModel->addFace (4, VERTEX_TEXCOORD_NORMAL, 4,2,2, 5,3,2, 7,0,2, 6,1,2); // left
212  this->skyModel->setMaterial(material[3]);
213  this->skyModel->addFace (4, VERTEX_TEXCOORD_NORMAL, 0,0,0, 1,1,0, 3,2,0, 2,3,0); // right
214  this->skyModel->setMaterial(material[4]);
215  this->skyModel->addFace (4, VERTEX_TEXCOORD_NORMAL, 1,0,5, 7,1,5, 5,2,5, 3,3,5); // front
216  this->skyModel->setMaterial(material[5]);
217  this->skyModel->addFace (4, VERTEX_TEXCOORD_NORMAL, 6,0,4, 0,1,4, 2,2,4, 4,3,4); // back
218 
219  this->skyModel->finalize();
220}
Note: See TracBrowser for help on using the repository browser.