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