2 // Copyright (C) 2014 Ingo Ruhnke <grumbel@gmail.com>
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_painter.hpp"
19 #include "video/drawing_request.hpp"
20 #include "video/gl/gl_surface_data.hpp"
21 #include "video/gl/gl_texture.hpp"
23 GLuint GLPainter::s_last_texture = static_cast<GLuint>(-1);
27 inline void intern_draw(float left, float top, float right, float bottom,
28 float uv_left, float uv_top,
29 float uv_right, float uv_bottom,
30 float angle, float alpha,
35 if(effect & HORIZONTAL_FLIP)
36 std::swap(uv_left, uv_right);
38 if(effect & VERTICAL_FLIP)
39 std::swap(uv_top, uv_bottom);
41 glBlendFunc(blend.sfactor, blend.dfactor);
42 glColor4f(color.red, color.green, color.blue, color.alpha * alpha);
52 glVertexPointer(2, GL_FLOAT, 0, vertices);
60 glTexCoordPointer(2, GL_FLOAT, 0, uvs);
62 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
65 float center_x = (left + right) / 2;
66 float center_y = (top + bottom) / 2;
68 float sa = sinf(angle/180.0f*M_PI);
69 float ca = cosf(angle/180.0f*M_PI);
78 left*ca - top*sa + center_x, left*sa + top*ca + center_y,
79 right*ca - top*sa + center_x, right*sa + top*ca + center_y,
80 right*ca - bottom*sa + center_x, right*sa + bottom*ca + center_y,
81 left*ca - bottom*sa + center_x, left*sa + bottom*ca + center_y
83 glVertexPointer(2, GL_FLOAT, 0, vertices);
91 glTexCoordPointer(2, GL_FLOAT, 0, uvs);
93 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
96 // FIXME: find a better way to restore the blend mode
97 glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
98 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
104 GLPainter::draw_surface(const DrawingRequest& request)
106 const Surface* surface = static_cast<const SurfaceRequest*>(request.request_data)->surface;
111 GLTexture* gltexture = static_cast<GLTexture*>(surface->get_texture().get());
112 if(gltexture == NULL)
116 GLSurfaceData *surface_data = static_cast<GLSurfaceData*>(surface->get_surface_data());
117 if(surface_data == NULL)
122 GLuint th = gltexture->get_handle();
123 if (th != s_last_texture) {
125 glBindTexture(GL_TEXTURE_2D, th);
127 intern_draw(request.pos.x, request.pos.y,
128 request.pos.x + surface->get_width(),
129 request.pos.y + surface->get_height(),
130 surface_data->get_uv_left(),
131 surface_data->get_uv_top(),
132 surface_data->get_uv_right(),
133 surface_data->get_uv_bottom(),
138 request.drawing_effect);
142 GLPainter::draw_surface_part(const DrawingRequest& request)
144 const SurfacePartRequest* surfacepartrequest
145 = (SurfacePartRequest*) request.request_data;
146 const Surface* surface = surfacepartrequest->surface;
147 std::shared_ptr<GLTexture> gltexture = std::dynamic_pointer_cast<GLTexture>(surface->get_texture());
148 GLSurfaceData *surface_data = reinterpret_cast<GLSurfaceData *>(surface->get_surface_data());
150 float uv_width = surface_data->get_uv_right() - surface_data->get_uv_left();
151 float uv_height = surface_data->get_uv_bottom() - surface_data->get_uv_top();
153 float uv_left = surface_data->get_uv_left() + (uv_width * surfacepartrequest->srcrect.p1.x) / surface->get_width();
154 float uv_top = surface_data->get_uv_top() + (uv_height * surfacepartrequest->srcrect.p1.y) / surface->get_height();
155 float uv_right = surface_data->get_uv_left() + (uv_width * surfacepartrequest->srcrect.p2.x) / surface->get_width();
156 float uv_bottom = surface_data->get_uv_top() + (uv_height * surfacepartrequest->srcrect.p2.y) / surface->get_height();
158 GLuint th = gltexture->get_handle();
159 if (th != s_last_texture) {
161 glBindTexture(GL_TEXTURE_2D, th);
163 intern_draw(request.pos.x, request.pos.y,
164 request.pos.x + surfacepartrequest->dstsize.width,
165 request.pos.y + surfacepartrequest->dstsize.height,
174 request.drawing_effect);
178 GLPainter::draw_gradient(const DrawingRequest& request)
180 const GradientRequest* gradientrequest
181 = (GradientRequest*) request.request_data;
182 const Color& top = gradientrequest->top;
183 const Color& bottom = gradientrequest->bottom;
185 glDisable(GL_TEXTURE_2D);
186 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
187 glEnableClientState(GL_COLOR_ARRAY);
191 float(SCREEN_WIDTH), 0,
192 float(SCREEN_WIDTH), float(SCREEN_HEIGHT),
193 0, float(SCREEN_HEIGHT)
195 glVertexPointer(2, GL_FLOAT, 0, vertices);
198 top.red, top.green, top.blue, top.alpha,
199 top.red, top.green, top.blue, top.alpha,
200 bottom.red, bottom.green, bottom.blue, bottom.alpha,
201 bottom.red, bottom.green, bottom.blue, bottom.alpha,
203 glColorPointer(4, GL_FLOAT, 0, colors);
205 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
207 glDisableClientState(GL_COLOR_ARRAY);
208 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
210 glEnable(GL_TEXTURE_2D);
211 glColor4f(1, 1, 1, 1);
215 GLPainter::draw_filled_rect(const DrawingRequest& request)
217 const FillRectRequest* fillrectrequest
218 = (FillRectRequest*) request.request_data;
220 glDisable(GL_TEXTURE_2D);
221 glColor4f(fillrectrequest->color.red, fillrectrequest->color.green,
222 fillrectrequest->color.blue, fillrectrequest->color.alpha);
223 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
225 if (fillrectrequest->radius != 0.0f)
228 // Keep radius in the limits, so that we get a circle instead of
230 float radius = std::min(fillrectrequest->radius,
231 std::min(fillrectrequest->size.x/2,
232 fillrectrequest->size.y/2));
235 Rectf irect(request.pos.x + radius,
236 request.pos.y + radius,
237 request.pos.x + fillrectrequest->size.x - radius,
238 request.pos.y + fillrectrequest->size.y - radius);
242 std::vector<float> vertices((n+1) * 4 * 2);
244 for(int i = 0; i <= n; ++i)
246 float x = sinf(i * (M_PI/2) / n) * radius;
247 float y = cosf(i * (M_PI/2) / n) * radius;
249 vertices[p++] = irect.get_left() - x;
250 vertices[p++] = irect.get_top() - y;
252 vertices[p++] = irect.get_right() + x;
253 vertices[p++] = irect.get_top() - y;
256 for(int i = 0; i <= n; ++i)
258 float x = cosf(i * (M_PI/2) / n) * radius;
259 float y = sinf(i * (M_PI/2) / n) * radius;
261 vertices[p++] = irect.get_left() - x;
262 vertices[p++] = irect.get_bottom() + y;
264 vertices[p++] = irect.get_right() + x;
265 vertices[p++] = irect.get_bottom() + y;
268 glVertexPointer(2, GL_FLOAT, 0, &*vertices.begin());
269 glDrawArrays(GL_TRIANGLE_STRIP, 0, vertices.size()/2);
273 float x = request.pos.x;
274 float y = request.pos.y;
275 float w = fillrectrequest->size.x;
276 float h = fillrectrequest->size.y;
284 glVertexPointer(2, GL_FLOAT, 0, vertices);
286 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
289 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
290 glEnable(GL_TEXTURE_2D);
291 glColor4f(1, 1, 1, 1);
295 GLPainter::draw_inverse_ellipse(const DrawingRequest& request)
297 const InverseEllipseRequest* ellipse = (InverseEllipseRequest*)request.request_data;
299 glDisable(GL_TEXTURE_2D);
300 glColor4f(ellipse->color.red, ellipse->color.green,
301 ellipse->color.blue, ellipse->color.alpha);
303 float x = request.pos.x;
304 float y = request.pos.y;
305 float w = ellipse->size.x/2.0f;
306 float h = ellipse->size.y/2.0f;
308 static const int slices = 16;
309 static const int points = (slices+1) * 12;
311 float vertices[points * 2];
315 vertices[p++] = SCREEN_WIDTH; vertices[p++] = SCREEN_HEIGHT;
316 vertices[p++] = 0; vertices[p++] = SCREEN_HEIGHT;
317 vertices[p++] = x; vertices[p++] = y+h;
320 vertices[p++] = SCREEN_WIDTH; vertices[p++] = 0;
321 vertices[p++] = 0; vertices[p++] = 0;
322 vertices[p++] = x; vertices[p++] = y-h;
325 vertices[p++] = SCREEN_WIDTH; vertices[p++] = 0;
326 vertices[p++] = SCREEN_WIDTH; vertices[p++] = SCREEN_HEIGHT;
327 vertices[p++] = x+w; vertices[p++] = y;
330 vertices[p++] = 0; vertices[p++] = 0;
331 vertices[p++] = 0; vertices[p++] = SCREEN_HEIGHT;
332 vertices[p++] = x-w; vertices[p++] = y;
334 for(int i = 0; i < slices; ++i)
336 float ex1 = sinf(M_PI/2 / slices * i) * w;
337 float ey1 = cosf(M_PI/2 / slices * i) * h;
339 float ex2 = sinf(M_PI/2 / slices * (i+1)) * w;
340 float ey2 = cosf(M_PI/2 / slices * (i+1)) * h;
343 vertices[p++] = SCREEN_WIDTH; vertices[p++] = SCREEN_HEIGHT;
344 vertices[p++] = x + ex1; vertices[p++] = y + ey1;
345 vertices[p++] = x + ex2; vertices[p++] = y + ey2;
348 vertices[p++] = 0; vertices[p++] = 0;
349 vertices[p++] = x - ex1; vertices[p++] = y - ey1;
350 vertices[p++] = x - ex2; vertices[p++] = y - ey2;
353 vertices[p++] = SCREEN_WIDTH; vertices[p++] = 0;
354 vertices[p++] = x + ex1; vertices[p++] = y - ey1;
355 vertices[p++] = x + ex2; vertices[p++] = y - ey2;
358 vertices[p++] = 0; vertices[p++] = SCREEN_HEIGHT;
359 vertices[p++] = x - ex1; vertices[p++] = y + ey1;
360 vertices[p++] = x - ex2; vertices[p++] = y + ey2;
363 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
364 glVertexPointer(2, GL_FLOAT, 0, vertices);
366 glDrawArrays(GL_TRIANGLES, 0, points);
368 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
370 glEnable(GL_TEXTURE_2D);
371 glColor4f(1, 1, 1, 1);