Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/branches/levelLoader: some minor changes

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