Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/network/src/world_entities/skybox.cc @ 6328

Last change on this file since 6328 was 6328, checked in by rennerc, 18 years ago

fixed some bugs

File size: 7.3 KB
Line 
1/*
2   orxonox - the future of 3D-vertical-scrollers
3
4   Copyright (C) 2004 orx
5
6   This program is free software; you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation; either version 2, or (at your option)
9   any later version.
10
11   ### File Specific:
12   main-programmer: Benjamin Grauer
13   co-programmer: ...
14*/
15
16#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_WORLD_ENTITY
17
18#include "skybox.h"
19
20#include "load_param.h"
21#include "factory.h"
22#include "static_model.h"
23#include "material.h"
24#include "network_game_manager.h"
25#include "converter.h"
26
27using namespace std;
28
29CREATE_FACTORY(SkyBox, CL_SKYBOX);
30
31/**
32 * Constructs a SkyBox and takes fileName as a map.
33 * @param fileName the file to take as input for the SkyBox
34*/
35SkyBox::SkyBox(const char* fileName)
36{
37  this->preInit();
38  if (fileName)
39    this->setTextureAndType(fileName, ".jpg");
40  this->postInit();
41}
42
43/**
44 *  initializes a skybox from a XmlElement
45*/
46SkyBox::SkyBox(const TiXmlElement* root)
47{
48  this->preInit();
49
50  this->loadParams(root);
51
52  this->postInit();
53}
54
55void SkyBox::loadParams(const TiXmlElement* root)
56{
57  static_cast<WorldEntity*>(this)->loadParams(root);
58
59  LoadParam(root, "Materialset", this, SkyBox, setTexture)
60      .describe("Sets the material on the SkyBox. The string must be the path relative to the data-dir, and without a trailing .jpg");
61
62  LoadParam(root, "Size", this, SkyBox, setSize)
63      .describe("Sets the Size of the SkyBox (normally this should be 90% of the maximal viewing Distance).");
64}
65
66void SkyBox::preInit()
67{
68  this->setClassID(CL_SKYBOX, "SkyBox");
69  this->toList(OM_ENVIRON_NOTICK);
70  this->size = 100.0;
71
72  this->material = new Material*[6];
73  for (int i = 0; i < 6; i++)
74    {
75      this->material[i] = new Material();
76      this->material[i]->setIllum(3);
77      this->material[i]->setDiffuse(0.0,0.0,0.0);
78      this->material[i]->setSpecular(0.0,0.0,0.0);
79      this->material[i]->setAmbient(2.0, 2.0, 2.0);
80    }
81  this->setParentMode(PNODE_MOVEMENT);
82
83  this->textureName = NULL;
84}
85
86void SkyBox::postInit()
87{
88  this->rebuild();
89}
90
91
92/**
93 *  default destructor
94*/
95SkyBox::~SkyBox()
96{
97  PRINTF(5)("Deleting SkyBox\n");
98  this->setModel(NULL); //< so that WorldEntity does not try to delete it again.
99  for (int i = 0; i < 6; i++)
100    delete this->material[i];
101  delete[] this->material;
102  if (this->textureName)
103  {
104    delete[] this->textureName;
105    this->textureName = NULL;
106  }
107 }
108
109/**
110 *  sets A set of textures when just giving a Name and an extension:
111
112   usage: give this function an argument like
113   setTexture("skybox", "jpg");
114   and it will convert this to
115   setTextures("skybox_top.jpg", "skybox_bottom.jpg", "skybox_left.jpg",
116               "skybox_right.jpg", "skybox_front.jpg", "skybox_back.jpg");
117*/
118void SkyBox::setTextureAndType(const char* name, const char* extension)
119{
120  char* top    = new char[strlen(name)+strlen(extension)+ 10];
121  char* bottom = new char[strlen(name)+strlen(extension)+ 10];
122  char* left   = new char[strlen(name)+strlen(extension)+ 10];
123  char* right  = new char[strlen(name)+strlen(extension)+ 10];
124  char* front  = new char[strlen(name)+strlen(extension)+ 10];
125  char* back   = new char[strlen(name)+strlen(extension)+ 10];
126
127  sprintf(top, "%s_top.%s", name, extension);
128  sprintf(bottom, "%s_bottom.%s", name, extension);
129  sprintf(left, "%s_left.%s", name, extension);
130  sprintf(right, "%s_right.%s", name, extension);
131  sprintf(front, "%s_front.%s", name, extension);
132  sprintf(back, "%s_back.%s", name, extension);
133
134  this->setTextures(top, bottom, left, right, front, back);
135
136  // deleted alocated memory of this function
137  delete []top;
138  delete []bottom;
139  delete []left;
140  delete []right;
141  delete []front;
142  delete []back;
143}
144
145/**
146 *  Defines which textures should be loaded onto the SkyBox.
147 * @param top the top texture.
148 * @param bottom the bottom texture.
149 * @param left the left texture.
150 * @param right the right texture.
151 * @param front the front texture.
152 * @param back the back texture.
153*/
154void SkyBox::setTextures(const char* top, const char* bottom, const char* left, const char* right, const char* front, const char* back)
155{
156  this->material[0]->setDiffuseMap(top);
157  this->material[1]->setDiffuseMap(bottom);
158  this->material[2]->setDiffuseMap(left);
159  this->material[3]->setDiffuseMap(right);
160  this->material[4]->setDiffuseMap(front);
161  this->material[5]->setDiffuseMap(back);
162}
163
164/**
165 * @param size The new size of the SkyBox
166
167 * do not forget to rebuild the SkyBox after this.
168*/
169void SkyBox::setSize(float size)
170{
171  this->size = size;
172}
173
174/**
175 *  rebuilds the SkyBox
176
177   this must be done, when changing the Size of the Skybox (runtime-efficency)
178*/
179void SkyBox::rebuild()
180{
181  StaticModel* model = new StaticModel();
182
183  model->addVertex (-0.5*size, -0.5*size, 0.5*size);
184  model->addVertex (0.5*size, -0.5*size, 0.5*size);
185  model->addVertex (-0.5*size, 0.5*size, 0.5*size);
186  model->addVertex (0.5*size, 0.5*size, 0.5*size);
187  model->addVertex (-0.5*size, 0.5*size, -0.5*size);
188  model->addVertex (0.5*size, 0.5*size, -0.5*size);
189  model->addVertex (-0.5*size, -0.5*size, -0.5*size);
190  model->addVertex (0.5*size, -0.5*size, -0.5*size);
191
192  model->addVertexTexture (0.0, 1.0);
193  model->addVertexTexture (1.0, 1.0);
194  model->addVertexTexture (1.0, 0.0);
195  model->addVertexTexture (0.0, 0.0);
196
197  model->addVertexNormal (0.0, 0.0, 1.0);
198  model->addVertexNormal (0.0, 1.0, 0.0);
199  model->addVertexNormal (0.0, 0.0, -1.0);
200  model->addVertexNormal (0.0, -1.0, 0.0);
201  model->addVertexNormal (1.0, 0.0, 0.0);
202  model->addVertexNormal (-1.0, 0.0, 0.0);
203
204  model->setMaterial(material[0]);
205  model->addFace (4, VERTEX_TEXCOORD_NORMAL, 2,0,3, 3,1,3, 5,2,3, 4,3,3); // top
206  model->setMaterial(material[1]);
207  model->addFace (4, VERTEX_TEXCOORD_NORMAL, 6,0,1, 7,1,1, 1,2,1, 0,3,1); // bottom
208  model->setMaterial(material[2]);
209  model->addFace (4, VERTEX_TEXCOORD_NORMAL, 4,2,2, 5,3,2, 7,0,2, 6,1,2); // left
210  model->setMaterial(material[3]);
211  model->addFace (4, VERTEX_TEXCOORD_NORMAL, 0,0,0, 1,1,0, 3,2,0, 2,3,0); // right
212  model->setMaterial(material[4]);
213  model->addFace (4, VERTEX_TEXCOORD_NORMAL, 1,0,5, 7,1,5, 5,2,5, 3,3,5); // front
214  model->setMaterial(material[5]);
215  model->addFace (4, VERTEX_TEXCOORD_NORMAL, 6,0,4, 0,1,4, 2,2,4, 4,3,4); // back
216
217  model->finalize();
218
219  this->setModel(model);
220}
221
222void SkyBox::writeBytes( const byte * data, int length, int sender )
223{
224  setRequestedSync( false );
225  setIsOutOfSync( false );
226
227  SYNCHELP_READ_BEGIN();
228  SYNCHELP_READ_FLOAT( size );
229  if ( textureName )
230  {
231    delete[] textureName;
232    textureName = NULL;
233  }
234  SYNCHELP_READ_STRINGM( textureName );
235
236  PRINT(0)("GOT DATA: size=%f texture='%s'\n", size, textureName);
237
238  this->setSize( size );
239  this->setTextureAndType( textureName, "jpg" );
240  this->rebuild();
241}
242
243
244
245int SkyBox::readBytes( byte * data, int maxLength, int * reciever )
246{
247  if ( isOutOfSync() && !requestedSync() && this->getHostID()!=this->getOwner() )
248  {
249    (NetworkGameManager::getInstance())->sync( this->getUniqueID(), this->getOwner() );
250    setRequestedSync( true );
251  }
252
253  int rec = this->getRequestSync();
254  if ( rec > 0 )
255  {
256    PRINT(0)("SEND DATA: size=%f texture='%s'\n", size, textureName);
257    *reciever = rec;
258
259    SYNCHELP_WRITE_BEGIN();
260    SYNCHELP_WRITE_FLOAT(this->size);
261    SYNCHELP_WRITE_STRING(this->textureName);
262
263    return SYNCHELP_WRITE_N;
264  }
265
266  *reciever = 0;
267  return 0;
268}
269
270void SkyBox::writeDebug( ) const
271{
272}
273
274void SkyBox::readDebug( ) const
275{
276}
Note: See TracBrowser for help on using the repository browser.