Replaced <c*> headers with <*.h>
[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 "obstack/obstackpp.hpp"
30 #include "supertux/gameconfig.hpp"
31 #include "supertux/main.hpp"
32 #include "video/lightmap.hpp"
33 #include "video/drawing_context.hpp"
34 #include "video/drawing_request.hpp"
35 #include "video/font.hpp"
36 #include "video/gl/gl_surface_data.hpp"
37 #include "video/gl/gl_texture.hpp"
38 #include "video/glutil.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 = 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);
150 }
151
152 GLLightmap::~GLLightmap()
153 {
154   if(texture_manager){
155     texture_manager->remove_texture(lightmap);
156   }
157   delete lightmap;
158 }
159
160 void
161 GLLightmap::start_draw(const Color &ambient_color)
162 {
163   glViewport(0, screen->h - lightmap_height, lightmap_width, lightmap_height);
164   glMatrixMode(GL_PROJECTION);
165   glLoadIdentity();
166 #ifdef GL_VERSION_ES_CM_1_0
167   glOrthof(0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, -1.0, 1.0);
168 #else
169   glOrtho(0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, -1.0, 1.0);
170 #endif
171   glMatrixMode(GL_MODELVIEW);
172   glLoadIdentity();
173
174   glClearColor( ambient_color.red, ambient_color.green, ambient_color.blue, 1 );
175   glClear(GL_COLOR_BUFFER_BIT);
176 }
177
178 void
179 GLLightmap::end_draw()
180 {
181   glDisable(GL_BLEND);
182   glBindTexture(GL_TEXTURE_2D, lightmap->get_handle());
183   glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, screen->h - lightmap_height, lightmap_width, lightmap_height);
184
185   glViewport(0, 0, screen->w, screen->h);
186   glMatrixMode(GL_PROJECTION);
187   glLoadIdentity();
188 #ifdef GL_VERSION_ES_CM_1_0
189   glOrthof(0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, -1.0, 1.0);
190 #else
191   glOrtho(0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, -1.0, 1.0);
192 #endif
193   glMatrixMode(GL_MODELVIEW);
194   glLoadIdentity();
195   glEnable(GL_BLEND);
196   //glClear(GL_COLOR_BUFFER_BIT);
197 }
198
199 void
200 GLLightmap::do_draw()
201 {
202   const GLTexture* texture = lightmap;
203
204   // multiple the lightmap with the framebuffer
205   glBlendFunc(GL_DST_COLOR, GL_ZERO);
206
207   glBindTexture(GL_TEXTURE_2D, texture->get_handle());
208
209   float vertices[] = {
210     0, 0,
211     SCREEN_WIDTH, 0,
212     SCREEN_WIDTH, SCREEN_HEIGHT,
213     0, SCREEN_HEIGHT
214   };
215   glVertexPointer(2, GL_FLOAT, 0, vertices);
216
217   float uvs[] = {
218     0,                 lightmap_uv_bottom,
219     lightmap_uv_right, lightmap_uv_bottom,
220     lightmap_uv_right, 0,
221     0, 0
222   };
223   glTexCoordPointer(2, GL_FLOAT, 0, uvs);
224
225   glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
226
227   glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
228 }
229
230 void
231 GLLightmap::draw_surface(const DrawingRequest& request)
232 {
233   const Surface* surface = (const Surface*) request.request_data;
234   GLTexture *gltexture = dynamic_cast<GLTexture *>(surface->get_texture());
235   GLSurfaceData *surface_data = reinterpret_cast<GLSurfaceData *>(surface->get_surface_data());
236
237   glBindTexture(GL_TEXTURE_2D, gltexture->get_handle());
238   intern_draw(request.pos.x, request.pos.y,
239               request.pos.x + surface->get_width(),
240               request.pos.y + surface->get_height(),
241               surface_data->get_uv_left(),
242               surface_data->get_uv_top(),
243               surface_data->get_uv_right(),
244               surface_data->get_uv_bottom(),
245               request.angle,
246               request.alpha,
247               request.color,
248               request.blend,
249               request.drawing_effect);
250 }
251
252 void
253 GLLightmap::draw_surface_part(const DrawingRequest& request)
254 {
255   const SurfacePartRequest* surfacepartrequest
256     = (SurfacePartRequest*) request.request_data;
257   const Surface *surface = surfacepartrequest->surface;
258   GLTexture *gltexture = dynamic_cast<GLTexture *>(surface->get_texture());
259   GLSurfaceData *surface_data = reinterpret_cast<GLSurfaceData *>(surface->get_surface_data());
260
261   float uv_width = surface_data->get_uv_right() - surface_data->get_uv_left();
262   float uv_height = surface_data->get_uv_bottom() - surface_data->get_uv_top();
263
264   float uv_left = surface_data->get_uv_left() + (uv_width * surfacepartrequest->source.x) / surface->get_width();
265   float uv_top = surface_data->get_uv_top() + (uv_height * surfacepartrequest->source.y) / surface->get_height();
266   float uv_right = surface_data->get_uv_left() + (uv_width * (surfacepartrequest->source.x + surfacepartrequest->size.x)) / surface->get_width();
267   float uv_bottom = surface_data->get_uv_top() + (uv_height * (surfacepartrequest->source.y + surfacepartrequest->size.y)) / surface->get_height();
268
269   glBindTexture(GL_TEXTURE_2D, gltexture->get_handle());
270   intern_draw(request.pos.x, request.pos.y,
271               request.pos.x + surfacepartrequest->size.x,
272               request.pos.y + surfacepartrequest->size.y,
273               uv_left,
274               uv_top,
275               uv_right,
276               uv_bottom,
277               0.0,
278               request.alpha,
279               Color(1.0, 1.0, 1.0),
280               Blend(),
281               request.drawing_effect);
282 }
283
284 void
285 GLLightmap::draw_gradient(const DrawingRequest& request)
286 {
287   const GradientRequest* gradientrequest 
288     = (GradientRequest*) request.request_data;
289   const Color& top = gradientrequest->top;
290   const Color& bottom = gradientrequest->bottom;
291
292   glDisable(GL_TEXTURE_2D);
293   glDisable(GL_TEXTURE_COORD_ARRAY);
294   glEnable(GL_COLOR_ARRAY);
295
296   float vertices[] = {
297     0, 0,
298     SCREEN_WIDTH, 0,
299     SCREEN_WIDTH, SCREEN_HEIGHT,
300     0, SCREEN_HEIGHT
301   };
302   glVertexPointer(2, GL_FLOAT, 0, vertices);
303
304   float colors[] = {
305     top.red, top.green, top.blue, top.alpha,
306     top.red, top.green, top.blue, top.alpha,
307     bottom.red, bottom.green, bottom.blue, bottom.alpha,
308     bottom.red, bottom.green, bottom.blue, bottom.alpha,
309   };
310   glColorPointer(4, GL_FLOAT, 0, colors);
311
312   glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
313
314   glDisable(GL_COLOR_ARRAY);
315   glEnable(GL_TEXTURE_COORD_ARRAY);
316
317   glEnable(GL_TEXTURE_2D);
318   glColor4f(1, 1, 1, 1);
319 }
320
321 void
322 GLLightmap::draw_filled_rect(const DrawingRequest& request)
323 {
324   const FillRectRequest* fillrectrequest
325     = (FillRectRequest*) request.request_data;
326
327   float x = request.pos.x;
328   float y = request.pos.y;
329   float w = fillrectrequest->size.x;
330   float h = fillrectrequest->size.y;
331
332   glDisable(GL_TEXTURE_2D);
333   glColor4f(fillrectrequest->color.red, fillrectrequest->color.green,
334             fillrectrequest->color.blue, fillrectrequest->color.alpha);
335   glDisable(GL_TEXTURE_COORD_ARRAY);
336
337   float vertices[] = {
338     x,   y,
339     x+w, y,
340     x+w, y+h,
341     x,   y+h
342   };
343   glVertexPointer(2, GL_FLOAT, 0, vertices);
344
345   glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
346
347   glEnable(GL_TEXTURE_COORD_ARRAY);
348   glEnable(GL_TEXTURE_2D);
349   glColor4f(1, 1, 1, 1);
350 }
351
352 void
353 GLLightmap::get_light(const DrawingRequest& request) const
354 {
355   const GetLightRequest* getlightrequest 
356     = (GetLightRequest*) request.request_data;
357
358   float pixels[3];
359   for( int i = 0; i<3; i++)
360     pixels[i] = 0.0f; //set to black
361
362   float posX = request.pos.x * lightmap_width / SCREEN_WIDTH;
363   float posY = screen->h - request.pos.y * lightmap_height / SCREEN_HEIGHT;
364   glReadPixels((GLint) posX, (GLint) posY , 1, 1, GL_RGB, GL_FLOAT, pixels);
365   *(getlightrequest->color_ptr) = Color( pixels[0], pixels[1], pixels[2]);
366 }
367
368 /* EOF */