Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 6455 in orxonox.OLD for trunk/src/world_entities


Ignore:
Timestamp:
Jan 9, 2006, 11:53:53 PM (19 years ago)
Author:
bensch
Message:

trunk: Simple water surface WE

Location:
trunk/src/world_entities
Files:
1 added
1 edited
2 copied

Legend:

Unmodified
Added
Removed
  • trunk/src/world_entities/Makefile.am

    r6434 r6455  
    5050                  world_entities/creatures/md2_creature.cc \
    5151                  \
    52                   world_entities/spawning_point.cc
     52                  world_entities/spawning_point.cc \
     53                  \
     54                  world_entities/environments/water.cc
    5355
    5456WorldEntities_HEADERS_ = \
     
    9698                 world_entities/creatures/md2_creature.h \
    9799                 \
    98                  world_entities/spawning_point.h
     100                 world_entities/spawning_point.h \
     101                 \
     102                 world_entities/environments/water.h
  • trunk/src/world_entities/environments/water.cc

    r6451 r6455  
    1616#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_WORLD_ENTITY
    1717
    18 #include "skybox.h"
     18#include "water.h"
     19#include "factory.h"
     20#include "load_param.h"
    1921
    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"
     22#include "grid.h"
    2623
    2724using namespace std;
    2825
    29 CREATE_FACTORY(SkyBox, CL_SKYBOX);
     26CREATE_FACTORY(Water, CL_WATER);
    3027
    31 /**
    32  * Constructs a SkyBox and takes fileName as a map.
    33  * @param fileName the file to take as input for the SkyBox
    34 */
    35 SkyBox::SkyBox(const char* fileName)
     28
     29Water::Water(const TiXmlElement* root)
    3630{
    37   this->preInit();
    38   if (fileName)
    39     this->setTextureAndType(fileName, ".jpg");
    40   this->postInit();
     31  this->resX = this->resY = 10;
     32  this->sizeX = this->sizeY = 1.0;
     33  this->grid = NULL;
     34
     35  if (root != NULL)
     36    this->loadParams(root);
    4137}
    4238
    43 /**
    44  *  initializes a skybox from a XmlElement
    45 */
    46 SkyBox::SkyBox(const TiXmlElement* root)
     39Water::~Water()
    4740{
    48   this->preInit();
    4941
    50   this->loadParams(root);
    51 
    52   this->postInit();
    5342}
    5443
    55 void SkyBox::loadParams(const TiXmlElement* root)
     44void Water::loadParams(const TiXmlElement* root)
    5645{
    57   static_cast<WorldEntity*>(this)->loadParams(root);
     46  WorldEntity::loadParams(root);
    5847
    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");
     48  LoadParam(root, "size", this, Water, setSize)
     49      .describe("the size of the WaterSurface")
     50      .defaultValues(2, 1.0f, 1.0f);
    6151
    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).");
     52  LoadParam(root, "resolution", this, Water, setResolution)
     53      .describe("sets the resolution of the water surface")
     54      .defaultValues(2, 10, 10);
    6455}
    6556
    66 void SkyBox::preInit()
     57void Water::rebuildGrid()
    6758{
    68   this->setClassID(CL_SKYBOX, "SkyBox");
    69   this->toList(OM_ENVIRON_NOTICK);
    70   this->size = 100.0;
     59  if (this->grid != NULL)
     60    delete this->grid;
     61  this->grid = new Grid(this->sizeX, this->sizeY, this->resX, this->resY);
    7162
    72   for (int i = 0; i < 6; i++)
    73     {
    74       this->material[i] = new Material();
    75       this->material[i]->setIllum(3);
    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);
    79     }
    80   this->setParentMode(PNODE_MOVEMENT);
    81 
    82   this->textureName = NULL;
    8363}
    8464
    85 void SkyBox::postInit()
     65void Water::setResolution(unsigned int resX, unsigned int resY)
    8666{
    87   this->rebuild();
     67  this->resX = resX;
     68  this->resY = resY;
    8869}
    8970
    90 
    91 /**
    92  *  default destructor
    93 */
    94 SkyBox::~SkyBox()
     71void Water::setSize(float sizeX, float sizeY)
    9572{
    96   PRINTF(5)("Deleting SkyBox\n");
    97   this->setModel(NULL); //< so that WorldEntity does not try to delete it again.
    98   for (int i = 0; i < 6; i++)
    99     delete this->material[i];
     73  this->sizeX = sizeX;
     74  this->sizeY = sizeY;
    10075}
    10176
    102 /**
    103  *  sets A set of textures when just giving a Name and an extension:
    104 
    105    usage: give this function an argument like
    106    setTexture("skybox", "jpg");
    107    and it will convert this to
    108    setTextures("skybox_top.jpg", "skybox_bottom.jpg", "skybox_left.jpg",
    109                "skybox_right.jpg", "skybox_front.jpg", "skybox_back.jpg");
    110 */
    111 void SkyBox::setTextureAndType(const char* name, const char* extension)
    112 {
    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];
    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);
    126 
    127   this->setTextures(top, bottom, left, right, front, back);
    128 
    129   // deleted alocated memory of this function
    130   delete []top;
    131   delete []bottom;
    132   delete []left;
    133   delete []right;
    134   delete []front;
    135   delete []back;
    136 }
    137 
    138 /**
    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.
    146 */
    147 void 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 /**
    158  * @param size The new size of the SkyBox
    159 
    160  * do not forget to rebuild the SkyBox after this.
    161 */
    162 void SkyBox::setSize(float size)
    163 {
    164   this->size = size;
    165 }
    166 
    167 /**
    168  *  rebuilds the SkyBox
    169 
    170    this must be done, when changing the Size of the Skybox (runtime-efficency)
    171 */
    172 void SkyBox::rebuild()
    173 {
    174   StaticModel* model = new StaticModel();
    175 
    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);
    184 
    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);
    189 
    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);
    196 
    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
    209 
    210   model->finalize();
    211 
    212   this->setModel(model);
    213 }
    214 
    215 int 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 
    243 int 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 
    271 void SkyBox::writeDebug( ) const
    272 {
    273 }
    274 
    275 void SkyBox::readDebug( ) const
    276 {
    277 }
  • trunk/src/world_entities/environments/water.h

    r6451 r6455  
    11/*!
    2  * @file skybox.h
     2 * @file water.h
    33 *  Definition of the SkyBox, that handles the Display of an atmosphere for orxonox.
    44 *
     
    77*/
    88
    9 #ifndef _SKYBOX_H
    10 #define _SKYBOX_H
     9#ifndef _WATER_H
     10#define _WATER_H
    1111
    1212/* INCLUDES */
     
    1515/* FORWARD DECLARATION */
    1616class Material;
     17class Grid;
    1718
    18 //! A Class to handle a SkyBox
    19 class SkyBox : public WorldEntity
     19//! A Class to handle a WaterEffects
     20class Water : public WorldEntity
    2021{
    2122 public:
    22   SkyBox(const char* fileName = NULL);
    23   SkyBox(const TiXmlElement* root);
     23   Water(const TiXmlElement* root = NULL);
     24   ~Water();
    2425
    25   virtual ~SkyBox();
     26   void loadParams(const TiXmlElement* root);
    2627
    27   void init();
    28   void preInit();
     28   void setResolution(unsigned int resX, unsigned int resY);
     29   void setSize(float sizeX, float sizeY);
     30   void rebuildGrid();
    2931
    30   void loadParams(const TiXmlElement* root);
     32  private:
     33    Grid*           grid;            //!< The water-surface-model to render with
    3134
    32   void postInit();
    33 
    34 
    35   void setSize(float size);
    36   /** assumes jpg as input-format */
    37   void setTexture(const char* name) { if (textureName) delete[] textureName; textureName = new char[strlen(name)+1]; strcpy(textureName, name); this->setTextureAndType (name, "jpg"); };
    38 
    39   void setTextureAndType(const char* name, const char* extension);
    40   void setTextures(const char* top, const char* bottom, const char* left,
    41                    const char* right, const char* front, const char* back);
    42 
    43   virtual int       writeBytes(const byte* data, int length, int sender);
    44   virtual int       readBytes(byte* data, int maxLength, int * reciever);
    45   virtual void      writeDebug() const;
    46   virtual void      readDebug() const;
    47 
    48  private:
    49   void rebuild();
    50 
    51   Material*       material[6];     //!< Materials for the SkyBox. sorted by number (0-5) top, bottom, left, right, front, back
    52   float           size;            //!< Size of the SkyBox. This should match the frustum maximum range.
    53   char*           textureName;     //!< Name of the Texture
    54 
     35    unsigned int    resX, resY;
     36    float           sizeX, sizeY;
    5537};
    5638
    57 #endif  /* _SKYBOX_H */
     39#endif  /* _WATER_H */
    5840
    5941
Note: See TracChangeset for help on using the changeset viewer.