Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 4079 was 4012, checked in by bensch, 20 years ago

orxonox/trunk: some fixes in the SkyBox, loading of skybox should now run smoothely

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