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