Make it build with -DCOMPILE_AMALGATION=ON. Still not certain how intern_draw/next_po...
[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->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);
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   glViewport(0, screen->h - lightmap_height, lightmap_width, lightmap_height);
74   glMatrixMode(GL_PROJECTION);
75   glLoadIdentity();
76 #ifdef GL_VERSION_ES_CM_1_0
77   glOrthof(0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, -1.0, 1.0);
78 #else
79   glOrtho(0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, -1.0, 1.0);
80 #endif
81   glMatrixMode(GL_MODELVIEW);
82   glLoadIdentity();
83
84   glClearColor( ambient_color.red, ambient_color.green, ambient_color.blue, 1 );
85   glClear(GL_COLOR_BUFFER_BIT);
86 }
87
88 void
89 GLLightmap::end_draw()
90 {
91   glDisable(GL_BLEND);
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);
94
95   glViewport(0, 0, screen->w, screen->h);
96   glMatrixMode(GL_PROJECTION);
97   glLoadIdentity();
98 #ifdef GL_VERSION_ES_CM_1_0
99   glOrthof(0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, -1.0, 1.0);
100 #else
101   glOrtho(0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, -1.0, 1.0);
102 #endif
103   glMatrixMode(GL_MODELVIEW);
104   glLoadIdentity();
105   glEnable(GL_BLEND);
106   //glClear(GL_COLOR_BUFFER_BIT);
107 }
108
109 void
110 GLLightmap::do_draw()
111 {
112   // multiple the lightmap with the framebuffer
113   glBlendFunc(GL_DST_COLOR, GL_ZERO);
114
115   glBindTexture(GL_TEXTURE_2D, lightmap->get_handle());
116
117   float vertices[] = {
118     0, 0,
119     SCREEN_WIDTH, 0,
120     SCREEN_WIDTH, SCREEN_HEIGHT,
121     0, SCREEN_HEIGHT
122   };
123   glVertexPointer(2, GL_FLOAT, 0, vertices);
124
125   float uvs[] = {
126     0,                 lightmap_uv_bottom,
127     lightmap_uv_right, lightmap_uv_bottom,
128     lightmap_uv_right, 0,
129     0, 0
130   };
131   glTexCoordPointer(2, GL_FLOAT, 0, uvs);
132
133   glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
134
135   glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
136 }
137
138 void
139 GLLightmap::draw_surface(const DrawingRequest& request)
140 {
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());
144
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(),
153               request.angle,
154               request.alpha,
155               request.color,
156               request.blend,
157               request.drawing_effect);
158 }
159
160 void
161 GLLightmap::draw_surface_part(const DrawingRequest& request)
162 {
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());
168
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();
171
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();
176
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,
181               uv_left,
182               uv_top,
183               uv_right,
184               uv_bottom,
185               0.0,
186               request.alpha,
187               Color(1.0, 1.0, 1.0),
188               Blend(),
189               request.drawing_effect);
190 }
191
192 void
193 GLLightmap::draw_gradient(const DrawingRequest& request)
194 {
195   const GradientRequest* gradientrequest 
196     = (GradientRequest*) request.request_data;
197   const Color& top = gradientrequest->top;
198   const Color& bottom = gradientrequest->bottom;
199
200   glDisable(GL_TEXTURE_2D);
201   glDisable(GL_TEXTURE_COORD_ARRAY);
202   glEnable(GL_COLOR_ARRAY);
203
204   float vertices[] = {
205     0, 0,
206     SCREEN_WIDTH, 0,
207     SCREEN_WIDTH, SCREEN_HEIGHT,
208     0, SCREEN_HEIGHT
209   };
210   glVertexPointer(2, GL_FLOAT, 0, vertices);
211
212   float colors[] = {
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,
217   };
218   glColorPointer(4, GL_FLOAT, 0, colors);
219
220   glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
221
222   glDisable(GL_COLOR_ARRAY);
223   glEnable(GL_TEXTURE_COORD_ARRAY);
224
225   glEnable(GL_TEXTURE_2D);
226   glColor4f(1, 1, 1, 1);
227 }
228
229 void
230 GLLightmap::draw_filled_rect(const DrawingRequest& request)
231 {
232   const FillRectRequest* fillrectrequest
233     = (FillRectRequest*) request.request_data;
234
235   float x = request.pos.x;
236   float y = request.pos.y;
237   float w = fillrectrequest->size.x;
238   float h = fillrectrequest->size.y;
239
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);
244
245   float vertices[] = {
246     x,   y,
247     x+w, y,
248     x+w, y+h,
249     x,   y+h
250   };
251   glVertexPointer(2, GL_FLOAT, 0, vertices);
252
253   glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
254
255   glEnable(GL_TEXTURE_COORD_ARRAY);
256   glEnable(GL_TEXTURE_2D);
257   glColor4f(1, 1, 1, 1);
258 }
259
260 void
261 GLLightmap::get_light(const DrawingRequest& request) const
262 {
263   const GetLightRequest* getlightrequest 
264     = (GetLightRequest*) request.request_data;
265
266   float pixels[3];
267   for( int i = 0; i<3; i++)
268     pixels[i] = 0.0f; //set to black
269
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]);
274 }
275
276 /* EOF */