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: Stefan Lienard |
---|
13 | co-programmer: ... |
---|
14 | */ |
---|
15 | |
---|
16 | #include "mapped_water.h" |
---|
17 | #include "util/loading/load_param.h" |
---|
18 | #include "util/loading/factory.h" |
---|
19 | #include "util/loading/resource_manager.h" |
---|
20 | #include "state.h" |
---|
21 | |
---|
22 | |
---|
23 | CREATE_FACTORY(MappedWater, CL_MAPPED_WATER); |
---|
24 | |
---|
25 | /** |
---|
26 | * @brief constructor |
---|
27 | * @param root xml data |
---|
28 | */ |
---|
29 | MappedWater::MappedWater(const TiXmlElement* root) |
---|
30 | { |
---|
31 | this->setClassID(CL_MAPPED_WATER, "MappedWater"); |
---|
32 | this->toList(OM_ENVIRON); |
---|
33 | |
---|
34 | if (root != NULL) |
---|
35 | this->loadParams(root); |
---|
36 | |
---|
37 | /// loads the textures |
---|
38 | // set up refleciton texture |
---|
39 | //mat.setDiffuseMap(this->texture, 0); |
---|
40 | mat.setDiffuseMap("pictures/refl.bmp", GL_TEXTURE_2D, 0); |
---|
41 | // load refraction texture |
---|
42 | mat.setDiffuseMap("pictures/refr.bmp", GL_TEXTURE_2D, 1); |
---|
43 | // load normal map |
---|
44 | mat.setDiffuseMap("pictures/normalmap.bmp", GL_TEXTURE_2D, 2); |
---|
45 | // load dudv map |
---|
46 | mat.setDiffuseMap("pictures/dudvmap.bmp", GL_TEXTURE_2D, 3); |
---|
47 | // set up depth texture |
---|
48 | //mat.setDiffuseMap("pictures/sky-replace.jpg", GL_TEXTURE_2D, 4); |
---|
49 | |
---|
50 | |
---|
51 | |
---|
52 | /// MAKE THE MAPPING TEXTURE. |
---|
53 | // THIS IS A HACK AND SHOULD BE IN TEXTURE SOMEWHERE |
---|
54 | // set the size of the refraction and reflection textures |
---|
55 | this->textureSize = 512; |
---|
56 | //unsigned int channels = 32; |
---|
57 | //GLenum type = GL_RGBA; |
---|
58 | //unsigned int* pTextureReflection = new unsigned int [this->textureSize * this->textureSize * channels]; |
---|
59 | //memset(pTextureReflection, 0, this->textureSize * this->textureSize * channels * sizeof(unsigned int)); |
---|
60 | //unsigned int* pTextureRefraction = new unsigned int [this->textureSize * this->textureSize * channels]; |
---|
61 | //memset(pTextureRefraction, 0, this->textureSize * this->textureSize * channels * sizeof(unsigned int)); |
---|
62 | // Register the texture with OpenGL and bind it to the texture ID |
---|
63 | //mat.select(); |
---|
64 | //glBindTexture(GL_TEXTURE_2D, this->mat.getDiffuseTexture(0)); |
---|
65 | |
---|
66 | // Create the texture and store it on the video card |
---|
67 | //glTexImage2D(GL_TEXTURE_2D, 0, channels, this->textureSize, this->textureSize, 0, type, GL_UNSIGNED_INT, pTextureReflection); |
---|
68 | |
---|
69 | //gluBuild2DMipmaps(GL_TEXTURE_2D, channels, this->textureSize, this->textureSize, type, GL_UNSIGNED_INT, pTexture); |
---|
70 | |
---|
71 | //the same for the refraction |
---|
72 | //glBindTexture(GL_TEXTURE_2D, this->mat.getDiffuseTexture(1)); |
---|
73 | //glTexImage2D(GL_TEXTURE_2D, 0, channels, this->textureSize, this->textureSize, 0, type, GL_UNSIGNED_INT, pTextureRefraction); |
---|
74 | |
---|
75 | unsigned int channels = 32; |
---|
76 | GLenum type = GL_RGBA; |
---|
77 | unsigned int* pTextureReflection = new unsigned int [this->textureSize * this->textureSize * channels]; |
---|
78 | memset(pTextureReflection, 0, this->textureSize * this->textureSize * channels * sizeof(unsigned int)); |
---|
79 | //mat.select(); |
---|
80 | glBindTexture(GL_TEXTURE_2D, this->mat.getDiffuseTexture(0)); |
---|
81 | glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); |
---|
82 | glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); |
---|
83 | glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
---|
84 | glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
---|
85 | glTexImage2D(GL_TEXTURE_2D, 0, channels, this->textureSize, this->textureSize, 0, type, GL_UNSIGNED_INT, pTextureReflection); |
---|
86 | |
---|
87 | |
---|
88 | unsigned int* pTextureRefraction = new unsigned int [this->textureSize * this->textureSize * channels]; |
---|
89 | memset(pTextureRefraction, 0, this->textureSize * this->textureSize * channels * sizeof(unsigned int)); |
---|
90 | glBindTexture(GL_TEXTURE_2D, this->mat.getDiffuseTexture(1)); |
---|
91 | glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); |
---|
92 | glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); |
---|
93 | glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
---|
94 | glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
---|
95 | glTexImage2D(GL_TEXTURE_2D, 0, channels, this->textureSize, this->textureSize, 0, type, GL_UNSIGNED_INT, pTextureRefraction); |
---|
96 | |
---|
97 | // Set the texture quality |
---|
98 | |
---|
99 | |
---|
100 | // Since we stored the texture space with OpenGL, we can delete the image data |
---|
101 | //delete [] pTextureReflection; |
---|
102 | //delete [] pTextureRefraction; |
---|
103 | |
---|
104 | |
---|
105 | /// initialization of the texture coords, speeds etc... |
---|
106 | this->move = 0.0f; |
---|
107 | this->kNormalMapScale = 0.25f; |
---|
108 | |
---|
109 | |
---|
110 | /// initialization of the shaders |
---|
111 | this->initShaders(); |
---|
112 | |
---|
113 | /// fog, doesnt work the way i want it to work |
---|
114 | /*this->fog = new FogEffect(); |
---|
115 | fog->setFogColor(0.1f, 0.2f, 0.4f); |
---|
116 | fog->setFogRange(0.0f, 500.0f); |
---|
117 | fog->setFogDensity(0.03f);*/ |
---|
118 | } |
---|
119 | |
---|
120 | /** |
---|
121 | * @brief deltes shader and the uniform used by the camera |
---|
122 | */ |
---|
123 | MappedWater::~MappedWater() |
---|
124 | { |
---|
125 | delete shader; |
---|
126 | delete cam_uni; |
---|
127 | //delete fog; |
---|
128 | } |
---|
129 | |
---|
130 | /** |
---|
131 | * @brief initialization of the shaders |
---|
132 | */ |
---|
133 | void MappedWater::initShaders() |
---|
134 | { |
---|
135 | // load shader files |
---|
136 | shader = new Shader( ResourceManager::getInstance()->getDataDir() + "/shaders/mapped_water.vert", ResourceManager::getInstance()->getDataDir() +"/shaders/mapped_water.frag"); |
---|
137 | |
---|
138 | this->shader->activateShader(); |
---|
139 | // Set the variable "reflection" to correspond to the first texture unit |
---|
140 | Shader::Uniform(shader, "reflection").set(0); |
---|
141 | // Set the variable "refraction" to correspond to the second texture unit |
---|
142 | Shader::Uniform(shader, "refraction").set(1); |
---|
143 | // Set the variable "normalMap" to correspond to the third texture unit |
---|
144 | Shader::Uniform(shader, "normalMap").set(2); |
---|
145 | // Set the variable "dudvMap" to correspond to the fourth texture unit |
---|
146 | Shader::Uniform(shader, "dudvMap").set(3); |
---|
147 | // Set the variable "depthMap" to correspond to the fifth texture unit |
---|
148 | Shader::Uniform(shader, "depthMap").set(2); |
---|
149 | // Give the variable "waterColor" a blue color |
---|
150 | Shader::Uniform(shader, "waterColor").set(0.1f, 0.2f, 0.4f, 1.0f); |
---|
151 | // Give the variable "lightPos" our hard coded light position |
---|
152 | Shader::Uniform(shader, "lightPos").set(lightPos.x, lightPos.y, lightPos.z, 1.0f); |
---|
153 | // uniform for the camera position |
---|
154 | cam_uni = new Shader::Uniform(shader, "cameraPos"); |
---|
155 | |
---|
156 | this->shader->deactivateShader(); |
---|
157 | } |
---|
158 | |
---|
159 | /** |
---|
160 | * @brief ends the refraction and saves the graphic buffer into a texture |
---|
161 | * @param root xml data |
---|
162 | */ |
---|
163 | void MappedWater::loadParams(const TiXmlElement* root) |
---|
164 | { |
---|
165 | WorldEntity::loadParams(root); |
---|
166 | |
---|
167 | LoadParam(root, "waterpos", this, MappedWater, setWaterPos); |
---|
168 | LoadParam(root, "watersize", this, MappedWater, setWaterSize); |
---|
169 | LoadParam(root, "lightpos", this, MappedWater, setLightPosition); |
---|
170 | LoadParam(root, "wateruv", this, MappedWater, setWaterUV); |
---|
171 | LoadParam(root, "waterflow", this, MappedWater, setWaterFlow); |
---|
172 | } |
---|
173 | |
---|
174 | /** |
---|
175 | * @brief activates the water shader and draws a quad with four textures on it |
---|
176 | */ |
---|
177 | void MappedWater::draw() const |
---|
178 | { |
---|
179 | glMatrixMode(GL_MODELVIEW); |
---|
180 | |
---|
181 | glPushMatrix(); |
---|
182 | glTranslatef(this->waterPos.x,this->waterPos.y,this->waterPos.z); |
---|
183 | |
---|
184 | mat.select(); |
---|
185 | |
---|
186 | this->shader->activateShader(); |
---|
187 | |
---|
188 | // reset the camera uniform to the current cam position |
---|
189 | Vector pos = State::getCameraNode()->getAbsCoor(); |
---|
190 | cam_uni->set(pos.x, pos.y, pos.z, 1.0f); |
---|
191 | |
---|
192 | glBegin(GL_QUADS); |
---|
193 | // The back left vertice for the water |
---|
194 | glMultiTexCoord2f(GL_TEXTURE0, 0.0f, g_WaterUV); // Reflection texture |
---|
195 | glMultiTexCoord2f(GL_TEXTURE1, 0.0f, refrUV - move); // Refraction texture |
---|
196 | glMultiTexCoord2f(GL_TEXTURE2, 0.0f, normalUV + move2); // Normal map texture |
---|
197 | glMultiTexCoord2f(GL_TEXTURE3, 0, 0); // DUDV map texture |
---|
198 | glVertex3f(0.0f, this->waterPos.y, 0.0f); |
---|
199 | |
---|
200 | // The front left vertice for the water |
---|
201 | glMultiTexCoord2f(GL_TEXTURE0, 0.0f, 0.0f); // Reflection texture |
---|
202 | glMultiTexCoord2f(GL_TEXTURE1, 0.0f, 0.0f - move); // Refraction texture |
---|
203 | glMultiTexCoord2f(GL_TEXTURE2, 0.0f, 0.0f + move2); // Normal map texture |
---|
204 | glMultiTexCoord2f(GL_TEXTURE3, 0, 0); // DUDV map texture |
---|
205 | glVertex3f(0.0f, this->waterPos.y, this->zWidth); |
---|
206 | |
---|
207 | // The front right vertice for the water |
---|
208 | glMultiTexCoord2f(GL_TEXTURE0, g_WaterUV, 0.0f); // Reflection texture |
---|
209 | glMultiTexCoord2f(GL_TEXTURE1, refrUV, 0.0f - move); // Refraction texture |
---|
210 | glMultiTexCoord2f(GL_TEXTURE2, normalUV, 0.0f + move2); // Normal map texture |
---|
211 | glMultiTexCoord2f(GL_TEXTURE3, 0, 0); // DUDV map texture |
---|
212 | glVertex3f(this->xWidth, this->waterPos.y, this->zWidth); |
---|
213 | |
---|
214 | // The back right vertice for the water |
---|
215 | glMultiTexCoord2f(GL_TEXTURE0, g_WaterUV, g_WaterUV); // Reflection texture |
---|
216 | glMultiTexCoord2f(GL_TEXTURE1, refrUV, refrUV - move); // Refraction texture |
---|
217 | glMultiTexCoord2f(GL_TEXTURE2, normalUV, normalUV + move2); // Normal map texture |
---|
218 | glMultiTexCoord2f(GL_TEXTURE3, 0, 0); // DUDV map texture |
---|
219 | glVertex3f(this->xWidth, this->waterPos.y, 0.0f); |
---|
220 | glEnd(); |
---|
221 | |
---|
222 | this->shader->deactivateShader(); |
---|
223 | |
---|
224 | mat.unselect(); |
---|
225 | |
---|
226 | /*if(pos.y < this->waterPos.y) |
---|
227 | { |
---|
228 | // draw some fog |
---|
229 | this->fog->activate(); |
---|
230 | } |
---|
231 | else |
---|
232 | { |
---|
233 | this->fog->deactivate(); |
---|
234 | }*/ |
---|
235 | |
---|
236 | glPopMatrix(); |
---|
237 | } |
---|
238 | |
---|
239 | /** |
---|
240 | * @brief tick tack, calculates the flow of the water |
---|
241 | */ |
---|
242 | void MappedWater::tick(float dt) |
---|
243 | { |
---|
244 | // makes the water flow |
---|
245 | this->move += this->g_WaterFlow; |
---|
246 | this->move2 = this->move * this->kNormalMapScale; |
---|
247 | this->refrUV = this->g_WaterUV; |
---|
248 | this->normalUV = this->g_WaterUV * this->kNormalMapScale; |
---|
249 | } |
---|
250 | |
---|
251 | /** |
---|
252 | * @brief prepares everything to render the reflection texutre |
---|
253 | */ |
---|
254 | void MappedWater::activateReflection() |
---|
255 | { |
---|
256 | // To create the reflection texture we just need to set the view port |
---|
257 | // to our texture map size, then render the current scene our camera |
---|
258 | // is looking at to the already allocated texture unit. Since this |
---|
259 | // is a reflection of the top of the water surface we use clipping |
---|
260 | // planes to only render the top of the world as a reflection. If |
---|
261 | // we are below the water we don't flip the reflection but just use |
---|
262 | // the current view of the top as we are seeing through the water. |
---|
263 | // When you look through water at the surface it isn't really reflected, |
---|
264 | // only when looking down from above the water on the surface. |
---|
265 | |
---|
266 | // save viewport matrix and change the viewport size |
---|
267 | glPushAttrib(GL_VIEWPORT_BIT); |
---|
268 | glViewport(0,0, textureSize, textureSize); |
---|
269 | |
---|
270 | glMatrixMode(GL_MODELVIEW); |
---|
271 | glPushMatrix(); |
---|
272 | |
---|
273 | // If our camera is above the water we will render the scene flipped upside down. |
---|
274 | // In order to line up the reflection nicely with the world we have to translate |
---|
275 | // the world to the position of our reflected surface, multiplied by two. |
---|
276 | glEnable(GL_CLIP_PLANE0); |
---|
277 | Vector pos = State::getCameraNode()->getAbsCoor(); |
---|
278 | |
---|
279 | if(pos.y > waterPos.y) |
---|
280 | { |
---|
281 | // Translate the world, then flip it upside down |
---|
282 | glTranslatef(0.0f, waterPos.y*2.0f, 0.0f); |
---|
283 | glScalef(1.0, -1.0, 1.0); |
---|
284 | |
---|
285 | // Since the world is updside down we need to change the culling to FRONT |
---|
286 | glCullFace(GL_FRONT); |
---|
287 | |
---|
288 | // Set our plane equation and turn clipping on |
---|
289 | double plane[4] = {0.0, 1.0, 0.0, -waterPos.y}; |
---|
290 | glClipPlane(GL_CLIP_PLANE0, plane); |
---|
291 | } |
---|
292 | else |
---|
293 | { |
---|
294 | // If the camera is below the water we don't want to flip the world, |
---|
295 | // but just render it clipped so only the top is drawn. |
---|
296 | double plane[4] = {0.0, 1.0, 0.0, waterPos.y}; |
---|
297 | glClipPlane(GL_CLIP_PLANE0, plane); |
---|
298 | } |
---|
299 | } |
---|
300 | |
---|
301 | /** |
---|
302 | * @brief ends the reflection and saves the graphic buffer into a texture |
---|
303 | */ |
---|
304 | void MappedWater::deactivateReflection() |
---|
305 | { |
---|
306 | glDisable(GL_CLIP_PLANE0); |
---|
307 | glCullFace(GL_BACK); |
---|
308 | |
---|
309 | // Create the texture and store it on the video card |
---|
310 | mat.renderToTexture(0, GL_TEXTURE_2D, 0, 0, 0, 0, 0, textureSize, textureSize); |
---|
311 | |
---|
312 | glPopMatrix(); |
---|
313 | glPopAttrib(); |
---|
314 | } |
---|
315 | |
---|
316 | /** |
---|
317 | * @brief prepares everything to render the refraction texutre |
---|
318 | */ |
---|
319 | void MappedWater::activateRefraction() |
---|
320 | { |
---|
321 | // To create the refraction and depth textures we do the same thing |
---|
322 | // we did for the reflection texture, except we don't need to turn |
---|
323 | // the world upside down. We want to find the depth of the water, |
---|
324 | // not the depth of the sky and above water terrain. |
---|
325 | |
---|
326 | // save viewport matrix and change the viewport size |
---|
327 | glPushAttrib(GL_VIEWPORT_BIT); |
---|
328 | glViewport(0,0, textureSize, textureSize); |
---|
329 | |
---|
330 | glMatrixMode(GL_MODELVIEW); |
---|
331 | glPushMatrix(); |
---|
332 | |
---|
333 | // If our camera is above the water we will render only the parts that |
---|
334 | // are under the water. If the camera is below the water we render |
---|
335 | // only the stuff above the water. Like the reflection texture, we |
---|
336 | // incorporate clipping planes to only render what we need. |
---|
337 | |
---|
338 | // If the camera is above water, render the data below the water |
---|
339 | glEnable(GL_CLIP_PLANE0); |
---|
340 | Vector pos = State::getCameraNode()->getAbsCoor(); |
---|
341 | if(pos.y > waterPos.y) |
---|
342 | { |
---|
343 | double plane[4] = {0.0, -1.0, 0.0, waterPos.y}; |
---|
344 | glClipPlane(GL_CLIP_PLANE0, plane); |
---|
345 | |
---|
346 | } |
---|
347 | // If the camera is below the water, only render the data above the water |
---|
348 | else |
---|
349 | { |
---|
350 | glCullFace(GL_FRONT); |
---|
351 | double plane[4] = {0.0, 1.0, 0.0, -waterPos.y}; |
---|
352 | glClipPlane(GL_CLIP_PLANE0, plane); |
---|
353 | } |
---|
354 | } |
---|
355 | |
---|
356 | /** |
---|
357 | * @brief ends the refraction and saves the graphic buffer into a texture |
---|
358 | */ |
---|
359 | void MappedWater::deactivateRefraction() |
---|
360 | { |
---|
361 | glDisable(GL_CLIP_PLANE0); |
---|
362 | glCullFace(GL_BACK); |
---|
363 | |
---|
364 | // Create the texture and store it on the video card |
---|
365 | mat.renderToTexture(1, GL_TEXTURE_2D, 0, 0, 0, 0, 0, textureSize, textureSize); |
---|
366 | |
---|
367 | glPopMatrix(); |
---|
368 | glPopAttrib(); |
---|
369 | } |
---|