| 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 "water.h" | 
|---|
| 19 | #include "factory.h" | 
|---|
| 20 | #include "load_param.h" | 
|---|
| 21 |  | 
|---|
| 22 | #include "grid.h" | 
|---|
| 23 | #include "material.h" | 
|---|
| 24 |  | 
|---|
| 25 | #include "resource_manager.h" | 
|---|
| 26 | #include "shader.h" | 
|---|
| 27 |  | 
|---|
| 28 | #include "skybox.h" | 
|---|
| 29 |  | 
|---|
| 30 |  | 
|---|
| 31 | using namespace std; | 
|---|
| 32 |  | 
|---|
| 33 | CREATE_FACTORY(Water, CL_WATER); | 
|---|
| 34 |  | 
|---|
| 35 |  | 
|---|
| 36 | Water::Water(const TiXmlElement* root) | 
|---|
| 37 | { | 
|---|
| 38 |   this->setClassID(CL_WATER, "Water"); | 
|---|
| 39 |   this->toList(OM_ENVIRON); | 
|---|
| 40 |  | 
|---|
| 41 |   this->resX = this->resY = 10; | 
|---|
| 42 |   this->sizeX = this->sizeY = 1.0f; | 
|---|
| 43 |   this->height = 0.5f; | 
|---|
| 44 |   this->grid = NULL; | 
|---|
| 45 |  | 
|---|
| 46 |   if (root != NULL) | 
|---|
| 47 |     this->loadParams(root); | 
|---|
| 48 |  | 
|---|
| 49 |   this->rebuildGrid(); | 
|---|
| 50 |   this->waterMaterial = new Material(); | 
|---|
| 51 |   this->waterShader = (Shader*)ResourceManager::getInstance()->load("shaders/water.vert", SHADER, RP_GAME, (void*)"shaders/water.frag"); | 
|---|
| 52 |  | 
|---|
| 53 | } | 
|---|
| 54 |  | 
|---|
| 55 | Water::~Water() | 
|---|
| 56 | { | 
|---|
| 57 |   delete this->grid; | 
|---|
| 58 |   delete this->waterMaterial; | 
|---|
| 59 | } | 
|---|
| 60 |  | 
|---|
| 61 | void Water::loadParams(const TiXmlElement* root) | 
|---|
| 62 | { | 
|---|
| 63 |   WorldEntity::loadParams(root); | 
|---|
| 64 |  | 
|---|
| 65 |   LoadParam(root, "size", this, Water, setSize) | 
|---|
| 66 |       .describe("the size of the WaterSurface") | 
|---|
| 67 |       .defaultValues(2, 1.0f, 1.0f); | 
|---|
| 68 |  | 
|---|
| 69 |   LoadParam(root, "resolution", this, Water, setResolution) | 
|---|
| 70 |       .describe("sets the resolution of the water surface") | 
|---|
| 71 |       .defaultValues(2, 10, 10); | 
|---|
| 72 |  | 
|---|
| 73 |   LoadParam(root, "height", this, Water, setHeight) | 
|---|
| 74 |       .describe("the height of the Waves") | 
|---|
| 75 |       .defaultValues(1, 0.5f); | 
|---|
| 76 | } | 
|---|
| 77 |  | 
|---|
| 78 | void Water::rebuildGrid() | 
|---|
| 79 | { | 
|---|
| 80 |   if (this->grid != NULL) | 
|---|
| 81 |     delete this->grid; | 
|---|
| 82 |   this->grid = new Grid(this->sizeX, this->sizeY, this->resX, this->resY); | 
|---|
| 83 |  | 
|---|
| 84 |   this->setModel(this->grid, 0); | 
|---|
| 85 | } | 
|---|
| 86 |  | 
|---|
| 87 | void Water::setResolution(unsigned int resX, unsigned int resY) | 
|---|
| 88 | { | 
|---|
| 89 |   this->resX = resX; | 
|---|
| 90 |   this->resY = resY; | 
|---|
| 91 | } | 
|---|
| 92 |  | 
|---|
| 93 | void Water::setSize(float sizeX, float sizeY) | 
|---|
| 94 | { | 
|---|
| 95 |   this->sizeX = sizeX; | 
|---|
| 96 |   this->sizeY = sizeY; | 
|---|
| 97 | } | 
|---|
| 98 |  | 
|---|
| 99 | void Water::setHeight(float height) | 
|---|
| 100 | { | 
|---|
| 101 |   this->height = height; | 
|---|
| 102 | } | 
|---|
| 103 |  | 
|---|
| 104 |  | 
|---|
| 105 | void Water::draw() const | 
|---|
| 106 | { | 
|---|
| 107 |   SkyBox::enableCubeMap(); | 
|---|
| 108 |  | 
|---|
| 109 |  // this->waterShader->activateShader(); | 
|---|
| 110 | //  this->waterMaterial->select(); | 
|---|
| 111 |   WorldEntity::draw(); | 
|---|
| 112 |   //Shader::deactivateShader(); | 
|---|
| 113 |  | 
|---|
| 114 |   SkyBox::disableCubeMap(); | 
|---|
| 115 | } | 
|---|
| 116 |  | 
|---|
| 117 | void Water::tick(float dt) | 
|---|
| 118 | { | 
|---|
| 119 |   phase += dt *.1; | 
|---|
| 120 |   for (unsigned int i = 0; i < this->grid->rows(); i++) | 
|---|
| 121 |   { | 
|---|
| 122 |     for (unsigned int j = 0; j < this->grid->columns(); j++) | 
|---|
| 123 |     { | 
|---|
| 124 |       this->grid->height(i,j) = this->height*sin(((float)i/(float)this->grid->rows() *phase)+ | 
|---|
| 125 |           this->height*cos((float)j/(float)this->grid->columns()) * phase * 2.0); | 
|---|
| 126 |     } | 
|---|
| 127 |   } | 
|---|
| 128 |   this->grid->rebuildNormals(this->height); | 
|---|
| 129 | } | 
|---|