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 "supertux/gameconfig.hpp"
30 #include "supertux/globals.hpp"
31 #include "util/obstackpp.hpp"
32 #include "video/drawing_context.hpp"
33 #include "video/drawing_request.hpp"
34 #include "video/font.hpp"
35 #include "video/gl/gl_surface_data.hpp"
36 #include "video/gl/gl_renderer.hpp"
37 #include "video/gl/gl_texture.hpp"
38 #include "video/glutil.hpp"
39 #include "video/lightmap.hpp"
40 #include "video/renderer.hpp"
41 #include "video/surface.hpp"
42 #include "video/texture_manager.hpp"
44 GLLightmap::GLLightmap() :
52 screen = SDL_GetVideoSurface();
54 lightmap_width = screen->w / LIGHTMAP_DIV;
55 lightmap_height = screen->h / LIGHTMAP_DIV;
56 unsigned int width = next_po2(lightmap_width);
57 unsigned int height = next_po2(lightmap_height);
59 lightmap.reset(new GLTexture(width, height));
61 lightmap_uv_right = static_cast<float>(lightmap_width) / static_cast<float>(width);
62 lightmap_uv_bottom = static_cast<float>(lightmap_height) / static_cast<float>(height);
63 texture_manager->register_texture(lightmap.get());
66 GLLightmap::~GLLightmap()
71 GLLightmap::start_draw(const Color &ambient_color)
73 glViewport(0, screen->h - lightmap_height, lightmap_width, lightmap_height);
74 glMatrixMode(GL_PROJECTION);
76 #ifdef GL_VERSION_ES_CM_1_0
77 glOrthof(0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, -1.0, 1.0);
79 glOrtho(0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, -1.0, 1.0);
81 glMatrixMode(GL_MODELVIEW);
84 glClearColor( ambient_color.red, ambient_color.green, ambient_color.blue, 1 );
85 glClear(GL_COLOR_BUFFER_BIT);
89 GLLightmap::end_draw()
92 glBindTexture(GL_TEXTURE_2D, lightmap->get_handle());
93 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, screen->h - lightmap_height, lightmap_width, lightmap_height);
95 glViewport(0, 0, screen->w, screen->h);
96 glMatrixMode(GL_PROJECTION);
98 #ifdef GL_VERSION_ES_CM_1_0
99 glOrthof(0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, -1.0, 1.0);
101 glOrtho(0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, -1.0, 1.0);
103 glMatrixMode(GL_MODELVIEW);
106 //glClear(GL_COLOR_BUFFER_BIT);
110 GLLightmap::do_draw()
112 // multiple the lightmap with the framebuffer
113 glBlendFunc(GL_DST_COLOR, GL_ZERO);
115 glBindTexture(GL_TEXTURE_2D, lightmap->get_handle());
120 SCREEN_WIDTH, SCREEN_HEIGHT,
123 glVertexPointer(2, GL_FLOAT, 0, vertices);
126 0, lightmap_uv_bottom,
127 lightmap_uv_right, lightmap_uv_bottom,
128 lightmap_uv_right, 0,
131 glTexCoordPointer(2, GL_FLOAT, 0, uvs);
133 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
135 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
139 GLLightmap::draw_surface(const DrawingRequest& request)
141 const Surface* surface = (const Surface*) request.request_data;
142 boost::shared_ptr<GLTexture> gltexture = boost::dynamic_pointer_cast<GLTexture>(surface->get_texture());
143 GLSurfaceData *surface_data = reinterpret_cast<GLSurfaceData *>(surface->get_surface_data());
145 glBindTexture(GL_TEXTURE_2D, gltexture->get_handle());
146 intern_draw(request.pos.x, request.pos.y,
147 request.pos.x + surface->get_width(),
148 request.pos.y + surface->get_height(),
149 surface_data->get_uv_left(),
150 surface_data->get_uv_top(),
151 surface_data->get_uv_right(),
152 surface_data->get_uv_bottom(),
157 request.drawing_effect);
161 GLLightmap::draw_surface_part(const DrawingRequest& request)
163 const SurfacePartRequest* surfacepartrequest
164 = (SurfacePartRequest*) request.request_data;
165 const Surface* surface = surfacepartrequest->surface;
166 boost::shared_ptr<GLTexture> gltexture = boost::dynamic_pointer_cast<GLTexture>(surface->get_texture());
167 GLSurfaceData *surface_data = reinterpret_cast<GLSurfaceData *>(surface->get_surface_data());
169 float uv_width = surface_data->get_uv_right() - surface_data->get_uv_left();
170 float uv_height = surface_data->get_uv_bottom() - surface_data->get_uv_top();
172 float uv_left = surface_data->get_uv_left() + (uv_width * surfacepartrequest->source.x) / surface->get_width();
173 float uv_top = surface_data->get_uv_top() + (uv_height * surfacepartrequest->source.y) / surface->get_height();
174 float uv_right = surface_data->get_uv_left() + (uv_width * (surfacepartrequest->source.x + surfacepartrequest->size.x)) / surface->get_width();
175 float uv_bottom = surface_data->get_uv_top() + (uv_height * (surfacepartrequest->source.y + surfacepartrequest->size.y)) / surface->get_height();
177 glBindTexture(GL_TEXTURE_2D, gltexture->get_handle());
178 intern_draw(request.pos.x, request.pos.y,
179 request.pos.x + surfacepartrequest->size.x,
180 request.pos.y + surfacepartrequest->size.y,
187 Color(1.0, 1.0, 1.0),
189 request.drawing_effect);
193 GLLightmap::draw_gradient(const DrawingRequest& request)
195 const GradientRequest* gradientrequest
196 = (GradientRequest*) request.request_data;
197 const Color& top = gradientrequest->top;
198 const Color& bottom = gradientrequest->bottom;
200 glDisable(GL_TEXTURE_2D);
201 glDisable(GL_TEXTURE_COORD_ARRAY);
202 glEnable(GL_COLOR_ARRAY);
207 SCREEN_WIDTH, SCREEN_HEIGHT,
210 glVertexPointer(2, GL_FLOAT, 0, vertices);
213 top.red, top.green, top.blue, top.alpha,
214 top.red, top.green, top.blue, top.alpha,
215 bottom.red, bottom.green, bottom.blue, bottom.alpha,
216 bottom.red, bottom.green, bottom.blue, bottom.alpha,
218 glColorPointer(4, GL_FLOAT, 0, colors);
220 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
222 glDisable(GL_COLOR_ARRAY);
223 glEnable(GL_TEXTURE_COORD_ARRAY);
225 glEnable(GL_TEXTURE_2D);
226 glColor4f(1, 1, 1, 1);
230 GLLightmap::draw_filled_rect(const DrawingRequest& request)
232 const FillRectRequest* fillrectrequest
233 = (FillRectRequest*) request.request_data;
235 float x = request.pos.x;
236 float y = request.pos.y;
237 float w = fillrectrequest->size.x;
238 float h = fillrectrequest->size.y;
240 glDisable(GL_TEXTURE_2D);
241 glColor4f(fillrectrequest->color.red, fillrectrequest->color.green,
242 fillrectrequest->color.blue, fillrectrequest->color.alpha);
243 glDisable(GL_TEXTURE_COORD_ARRAY);
251 glVertexPointer(2, GL_FLOAT, 0, vertices);
253 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
255 glEnable(GL_TEXTURE_COORD_ARRAY);
256 glEnable(GL_TEXTURE_2D);
257 glColor4f(1, 1, 1, 1);
261 GLLightmap::get_light(const DrawingRequest& request) const
263 const GetLightRequest* getlightrequest
264 = (GetLightRequest*) request.request_data;
267 for( int i = 0; i<3; i++)
268 pixels[i] = 0.0f; //set to black
270 float posX = request.pos.x * lightmap_width / SCREEN_WIDTH;
271 float posY = screen->h - request.pos.y * lightmap_height / SCREEN_HEIGHT;
272 glReadPixels((GLint) posX, (GLint) posY , 1, 1, GL_RGB, GL_FLOAT, pixels);
273 *(getlightrequest->color_ptr) = Color( pixels[0], pixels[1], pixels[2]);