Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/branches/levelLoader: SkyBox loadable in the new Style

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: David Gruetter
14   co-programmer: Benjamin Grauer
15   
16   Created by Dave: this file is actually quite similar to player.cc and so is
17   skybox.h similar to player.h
18   With that said, things should be clear:)
19   
20   Edited:
21   Bensch: more constructors, changeability, comments...
22   Patrick: giving it the common orxonox style, not much to do... good work Dave!
23
24*/
25
26#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_WORLD_ENTITY
27
28
29#include "skybox.h"
30
31#include "stdincl.h"
32#include "factory.h"
33
34#include "material.h"
35#include "vector.h"
36#include "resource_manager.h"
37#include "model.h"
38//#include "world_entity.h"
39
40CREATE_FACTORY(SkyBox);
41
42using namespace std;
43
44/**
45   \brief Constructs a SkyBox and takes fileName as a map.
46   \param fileName the file to take as input for the SkyBox
47*/
48SkyBox::SkyBox(char* fileName)
49{
50  this->preInit();
51  this->postInit();
52}
53
54SkyBox::SkyBox(TiXmlElement* root) : WorldEntity(root)
55{
56  this->preInit();
57
58  const char* string;
59
60  // Model Loading     
61  LoadParam<SkyBox>(root, "Materialset", this, &SkyBox::setTexture);
62
63  this->postInit();
64}
65
66void SkyBox::preInit(void)
67{
68  this->setClassName("SkyBox");
69  this->skyModel = NULL;
70  this->material = new Material*[6];
71  for (int i = 0; i < 6; i++) 
72    {
73      this->material[i] = new Material();
74      this->material[i]->setIllum(3);
75      this->material[i]->setDiffuse(0.0,0.0,0.0);
76      this->material[i]->setSpecular(0.0,0.0,0.0);
77      this->material[i]->setAmbient(2.0, 2.0, 2.0);
78    }
79  this->setMode(PNODE_MOVEMENT);
80}
81
82void SkyBox::postInit(void)
83{
84  this->setSize(1900.0);
85  this->rebuild();
86}
87
88
89/**
90   \brief default destructor
91*/
92SkyBox::~SkyBox()
93{
94  PRINTF(5)("Deleting SkyBox\n");
95  for (int i = 0; i < 6; i++)
96    delete this->material[i];
97  delete []this->material;
98}
99
100/**
101   \brief sets A set of textures when just giving a Name and an extension:
102
103   usage: give this function an argument like
104   setTexture("skybox", "jpg");
105   and it will convert this to
106   setTextures("skybox_top.jpg", "skybox_bottom.jpg", "skybox_left.jpg",
107               "skybox_right.jpg", "skybox_front.jpg", "skybox_back.jpg");
108*/
109void SkyBox::setTexture(const char* name, const char* extension)
110{
111  char* top    = new char[strlen(name)+strlen(extension)+ 10];
112  char* bottom = new char[strlen(name)+strlen(extension)+ 10];
113  char* left   = new char[strlen(name)+strlen(extension)+ 10];
114  char* right  = new char[strlen(name)+strlen(extension)+ 10];
115  char* front  = new char[strlen(name)+strlen(extension)+ 10];
116  char* back   = new char[strlen(name)+strlen(extension)+ 10];
117
118  sprintf(top, "%s_top.%s", name, extension);
119  sprintf(bottom, "%s_bottom.%s", name, extension);
120  sprintf(left, "%s_left.%s", name, extension);
121  sprintf(right, "%s_right.%s", name, extension);
122  sprintf(front, "%s_front.%s", name, extension);
123  sprintf(back, "%s_back.%s", name, extension);
124 
125  this->setTextures(top, bottom, left, right, front, back);
126
127  // deleted alocated memory of this function
128  delete []top;
129  delete []bottom;
130  delete []left;
131  delete []right;
132  delete []front;
133  delete []back;
134}
135
136/**
137   \brief Defines which textures should be loaded onto the SkyBox.
138   \param top the top texture.
139   \param bottom the bottom texture.
140   \param left the left texture.
141   \param right the right texture.
142   \param front the front texture.
143   \param back the back texture.
144*/
145void SkyBox::setTextures(const char* top, const char* bottom, const char* left, const char* right, const char* front, const char* back)
146{
147  this->material[0]->setDiffuseMap(top);
148  this->material[1]->setDiffuseMap(bottom);
149  this->material[2]->setDiffuseMap(left);
150  this->material[3]->setDiffuseMap(right);
151  this->material[4]->setDiffuseMap(front);
152  this->material[5]->setDiffuseMap(back);
153}
154
155/**
156   \param size The new size of the SkyBox
157*/
158void SkyBox::setSize(float size)
159{
160  this->size = size;
161}
162
163/**
164   \brief draws the SkyBox
165*/
166void SkyBox::draw()
167{
168  glPushMatrix();
169  glMatrixMode(GL_MODELVIEW);
170  Vector r = this->getAbsCoor();
171  glTranslatef(r.x, r.y, r.z);
172
173  this->skyModel->draw();
174
175  glPopMatrix();
176}
177
178
179/**
180   \brief rebuilds the SkyBox
181   
182   this must be done, when changing the Size of the Skybox (runtime-efficency)
183*/
184void SkyBox::rebuild()
185{
186  if (this->skyModel)
187    delete skyModel;
188  skyModel = new Model();
189
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  this->skyModel->addVertex (0.5*size, 0.5*size, 0.5*size);
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
199  this->skyModel->addVertexTexture (0.0, 0.0);
200  this->skyModel->addVertexTexture (1.0, 0.0);
201  this->skyModel->addVertexTexture (1.0, 1.0);
202  this->skyModel->addVertexTexture (0.0, 1.0);
203
204  this->skyModel->addVertexNormal (0.0, 0.0, 1.0);
205  this->skyModel->addVertexNormal (0.0, 1.0, 0.0);
206  this->skyModel->addVertexNormal (0.0, 0.0, -1.0);
207  this->skyModel->addVertexNormal (0.0, -1.0, 0.0);
208  this->skyModel->addVertexNormal (1.0, 0.0, 0.0);
209  this->skyModel->addVertexNormal (-1.0, 0.0, 0.0);
210
211  this->skyModel->setMaterial(material[0]);
212  this->skyModel->addFace (4, VERTEX_TEXCOORD_NORMAL, 2,1,3, 3,2,3, 5,3,3, 4,0,3); // top
213  this->skyModel->setMaterial(material[1]);
214  this->skyModel->addFace (4, VERTEX_TEXCOORD_NORMAL, 6,3,1, 7,0,1, 1,1,1, 0,2,1); // bottom
215  this->skyModel->setMaterial(material[2]);
216  this->skyModel->addFace (4, VERTEX_TEXCOORD_NORMAL, 0,0,2, 1,1,2, 3,2,2, 2,3,2); // left
217  this->skyModel->setMaterial(material[3]);
218  this->skyModel->addFace (4, VERTEX_TEXCOORD_NORMAL, 4,2,0, 5,3,0, 7,0,0, 6,1,0); // right
219  this->skyModel->setMaterial(material[4]);
220  this->skyModel->addFace (4, VERTEX_TEXCOORD_NORMAL, 1,0,5, 7,1,5, 5,2,5, 3,3,6); // front
221  this->skyModel->setMaterial(material[5]);
222  this->skyModel->addFace (4, VERTEX_TEXCOORD_NORMAL, 6,0,4, 0,1,4, 2,2,4, 4,3,4); // back
223 
224  this->skyModel->finalize();
225}
Note: See TracBrowser for help on using the repository browser.