Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 6325 was 6307, checked in by bensch, 19 years ago

orxonox/trunk: PNode fix: PNODE_MOVEMENT didn't work, because the mask was bork

File size: 6.0 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"
[3608]24
[5356]25using namespace std;
[5357]26
[5750]27CREATE_FACTORY(SkyBox, CL_SKYBOX);
[3608]28
[3416]29/**
[5357]30 * Constructs a SkyBox and takes fileName as a map.
[4836]31 * @param fileName the file to take as input for the SkyBox
[3419]32*/
[4261]33SkyBox::SkyBox(const char* fileName)
[3419]34{
[4012]35  this->preInit();
[4261]36  if (fileName)
37    this->setTextureAndType(fileName, ".jpg");
[4012]38  this->postInit();
[4010]39}
40
[4444]41/**
[4836]42 *  initializes a skybox from a XmlElement
[4444]43*/
[4436]44SkyBox::SkyBox(const TiXmlElement* root)
[4010]45{
[4012]46  this->preInit();
[4010]47
[4261]48  this->loadParams(root);
[4010]49
[4012]50  this->postInit();
[4010]51}
52
[4261]53void SkyBox::loadParams(const TiXmlElement* root)
54{
[4436]55  static_cast<WorldEntity*>(this)->loadParams(root);
56
[5671]57  LoadParam(root, "Materialset", this, SkyBox, setTexture)
[4621]58      .describe("Sets the material on the SkyBox. The string must be the path relative to the data-dir, and without a trailing .jpg");
59
[5671]60  LoadParam(root, "Size", this, SkyBox, setSize)
[4621]61      .describe("Sets the Size of the SkyBox (normally this should be 90% of the maximal viewing Distance).");
[4261]62}
63
[4746]64void SkyBox::preInit()
[4010]65{
[4320]66  this->setClassID(CL_SKYBOX, "SkyBox");
[6142]67  this->toList(OM_ENVIRON_NOTICK);
[4621]68  this->size = 100.0;
[4620]69
[4597]70  for (int i = 0; i < 6; i++)
[3801]71    {
72      this->material[i] = new Material();
73      this->material[i]->setIllum(3);
[3805]74      this->material[i]->setDiffuse(0.0,0.0,0.0);
75      this->material[i]->setSpecular(0.0,0.0,0.0);
76      this->material[i]->setAmbient(2.0, 2.0, 2.0);
[3801]77    }
[4444]78  this->setParentMode(PNODE_MOVEMENT);
[4012]79}
[3803]80
[4746]81void SkyBox::postInit()
[4012]82{
83  this->rebuild();
[3411]84}
85
[3507]86
[3416]87/**
[4836]88 *  default destructor
[3416]89*/
[3796]90SkyBox::~SkyBox()
[3411]91{
[4136]92  PRINTF(5)("Deleting SkyBox\n");
[5994]93  this->setModel(NULL); //< so that WorldEntity does not try to delete it again.
[3801]94  for (int i = 0; i < 6; i++)
95    delete this->material[i];
[6307]96}
[3411]97
[3803]98/**
[4836]99 *  sets A set of textures when just giving a Name and an extension:
[3763]100
[3803]101   usage: give this function an argument like
102   setTexture("skybox", "jpg");
[4597]103   and it will convert this to
[3803]104   setTextures("skybox_top.jpg", "skybox_bottom.jpg", "skybox_left.jpg",
105               "skybox_right.jpg", "skybox_front.jpg", "skybox_back.jpg");
106*/
[4261]107void SkyBox::setTextureAndType(const char* name, const char* extension)
[3803]108{
[4012]109  char* top    = new char[strlen(name)+strlen(extension)+ 10];
110  char* bottom = new char[strlen(name)+strlen(extension)+ 10];
111  char* left   = new char[strlen(name)+strlen(extension)+ 10];
112  char* right  = new char[strlen(name)+strlen(extension)+ 10];
113  char* front  = new char[strlen(name)+strlen(extension)+ 10];
114  char* back   = new char[strlen(name)+strlen(extension)+ 10];
[3803]115
116  sprintf(top, "%s_top.%s", name, extension);
117  sprintf(bottom, "%s_bottom.%s", name, extension);
118  sprintf(left, "%s_left.%s", name, extension);
119  sprintf(right, "%s_right.%s", name, extension);
120  sprintf(front, "%s_front.%s", name, extension);
121  sprintf(back, "%s_back.%s", name, extension);
[4597]122
[3803]123  this->setTextures(top, bottom, left, right, front, back);
124
[4012]125  // deleted alocated memory of this function
[3803]126  delete []top;
127  delete []bottom;
128  delete []left;
129  delete []right;
130  delete []front;
131  delete []back;
132}
133
134/**
[4836]135 *  Defines which textures should be loaded onto the SkyBox.
136 * @param top the top texture.
137 * @param bottom the bottom texture.
138 * @param left the left texture.
139 * @param right the right texture.
140 * @param front the front texture.
141 * @param back the back texture.
[3803]142*/
143void SkyBox::setTextures(const char* top, const char* bottom, const char* left, const char* right, const char* front, const char* back)
144{
145  this->material[0]->setDiffuseMap(top);
146  this->material[1]->setDiffuseMap(bottom);
147  this->material[2]->setDiffuseMap(left);
148  this->material[3]->setDiffuseMap(right);
149  this->material[4]->setDiffuseMap(front);
150  this->material[5]->setDiffuseMap(back);
151}
152
153/**
[4836]154 * @param size The new size of the SkyBox
[4621]155
156 * do not forget to rebuild the SkyBox after this.
[3803]157*/
158void SkyBox::setSize(float size)
159{
160  this->size = size;
161}
162
163/**
[4836]164 *  rebuilds the SkyBox
[4597]165
[3803]166   this must be done, when changing the Size of the Skybox (runtime-efficency)
167*/
[3801]168void SkyBox::rebuild()
169{
[6022]170  StaticModel* model = new StaticModel();
[3801]171
[5994]172  model->addVertex (-0.5*size, -0.5*size, 0.5*size);
173  model->addVertex (0.5*size, -0.5*size, 0.5*size);
174  model->addVertex (-0.5*size, 0.5*size, 0.5*size);
175  model->addVertex (0.5*size, 0.5*size, 0.5*size);
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);
[3801]180
[5994]181  model->addVertexTexture (0.0, 1.0);
182  model->addVertexTexture (1.0, 1.0);
183  model->addVertexTexture (1.0, 0.0);
184  model->addVertexTexture (0.0, 0.0);
[3801]185
[5994]186  model->addVertexNormal (0.0, 0.0, 1.0);
187  model->addVertexNormal (0.0, 1.0, 0.0);
188  model->addVertexNormal (0.0, 0.0, -1.0);
189  model->addVertexNormal (0.0, -1.0, 0.0);
190  model->addVertexNormal (1.0, 0.0, 0.0);
191  model->addVertexNormal (-1.0, 0.0, 0.0);
[3801]192
[5994]193  model->setMaterial(material[0]);
194  model->addFace (4, VERTEX_TEXCOORD_NORMAL, 2,0,3, 3,1,3, 5,2,3, 4,3,3); // top
195  model->setMaterial(material[1]);
196  model->addFace (4, VERTEX_TEXCOORD_NORMAL, 6,0,1, 7,1,1, 1,2,1, 0,3,1); // bottom
197  model->setMaterial(material[2]);
198  model->addFace (4, VERTEX_TEXCOORD_NORMAL, 4,2,2, 5,3,2, 7,0,2, 6,1,2); // left
199  model->setMaterial(material[3]);
200  model->addFace (4, VERTEX_TEXCOORD_NORMAL, 0,0,0, 1,1,0, 3,2,0, 2,3,0); // right
201  model->setMaterial(material[4]);
202  model->addFace (4, VERTEX_TEXCOORD_NORMAL, 1,0,5, 7,1,5, 5,2,5, 3,3,5); // front
203  model->setMaterial(material[5]);
204  model->addFace (4, VERTEX_TEXCOORD_NORMAL, 6,0,4, 0,1,4, 2,2,4, 4,3,4); // back
[4597]205
[5994]206  model->finalize();
207
208  this->setModel(model);
[3801]209}
Note: See TracBrowser for help on using the repository browser.