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