2 // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
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.
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.
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/>.
17 #include "video/gl/gl_lightmap.hpp"
19 #include <SDL_image.h>
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"
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,
53 if(effect & HORIZONTAL_FLIP)
54 std::swap(uv_left, uv_right);
56 if(effect & VERTICAL_FLIP)
57 std::swap(uv_top, uv_bottom);
60 glBlendFunc(blend.sfactor, blend.dfactor);
61 glColor4f(color.red, color.green, color.blue, color.alpha * alpha);
70 glVertexPointer(2, GL_FLOAT, 0, vertices);
78 glTexCoordPointer(2, GL_FLOAT, 0, uvs);
80 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
83 float center_x = (left + right) / 2;
84 float center_y = (top + bottom) / 2;
86 float sa = sinf(angle/180.0f*M_PI);
87 float ca = cosf(angle/180.0f*M_PI);
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
101 glVertexPointer(2, GL_FLOAT, 0, vertices);
109 glTexCoordPointer(2, GL_FLOAT, 0, uvs);
111 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
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);
119 static inline int next_po2(int val)
130 GLLightmap::GLLightmap() :
138 screen = SDL_GetVideoSurface();
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);
145 lightmap = new GLTexture(width, height);
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);
152 GLLightmap::~GLLightmap()
155 texture_manager->remove_texture(lightmap);
161 GLLightmap::start_draw(const Color &ambient_color)
163 glViewport(0, screen->h - lightmap_height, lightmap_width, lightmap_height);
164 glMatrixMode(GL_PROJECTION);
166 #ifdef GL_VERSION_ES_CM_1_0
167 glOrthof(0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, -1.0, 1.0);
169 glOrtho(0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, -1.0, 1.0);
171 glMatrixMode(GL_MODELVIEW);
174 glClearColor( ambient_color.red, ambient_color.green, ambient_color.blue, 1 );
175 glClear(GL_COLOR_BUFFER_BIT);
179 GLLightmap::end_draw()
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);
185 glViewport(0, 0, screen->w, screen->h);
186 glMatrixMode(GL_PROJECTION);
188 #ifdef GL_VERSION_ES_CM_1_0
189 glOrthof(0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, -1.0, 1.0);
191 glOrtho(0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, -1.0, 1.0);
193 glMatrixMode(GL_MODELVIEW);
196 //glClear(GL_COLOR_BUFFER_BIT);
200 GLLightmap::do_draw()
202 const GLTexture* texture = lightmap;
204 // multiple the lightmap with the framebuffer
205 glBlendFunc(GL_DST_COLOR, GL_ZERO);
207 glBindTexture(GL_TEXTURE_2D, texture->get_handle());
212 SCREEN_WIDTH, SCREEN_HEIGHT,
215 glVertexPointer(2, GL_FLOAT, 0, vertices);
218 0, lightmap_uv_bottom,
219 lightmap_uv_right, lightmap_uv_bottom,
220 lightmap_uv_right, 0,
223 glTexCoordPointer(2, GL_FLOAT, 0, uvs);
225 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
227 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
231 GLLightmap::draw_surface(const DrawingRequest& request)
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());
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(),
249 request.drawing_effect);
253 GLLightmap::draw_surface_part(const DrawingRequest& request)
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());
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();
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();
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,
279 Color(1.0, 1.0, 1.0),
281 request.drawing_effect);
285 GLLightmap::draw_gradient(const DrawingRequest& request)
287 const GradientRequest* gradientrequest
288 = (GradientRequest*) request.request_data;
289 const Color& top = gradientrequest->top;
290 const Color& bottom = gradientrequest->bottom;
292 glDisable(GL_TEXTURE_2D);
293 glDisable(GL_TEXTURE_COORD_ARRAY);
294 glEnable(GL_COLOR_ARRAY);
299 SCREEN_WIDTH, SCREEN_HEIGHT,
302 glVertexPointer(2, GL_FLOAT, 0, vertices);
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,
310 glColorPointer(4, GL_FLOAT, 0, colors);
312 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
314 glDisable(GL_COLOR_ARRAY);
315 glEnable(GL_TEXTURE_COORD_ARRAY);
317 glEnable(GL_TEXTURE_2D);
318 glColor4f(1, 1, 1, 1);
322 GLLightmap::draw_filled_rect(const DrawingRequest& request)
324 const FillRectRequest* fillrectrequest
325 = (FillRectRequest*) request.request_data;
327 float x = request.pos.x;
328 float y = request.pos.y;
329 float w = fillrectrequest->size.x;
330 float h = fillrectrequest->size.y;
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);
343 glVertexPointer(2, GL_FLOAT, 0, vertices);
345 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
347 glEnable(GL_TEXTURE_COORD_ARRAY);
348 glEnable(GL_TEXTURE_2D);
349 glColor4f(1, 1, 1, 1);
353 GLLightmap::get_light(const DrawingRequest& request) const
355 const GetLightRequest* getlightrequest
356 = (GetLightRequest*) request.request_data;
359 for( int i = 0; i<3; i++)
360 pixels[i] = 0.0f; //set to black
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]);