57b9730a075bd4af4b66da74d4177b3447847d46
[supertux.git] / src / video / gl / gl_lightmap.cpp
1 //  SuperTux
2 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
3 //
4 //  This program is free software: you can redistribute it and/or modify
5 //  it under the terms of the GNU General Public License as published by
6 //  the Free Software Foundation, either version 3 of the License, or
7 //  (at your option) any later version.
8 //
9 //  This program is distributed in the hope that it will be useful,
10 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
11 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 //  GNU General Public License for more details.
13 //
14 //  You should have received a copy of the GNU General Public License
15 //  along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
17 #include "video/gl/gl_lightmap.hpp"
18
19 #include <SDL_image.h>
20 #include <algorithm>
21 #include <assert.h>
22 #include <functional>
23 #include <iomanip>
24 #include <iostream>
25 #include <math.h>
26 #include <physfs.h>
27 #include <sstream>
28
29 #include "supertux/gameconfig.hpp"
30 #include "supertux/globals.hpp"
31 #include "util/obstackpp.hpp"
32 #include "video/drawing_context.hpp"
33 #include "video/drawing_request.hpp"
34 #include "video/font.hpp"
35 #include "video/gl/gl_surface_data.hpp"
36 #include "video/gl/gl_texture.hpp"
37 #include "video/glutil.hpp"
38 #include "video/lightmap.hpp"
39 #include "video/renderer.hpp"
40 #include "video/surface.hpp"
41 #include "video/texture_manager.hpp"
42
43 namespace {
44
45 inline void intern_draw(float left, float top, float right, float bottom,
46                         float uv_left, float uv_top,
47                         float uv_right, float uv_bottom,
48                         float angle, float alpha,
49                         const Color& color,
50                         const Blend& blend,
51                         DrawingEffect effect)
52 {
53   if(effect & HORIZONTAL_FLIP)
54     std::swap(uv_left, uv_right);
55  
56   if(effect & VERTICAL_FLIP) 
57     std::swap(uv_top, uv_bottom);
58
59   // unrotated blit
60   glBlendFunc(blend.sfactor, blend.dfactor);
61   glColor4f(color.red, color.green, color.blue, color.alpha * alpha);
62  
63   if (angle == 0.0f) {
64     float vertices[] = {
65       left, top,
66       right, top,
67       right, bottom,
68       left, bottom,
69     };
70     glVertexPointer(2, GL_FLOAT, 0, vertices);
71
72     float uvs[] = {
73       uv_left, uv_top,
74       uv_right, uv_top,
75       uv_right, uv_bottom,
76       uv_left, uv_bottom,
77     };
78     glTexCoordPointer(2, GL_FLOAT, 0, uvs);
79
80     glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
81   } else {
82     // rotated blit
83     float center_x = (left + right) / 2;
84     float center_y = (top + bottom) / 2;
85
86     float sa = sinf(angle/180.0f*M_PI);
87     float ca = cosf(angle/180.0f*M_PI);
88
89     left  -= center_x;
90     right -= center_x;
91
92     top    -= center_y;
93     bottom -= center_y;
94
95     float vertices[] = {
96       left*ca - top*sa + center_x, left*sa + top*ca + center_y,
97       right*ca - top*sa + center_x, right*sa + top*ca + center_y,
98       right*ca - bottom*sa + center_x, right*sa + bottom*ca + center_y,
99       left*ca - bottom*sa + center_x, left*sa + bottom*ca + center_y
100     };
101     glVertexPointer(2, GL_FLOAT, 0, vertices);
102
103     float uvs[] = {
104       uv_left, uv_top,
105       uv_right, uv_top,
106       uv_right, uv_bottom,
107       uv_left, uv_bottom,
108     };
109     glTexCoordPointer(2, GL_FLOAT, 0, uvs);
110
111     glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
112   }
113
114   // FIXME: find a better way to restore the blend mode
115   glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
116   glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
117 }
118
119 static inline int next_po2(int val)
120 {
121   int result = 1;
122   while(result < val)
123     result *= 2;
124
125   return result;
126 }
127
128 } // namespace
129
130 GLLightmap::GLLightmap() :
131   screen(),
132   lightmap(),
133   lightmap_width(),
134   lightmap_height(),
135   lightmap_uv_right(),
136   lightmap_uv_bottom()
137 {
138   screen = SDL_GetVideoSurface();
139
140   lightmap_width = screen->w / LIGHTMAP_DIV;
141   lightmap_height = screen->h / LIGHTMAP_DIV;
142   unsigned int width = next_po2(lightmap_width);
143   unsigned int height = next_po2(lightmap_height);
144
145   lightmap.reset(new GLTexture(width, height));
146
147   lightmap_uv_right = static_cast<float>(lightmap_width) / static_cast<float>(width);
148   lightmap_uv_bottom = static_cast<float>(lightmap_height) / static_cast<float>(height);
149   texture_manager->register_texture(lightmap.get());
150 }
151
152 GLLightmap::~GLLightmap()
153 {
154 }
155
156 void
157 GLLightmap::start_draw(const Color &ambient_color)
158 {
159   glViewport(0, screen->h - lightmap_height, lightmap_width, lightmap_height);
160   glMatrixMode(GL_PROJECTION);
161   glLoadIdentity();
162 #ifdef GL_VERSION_ES_CM_1_0
163   glOrthof(0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, -1.0, 1.0);
164 #else
165   glOrtho(0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, -1.0, 1.0);
166 #endif
167   glMatrixMode(GL_MODELVIEW);
168   glLoadIdentity();
169
170   glClearColor( ambient_color.red, ambient_color.green, ambient_color.blue, 1 );
171   glClear(GL_COLOR_BUFFER_BIT);
172 }
173
174 void
175 GLLightmap::end_draw()
176 {
177   glDisable(GL_BLEND);
178   glBindTexture(GL_TEXTURE_2D, lightmap->get_handle());
179   glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, screen->h - lightmap_height, lightmap_width, lightmap_height);
180
181   glViewport(0, 0, screen->w, screen->h);
182   glMatrixMode(GL_PROJECTION);
183   glLoadIdentity();
184 #ifdef GL_VERSION_ES_CM_1_0
185   glOrthof(0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, -1.0, 1.0);
186 #else
187   glOrtho(0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, -1.0, 1.0);
188 #endif
189   glMatrixMode(GL_MODELVIEW);
190   glLoadIdentity();
191   glEnable(GL_BLEND);
192   //glClear(GL_COLOR_BUFFER_BIT);
193 }
194
195 void
196 GLLightmap::do_draw()
197 {
198   // multiple the lightmap with the framebuffer
199   glBlendFunc(GL_DST_COLOR, GL_ZERO);
200
201   glBindTexture(GL_TEXTURE_2D, lightmap->get_handle());
202
203   float vertices[] = {
204     0, 0,
205     SCREEN_WIDTH, 0,
206     SCREEN_WIDTH, SCREEN_HEIGHT,
207     0, SCREEN_HEIGHT
208   };
209   glVertexPointer(2, GL_FLOAT, 0, vertices);
210
211   float uvs[] = {
212     0,                 lightmap_uv_bottom,
213     lightmap_uv_right, lightmap_uv_bottom,
214     lightmap_uv_right, 0,
215     0, 0
216   };
217   glTexCoordPointer(2, GL_FLOAT, 0, uvs);
218
219   glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
220
221   glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
222 }
223
224 void
225 GLLightmap::draw_surface(const DrawingRequest& request)
226 {
227   const Surface* surface = (const Surface*) request.request_data;
228   boost::shared_ptr<GLTexture> gltexture = boost::dynamic_pointer_cast<GLTexture>(surface->get_texture());
229   GLSurfaceData *surface_data = reinterpret_cast<GLSurfaceData *>(surface->get_surface_data());
230
231   glBindTexture(GL_TEXTURE_2D, gltexture->get_handle());
232   intern_draw(request.pos.x, request.pos.y,
233               request.pos.x + surface->get_width(),
234               request.pos.y + surface->get_height(),
235               surface_data->get_uv_left(),
236               surface_data->get_uv_top(),
237               surface_data->get_uv_right(),
238               surface_data->get_uv_bottom(),
239               request.angle,
240               request.alpha,
241               request.color,
242               request.blend,
243               request.drawing_effect);
244 }
245
246 void
247 GLLightmap::draw_surface_part(const DrawingRequest& request)
248 {
249   const SurfacePartRequest* surfacepartrequest
250     = (SurfacePartRequest*) request.request_data;
251   const Surface* surface = surfacepartrequest->surface;
252   boost::shared_ptr<GLTexture> gltexture = boost::dynamic_pointer_cast<GLTexture>(surface->get_texture());
253   GLSurfaceData *surface_data = reinterpret_cast<GLSurfaceData *>(surface->get_surface_data());
254
255   float uv_width = surface_data->get_uv_right() - surface_data->get_uv_left();
256   float uv_height = surface_data->get_uv_bottom() - surface_data->get_uv_top();
257
258   float uv_left = surface_data->get_uv_left() + (uv_width * surfacepartrequest->source.x) / surface->get_width();
259   float uv_top = surface_data->get_uv_top() + (uv_height * surfacepartrequest->source.y) / surface->get_height();
260   float uv_right = surface_data->get_uv_left() + (uv_width * (surfacepartrequest->source.x + surfacepartrequest->size.x)) / surface->get_width();
261   float uv_bottom = surface_data->get_uv_top() + (uv_height * (surfacepartrequest->source.y + surfacepartrequest->size.y)) / surface->get_height();
262
263   glBindTexture(GL_TEXTURE_2D, gltexture->get_handle());
264   intern_draw(request.pos.x, request.pos.y,
265               request.pos.x + surfacepartrequest->size.x,
266               request.pos.y + surfacepartrequest->size.y,
267               uv_left,
268               uv_top,
269               uv_right,
270               uv_bottom,
271               0.0,
272               request.alpha,
273               Color(1.0, 1.0, 1.0),
274               Blend(),
275               request.drawing_effect);
276 }
277
278 void
279 GLLightmap::draw_gradient(const DrawingRequest& request)
280 {
281   const GradientRequest* gradientrequest 
282     = (GradientRequest*) request.request_data;
283   const Color& top = gradientrequest->top;
284   const Color& bottom = gradientrequest->bottom;
285
286   glDisable(GL_TEXTURE_2D);
287   glDisable(GL_TEXTURE_COORD_ARRAY);
288   glEnable(GL_COLOR_ARRAY);
289
290   float vertices[] = {
291     0, 0,
292     SCREEN_WIDTH, 0,
293     SCREEN_WIDTH, SCREEN_HEIGHT,
294     0, SCREEN_HEIGHT
295   };
296   glVertexPointer(2, GL_FLOAT, 0, vertices);
297
298   float colors[] = {
299     top.red, top.green, top.blue, top.alpha,
300     top.red, top.green, top.blue, top.alpha,
301     bottom.red, bottom.green, bottom.blue, bottom.alpha,
302     bottom.red, bottom.green, bottom.blue, bottom.alpha,
303   };
304   glColorPointer(4, GL_FLOAT, 0, colors);
305
306   glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
307
308   glDisable(GL_COLOR_ARRAY);
309   glEnable(GL_TEXTURE_COORD_ARRAY);
310
311   glEnable(GL_TEXTURE_2D);
312   glColor4f(1, 1, 1, 1);
313 }
314
315 void
316 GLLightmap::draw_filled_rect(const DrawingRequest& request)
317 {
318   const FillRectRequest* fillrectrequest
319     = (FillRectRequest*) request.request_data;
320
321   float x = request.pos.x;
322   float y = request.pos.y;
323   float w = fillrectrequest->size.x;
324   float h = fillrectrequest->size.y;
325
326   glDisable(GL_TEXTURE_2D);
327   glColor4f(fillrectrequest->color.red, fillrectrequest->color.green,
328             fillrectrequest->color.blue, fillrectrequest->color.alpha);
329   glDisable(GL_TEXTURE_COORD_ARRAY);
330
331   float vertices[] = {
332     x,   y,
333     x+w, y,
334     x+w, y+h,
335     x,   y+h
336   };
337   glVertexPointer(2, GL_FLOAT, 0, vertices);
338
339   glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
340
341   glEnable(GL_TEXTURE_COORD_ARRAY);
342   glEnable(GL_TEXTURE_2D);
343   glColor4f(1, 1, 1, 1);
344 }
345
346 void
347 GLLightmap::get_light(const DrawingRequest& request) const
348 {
349   const GetLightRequest* getlightrequest 
350     = (GetLightRequest*) request.request_data;
351
352   float pixels[3];
353   for( int i = 0; i<3; i++)
354     pixels[i] = 0.0f; //set to black
355
356   float posX = request.pos.x * lightmap_width / SCREEN_WIDTH;
357   float posY = screen->h - request.pos.y * lightmap_height / SCREEN_HEIGHT;
358   glReadPixels((GLint) posX, (GLint) posY , 1, 1, GL_RGB, GL_FLOAT, pixels);
359   *(getlightrequest->color_ptr) = Color( pixels[0], pixels[1], pixels[2]);
360 }
361
362 /* EOF */