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 | |
---|
21 | |
---|
22 | |
---|
23 | |
---|
24 | CREATE_FACTORY(MappedWater, CL_MAPPED_WATER); |
---|
25 | |
---|
26 | |
---|
27 | MappedWater::MappedWater(const TiXmlElement* root) |
---|
28 | : texture(GL_TEXTURE_2D) |
---|
29 | { |
---|
30 | this->setClassID(CL_MAPPED_WATER, "MappedWater"); |
---|
31 | this->toList(OM_ENVIRON); |
---|
32 | |
---|
33 | if (root != NULL) |
---|
34 | this->loadParams(root); |
---|
35 | |
---|
36 | //! HACK FIXME HACK FIXME HACK FIXME HACK FIXME |
---|
37 | //! todo: rename texture to reflection texture |
---|
38 | // set up refleciton texture |
---|
39 | //mat.setDiffuseMap(&this->texture, 0); |
---|
40 | mat.setDiffuseMap("pictures/dudvmap.bmp", GL_TEXTURE_2D, 0); |
---|
41 | /* mat.setDiffuseMap("pictures/sky-replace.jpg", GL_TEXTURE_2D, 1); |
---|
42 | mat.setDiffuseMap("pictures/sky-replace.jpg", GL_TEXTURE_2D, 2); |
---|
43 | mat.setDiffuseMap("pictures/sky-replace.jpg", GL_TEXTURE_2D, 3); |
---|
44 | mat.setDiffuseMap("pictures/sky-replace.jpg", GL_TEXTURE_2D, 4);*/ |
---|
45 | // load refraction texture |
---|
46 | //mat.setDiffuseMap(&this->refractionTexture, 1); |
---|
47 | // load normal map |
---|
48 | //mat.setDiffuseMap("pictures/normalmap.bmp", GL_TEXTURE_2D, 2); |
---|
49 | // load dudv map |
---|
50 | //mat.setDiffuseMap("pictures/dudvmap.bmp", GL_TEXTURE_2D, 3); |
---|
51 | // set up depth texture |
---|
52 | //mat.setDiffuseMap(&this->depthTexture, 4); |
---|
53 | |
---|
54 | // set the size of the refraction and reflection textures |
---|
55 | |
---|
56 | |
---|
57 | |
---|
58 | /// MAKE THE MAPPING TEXTURE. |
---|
59 | // THIS IS A HACK AND SHOULD BE IN TEXTURE SOMEWHERE. |
---|
60 | this->textureSize = 512; |
---|
61 | unsigned int channels = 32; |
---|
62 | GLenum type = GL_RGBA; |
---|
63 | unsigned int* pTexture = new unsigned int [this->textureSize * this->textureSize * channels]; |
---|
64 | memset(pTexture, 0, this->textureSize * this->textureSize * channels * sizeof(unsigned int)); |
---|
65 | // Register the texture with OpenGL and bind it to the texture ID |
---|
66 | mat.select(); |
---|
67 | glBindTexture(GL_TEXTURE_2D, this->mat.getDiffuseTexture(0)); |
---|
68 | printf("========= CREATE %d\n", this->texture.getTexture()); |
---|
69 | |
---|
70 | // Create the texture and store it on the video card |
---|
71 | glTexImage2D(GL_TEXTURE_2D, 0, channels, this->textureSize, this->textureSize, 0, type, GL_UNSIGNED_INT, pTexture); |
---|
72 | |
---|
73 | // Set the texture quality |
---|
74 | glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); |
---|
75 | glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); |
---|
76 | glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
---|
77 | glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
---|
78 | |
---|
79 | // Since we stored the texture space with OpenGL, we can delete the image data |
---|
80 | delete [] pTexture; |
---|
81 | |
---|
82 | // shader = new Shader( ResourceManager::getInstance()->getDataDir() + "/shaders/water.vert", ResourceManager::getInstance()->getDataDir() +"/shaders/mapped_water.frag"); |
---|
83 | } |
---|
84 | |
---|
85 | MappedWater::~MappedWater() |
---|
86 | { |
---|
87 | //delete shader; |
---|
88 | } |
---|
89 | |
---|
90 | void MappedWater::loadParams(const TiXmlElement* root) |
---|
91 | { |
---|
92 | WorldEntity::loadParams(root); |
---|
93 | |
---|
94 | LoadParam(root, "waterHeight", this, MappedWater, setHeight); |
---|
95 | } |
---|
96 | |
---|
97 | |
---|
98 | void MappedWater::draw() const |
---|
99 | { |
---|
100 | glPushMatrix(); |
---|
101 | glTranslatef(0,this->waterHeight,0); |
---|
102 | |
---|
103 | //glEnable(GL_LIGHTING); |
---|
104 | |
---|
105 | |
---|
106 | //mat.setDiffuseMap("pictures/sky-replace.jpg", GL_TEXTURE_2D, 1); |
---|
107 | //mat.setTransparency(1.0); |
---|
108 | //mat.setDiffuse(1.0, 0, .1); |
---|
109 | |
---|
110 | |
---|
111 | // HACK |
---|
112 | |
---|
113 | //glDisable(GL_BLEND); |
---|
114 | //glActiveTexture(GL_TEXTURE0); |
---|
115 | //glBindTexture(GL_TEXTURE_2D, this->texture); |
---|
116 | |
---|
117 | mat.select(); |
---|
118 | glBindTexture(GL_TEXTURE_2D, this->mat.getDiffuseTexture(0)); |
---|
119 | |
---|
120 | // Shader init |
---|
121 | //this->shader->activateShader(); |
---|
122 | GLint uniform; |
---|
123 | |
---|
124 | // Set the variable "reflection" to correspond to the first texture unit |
---|
125 | uniform = glGetUniformLocationARB(shader->getProgram(), "reflection"); |
---|
126 | glUniform1iARB(uniform, 0); //second paramter is the texture unit |
---|
127 | |
---|
128 | // Set the variable "refraction" to correspond to the second texture unit |
---|
129 | //uniform = glGetUniformLocationARB(shader->getProgram(), "refraction"); |
---|
130 | //glUniform1iARB(uniform, 0); |
---|
131 | // FIXME glUniform1iARB(uniform, 1); |
---|
132 | |
---|
133 | // Set the variable "normalMap" to correspond to the third texture unit |
---|
134 | //uniform = glGetUniformLocationARB(shader->getProgram(), "normalMap"); |
---|
135 | //glUniform1iARB(uniform, 2); |
---|
136 | |
---|
137 | // Set the variable "dudvMap" to correspond to the fourth texture unit |
---|
138 | uniform = glGetUniformLocationARB(shader->getProgram(), "dudvMap"); |
---|
139 | glUniform1iARB(uniform, 3); |
---|
140 | |
---|
141 | // Set the variable "depthMap" to correspond to the fifth texture unit |
---|
142 | //uniform = glGetUniformLocationARB(shader->getProgram(), "depthMap"); |
---|
143 | //glUniform1iARB(uniform, 4); |
---|
144 | // FIXME we dont have a depthMap yet :-( |
---|
145 | |
---|
146 | // Give the variable "waterColor" a blue color |
---|
147 | uniform = glGetUniformLocationARB(shader->getProgram(), "waterColor"); |
---|
148 | glUniform4fARB(uniform, 0.1f, 0.2f, 0.4f, 1.0f); |
---|
149 | |
---|
150 | // FIXME set camera and light information |
---|
151 | Vector lightPos(100.0f, 150.0f, 100.0f); |
---|
152 | |
---|
153 | // Store the camera position in a variable |
---|
154 | //CVector3 vPosition = g_Camera.Position(); |
---|
155 | Vector vPosition(50.0f, 50.0f, 50.0f); |
---|
156 | |
---|
157 | // Give the variable "lightPos" our hard coded light position |
---|
158 | uniform = glGetUniformLocationARB(shader->getProgram(), "lightPos"); |
---|
159 | glUniform4fARB(uniform, lightPos.x, lightPos.y, lightPos.z, 1.0f); |
---|
160 | |
---|
161 | // Give the variable "cameraPos" our camera position |
---|
162 | uniform = glGetUniformLocationARB(shader->getProgram(), "cameraPos"); |
---|
163 | glUniform4fARB(uniform, vPosition.x, vPosition.y, vPosition.z, 1.0f); |
---|
164 | |
---|
165 | glBegin(GL_QUADS); |
---|
166 | glNormal3f(0,1,0); |
---|
167 | glMultiTexCoord2f(GL_TEXTURE0, 0,0); |
---|
168 | glMultiTexCoord2f(GL_TEXTURE1, 0,0); |
---|
169 | glMultiTexCoord2f(GL_TEXTURE2, 0,0); |
---|
170 | glMultiTexCoord2f(GL_TEXTURE3, 0,0); |
---|
171 | glMultiTexCoord2f(GL_TEXTURE4, 0,0); |
---|
172 | glVertex3f(0,0,0); |
---|
173 | |
---|
174 | glMultiTexCoord2f(GL_TEXTURE0, 1,0); |
---|
175 | glMultiTexCoord2f(GL_TEXTURE1, 1,0); |
---|
176 | glMultiTexCoord2f(GL_TEXTURE2, 1,0); |
---|
177 | glMultiTexCoord2f(GL_TEXTURE3, 1,0); |
---|
178 | glMultiTexCoord2f(GL_TEXTURE4, 1,0); |
---|
179 | glVertex3f(100,0,0); |
---|
180 | |
---|
181 | glMultiTexCoord2f(GL_TEXTURE0, 1,1); |
---|
182 | glMultiTexCoord2f(GL_TEXTURE1, 1,1); |
---|
183 | glMultiTexCoord2f(GL_TEXTURE2, 1,1); |
---|
184 | glMultiTexCoord2f(GL_TEXTURE3, 1,1); |
---|
185 | glMultiTexCoord2f(GL_TEXTURE4, 1,1); |
---|
186 | glVertex3f(100,0,-100); |
---|
187 | |
---|
188 | glMultiTexCoord2f(GL_TEXTURE0, 0,1); |
---|
189 | glMultiTexCoord2f(GL_TEXTURE1, 0,1); |
---|
190 | glMultiTexCoord2f(GL_TEXTURE2, 0,1); |
---|
191 | glMultiTexCoord2f(GL_TEXTURE3, 0,1); |
---|
192 | glMultiTexCoord2f(GL_TEXTURE4, 0,1); |
---|
193 | glVertex3f(0,0,-100); |
---|
194 | glEnd(); |
---|
195 | //this->shader->deactivateShader(); |
---|
196 | |
---|
197 | glPopMatrix(); |
---|
198 | } |
---|
199 | |
---|
200 | void MappedWater::tick(float dt) |
---|
201 | { |
---|
202 | } |
---|
203 | |
---|
204 | void MappedWater::setHeight(float height) |
---|
205 | { |
---|
206 | this->waterHeight = height; |
---|
207 | } |
---|
208 | |
---|
209 | void MappedWater::activateReflection() |
---|
210 | { |
---|
211 | glPushAttrib(GL_VIEWPORT_BIT); |
---|
212 | // glPushMatrix(); |
---|
213 | |
---|
214 | //glLoadIdentity(); |
---|
215 | glViewport(0,0, textureSize, textureSize); |
---|
216 | |
---|
217 | |
---|
218 | // Clear the color and depth bits, reset the matrix and position our camera. |
---|
219 | |
---|
220 | //g_Camera.Look(); |
---|
221 | |
---|
222 | |
---|
223 | // If our camera is above the water we will render the scene flipped upside down. |
---|
224 | // In order to line up the reflection nicely with the world we have to translate |
---|
225 | // the world to the position of our reflected surface, multiplied by two. |
---|
226 | //if(g_Camera.Position().y > waterHeight) |
---|
227 | //{ |
---|
228 | // Translate the world, then flip it upside down |
---|
229 | // glTranslatef(0.0f, waterHeight*2.0f, 0.0f); |
---|
230 | // glScalef(1.0, -1.0, 1.0); |
---|
231 | |
---|
232 | // Since the world is updside down we need to change the culling to FRONT |
---|
233 | //glCullFace(GL_FRONT); |
---|
234 | |
---|
235 | // Set our plane equation and turn clipping on |
---|
236 | // double plane[4] = {0.0, 1.0, 0.0, -waterHeight}; |
---|
237 | // glEnable(GL_CLIP_PLANE0); |
---|
238 | // glClipPlane(GL_CLIP_PLANE0, plane); |
---|
239 | |
---|
240 | // Render the world upside down and clipped (only render the top flipped). |
---|
241 | // If we don't turn OFF caustics for the reflection texture we get horrible |
---|
242 | // artifacts in the water. That is why we set bRenderCaustics to FALSE. |
---|
243 | //RenderWorld(false); |
---|
244 | |
---|
245 | // Turn clipping off |
---|
246 | // glDisable(GL_CLIP_PLANE0); |
---|
247 | |
---|
248 | // Restore back-face culling |
---|
249 | // glCullFace(GL_BACK); |
---|
250 | //} |
---|
251 | /*else |
---|
252 | { |
---|
253 | // If the camera is below the water we don't want to flip the world, |
---|
254 | // but just render it clipped so only the top is drawn. |
---|
255 | double plane[4] = {0.0, 1.0, 0.0, waterHeight}; |
---|
256 | glEnable(GL_CLIP_PLANE0); |
---|
257 | glClipPlane(GL_CLIP_PLANE0, plane); |
---|
258 | RenderWorld(true); |
---|
259 | glDisable(GL_CLIP_PLANE0); |
---|
260 | }*/ |
---|
261 | } |
---|
262 | |
---|
263 | |
---|
264 | void MappedWater::deactivateReflection() |
---|
265 | { |
---|
266 | // glBindTexture(GL_TEXTURE_2D, texture.getTexture()); //is done by mat.select(); |
---|
267 | glBindTexture(GL_TEXTURE_2D, this->mat.getDiffuseTexture(0)); |
---|
268 | |
---|
269 | //mat.setDiffuseMap(&texture, 0); |
---|
270 | // mat.select(); |
---|
271 | glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, textureSize, textureSize); |
---|
272 | |
---|
273 | |
---|
274 | glPopAttrib(); |
---|
275 | // glPopMatrix(); |
---|
276 | } |
---|
277 | |
---|
278 | void MappedWater::activateRefraction() |
---|
279 | { |
---|
280 | } |
---|
281 | |
---|
282 | void MappedWater::deactivateRefraction() |
---|
283 | { |
---|
284 | } |
---|