Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 6326 was 6326, checked in by rennerc, 18 years ago
File size: 7.2 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"
[6273]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
[3801]72  this->material = new Material*[6];
[4597]73  for (int i = 0; i < 6; i++)
[3801]74    {
75      this->material[i] = new Material();
76      this->material[i]->setIllum(3);
[3805]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);
[3801]80    }
[4444]81  this->setParentMode(PNODE_MOVEMENT);
[4012]82}
[3803]83
[4746]84void SkyBox::postInit()
[4012]85{
86  this->rebuild();
[3411]87}
88
[3507]89
[3416]90/**
[4836]91 *  default destructor
[3416]92*/
[3796]93SkyBox::~SkyBox()
[3411]94{
[4136]95  PRINTF(5)("Deleting SkyBox\n");
[5994]96  this->setModel(NULL); //< so that WorldEntity does not try to delete it again.
[3801]97  for (int i = 0; i < 6; i++)
98    delete this->material[i];
[5308]99  delete[] this->material;
[5994]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}
[6273]214
215void SkyBox::writeBytes( const byte * data, int length, int sender )
216{
217  setRequestedSync( false );
218  setIsOutOfSync( false );
219
[6275]220  SYNCHELP_READ_BEGIN();
[6326]221  SYNCHELP_READ_FLOAT( size );
[6275]222  SYNCHELP_READ_STRING( textureName, 1024 );
[6273]223
[6326]224  PRINT(0)("GOT DATA: size=%f texture='%s'\n", size, textureName);
[6275]225
[6273]226  this->setSize( size );
[6326]227  this->setTextureAndType( textureName, "jpg" );
[6273]228  this->rebuild();
229}
230
[6275]231
232
[6273]233int SkyBox::readBytes( byte * data, int maxLength, int * reciever )
234{
235  if ( isOutOfSync() && !requestedSync() && this->getHostID()!=this->getOwner() )
236  {
237    (NetworkGameManager::getInstance())->sync( this->getUniqueID(), this->getOwner() );
238    setRequestedSync( true );
239  }
240
241  int rec = this->getRequestSync();
242  if ( rec > 0 )
243  {
[6326]244    PRINT(0)("SEND DATA: size=%f texture='%s'\n", size, textureName);
[6273]245    *reciever = rec;
246
[6275]247    SYNCHELP_WRITE_BEGIN();
248    SYNCHELP_WRITE_FLOAT(this->size);
249    SYNCHELP_WRITE_STRING(this->textureName);
[6273]250
[6275]251    return SYNCHELP_WRITE_N;
[6273]252  }
253
254  *reciever = 0;
255  return 0;
256}
257
258void SkyBox::writeDebug( ) const
259{
260}
261
262void SkyBox::readDebug( ) const
263{
264}
Note: See TracBrowser for help on using the repository browser.