1 // $Id: gl_lightmap.cpp 5063 2007-05-27 11:32:00Z matzeb $
4 // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
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.
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.
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.
28 #include <SDL_image.h>
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"
42 #include "gameconfig.hpp"
43 #include "gl_texture.hpp"
44 #include "texture_manager.hpp"
45 #include "obstack/obstackpp.hpp"
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,
58 if(effect & HORIZONTAL_FLIP)
59 std::swap(uv_left, uv_right);
61 if(effect & VERTICAL_FLIP)
62 std::swap(uv_top, uv_bottom);
65 glBlendFunc(blend.sfactor, blend.dfactor);
66 glColor4f(color.red, color.green, color.blue, color.alpha * alpha);
75 glVertexPointer(2, GL_FLOAT, 0, vertices);
83 glTexCoordPointer(2, GL_FLOAT, 0, uvs);
85 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
88 float center_x = (left + right) / 2;
89 float center_y = (top + bottom) / 2;
91 float sa = sinf(angle/180.0f*M_PI);
92 float ca = cosf(angle/180.0f*M_PI);
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
106 glVertexPointer(2, GL_FLOAT, 0, vertices);
114 glTexCoordPointer(2, GL_FLOAT, 0, uvs);
116 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
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);
128 static inline int next_po2(int val)
139 screen = SDL_GetVideoSurface();
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);
146 lightmap = new Texture(width, height);
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);
153 Lightmap::~Lightmap()
155 texture_manager->remove_texture(lightmap);
160 Lightmap::start_draw(const Color &ambient_color)
162 glViewport(0, screen->h - lightmap_height, lightmap_width, lightmap_height);
163 glMatrixMode(GL_PROJECTION);
165 glOrtho(0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, -1.0, 1.0);
166 glMatrixMode(GL_MODELVIEW);
169 glClearColor( ambient_color.red, ambient_color.green, ambient_color.blue, 1 );
170 glClear(GL_COLOR_BUFFER_BIT);
177 glBindTexture(GL_TEXTURE_2D, lightmap->get_handle());
178 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, screen->h - lightmap_height, lightmap_width, lightmap_height);
180 glViewport(0, 0, screen->w, screen->h);
181 glMatrixMode(GL_PROJECTION);
183 glOrtho(0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, -1.0, 1.0);
184 glMatrixMode(GL_MODELVIEW);
187 //glClear(GL_COLOR_BUFFER_BIT);
193 const Texture* texture = lightmap;
195 // multiple the lightmap with the framebuffer
196 glBlendFunc(GL_DST_COLOR, GL_ZERO);
198 glBindTexture(GL_TEXTURE_2D, texture->get_handle());
201 glTexCoord2f(0, lightmap_uv_bottom);
204 glTexCoord2f(lightmap_uv_right, lightmap_uv_bottom);
205 glVertex2f(SCREEN_WIDTH, 0);
207 glTexCoord2f(lightmap_uv_right, 0);
208 glVertex2f(SCREEN_WIDTH, SCREEN_HEIGHT);
211 glVertex2f(0, SCREEN_HEIGHT);
215 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
219 Lightmap::draw_surface(const DrawingRequest& request)
221 const Surface* surface = (const Surface*) request.request_data;
222 GL::Texture *gltexture = dynamic_cast<GL::Texture *>(surface->get_texture());
223 GL::SurfaceData *surface_data = reinterpret_cast<GL::SurfaceData *>(surface->get_surface_data());
225 glBindTexture(GL_TEXTURE_2D, gltexture->get_handle());
226 intern_draw(request.pos.x, request.pos.y,
227 request.pos.x + surface->get_width(),
228 request.pos.y + surface->get_height(),
229 surface_data->get_uv_left(),
230 surface_data->get_uv_top(),
231 surface_data->get_uv_right(),
232 surface_data->get_uv_bottom(),
237 request.drawing_effect);
241 Lightmap::draw_surface_part(const DrawingRequest& request)
243 const SurfacePartRequest* surfacepartrequest
244 = (SurfacePartRequest*) request.request_data;
245 const Surface *surface = surfacepartrequest->surface;
246 GL::Texture *gltexture = dynamic_cast<GL::Texture *>(surface->get_texture());
247 GL::SurfaceData *surface_data = reinterpret_cast<GL::SurfaceData *>(surface->get_surface_data());
249 float uv_width = surface_data->get_uv_right() - surface_data->get_uv_left();
250 float uv_height = surface_data->get_uv_bottom() - surface_data->get_uv_top();
252 float uv_left = surface_data->get_uv_left() + (uv_width * surfacepartrequest->source.x) / surface->get_width();
253 float uv_top = surface_data->get_uv_top() + (uv_height * surfacepartrequest->source.y) / surface->get_height();
254 float uv_right = surface_data->get_uv_left() + (uv_width * (surfacepartrequest->source.x + surfacepartrequest->size.x)) / surface->get_width();
255 float uv_bottom = surface_data->get_uv_top() + (uv_height * (surfacepartrequest->source.y + surfacepartrequest->size.y)) / surface->get_height();
257 glBindTexture(GL_TEXTURE_2D, gltexture->get_handle());
258 intern_draw(request.pos.x, request.pos.y,
259 request.pos.x + surfacepartrequest->size.x,
260 request.pos.y + surfacepartrequest->size.y,
267 Color(1.0, 1.0, 1.0),
269 request.drawing_effect);
273 Lightmap::draw_gradient(const DrawingRequest& request)
275 const GradientRequest* gradientrequest
276 = (GradientRequest*) request.request_data;
277 const Color& top = gradientrequest->top;
278 const Color& bottom = gradientrequest->bottom;
280 glDisable(GL_TEXTURE_2D);
281 glDisable(GL_TEXTURE_COORD_ARRAY);
282 glEnable(GL_COLOR_ARRAY);
287 SCREEN_WIDTH, SCREEN_HEIGHT,
290 glVertexPointer(2, GL_FLOAT, 0, vertices);
293 top.red, top.green, top.blue, top.alpha,
294 top.red, top.green, top.blue, top.alpha,
295 bottom.red, bottom.green, bottom.blue, bottom.alpha,
296 bottom.red, bottom.green, bottom.blue, bottom.alpha,
298 glColorPointer(4, GL_FLOAT, 0, colors);
300 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
302 glDisable(GL_COLOR_ARRAY);
303 glEnable(GL_TEXTURE_COORD_ARRAY);
305 glEnable(GL_TEXTURE_2D);
306 glColor4f(1, 1, 1, 1);
310 Lightmap::draw_filled_rect(const DrawingRequest& request)
312 const FillRectRequest* fillrectrequest
313 = (FillRectRequest*) request.request_data;
315 float x = request.pos.x;
316 float y = request.pos.y;
317 float w = fillrectrequest->size.x;
318 float h = fillrectrequest->size.y;
320 glDisable(GL_TEXTURE_2D);
321 glColor4f(fillrectrequest->color.red, fillrectrequest->color.green,
322 fillrectrequest->color.blue, fillrectrequest->color.alpha);
323 glDisable(GL_TEXTURE_COORD_ARRAY);
331 glVertexPointer(2, GL_FLOAT, 0, vertices);
333 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
335 glEnable(GL_TEXTURE_COORD_ARRAY);
336 glEnable(GL_TEXTURE_2D);
337 glColor4f(1, 1, 1, 1);
341 Lightmap::get_light(const DrawingRequest& request) const
343 const GetLightRequest* getlightrequest
344 = (GetLightRequest*) request.request_data;
347 for( int i = 0; i<3; i++)
348 pixels[i] = 0.0f; //set to black
350 float posX = request.pos.x * lightmap_width / SCREEN_WIDTH;
351 float posY = screen->h - request.pos.y * lightmap_height / SCREEN_HEIGHT;
352 glReadPixels((GLint) posX, (GLint) posY , 1, 1, GL_RGB, GL_FLOAT, pixels);
353 *(getlightrequest->color_ptr) = Color( pixels[0], pixels[1], pixels[2]);