added 3dengfx into the repo, probably not the correct version for this
[summerhack] / src / 3dengfx / src / dsys / fx.cpp
1 /*
2 This file is part of the 3dengfx demo system.
3
4 Copyright (c) 2004, 2005 John Tsiombikas <nuclear@siggraph.org>
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19 */
20
21 #include "3dengfx_config.h"
22
23 #include <list>
24 #include <algorithm>
25 #include "fx.hpp"
26 #include "dsys.hpp"
27 #include "script.h"
28 #include "3dengfx/3dengfx.hpp"
29 #include "common/err_msg.h"
30
31
32 static bool str_to_color(const char *str, Color *col);
33
34
35 void dsys::radial_blur(Texture *tex, float ammount, const Vector2 &origin, bool additive) {
36         Vector2 c1(0.0f, 0.0f), c2(1.0f, 1.0f);
37
38         set_alpha_blending(true);
39         if(additive) {
40                 set_blend_func(BLEND_SRC_ALPHA, BLEND_ONE);
41         } else {
42                 set_blend_func(BLEND_SRC_ALPHA, BLEND_ONE_MINUS_SRC_ALPHA);
43         }
44         
45         ammount += 1.0f;
46         int quad_count = (int)(ammount * 20.0f);
47         float dscale = (ammount - 1.0f) / (float)quad_count;
48         float scale = 1.0f;
49         for(int i=0; i<quad_count; i++) {
50                 Vector2 v1 = c1, v2 = c2;
51                 v1 -= origin;
52                 v1 *= scale;
53                 v1 += origin;
54
55                 v2 -= origin;
56                 v2 *= scale;
57                 v2 += origin;
58                 
59                 float alpha = (float)((quad_count-1) - i) / (float)quad_count;
60                 dsys::overlay(tex, v1, v2, Color(1.0f, 1.0f, 1.0f, alpha), false);
61                 scale += dscale;
62         }
63
64         set_alpha_blending(false);
65 }
66
67 void dsys::dir_blur(Texture *tex, float ammount, int dir) {
68         Vector2 c1(0.0f, 1.0f), c2(1.0f, 0.0f);
69         
70         ammount *= 0.5f;
71         int quad_count = (int)(ammount * 100.0f);
72         float offs_inc = ammount / (float)(quad_count/2);
73         float offs = 0.0f;
74         for(int i=0; i<quad_count/2; i++) {
75                 Vector2 off_vec = dir == BLUR_DIR_X ? Vector2(offs, 0) : Vector2(0, offs);
76
77                 float alpha = 1.0f - offs / ammount;
78                 dsys::overlay(tex, c1 + off_vec, c2 + off_vec, Color(1.0f, 1.0f, 1.0f, alpha));
79                 //dsys::overlay(tex, c1 - off_vec, c2 - off_vec, Color(1.0f, 1.0f, 1.0f, alpha));
80                 offs += offs_inc;
81         }
82 }
83
84 /*
85 static Vector2 *blur_pos;
86 static int pos_count;
87
88 void dsys::blur(Texture *tex, float ammount, bool additive) {
89         //additive = true;
90
91         set_alpha_blending(true);
92         if(additive) {
93                 set_blend_func(BLEND_SRC_ALPHA, BLEND_ONE);
94         } else {
95                 set_blend_func(BLEND_SRC_ALPHA, BLEND_ONE_MINUS_SRC_ALPHA);
96         }
97
98         int quad_count = (int)(ammount * 700.0f);
99
100         if(quad_count != pos_count) {
101                 if(blur_pos) delete [] blur_pos;
102                 blur_pos = new Vector2[quad_count];
103
104                 for(int i=0; i<quad_count; i++) {
105                         blur_pos[i] = Vector2(frand(1.0f) - 0.5f, frand(1.0f) - 0.5f);
106                         blur_pos[i] *= ammount;
107                 }
108                 pos_count = quad_count;
109         }
110         
111         for(int i=0; i<quad_count; i++) {
112                 float dist = blur_pos[i].length_sq();
113                 float alpha = 1.0f / dist;
114                 dsys::overlay(tex, Vector2(0, 0) + blur_pos[i], Vector2(1, 1) + blur_pos[i], Color(1.0f, 1.0f, 1.0f, alpha), false);
115         }
116
117         set_alpha_blending(false);
118 }
119 */
120
121 void dsys::overlay(Texture *tex, const Vector2 &corner1, const Vector2 &corner2, const Color &color, GfxProg *pprog, bool handle_blending) {
122         glMatrixMode(GL_MODELVIEW);
123         glPushMatrix();
124         glLoadIdentity();
125         
126         glMatrixMode(GL_PROJECTION);
127         glPushMatrix();
128         glLoadIdentity();
129         glOrtho(0.0, 1.0, 1.0, 0.0, 0.0, 1.0);
130
131         set_lighting(false);
132         set_zbuffering(false);
133         set_backface_culling(false);
134         if(handle_blending) {
135                 set_alpha_blending(true);
136                 set_blend_func(BLEND_SRC_ALPHA, BLEND_ONE_MINUS_SRC_ALPHA);
137         }
138
139         if(tex) {
140                 enable_texture_unit(0);
141                 disable_texture_unit(1);
142                 set_texture_unit_color(0, TOP_REPLACE, TARG_TEXTURE, TARG_COLOR);
143                 set_texture_unit_alpha(0, TOP_MODULATE, TARG_TEXTURE, TARG_COLOR);
144                 set_texture_coord_index(0, 0);
145                 set_texture(0, tex);
146                 set_texture_addressing(0, TEXADDR_CLAMP, TEXADDR_CLAMP);
147
148                 for(int i=0; i<4; i++) {
149                         if(tex == dsys::tex[i]) {
150                                 glMatrixMode(GL_TEXTURE);
151                                 load_matrix_gl(dsys::tex_mat[i]);
152                                 break;
153                         }
154                 }
155         }
156
157         if(pprog) {
158                 set_gfx_program(pprog);
159         }
160         
161         glBegin(GL_QUADS);
162         glColor4f(color.r, color.g, color.b, color.a);
163         glTexCoord2f(0.0f, 1.0f);
164         glVertex3f(corner1.x, corner1.y, -0.5);
165         glTexCoord2f(1.0f, 1.0f);
166         glVertex3f(corner2.x, corner1.y, -0.5);
167         glTexCoord2f(1.0f, 0.0f);
168         glVertex3f(corner2.x, corner2.y, -0.5);
169         glTexCoord2f(0.0f, 0.0f);
170         glVertex3f(corner1.x, corner2.y, -0.5);
171         glEnd();
172
173         if(pprog) {
174                 set_gfx_program(0);
175         }
176         
177         if(tex) {
178                 for(int i=0; i<4; i++) {
179                         if(tex == dsys::tex[i]) {
180                                 glMatrixMode(GL_TEXTURE);
181                                 load_matrix_gl(Matrix4x4::identity_matrix);
182                                 break;
183                         }
184                 }
185                 set_texture_addressing(0, TEXADDR_WRAP, TEXADDR_WRAP);
186                 disable_texture_unit(0);
187         }
188         if(handle_blending) set_alpha_blending(false);
189         set_backface_culling(true);
190         set_zbuffering(true);
191         set_lighting(true);
192
193         glMatrixMode(GL_PROJECTION);
194         glPopMatrix();
195         glMatrixMode(GL_MODELVIEW);
196         glPopMatrix();
197 }
198
199 void dsys::negative(const Vector2 &corner1, const Vector2 &corner2) {
200         set_alpha_blending(true);
201         set_blend_func(BLEND_ONE_MINUS_DST_COLOR, BLEND_ZERO);
202         dsys::overlay(0, corner1, corner2, Color(1.0f, 1.0f, 1.0f, 1.0f), 0, false);
203         set_alpha_blending(false);
204 }
205
206 void dsys::flash(unsigned long time, unsigned long when, unsigned long dur, const Color &col) {
207         long start = when - dur/2;
208         long end = when + dur/2;
209         
210         if((long)time >= start && (long)time < end) {
211                 scalar_t t = (scalar_t)time / 1000.0;
212                 scalar_t dt = (scalar_t)dur / 1000.0;
213                 scalar_t wt = (scalar_t)when / 1000.0;
214                 scalar_t half_dt = dt / 2.0;
215                 
216                 scalar_t alpha = cos(pi * (t - wt) / half_dt);
217
218                 dsys::overlay(0, Vector3(0,0), Vector3(1,1), Color(col.r, col.g, col.b, alpha));
219         }
220 }
221
222
223
224 // ------------- Image Effect manager --------------
225
226 using std::list;
227 using namespace dsys;
228
229 static list<ImageFx*> fx_list;
230
231 void dsys::add_image_fx(ImageFx *fx) {
232         fx_list.push_back(fx);
233 }
234
235 void dsys::remove_image_fx(ImageFx *fx) {
236         fx_list.erase(find(fx_list.begin(), fx_list.end(), fx));
237 }
238
239 void dsys::apply_image_fx(unsigned long time) {
240         list<ImageFx*>::iterator iter = fx_list.begin();
241
242         while(iter != fx_list.end()) {
243                 (*iter++)->apply(time);
244         }
245 }
246
247
248 // -------------- Image Effect class --------------
249
250 ImageFx::ImageFx() {
251         time = 0;
252         duration = 0;
253 }
254
255 ImageFx::~ImageFx() {}
256
257 bool ImageFx::parse_script_args(const char **args) {
258         long t, d;
259
260         if(!args[0] || (t = str_to_time(args[0])) == -1) {
261                 return false;
262         }
263
264         if(!args[1] || (d = str_to_time(args[1])) == -1) {
265                 return false;
266         }
267
268         time = t;
269         duration = d;
270         return true;
271 }
272
273 void ImageFx::set_time(unsigned long time) {
274         this->time = time;
275 }
276
277 void ImageFx::set_duration(unsigned long dur) {
278         duration = dur;
279 }
280
281
282 // ------------- Negative (inverse video) effect ---------------
283
284 FxNegative::~FxNegative() {}
285
286 void FxNegative::apply(unsigned long time) {
287         if(time < this->time || time > this->time + duration) return;
288
289         negative();
290 }
291
292 // ------------ Screen Flash -------------
293
294 FxFlash::~FxFlash() {}
295
296 FxFlash::FxFlash() {
297         color = Color(1.0, 1.0, 1.0);
298 }
299
300 bool FxFlash::parse_script_args(const char **args) {
301         if(!ImageFx::parse_script_args(args)) {
302                 return false;
303         }
304
305         if(args[2]) {
306                 if(!str_to_color(args[2], &color)) return false;
307         }
308
309         return true;
310 }
311
312 void FxFlash::set_color(const Color &col) {
313         color = col;
314 }
315
316 void FxFlash::apply(unsigned long time) {
317         flash(time, this->time, duration, color);
318 }
319
320
321 // ------------- Fade in/out -------------
322
323 FxFade::FxFade() {
324         color1 = Color(0.0, 0.0, 0.0);
325         color2 = Color(1.0, 1.0, 1.0);
326         tex1 = tex2 = 0;
327 }
328
329 FxFade::~FxFade() {}
330
331 bool FxFade::parse_script_args(const char **args) {
332         if(!ImageFx::parse_script_args(args)) {
333                 return false;
334         }
335
336         if(args[2]) {
337                 if(!str_to_color(args[2], &color1)) return false;
338         }
339
340         if(args[3] && !(args[3][0] == '0' && args[3][1] == 0)) {
341                 // texture register? (t0, t1, t2, t3)
342                 if(args[3][0] == 't' && isdigit(args[3][1]) && !args[3][2]) {
343                         int reg = args[3][1] - '0';
344                         if(reg > 3) return false;
345                         tex1 = dsys::tex[reg];
346                 } else {        // or a texture from file?
347                         if(!(tex1 = get_texture(args[3]))) {
348                                 return false;
349                         }
350                 }
351         }
352
353         if(args[4]) {
354                 if(!str_to_color(args[4], &color2)) return false;
355         }
356
357         if(args[5] && !(args[5][0] == '0' && args[5][1] == 0)) {
358                 // texture register? (t0, t1, t2, t3)
359                 if(args[5][0] == 't' && isdigit(args[5][1]) && !args[5][2]) {
360                         int reg = args[5][1] - '0';
361                         if(reg > 3) return false;
362                         tex2 = dsys::tex[reg];
363                 } else {        // or a texture from file?
364                         if(!(tex2 = get_texture(args[5]))) {
365                                 return false;
366                         }
367                 }
368         }
369
370         return true;
371 }
372
373 void FxFade::apply(unsigned long time) {
374         if(time >= this->time && time < this->time + duration) {
375                 float fsec = (float)(time - this->time) / 1000.0;
376                 float t = fsec / ((float)duration / 1000.0);
377
378                 if(tex1) {
379                         set_texture(0, tex1);
380                         enable_texture_unit(0);
381                 }
382                 
383                 if(tex2) {
384                         set_texture(1, tex2);
385                         enable_texture_unit(1);
386
387                         set_texture_constant(1, t);
388                         set_texture_unit_color(1, TOP_LERP, TARG_PREV, TARG_TEXTURE, TARG_CONSTANT);
389                 }
390
391                 set_alpha_blending(true);
392                 set_blend_func(BLEND_SRC_ALPHA, BLEND_ONE_MINUS_SRC_ALPHA);
393                 set_lighting(false);
394
395                 Color col = color1 + (color2 - color1) * t;
396                 col.a = color1.a + (color2.a - color1.a) * t;
397                 
398                 draw_scr_quad(Vector2(0, 0), Vector2(1, 1), col);
399
400                 set_lighting(true);
401                 set_alpha_blending(false);
402
403                 if(tex1) disable_texture_unit(0);
404                 if(tex2) disable_texture_unit(1);
405         }
406 }
407
408 // ------------ Image Overlay ------------
409
410
411 FxOverlay::FxOverlay() {
412         tex = 0;
413         shader = 0;
414 }
415
416 FxOverlay::~FxOverlay() {
417         delete shader;
418 }
419
420 bool FxOverlay::parse_script_args(const char **args) {
421         if(!ImageFx::parse_script_args(args)) {
422                 return false;
423         }
424         if(!args[2]) return false;
425
426         // texture register? (t0, t1, t2, t3)
427         if(args[2][0] == 't' && isdigit(args[2][1]) && !args[2][2]) {
428                 int reg = args[2][1] - '0';
429                 if(reg > 3) return false;
430                 tex = dsys::tex[reg];
431         } else {        // or a texture from file?
432                 if(!(tex = get_texture(args[2]))) {
433                         return false;
434                 }
435         }
436
437         // check if a shader is specified
438         if(args[3]) {
439                 Shader sdr = get_shader(args[3], PROG_PIXEL);
440                 if(!sdr) {
441                         error("failed loading shader %s", args[3]);
442                         return false;
443                 }
444
445                 shader = new GfxProg(0, sdr);
446                 shader->link();
447         }
448         return true;
449 }
450
451 void FxOverlay::set_texture(Texture *tex) {
452         this->tex = tex;
453 }
454
455 void FxOverlay::set_shader(GfxProg *sdr) {
456         shader = sdr;
457 }
458
459 void FxOverlay::apply(unsigned long time) {
460         if(time >= this->time && time < this->time + duration) {
461                 if(shader) {
462                         float fsec = (float)(time - this->time) / 1000.0;
463                         shader->set_parameter("time", fsec);
464
465                         float t = fsec / ((float)duration / 1000.0f);
466                         shader->set_parameter("t", t);
467
468                         float ease = (t < 0.25 || t >= 0.75) ? fabs(sin(t * 2.0 * pi)) : 1.0;
469                         shader->set_parameter("ease", ease);
470
471                         float ease_sin = sin(t * pi);
472                         shader->set_parameter("ease_sin", ease_sin);
473                 }
474                 overlay(tex, Vector2(0, 0), Vector2(1, 1), Color(1, 1, 1), shader);
475         }
476 }
477
478
479
480 // just a little helper function to get a color out of an <r,g,b> string
481 static bool str_to_color(const char *str, Color *col) {
482         // get red
483         if(*str++ != '<') return false;
484         if(!isdigit(*str) && *str != '.') return false;
485         col->r = atof(str);
486
487         while(isdigit(*str) || *str == '.') str++;
488         if(*str++ != ',') return false;
489
490         // get green
491         if(!isdigit(*str) && *str != '.') return false;
492         col->g = atof(str);
493
494         while(isdigit(*str) || *str == '.') str++;
495         if(*str++ != ',') return false;
496
497         // get blue
498         if(!isdigit(*str) && *str != '.') return false;
499         col->b = atof(str);
500         
501         while(isdigit(*str) || *str == '.') str++;
502         if(*str != ',') {
503                 col->a = 1.0;
504                 return *str == '>' ? true : false;
505         }
506
507         // get alpha
508         if(!isdigit(*++str) && *str != '.') return false;
509         col->a = atof(str);
510         
511         while(isdigit(*str) || *str == '.') str++;
512         return *str == '>' ? true : false;
513 }