2 This file is part of the 3dengfx demo system.
4 Copyright (c) 2004, 2005 John Tsiombikas <nuclear@siggraph.org>
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.
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.
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
21 #include "3dengfx_config.h"
28 #include "3dengfx/3dengfx.hpp"
29 #include "common/err_msg.h"
32 static bool str_to_color(const char *str, Color *col);
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);
38 set_alpha_blending(true);
40 set_blend_func(BLEND_SRC_ALPHA, BLEND_ONE);
42 set_blend_func(BLEND_SRC_ALPHA, BLEND_ONE_MINUS_SRC_ALPHA);
46 int quad_count = (int)(ammount * 20.0f);
47 float dscale = (ammount - 1.0f) / (float)quad_count;
49 for(int i=0; i<quad_count; i++) {
50 Vector2 v1 = c1, v2 = c2;
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);
64 set_alpha_blending(false);
67 void dsys::dir_blur(Texture *tex, float ammount, int dir) {
68 Vector2 c1(0.0f, 1.0f), c2(1.0f, 0.0f);
71 int quad_count = (int)(ammount * 100.0f);
72 float offs_inc = ammount / (float)(quad_count/2);
74 for(int i=0; i<quad_count/2; i++) {
75 Vector2 off_vec = dir == BLUR_DIR_X ? Vector2(offs, 0) : Vector2(0, offs);
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));
85 static Vector2 *blur_pos;
88 void dsys::blur(Texture *tex, float ammount, bool additive) {
91 set_alpha_blending(true);
93 set_blend_func(BLEND_SRC_ALPHA, BLEND_ONE);
95 set_blend_func(BLEND_SRC_ALPHA, BLEND_ONE_MINUS_SRC_ALPHA);
98 int quad_count = (int)(ammount * 700.0f);
100 if(quad_count != pos_count) {
101 if(blur_pos) delete [] blur_pos;
102 blur_pos = new Vector2[quad_count];
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;
108 pos_count = quad_count;
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);
117 set_alpha_blending(false);
121 void dsys::overlay(Texture *tex, const Vector2 &corner1, const Vector2 &corner2, const Color &color, GfxProg *pprog, bool handle_blending) {
122 glMatrixMode(GL_MODELVIEW);
126 glMatrixMode(GL_PROJECTION);
129 glOrtho(0.0, 1.0, 1.0, 0.0, 0.0, 1.0);
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);
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);
146 set_texture_addressing(0, TEXADDR_CLAMP, TEXADDR_CLAMP);
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]);
158 set_gfx_program(pprog);
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);
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);
185 set_texture_addressing(0, TEXADDR_WRAP, TEXADDR_WRAP);
186 disable_texture_unit(0);
188 if(handle_blending) set_alpha_blending(false);
189 set_backface_culling(true);
190 set_zbuffering(true);
193 glMatrixMode(GL_PROJECTION);
195 glMatrixMode(GL_MODELVIEW);
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);
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;
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;
216 scalar_t alpha = cos(pi * (t - wt) / half_dt);
218 dsys::overlay(0, Vector3(0,0), Vector3(1,1), Color(col.r, col.g, col.b, alpha));
224 // ------------- Image Effect manager --------------
227 using namespace dsys;
229 static list<ImageFx*> fx_list;
231 void dsys::add_image_fx(ImageFx *fx) {
232 fx_list.push_back(fx);
235 void dsys::remove_image_fx(ImageFx *fx) {
236 fx_list.erase(find(fx_list.begin(), fx_list.end(), fx));
239 void dsys::apply_image_fx(unsigned long time) {
240 list<ImageFx*>::iterator iter = fx_list.begin();
242 while(iter != fx_list.end()) {
243 (*iter++)->apply(time);
248 // -------------- Image Effect class --------------
255 ImageFx::~ImageFx() {}
257 bool ImageFx::parse_script_args(const char **args) {
260 if(!args[0] || (t = str_to_time(args[0])) == -1) {
264 if(!args[1] || (d = str_to_time(args[1])) == -1) {
273 void ImageFx::set_time(unsigned long time) {
277 void ImageFx::set_duration(unsigned long dur) {
282 // ------------- Negative (inverse video) effect ---------------
284 FxNegative::~FxNegative() {}
286 void FxNegative::apply(unsigned long time) {
287 if(time < this->time || time > this->time + duration) return;
292 // ------------ Screen Flash -------------
294 FxFlash::~FxFlash() {}
297 color = Color(1.0, 1.0, 1.0);
300 bool FxFlash::parse_script_args(const char **args) {
301 if(!ImageFx::parse_script_args(args)) {
306 if(!str_to_color(args[2], &color)) return false;
312 void FxFlash::set_color(const Color &col) {
316 void FxFlash::apply(unsigned long time) {
317 flash(time, this->time, duration, color);
321 // ------------- Fade in/out -------------
324 color1 = Color(0.0, 0.0, 0.0);
325 color2 = Color(1.0, 1.0, 1.0);
331 bool FxFade::parse_script_args(const char **args) {
332 if(!ImageFx::parse_script_args(args)) {
337 if(!str_to_color(args[2], &color1)) return false;
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]))) {
354 if(!str_to_color(args[4], &color2)) return false;
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]))) {
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);
379 set_texture(0, tex1);
380 enable_texture_unit(0);
384 set_texture(1, tex2);
385 enable_texture_unit(1);
387 set_texture_constant(1, t);
388 set_texture_unit_color(1, TOP_LERP, TARG_PREV, TARG_TEXTURE, TARG_CONSTANT);
391 set_alpha_blending(true);
392 set_blend_func(BLEND_SRC_ALPHA, BLEND_ONE_MINUS_SRC_ALPHA);
395 Color col = color1 + (color2 - color1) * t;
396 col.a = color1.a + (color2.a - color1.a) * t;
398 draw_scr_quad(Vector2(0, 0), Vector2(1, 1), col);
401 set_alpha_blending(false);
403 if(tex1) disable_texture_unit(0);
404 if(tex2) disable_texture_unit(1);
408 // ------------ Image Overlay ------------
411 FxOverlay::FxOverlay() {
416 FxOverlay::~FxOverlay() {
420 bool FxOverlay::parse_script_args(const char **args) {
421 if(!ImageFx::parse_script_args(args)) {
424 if(!args[2]) return false;
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]))) {
437 // check if a shader is specified
439 Shader sdr = get_shader(args[3], PROG_PIXEL);
441 error("failed loading shader %s", args[3]);
445 shader = new GfxProg(0, sdr);
451 void FxOverlay::set_texture(Texture *tex) {
455 void FxOverlay::set_shader(GfxProg *sdr) {
459 void FxOverlay::apply(unsigned long time) {
460 if(time >= this->time && time < this->time + duration) {
462 float fsec = (float)(time - this->time) / 1000.0;
463 shader->set_parameter("time", fsec);
465 float t = fsec / ((float)duration / 1000.0f);
466 shader->set_parameter("t", t);
468 float ease = (t < 0.25 || t >= 0.75) ? fabs(sin(t * 2.0 * pi)) : 1.0;
469 shader->set_parameter("ease", ease);
471 float ease_sin = sin(t * pi);
472 shader->set_parameter("ease_sin", ease_sin);
474 overlay(tex, Vector2(0, 0), Vector2(1, 1), Color(1, 1, 1), shader);
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) {
483 if(*str++ != '<') return false;
484 if(!isdigit(*str) && *str != '.') return false;
487 while(isdigit(*str) || *str == '.') str++;
488 if(*str++ != ',') return false;
491 if(!isdigit(*str) && *str != '.') return false;
494 while(isdigit(*str) || *str == '.') str++;
495 if(*str++ != ',') return false;
498 if(!isdigit(*str) && *str != '.') return false;
501 while(isdigit(*str) || *str == '.') str++;
504 return *str == '>' ? true : false;
508 if(!isdigit(*++str) && *str != '.') return false;
511 while(isdigit(*str) || *str == '.') str++;
512 return *str == '>' ? true : false;