Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/trunk/src/world_entities/skybox.cc @ 4011

Last change on this file since 4011 was 4010, checked in by bensch, 20 years ago

orxonox/trunk: merged the levelloader from lltrunktemp to the trunk. Big thanks to fuzzy to make this so easy for us, and for implementing it in the first place.

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