Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/branches/levelLoader: better compatibility with more functions

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