committed stpatch20110110.diff by Jonas Kümmerlin
[supertux.git] / src / video / gl / gl_lightmap.cpp
1 //  SuperTux
2 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
3 //
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.
8 //
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.
13 //
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/>.
16
17 #include "video/gl/gl_lightmap.hpp"
18
19 #include <SDL_image.h>
20 #include <algorithm>
21 #include <assert.h>
22 #include <functional>
23 #include <iomanip>
24 #include <iostream>
25 #include <math.h>
26 #include <physfs.h>
27 #include <sstream>
28
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"
43
44 GLLightmap::GLLightmap() :
45   screen(),
46   lightmap(),
47   lightmap_width(),
48   lightmap_height(),
49   lightmap_uv_right(),
50   lightmap_uv_bottom()
51 {
52   screen = SDL_GetVideoSurface();
53
54   lightmap_width = SCREEN_WIDTH / LIGHTMAP_DIV;
55   lightmap_height = SCREEN_HEIGHT / LIGHTMAP_DIV;
56   unsigned int width = next_po2(lightmap_width);
57   unsigned int height = next_po2(lightmap_height);
58
59   lightmap.reset(new GLTexture(width, height));
60
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());
64 }
65
66 GLLightmap::~GLLightmap()
67 {
68 }
69
70 void
71 GLLightmap::start_draw(const Color &ambient_color)
72 {
73   
74   glGetFloatv(GL_VIEWPORT, old_viewport); //save viewport
75   glViewport(old_viewport[0], SCREEN_HEIGHT - lightmap_height + old_viewport[1], lightmap_width, lightmap_height);
76   glMatrixMode(GL_PROJECTION);
77   glLoadIdentity();
78 #ifdef GL_VERSION_ES_CM_1_0
79   glOrthof(0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, -1.0, 1.0);
80 #else
81   glOrtho(0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, -1.0, 1.0);
82 #endif
83   glMatrixMode(GL_MODELVIEW);
84   glLoadIdentity();
85
86   glClearColor( ambient_color.red, ambient_color.green, ambient_color.blue, 1 );
87   glClear(GL_COLOR_BUFFER_BIT);
88 }
89
90 void
91 GLLightmap::end_draw()
92 {
93   glDisable(GL_BLEND);
94   glBindTexture(GL_TEXTURE_2D, lightmap->get_handle());
95   glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, old_viewport[0], SCREEN_HEIGHT  - lightmap_height + old_viewport[1], lightmap_width, lightmap_height);
96   
97   glViewport(old_viewport[0], old_viewport[1], old_viewport[2], old_viewport[3]);
98   glMatrixMode(GL_PROJECTION);
99   glLoadIdentity();
100 #ifdef GL_VERSION_ES_CM_1_0
101   glOrthof(0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, -1.0, 1.0);
102 #else
103   glOrtho(0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, -1.0, 1.0);
104 #endif
105   glMatrixMode(GL_MODELVIEW);
106   glLoadIdentity();
107   glEnable(GL_BLEND);
108   //glClear(GL_COLOR_BUFFER_BIT);
109 }
110
111 void
112 GLLightmap::do_draw()
113 {
114   // multiple the lightmap with the framebuffer
115   glBlendFunc(GL_DST_COLOR, GL_ZERO);
116
117   glBindTexture(GL_TEXTURE_2D, lightmap->get_handle());
118
119   float vertices[] = {
120     0, 0,
121     SCREEN_WIDTH, 0,
122     SCREEN_WIDTH, SCREEN_HEIGHT,
123     0, SCREEN_HEIGHT
124   };
125   glVertexPointer(2, GL_FLOAT, 0, vertices);
126
127   float uvs[] = {
128     0,                 lightmap_uv_bottom,
129     lightmap_uv_right, lightmap_uv_bottom,
130     lightmap_uv_right, 0,
131     0, 0
132   };
133   glTexCoordPointer(2, GL_FLOAT, 0, uvs);
134
135   glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
136
137   glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
138 }
139
140 void
141 GLLightmap::draw_surface(const DrawingRequest& request)
142 {
143   const Surface* surface = (const Surface*) request.request_data;
144   boost::shared_ptr<GLTexture> gltexture = boost::dynamic_pointer_cast<GLTexture>(surface->get_texture());
145   GLSurfaceData *surface_data = reinterpret_cast<GLSurfaceData *>(surface->get_surface_data());
146
147   glBindTexture(GL_TEXTURE_2D, gltexture->get_handle());
148   intern_draw(request.pos.x, request.pos.y,
149               request.pos.x + surface->get_width(),
150               request.pos.y + surface->get_height(),
151               surface_data->get_uv_left(),
152               surface_data->get_uv_top(),
153               surface_data->get_uv_right(),
154               surface_data->get_uv_bottom(),
155               request.angle,
156               request.alpha,
157               request.color,
158               request.blend,
159               request.drawing_effect);
160 }
161
162 void
163 GLLightmap::draw_surface_part(const DrawingRequest& request)
164 {
165   const SurfacePartRequest* surfacepartrequest
166     = (SurfacePartRequest*) request.request_data;
167   const Surface* surface = surfacepartrequest->surface;
168   boost::shared_ptr<GLTexture> gltexture = boost::dynamic_pointer_cast<GLTexture>(surface->get_texture());
169   GLSurfaceData *surface_data = reinterpret_cast<GLSurfaceData *>(surface->get_surface_data());
170
171   float uv_width = surface_data->get_uv_right() - surface_data->get_uv_left();
172   float uv_height = surface_data->get_uv_bottom() - surface_data->get_uv_top();
173
174   float uv_left = surface_data->get_uv_left() + (uv_width * surfacepartrequest->source.x) / surface->get_width();
175   float uv_top = surface_data->get_uv_top() + (uv_height * surfacepartrequest->source.y) / surface->get_height();
176   float uv_right = surface_data->get_uv_left() + (uv_width * (surfacepartrequest->source.x + surfacepartrequest->size.x)) / surface->get_width();
177   float uv_bottom = surface_data->get_uv_top() + (uv_height * (surfacepartrequest->source.y + surfacepartrequest->size.y)) / surface->get_height();
178
179   glBindTexture(GL_TEXTURE_2D, gltexture->get_handle());
180   intern_draw(request.pos.x, request.pos.y,
181               request.pos.x + surfacepartrequest->size.x,
182               request.pos.y + surfacepartrequest->size.y,
183               uv_left,
184               uv_top,
185               uv_right,
186               uv_bottom,
187               0.0,
188               request.alpha,
189               Color(1.0, 1.0, 1.0),
190               Blend(),
191               request.drawing_effect);
192 }
193
194 void
195 GLLightmap::draw_gradient(const DrawingRequest& request)
196 {
197   const GradientRequest* gradientrequest 
198     = (GradientRequest*) request.request_data;
199   const Color& top = gradientrequest->top;
200   const Color& bottom = gradientrequest->bottom;
201
202   glDisable(GL_TEXTURE_2D);
203   glDisable(GL_TEXTURE_COORD_ARRAY);
204   glEnable(GL_COLOR_ARRAY);
205
206   float vertices[] = {
207     0, 0,
208     SCREEN_WIDTH, 0,
209     SCREEN_WIDTH, SCREEN_HEIGHT,
210     0, SCREEN_HEIGHT
211   };
212   glVertexPointer(2, GL_FLOAT, 0, vertices);
213
214   float colors[] = {
215     top.red, top.green, top.blue, top.alpha,
216     top.red, top.green, top.blue, top.alpha,
217     bottom.red, bottom.green, bottom.blue, bottom.alpha,
218     bottom.red, bottom.green, bottom.blue, bottom.alpha,
219   };
220   glColorPointer(4, GL_FLOAT, 0, colors);
221
222   glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
223
224   glDisable(GL_COLOR_ARRAY);
225   glEnable(GL_TEXTURE_COORD_ARRAY);
226
227   glEnable(GL_TEXTURE_2D);
228   glColor4f(1, 1, 1, 1);
229 }
230
231 void
232 GLLightmap::draw_filled_rect(const DrawingRequest& request)
233 {
234   const FillRectRequest* fillrectrequest
235     = (FillRectRequest*) request.request_data;
236
237   float x = request.pos.x;
238   float y = request.pos.y;
239   float w = fillrectrequest->size.x;
240   float h = fillrectrequest->size.y;
241
242   glDisable(GL_TEXTURE_2D);
243   glColor4f(fillrectrequest->color.red, fillrectrequest->color.green,
244             fillrectrequest->color.blue, fillrectrequest->color.alpha);
245   glDisable(GL_TEXTURE_COORD_ARRAY);
246
247   float vertices[] = {
248     x,   y,
249     x+w, y,
250     x+w, y+h,
251     x,   y+h
252   };
253   glVertexPointer(2, GL_FLOAT, 0, vertices);
254
255   glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
256
257   glEnable(GL_TEXTURE_COORD_ARRAY);
258   glEnable(GL_TEXTURE_2D);
259   glColor4f(1, 1, 1, 1);
260 }
261
262 void
263 GLLightmap::get_light(const DrawingRequest& request) const
264 {
265   const GetLightRequest* getlightrequest 
266     = (GetLightRequest*) request.request_data;
267
268   float pixels[3];
269   for( int i = 0; i<3; i++)
270     pixels[i] = 0.0f; //set to black
271
272   float posX = request.pos.x * lightmap_width / SCREEN_WIDTH + old_viewport[0];
273   float posY = SCREEN_HEIGHT + old_viewport[1] - request.pos.y * lightmap_height / SCREEN_HEIGHT;
274   glReadPixels((GLint) posX, (GLint) posY , 1, 1, GL_RGB, GL_FLOAT, pixels);
275   *(getlightrequest->color_ptr) = Color( pixels[0], pixels[1], pixels[2]);
276 }
277
278 /* EOF */