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() :
51 lightmap_width = SCREEN_WIDTH / LIGHTMAP_DIV;
52 lightmap_height = SCREEN_HEIGHT / LIGHTMAP_DIV;
53 unsigned int width = next_po2(lightmap_width);
54 unsigned int height = next_po2(lightmap_height);
56 lightmap.reset(new GLTexture(width, height));
58 lightmap_uv_right = static_cast<float>(lightmap_width) / static_cast<float>(width);
59 lightmap_uv_bottom = static_cast<float>(lightmap_height) / static_cast<float>(height);
60 texture_manager->register_texture(lightmap.get());
63 GLLightmap::~GLLightmap()
68 GLLightmap::start_draw(const Color &ambient_color)
71 glGetFloatv(GL_VIEWPORT, old_viewport); //save viewport
72 glViewport(old_viewport[0], old_viewport[3] - lightmap_height + old_viewport[1], lightmap_width, lightmap_height);
73 glMatrixMode(GL_PROJECTION);
75 #ifdef GL_VERSION_ES_CM_1_0
76 glOrthof(0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, -1.0, 1.0);
78 glOrtho(0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, -1.0, 1.0);
80 glMatrixMode(GL_MODELVIEW);
83 glClearColor( ambient_color.red, ambient_color.green, ambient_color.blue, 1 );
84 glClear(GL_COLOR_BUFFER_BIT);
88 GLLightmap::end_draw()
91 glBindTexture(GL_TEXTURE_2D, lightmap->get_handle());
92 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, old_viewport[0], old_viewport[3] - lightmap_height + old_viewport[1], lightmap_width, lightmap_height);
94 glViewport(old_viewport[0], old_viewport[1], old_viewport[2], old_viewport[3]);
95 glMatrixMode(GL_PROJECTION);
97 #ifdef GL_VERSION_ES_CM_1_0
98 glOrthof(0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, -1.0, 1.0);
100 glOrtho(0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, -1.0, 1.0);
102 glMatrixMode(GL_MODELVIEW);
106 glClearColor(0, 0, 0, 1 );
107 glClear(GL_COLOR_BUFFER_BIT);
111 GLLightmap::do_draw()
113 // multiple the lightmap with the framebuffer
114 glBlendFunc(GL_DST_COLOR, GL_ZERO);
116 glBindTexture(GL_TEXTURE_2D, lightmap->get_handle());
120 float(SCREEN_WIDTH), 0,
121 float(SCREEN_WIDTH), float(SCREEN_HEIGHT),
122 0, float(SCREEN_HEIGHT)
124 glVertexPointer(2, GL_FLOAT, 0, vertices);
127 0, lightmap_uv_bottom,
128 lightmap_uv_right, lightmap_uv_bottom,
129 lightmap_uv_right, 0,
132 glTexCoordPointer(2, GL_FLOAT, 0, uvs);
134 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
136 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
140 GLLightmap::draw_surface(const DrawingRequest& request)
142 const Surface* surface = (const Surface*) request.request_data;
143 boost::shared_ptr<GLTexture> gltexture = boost::dynamic_pointer_cast<GLTexture>(surface->get_texture());
144 GLSurfaceData *surface_data = reinterpret_cast<GLSurfaceData *>(surface->get_surface_data());
146 glBindTexture(GL_TEXTURE_2D, gltexture->get_handle());
147 intern_draw(request.pos.x, request.pos.y,
148 request.pos.x + surface->get_width(),
149 request.pos.y + surface->get_height(),
150 surface_data->get_uv_left(),
151 surface_data->get_uv_top(),
152 surface_data->get_uv_right(),
153 surface_data->get_uv_bottom(),
158 request.drawing_effect);
162 GLLightmap::draw_surface_part(const DrawingRequest& request)
164 const SurfacePartRequest* surfacepartrequest
165 = (SurfacePartRequest*) request.request_data;
166 const Surface* surface = surfacepartrequest->surface;
167 boost::shared_ptr<GLTexture> gltexture = boost::dynamic_pointer_cast<GLTexture>(surface->get_texture());
168 GLSurfaceData *surface_data = reinterpret_cast<GLSurfaceData *>(surface->get_surface_data());
170 float uv_width = surface_data->get_uv_right() - surface_data->get_uv_left();
171 float uv_height = surface_data->get_uv_bottom() - surface_data->get_uv_top();
173 float uv_left = surface_data->get_uv_left() + (uv_width * surfacepartrequest->source.x) / surface->get_width();
174 float uv_top = surface_data->get_uv_top() + (uv_height * surfacepartrequest->source.y) / surface->get_height();
175 float uv_right = surface_data->get_uv_left() + (uv_width * (surfacepartrequest->source.x + surfacepartrequest->size.x)) / surface->get_width();
176 float uv_bottom = surface_data->get_uv_top() + (uv_height * (surfacepartrequest->source.y + surfacepartrequest->size.y)) / surface->get_height();
178 glBindTexture(GL_TEXTURE_2D, gltexture->get_handle());
179 intern_draw(request.pos.x, request.pos.y,
180 request.pos.x + surfacepartrequest->size.x,
181 request.pos.y + surfacepartrequest->size.y,
188 Color(1.0, 1.0, 1.0),
190 request.drawing_effect);
194 GLLightmap::draw_gradient(const DrawingRequest& request)
196 const GradientRequest* gradientrequest
197 = (GradientRequest*) request.request_data;
198 const Color& top = gradientrequest->top;
199 const Color& bottom = gradientrequest->bottom;
201 glDisable(GL_TEXTURE_2D);
202 glDisable(GL_TEXTURE_COORD_ARRAY);
203 glEnable(GL_COLOR_ARRAY);
207 float(SCREEN_WIDTH), 0,
208 float(SCREEN_WIDTH), float(SCREEN_HEIGHT),
209 0, float(SCREEN_HEIGHT)
211 glVertexPointer(2, GL_FLOAT, 0, vertices);
214 top.red, top.green, top.blue, top.alpha,
215 top.red, top.green, top.blue, top.alpha,
216 bottom.red, bottom.green, bottom.blue, bottom.alpha,
217 bottom.red, bottom.green, bottom.blue, bottom.alpha,
219 glColorPointer(4, GL_FLOAT, 0, colors);
221 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
223 glDisable(GL_COLOR_ARRAY);
224 glEnable(GL_TEXTURE_COORD_ARRAY);
226 glEnable(GL_TEXTURE_2D);
227 glColor4f(1, 1, 1, 1);
231 GLLightmap::draw_filled_rect(const DrawingRequest& request)
233 const FillRectRequest* fillrectrequest
234 = (FillRectRequest*) request.request_data;
236 float x = request.pos.x;
237 float y = request.pos.y;
238 float w = fillrectrequest->size.x;
239 float h = fillrectrequest->size.y;
241 glDisable(GL_TEXTURE_2D);
242 glColor4f(fillrectrequest->color.red, fillrectrequest->color.green,
243 fillrectrequest->color.blue, fillrectrequest->color.alpha);
244 glDisable(GL_TEXTURE_COORD_ARRAY);
252 glVertexPointer(2, GL_FLOAT, 0, vertices);
254 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
256 glEnable(GL_TEXTURE_COORD_ARRAY);
257 glEnable(GL_TEXTURE_2D);
258 glColor4f(1, 1, 1, 1);
262 GLLightmap::get_light(const DrawingRequest& request) const
264 const GetLightRequest* getlightrequest
265 = (GetLightRequest*) request.request_data;
268 for( int i = 0; i<3; i++)
269 pixels[i] = 0.0f; //set to black
271 float posX = request.pos.x * lightmap_width / SCREEN_WIDTH + old_viewport[0];
272 float posY = old_viewport[3] + old_viewport[1] - request.pos.y * lightmap_height / SCREEN_HEIGHT;
273 glReadPixels((GLint) posX, (GLint) posY , 1, 1, GL_RGB, GL_FLOAT, pixels);
274 *(getlightrequest->color_ptr) = Color( pixels[0], pixels[1], pixels[2]);