Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/2d-recalc/src/world_entities/skybox.cc @ 5535

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

orxonox/trunk: some minor cleanup, of the mess i made with AutoMake-sh

File size: 6.2 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 "load_param.h"
21#include "factory.h"
22
23using namespace std;
24
25CREATE_FACTORY(SkyBox);
26
27/**
28 * Constructs a SkyBox and takes fileName as a map.
29 * @param fileName the file to take as input for the SkyBox
30*/
31SkyBox::SkyBox(const char* fileName)
32{
33  this->preInit();
34  if (fileName)
35    this->setTextureAndType(fileName, ".jpg");
36  this->postInit();
37}
38
39/**
40 *  initializes a skybox from a XmlElement
41*/
42SkyBox::SkyBox(const TiXmlElement* root)
43{
44  this->preInit();
45
46  this->loadParams(root);
47
48  this->postInit();
49}
50
51void SkyBox::loadParams(const TiXmlElement* root)
52{
53  static_cast<WorldEntity*>(this)->loadParams(root);
54
55  LoadParam<SkyBox>(root, "Materialset", this, &SkyBox::setTexture)
56      .describe("Sets the material on the SkyBox. The string must be the path relative to the data-dir, and without a trailing .jpg");
57
58  LoadParam<SkyBox>(root, "Size", this, &SkyBox::setSize)
59      .describe("Sets the Size of the SkyBox (normally this should be 90% of the maximal viewing Distance).");
60}
61
62void SkyBox::preInit()
63{
64  this->setClassID(CL_SKYBOX, "SkyBox");
65
66  this->size = 100.0;
67
68  this->material = new Material*[6];
69  for (int i = 0; i < 6; i++)
70    {
71      this->material[i] = new Material();
72      this->material[i]->setIllum(3);
73      this->material[i]->setDiffuse(0.0,0.0,0.0);
74      this->material[i]->setSpecular(0.0,0.0,0.0);
75      this->material[i]->setAmbient(2.0, 2.0, 2.0);
76    }
77  this->setParentMode(PNODE_MOVEMENT);
78}
79
80void SkyBox::postInit()
81{
82  this->rebuild();
83}
84
85
86/**
87 *  default destructor
88*/
89SkyBox::~SkyBox()
90{
91  PRINTF(5)("Deleting SkyBox\n");
92  for (int i = 0; i < 6; i++)
93    delete this->material[i];
94  delete[] this->material;
95  delete this->model;
96  this->model = NULL; //< so that WorldEntity does not try to delete it again.
97}
98
99/**
100 *  sets A set of textures when just giving a Name and an extension:
101
102   usage: give this function an argument like
103   setTexture("skybox", "jpg");
104   and it will convert this to
105   setTextures("skybox_top.jpg", "skybox_bottom.jpg", "skybox_left.jpg",
106               "skybox_right.jpg", "skybox_front.jpg", "skybox_back.jpg");
107*/
108void SkyBox::setTextureAndType(const char* name, const char* extension)
109{
110  char* top    = new char[strlen(name)+strlen(extension)+ 10];
111  char* bottom = new char[strlen(name)+strlen(extension)+ 10];
112  char* left   = new char[strlen(name)+strlen(extension)+ 10];
113  char* right  = new char[strlen(name)+strlen(extension)+ 10];
114  char* front  = new char[strlen(name)+strlen(extension)+ 10];
115  char* back   = new char[strlen(name)+strlen(extension)+ 10];
116
117  sprintf(top, "%s_top.%s", name, extension);
118  sprintf(bottom, "%s_bottom.%s", name, extension);
119  sprintf(left, "%s_left.%s", name, extension);
120  sprintf(right, "%s_right.%s", name, extension);
121  sprintf(front, "%s_front.%s", name, extension);
122  sprintf(back, "%s_back.%s", name, extension);
123
124  this->setTextures(top, bottom, left, right, front, back);
125
126  // deleted alocated memory of this function
127  delete []top;
128  delete []bottom;
129  delete []left;
130  delete []right;
131  delete []front;
132  delete []back;
133}
134
135/**
136 *  Defines which textures should be loaded onto the SkyBox.
137 * @param top the top texture.
138 * @param bottom the bottom texture.
139 * @param left the left texture.
140 * @param right the right texture.
141 * @param front the front texture.
142 * @param back the back texture.
143*/
144void SkyBox::setTextures(const char* top, const char* bottom, const char* left, const char* right, const char* front, const char* back)
145{
146  this->material[0]->setDiffuseMap(top);
147  this->material[1]->setDiffuseMap(bottom);
148  this->material[2]->setDiffuseMap(left);
149  this->material[3]->setDiffuseMap(right);
150  this->material[4]->setDiffuseMap(front);
151  this->material[5]->setDiffuseMap(back);
152}
153
154/**
155 * @param size The new size of the SkyBox
156
157 * do not forget to rebuild the SkyBox after this.
158*/
159void SkyBox::setSize(float size)
160{
161  this->size = size;
162}
163
164/**
165 *  rebuilds the SkyBox
166
167   this must be done, when changing the Size of the Skybox (runtime-efficency)
168*/
169void SkyBox::rebuild()
170{
171  if (this->model)
172    delete this->model;
173  model = new Model();
174
175  this->model->addVertex (-0.5*size, -0.5*size, 0.5*size);
176  this->model->addVertex (0.5*size, -0.5*size, 0.5*size);
177  this->model->addVertex (-0.5*size, 0.5*size, 0.5*size);
178  this->model->addVertex (0.5*size, 0.5*size, 0.5*size);
179  this->model->addVertex (-0.5*size, 0.5*size, -0.5*size);
180  this->model->addVertex (0.5*size, 0.5*size, -0.5*size);
181  this->model->addVertex (-0.5*size, -0.5*size, -0.5*size);
182  this->model->addVertex (0.5*size, -0.5*size, -0.5*size);
183
184  this->model->addVertexTexture (0.0, 1.0);
185  this->model->addVertexTexture (1.0, 1.0);
186  this->model->addVertexTexture (1.0, 0.0);
187  this->model->addVertexTexture (0.0, 0.0);
188
189  this->model->addVertexNormal (0.0, 0.0, 1.0);
190  this->model->addVertexNormal (0.0, 1.0, 0.0);
191  this->model->addVertexNormal (0.0, 0.0, -1.0);
192  this->model->addVertexNormal (0.0, -1.0, 0.0);
193  this->model->addVertexNormal (1.0, 0.0, 0.0);
194  this->model->addVertexNormal (-1.0, 0.0, 0.0);
195
196  this->model->setMaterial(material[0]);
197  this->model->addFace (4, VERTEX_TEXCOORD_NORMAL, 2,0,3, 3,1,3, 5,2,3, 4,3,3); // top
198  this->model->setMaterial(material[1]);
199  this->model->addFace (4, VERTEX_TEXCOORD_NORMAL, 6,0,1, 7,1,1, 1,2,1, 0,3,1); // bottom
200  this->model->setMaterial(material[2]);
201  this->model->addFace (4, VERTEX_TEXCOORD_NORMAL, 4,2,2, 5,3,2, 7,0,2, 6,1,2); // left
202  this->model->setMaterial(material[3]);
203  this->model->addFace (4, VERTEX_TEXCOORD_NORMAL, 0,0,0, 1,1,0, 3,2,0, 2,3,0); // right
204  this->model->setMaterial(material[4]);
205  this->model->addFace (4, VERTEX_TEXCOORD_NORMAL, 1,0,5, 7,1,5, 5,2,5, 3,3,5); // front
206  this->model->setMaterial(material[5]);
207  this->model->addFace (4, VERTEX_TEXCOORD_NORMAL, 6,0,4, 0,1,4, 2,2,4, 4,3,4); // back
208
209  this->model->finalize();
210}
Note: See TracBrowser for help on using the repository browser.