Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: merged the network branche back to the trunk, so we do not get away from each other to fast

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