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