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