Precalculated renderer specific surface data, better management of multiple renderers...
[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 #define LIGHTMAP_DIV 5
47
48 namespace
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     if(effect & VERTICAL_FLIP) {
61       std::swap(uv_top, uv_bottom);
62     }
63
64     float center_x = (left + right) / 2;
65     float center_y = (top + bottom) / 2;
66
67     float sa = sinf(angle/180.0f*M_PI);
68     float ca = cosf(angle/180.0f*M_PI);
69
70     left  -= center_x;
71     right -= center_x;
72
73     top    -= center_y;
74     bottom -= center_y;
75
76     glBlendFunc(blend.sfactor, blend.dfactor);
77     glColor4f(color.red, color.green, color.blue, color.alpha * alpha);
78     glBegin(GL_QUADS);
79     glTexCoord2f(uv_left, uv_top);
80     glVertex2f(left*ca - top*sa + center_x,
81                left*sa + top*ca + center_y);
82
83     glTexCoord2f(uv_right, uv_top);
84     glVertex2f(right*ca - top*sa + center_x,
85                right*sa + top*ca + center_y);
86
87     glTexCoord2f(uv_right, uv_bottom);
88     glVertex2f(right*ca - bottom*sa + center_x,
89                right*sa + bottom*ca + center_y);
90
91     glTexCoord2f(uv_left, uv_bottom);
92     glVertex2f(left*ca - bottom*sa + center_x,
93                left*sa + bottom*ca + center_y);
94     glEnd();
95
96     // FIXME: find a better way to restore the blend mode
97     glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
98     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
99   }
100 }
101
102 namespace GL
103 {
104   static inline int next_po2(int val)
105   {
106     int result = 1;
107     while(result < val)
108       result *= 2;
109
110     return result;
111   }
112
113   Lightmap::Lightmap()
114   {
115     screen = SDL_GetVideoSurface();
116
117     lightmap_width = screen->w / LIGHTMAP_DIV;
118     lightmap_height = screen->h / LIGHTMAP_DIV;
119     unsigned int width = next_po2(lightmap_width);
120     unsigned int height = next_po2(lightmap_height);
121
122     lightmap = new Texture(width, height);
123
124     lightmap_uv_right = static_cast<float>(lightmap_width) / static_cast<float>(width);
125     lightmap_uv_bottom = static_cast<float>(lightmap_height) / static_cast<float>(height);
126     texture_manager->register_texture(lightmap);
127   }
128
129   Lightmap::~Lightmap()
130   {
131     texture_manager->remove_texture(lightmap);
132     delete lightmap;
133   }
134
135   void
136   Lightmap::start_draw(const Color &ambient_color)
137   {
138     glViewport(0, screen->h - lightmap_height, lightmap_width, lightmap_height);
139     glMatrixMode(GL_PROJECTION);
140     glLoadIdentity();
141     glOrtho(0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, -1.0, 1.0);
142     glMatrixMode(GL_MODELVIEW);
143     glLoadIdentity();
144
145     glClearColor( ambient_color.red, ambient_color.green, ambient_color.blue, 1 );
146     glClear(GL_COLOR_BUFFER_BIT);
147   }
148
149   void
150   Lightmap::end_draw()
151   {
152     glDisable(GL_BLEND);
153     glBindTexture(GL_TEXTURE_2D, lightmap->get_handle());
154     glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, screen->h - lightmap_height, lightmap_width, lightmap_height);
155
156     glViewport(0, 0, screen->w, screen->h);
157     glMatrixMode(GL_PROJECTION);
158     glLoadIdentity();
159     glOrtho(0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, -1.0, 1.0);
160     glMatrixMode(GL_MODELVIEW);
161     glLoadIdentity();
162     glEnable(GL_BLEND);
163     //glClear(GL_COLOR_BUFFER_BIT);
164   }
165
166   void
167   Lightmap::do_draw()
168   {
169     const Texture* texture = lightmap;
170
171     // multiple the lightmap with the framebuffer
172     glBlendFunc(GL_DST_COLOR, GL_ZERO);
173
174     glBindTexture(GL_TEXTURE_2D, texture->get_handle());
175     glBegin(GL_QUADS);
176
177     glTexCoord2f(0, lightmap_uv_bottom);
178     glVertex2f(0, 0);
179
180     glTexCoord2f(lightmap_uv_right, lightmap_uv_bottom);
181     glVertex2f(SCREEN_WIDTH, 0);
182
183     glTexCoord2f(lightmap_uv_right, 0);
184     glVertex2f(SCREEN_WIDTH, SCREEN_HEIGHT);
185
186     glTexCoord2f(0, 0);
187     glVertex2f(0, SCREEN_HEIGHT);
188
189     glEnd();
190
191     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
192   }
193
194   void
195   Lightmap::draw_surface(const DrawingRequest& request)
196   {
197     const Surface* surface = (const Surface*) request.request_data;
198     GL::Texture *gltexture = dynamic_cast<GL::Texture *>(surface->get_texture());
199     GL::SurfaceData *surface_data = reinterpret_cast<GL::SurfaceData *>(surface->get_surface_data());
200
201     glBindTexture(GL_TEXTURE_2D, gltexture->get_handle());
202     intern_draw(request.pos.x, request.pos.y,
203                 request.pos.x + surface->get_width(),
204                 request.pos.y + surface->get_height(),
205                 surface_data->get_uv_left(),
206                 surface_data->get_uv_top(),
207                 surface_data->get_uv_right(),
208                 surface_data->get_uv_bottom(),
209                 request.angle,
210                 request.alpha,
211                 request.color,
212                 request.blend,
213                 request.drawing_effect);
214   }
215
216   void
217   Lightmap::draw_surface_part(const DrawingRequest& request)
218   {
219     const SurfacePartRequest* surfacepartrequest
220       = (SurfacePartRequest*) request.request_data;
221     const Surface *surface = surfacepartrequest->surface;
222     GL::Texture *gltexture = dynamic_cast<GL::Texture *>(surface->get_texture());
223     GL::SurfaceData *surface_data = reinterpret_cast<GL::SurfaceData *>(surface->get_surface_data());
224
225     float uv_width = surface_data->get_uv_right() - surface_data->get_uv_left();
226     float uv_height = surface_data->get_uv_bottom() - surface_data->get_uv_top();
227
228     float uv_left = surface_data->get_uv_left() + (uv_width * surfacepartrequest->source.x) / surface->get_width();
229     float uv_top = surface_data->get_uv_top() + (uv_height * surfacepartrequest->source.y) / surface->get_height();
230     float uv_right = surface_data->get_uv_left() + (uv_width * (surfacepartrequest->source.x + surfacepartrequest->size.x)) / surface->get_width();
231     float uv_bottom = surface_data->get_uv_top() + (uv_height * (surfacepartrequest->source.y + surfacepartrequest->size.y)) / surface->get_height();
232
233     glBindTexture(GL_TEXTURE_2D, gltexture->get_handle());
234     intern_draw(request.pos.x, request.pos.y,
235                 request.pos.x + surfacepartrequest->size.x,
236                 request.pos.y + surfacepartrequest->size.y,
237                 uv_left,
238                 uv_top,
239                 uv_right,
240                 uv_bottom,
241                 0.0,
242                 request.alpha,
243                 Color(1.0, 1.0, 1.0),
244                 Blend(),
245                 request.drawing_effect);
246   }
247
248   void
249   Lightmap::draw_gradient(const DrawingRequest& request)
250   {
251     const GradientRequest* gradientrequest 
252       = (GradientRequest*) request.request_data;
253     const Color& top = gradientrequest->top;
254     const Color& bottom = gradientrequest->bottom;
255
256     glDisable(GL_TEXTURE_2D);
257     glBegin(GL_QUADS);
258     glColor4f(top.red, top.green, top.blue, top.alpha);
259     glVertex2f(0, 0);
260     glVertex2f(SCREEN_WIDTH, 0);
261     glColor4f(bottom.red, bottom.green, bottom.blue, bottom.alpha);
262     glVertex2f(SCREEN_WIDTH, SCREEN_HEIGHT);
263     glVertex2f(0, SCREEN_HEIGHT);
264     glEnd();
265     glEnable(GL_TEXTURE_2D);
266     glColor4f(1, 1, 1, 1);
267   }
268
269   void
270   Lightmap::draw_text(const DrawingRequest& /*request*/)
271   {
272     //const TextRequest* textrequest = (TextRequest*) request.request_data;
273
274     //textrequest->font->draw(textrequest->text, request.pos,
275     //    textrequest->alignment, request.drawing_effect, request.alpha);
276   }
277
278   void
279   Lightmap::draw_filled_rect(const DrawingRequest& request)
280   {
281     const FillRectRequest* fillrectrequest
282       = (FillRectRequest*) request.request_data;
283
284     float x = request.pos.x;
285     float y = request.pos.y;
286     float w = fillrectrequest->size.x;
287     float h = fillrectrequest->size.y;
288
289     glDisable(GL_TEXTURE_2D);
290     glColor4f(fillrectrequest->color.red, fillrectrequest->color.green,
291               fillrectrequest->color.blue, fillrectrequest->color.alpha);
292
293     glBegin(GL_QUADS);
294     glVertex2f(x, y);
295     glVertex2f(x+w, y);
296     glVertex2f(x+w, y+h);
297     glVertex2f(x, y+h);
298     glEnd();
299     glEnable(GL_TEXTURE_2D);
300     glColor4f(1, 1, 1, 1);
301   }
302
303   void
304   Lightmap::get_light(const DrawingRequest& request) const
305   {
306     const GetLightRequest* getlightrequest 
307       = (GetLightRequest*) request.request_data;
308
309     float pixels[3];
310     for( int i = 0; i<3; i++)
311       pixels[i] = 0.0f; //set to black
312
313     float posX = request.pos.x * lightmap_width / SCREEN_WIDTH;
314     float posY = screen->h - request.pos.y * lightmap_height / SCREEN_HEIGHT;
315     glReadPixels((GLint) posX, (GLint) posY , 1, 1, GL_RGB, GL_FLOAT, pixels);
316     *(getlightrequest->color_ptr) = Color( pixels[0], pixels[1], pixels[2]);
317     //printf("get_light %f/%f =>%f/%f r%f g%f b%f\n", request.pos.x, request.pos.y, posX, posY, pixels[0], pixels[1], pixels[2]);
318   }
319 }
320
321 #endif