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"
46 #define LIGHTMAP_DIV 5
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);
60 if(effect & VERTICAL_FLIP) {
61 std::swap(uv_top, uv_bottom);
64 float center_x = (left + right) / 2;
65 float center_y = (top + bottom) / 2;
67 float sa = sinf(angle/180.0f*M_PI);
68 float ca = cosf(angle/180.0f*M_PI);
76 glBlendFunc(blend.sfactor, blend.dfactor);
77 glColor4f(color.red, color.green, color.blue, color.alpha * alpha);
79 glTexCoord2f(uv_left, uv_top);
80 glVertex2f(left*ca - top*sa + center_x,
81 left*sa + top*ca + center_y);
83 glTexCoord2f(uv_right, uv_top);
84 glVertex2f(right*ca - top*sa + center_x,
85 right*sa + top*ca + center_y);
87 glTexCoord2f(uv_right, uv_bottom);
88 glVertex2f(right*ca - bottom*sa + center_x,
89 right*sa + bottom*ca + center_y);
91 glTexCoord2f(uv_left, uv_bottom);
92 glVertex2f(left*ca - bottom*sa + center_x,
93 left*sa + bottom*ca + center_y);
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);
104 static inline int next_po2(int val)
115 screen = SDL_GetVideoSurface();
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);
122 lightmap = new Texture(width, height);
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);
129 Lightmap::~Lightmap()
131 texture_manager->remove_texture(lightmap);
136 Lightmap::start_draw(const Color &ambient_color)
138 glViewport(0, screen->h - lightmap_height, lightmap_width, lightmap_height);
139 glMatrixMode(GL_PROJECTION);
141 glOrtho(0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, -1.0, 1.0);
142 glMatrixMode(GL_MODELVIEW);
145 glClearColor( ambient_color.red, ambient_color.green, ambient_color.blue, 1 );
146 glClear(GL_COLOR_BUFFER_BIT);
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);
156 glViewport(0, 0, screen->w, screen->h);
157 glMatrixMode(GL_PROJECTION);
159 glOrtho(0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, -1.0, 1.0);
160 glMatrixMode(GL_MODELVIEW);
163 //glClear(GL_COLOR_BUFFER_BIT);
169 const Texture* texture = lightmap;
171 // multiple the lightmap with the framebuffer
172 glBlendFunc(GL_DST_COLOR, GL_ZERO);
174 glBindTexture(GL_TEXTURE_2D, texture->get_handle());
177 glTexCoord2f(0, lightmap_uv_bottom);
180 glTexCoord2f(lightmap_uv_right, lightmap_uv_bottom);
181 glVertex2f(SCREEN_WIDTH, 0);
183 glTexCoord2f(lightmap_uv_right, 0);
184 glVertex2f(SCREEN_WIDTH, SCREEN_HEIGHT);
187 glVertex2f(0, SCREEN_HEIGHT);
191 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
195 Lightmap::draw_surface(const DrawingRequest& request)
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());
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(),
213 request.drawing_effect);
217 Lightmap::draw_surface_part(const DrawingRequest& request)
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());
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();
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();
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,
243 Color(1.0, 1.0, 1.0),
245 request.drawing_effect);
249 Lightmap::draw_gradient(const DrawingRequest& request)
251 const GradientRequest* gradientrequest
252 = (GradientRequest*) request.request_data;
253 const Color& top = gradientrequest->top;
254 const Color& bottom = gradientrequest->bottom;
256 glDisable(GL_TEXTURE_2D);
258 glColor4f(top.red, top.green, top.blue, top.alpha);
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);
265 glEnable(GL_TEXTURE_2D);
266 glColor4f(1, 1, 1, 1);
270 Lightmap::draw_text(const DrawingRequest& /*request*/)
272 //const TextRequest* textrequest = (TextRequest*) request.request_data;
274 //textrequest->font->draw(textrequest->text, request.pos,
275 // textrequest->alignment, request.drawing_effect, request.alpha);
279 Lightmap::draw_filled_rect(const DrawingRequest& request)
281 const FillRectRequest* fillrectrequest
282 = (FillRectRequest*) request.request_data;
284 float x = request.pos.x;
285 float y = request.pos.y;
286 float w = fillrectrequest->size.x;
287 float h = fillrectrequest->size.y;
289 glDisable(GL_TEXTURE_2D);
290 glColor4f(fillrectrequest->color.red, fillrectrequest->color.green,
291 fillrectrequest->color.blue, fillrectrequest->color.alpha);
296 glVertex2f(x+w, y+h);
299 glEnable(GL_TEXTURE_2D);
300 glColor4f(1, 1, 1, 1);
304 Lightmap::get_light(const DrawingRequest& request) const
306 const GetLightRequest* getlightrequest
307 = (GetLightRequest*) request.request_data;
310 for( int i = 0; i<3; i++)
311 pixels[i] = 0.0f; //set to black
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]);