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_painter.hpp"
36 #include "video/gl/gl_renderer.hpp"
37 #include "video/gl/gl_surface_data.hpp"
38 #include "video/gl/gl_texture.hpp"
39 #include "video/glutil.hpp"
40 #include "video/lightmap.hpp"
41 #include "video/renderer.hpp"
42 #include "video/surface.hpp"
43 #include "video/texture_manager.hpp"
45 inline int next_po2(int val)
54 GLLightmap::GLLightmap() :
58 m_lightmap_uv_right(),
59 m_lightmap_uv_bottom()
61 m_lightmap_width = SCREEN_WIDTH / s_LIGHTMAP_DIV;
62 m_lightmap_height = SCREEN_HEIGHT / s_LIGHTMAP_DIV;
63 unsigned int width = next_po2(m_lightmap_width);
64 unsigned int height = next_po2(m_lightmap_height);
66 m_lightmap.reset(new GLTexture(width, height));
68 m_lightmap_uv_right = static_cast<float>(m_lightmap_width) / static_cast<float>(width);
69 m_lightmap_uv_bottom = static_cast<float>(m_lightmap_height) / static_cast<float>(height);
70 TextureManager::current()->register_texture(m_lightmap.get());
73 GLLightmap::~GLLightmap()
78 GLLightmap::start_draw(const Color &ambient_color)
81 glGetFloatv(GL_VIEWPORT, m_old_viewport); //save viewport
82 glViewport(m_old_viewport[0], m_old_viewport[3] - m_lightmap_height + m_old_viewport[1], m_lightmap_width, m_lightmap_height);
83 glMatrixMode(GL_PROJECTION);
85 #ifdef GL_VERSION_ES_CM_1_0
86 glOrthof(0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, -1.0, 1.0);
88 glOrtho(0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, -1.0, 1.0);
90 glMatrixMode(GL_MODELVIEW);
93 glClearColor( ambient_color.red, ambient_color.green, ambient_color.blue, 1 );
94 glClear(GL_COLOR_BUFFER_BIT);
98 GLLightmap::end_draw()
101 glBindTexture(GL_TEXTURE_2D, m_lightmap->get_handle());
102 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, m_old_viewport[0], m_old_viewport[3] - m_lightmap_height + m_old_viewport[1], m_lightmap_width, m_lightmap_height);
104 glViewport(m_old_viewport[0], m_old_viewport[1], m_old_viewport[2], m_old_viewport[3]);
105 glMatrixMode(GL_PROJECTION);
107 #ifdef GL_VERSION_ES_CM_1_0
108 glOrthof(0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, -1.0, 1.0);
110 glOrtho(0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, -1.0, 1.0);
112 glMatrixMode(GL_MODELVIEW);
116 glClearColor(0, 0, 0, 1 );
117 glClear(GL_COLOR_BUFFER_BIT);
121 GLLightmap::do_draw()
123 // multiple the lightmap with the framebuffer
124 glBlendFunc(GL_DST_COLOR, GL_ZERO);
126 glBindTexture(GL_TEXTURE_2D, m_lightmap->get_handle());
130 float(SCREEN_WIDTH), 0,
131 float(SCREEN_WIDTH), float(SCREEN_HEIGHT),
132 0, float(SCREEN_HEIGHT)
134 glVertexPointer(2, GL_FLOAT, 0, vertices);
137 0, m_lightmap_uv_bottom,
138 m_lightmap_uv_right, m_lightmap_uv_bottom,
139 m_lightmap_uv_right, 0,
142 glTexCoordPointer(2, GL_FLOAT, 0, uvs);
144 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
146 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
150 GLLightmap::draw_surface(const DrawingRequest& request)
152 GLPainter::draw_surface(request);
156 GLLightmap::draw_surface_part(const DrawingRequest& request)
158 GLPainter::draw_surface_part(request);
162 GLLightmap::draw_gradient(const DrawingRequest& request)
164 GLPainter::draw_gradient(request);
168 GLLightmap::draw_filled_rect(const DrawingRequest& request)
170 GLPainter::draw_filled_rect(request);
174 GLLightmap::draw_inverse_ellipse(const DrawingRequest& request)
176 GLPainter::draw_inverse_ellipse(request);
180 GLLightmap::get_light(const DrawingRequest& request) const
182 const GetLightRequest* getlightrequest
183 = (GetLightRequest*) request.request_data;
186 for( int i = 0; i<3; i++)
187 pixels[i] = 0.0f; //set to black
189 float posX = request.pos.x * m_lightmap_width / SCREEN_WIDTH + m_old_viewport[0];
190 float posY = m_old_viewport[3] + m_old_viewport[1] - request.pos.y * m_lightmap_height / SCREEN_HEIGHT;
191 glReadPixels((GLint) posX, (GLint) posY , 1, 1, GL_RGB, GL_FLOAT, pixels);
192 *(getlightrequest->color_ptr) = Color( pixels[0], pixels[1], pixels[2]);