source: trunk/tools/neercs/video/render.cpp @ 1882

Last change on this file since 1882 was 1882, checked in by rez, 8 years ago

fixed a stupid error

File size: 38.2 KB
Line 
1//
2// Neercs
3//
4
5#if defined HAVE_CONFIG_H
6#   include "config.h"
7#endif
8
9#if defined _XBOX
10#   define _USE_MATH_DEFINES /* for M_PI */
11#   include <xtl.h>
12#elif defined _WIN32
13#   define _USE_MATH_DEFINES /* for M_PI */
14#   define WIN32_LEAN_AND_MEAN
15#   include <windows.h>
16#endif
17
18#include <cmath>
19#include <cstdio>
20#include <cstdlib>
21#include <ctime>
22#include <string>
23
24#include "core.h"
25#include "lolgl.h"
26
27using namespace std;
28using namespace lol;
29
30#include "../neercs.h"
31#include "render.h"
32#include "text-render.h"
33
34extern char const *lolfx_simple;
35extern char const *lolfx_blurh;
36extern char const *lolfx_blurv;
37extern char const *lolfx_glow;
38extern char const *lolfx_remanency;
39extern char const *lolfx_copper;
40extern char const *lolfx_color;
41extern char const *lolfx_noise;
42extern char const *lolfx_postfx;
43
44#define PID M_PI/180.0f    // pi ratio
45#define CR 1.0f/256.0f     // color ratio
46
47/*
48 * Various variables
49 */
50
51int active = true;         // window active flag
52float nearplane = 0.1f;    // nearplane
53float farplane = 1000.0f;  // farplane
54int polygon_fillmode = GL_FILL; // fill mode
55/* timer variable */
56float timer = 0;           // timer
57/* window variable */
58ivec2 screen_size;         // screen size
59vec3 screen_color = CR * vec3(32, 32, 32); // screen color
60/* object variable */
61float main_angle = 0.0f;   // main angle
62float part_angle = 0.0f;   // part angle
63float fx_angle;            // current angle
64/* fs_quad variable */
65float fs_quad_vtx[] = {-1.0f, 1.0f, 0, 1.0f, -1.0f, -1.0f, 0, 1.0f, 1.0f, -1.0f, 0, 1.0f, 1.0f, 1.0f, 0, 1.0f};
66float fs_quad_tex[] = {0, 1.0f, 0, 0, 1.0f, 0, 1.0f, 1.0f};
67/* flash variable */
68bool flash_flag = false;   // flag
69float flash_angle = 0;     // angle
70float flash_value = 0;     // value
71float flash_speed = 2.0f;  // speed
72/* fade variable */
73bool fade_flag = false;    // flag
74float fade_angle = 0;      // angle
75float fade_value = 0;      // value
76float fade_speed = 0.2f;   // speed
77/* sync variable */
78bool sync_flag = false;    // flagsh
79float sync_angle = 0;      // angle
80float sync_value = 0;      // value
81float sync_speed = 1.0f;   // speed
82/* beat variable */
83bool beat_flag = false;    // flag
84float beat_angle = 0;      // angle
85float beat_value = 0;      // value
86float beat_speed = 2.0f;   // speed
87/* common variable */
88float value, angle, radius, scale, speed;
89/* shader variable */
90vec2 buffer(0.7f,0.3f);         // [new frame mix,old frame mix]
91vec2 remanency(0.5f,0.5f);      // remanency [source mix,buffer mix]
92vec2 glow_mix(0.6f,0.4f);       // glow mix [source mix,glow mix]
93vec2 glow_large(2.0f,2.0f);     // large glow radius [center,corner]
94vec2 glow_small(1.0f,1.0f);     // small glow radius [center,corner]
95vec2 blur(0.5f,0.5f);           // blur radius [center,corner]
96vec4 copper(0.125,0.125,32,64); // copper [base,variable,repeat x,repeat y]
97vec3 color_filter(0.9f,0.9f,1.0f);    // color filter [red,green,blue]
98vec3 color_color(1.2f,1.1f,0.25f);    // color modifier [brightness,contrast,grayscale]
99vec2 noise_offset(2.0f,2.0f);         // random line [horizontal,vertical]
100float noise_noise = 0.25f;            // noise
101vec3 noise_retrace(1.0f,1.0f,0.5f);   // retrace [strength,length,speed]
102vec2 postfx_deform(0.7f,0.54f);       // deformation [ratio,zoom]
103float postfx_vignetting = -0.5f;      // vignetting strength
104float postfx_aberration = 3.0f;       // chromatic aberration
105vec4 postfx_ghost1(0.01f,0.0f,0.1f,-0.25f);     // ghost picture 1 [position x,position y,position z,strength]
106vec4 postfx_ghost2(0.02f,0.0f,0.1f,0.25f);      // ghost picture 2 [position x,position y,position z,strength]
107vec4 postfx_moire_h(0.75f,-0.25f,0.0f,1.0f);    // vertical moire [base,variable,repeat x,repeat y]
108vec4 postfx_moire_v(0.75f,-0.25f,1.0f,1.5f);    // horizontal moire [base,variable,repeat x,repeat y]
109vec4 postfx_scanline_h(0.75f, 0.25f,0.0f,2.0f); // vertical scanline [base,variable,repeat x,repeat y]
110vec4 postfx_scanline_v(0.75f,-0.25f,2.0f,0.0f); // horizontal scanline [base,variable,repeat x,repeat y]
111vec2 postfx_corner(0.75f,0.95f);      // corner [radius,blur]
112/* text variable */
113ivec2 ratio_2d(2,4);            // 2d ratio
114ivec2 map_size(256,256);        // texture map size
115ivec2 font_size(8,8);           // font size
116ivec2 canvas_char(0,0);         // canvas char number
117ivec2 canvas_size(0,0);         // caca size
118/* window variable */
119ivec2 border = 18 * ratio_2d;   // border width
120/* setup variable */
121bool setup_switch = false;      // switch [option/item]
122int setup_n = 0;                // item/option number
123int setup_h = 6;                // height
124int setup_cursor = 0;           // cursor position
125int setup_option_i = 0;         // selected option
126int setup_option_n = 10;        // option number
127int setup_option_p = 0;         // option position
128int setup_item_i = 0;           // selected item
129int setup_item_n = 8;           // item number
130int setup_item_p = 0;           // item position
131int setup_item_key = 0;         // item array key
132ivec2 setup_p(1,1);             // position [x,y]
133ivec3 setup_size(30,7,12);      // size [w,h,split]
134ivec2 setup_color(0x678,0x234); // color [foreground,background]
135char const *setup_text[] = {
136    "main",
137        "2d ratio w",
138        "2d ratio h",
139        "border w",
140        "border h",
141        "",
142        "",
143        "",
144        "",
145    "remanency",
146        "enable",
147        "buffer new frame",
148        "buffer old frame",
149        "source mix",
150        "buffer mix",
151        "",
152        "",
153        "",
154    "glow",
155        "enable",
156        "source mix",
157        "glow mix",
158        "large center",
159        "large corner",
160        "small center",
161        "small corner",
162        "",
163    "blur",
164        "enable",
165        "blur center",
166        "blur corner",
167        "",
168        "",
169        "",
170        "",
171        "",
172    "screen",
173        "enable",
174        "deform ratio",
175        "zoom base",
176        "corner radius",
177        "corner blur",
178        "vignetting",
179        "",
180        "",
181    "color",
182        "filter red",
183        "filter green",
184        "filter blue",
185        "brightness",
186        "contrast",
187        "grayscale",
188        "aberration",
189        "",
190    "noise",
191        "enable",
192        "offset h",
193        "offset v",
194        "noise",
195        "retrace strength",
196        "retrace length",
197        "retrace speed",
198        "",
199    "ghost",
200        "back x",
201        "back y",
202        "back z",
203        "back strength",
204        "front x",
205        "front y",
206        "front z",
207        "front strength",
208    "moire",
209        "h base",
210        "h variable",
211        "h repeat x",
212        "h repeat y",
213        "v base",
214        "v variable",
215        "v repeat x",
216        "v repeat y",
217    "scanline",
218        "h base",
219        "h variable",
220        "h repeat x",
221        "h repeat y",
222        "v base",
223        "v variable",
224        "v repeat x",
225        "v repeat y"
226    };
227
228vec4 setup_var[]={ // setup variable [start,end,step,value]
229    vec4(0), /* main */
230        vec4( 1,  8, 1, ratio_2d.x),
231        vec4( 1,  8, 1, ratio_2d.y),
232        vec4( 0, 64, 1, border.x / ratio_2d.x),
233        vec4( 0, 64, 1, border.y / ratio_2d.y),
234        vec4(0),
235        vec4(0),
236        vec4(0),
237        vec4(0),
238    vec4(0), /* remanency */
239        vec4(0, 1, 1, 1),
240        vec4(0.0f, 1.0f, 0.1f, buffer.x),
241        vec4(0.0f, 1.0f, 0.1f, buffer.y),
242        vec4(0.0f, 1.0f, 0.1f, remanency.x),
243        vec4(0.0f, 1.0f, 0.1f, remanency.y),
244        vec4(0),
245        vec4(0),
246        vec4(0),
247    vec4(0), /* glow */
248        vec4(0, 1, 1, 1),
249        vec4(0.0f, 1.0f, 0.1f, glow_mix.x),
250        vec4(0.0f, 1.0f, 0.1f, glow_mix.y),
251        vec4(0.0f, 4.0f, 0.1f, glow_large.x),
252        vec4(0.0f, 4.0f, 0.1f, glow_large.y),
253        vec4(0.0f, 2.0f, 0.1f, glow_small.x),
254        vec4(0.0f, 2.0f, 0.1f, glow_small.y),
255        vec4(0),
256    vec4(0), /* blur */
257        vec4(0, 1, 1, 1),
258        vec4(0.0f, 2.0f, 0.05f, blur.x),
259        vec4(0.0f, 2.0f, 0.05f, blur.y),
260        vec4(0),
261        vec4(0),
262        vec4(0),
263        vec4(0),
264        vec4(0),
265    vec4(0), /* screen */
266        vec4( 0, 1, 1, 1),
267        vec4( 0.0f, 1.0f, 0.05f, postfx_deform.x),
268        vec4( 0.5f, 0.7f, 0.01f, postfx_deform.y),
269        vec4( 0.0f, 1.0f, 0.05f, postfx_corner.x),
270        vec4( 0.0f, 1.0f, 0.05f, postfx_corner.y),
271        vec4(-1.0f, 1.0f, 0.10f, postfx_vignetting),
272        vec4(0),
273        vec4(0),
274    vec4(0), /* color */
275        vec4(0.0f, 1.0f, 0.05f, color_filter.x),
276        vec4(0.0f, 1.0f, 0.05f, color_filter.y),
277        vec4(0.0f, 1.0f, 0.05f, color_filter.z),
278        vec4(0.0f, 2.0f, 0.05f, color_color.x),
279        vec4(0.0f, 2.0f, 0.05f, color_color.y),
280        vec4(0.0f, 1.0f, 0.05f, color_color.z),
281        vec4(0.0f, 8.0f, 0.50f, postfx_aberration),
282        vec4(0),
283    vec4(0), /* noise */
284        vec4( 0, 1, 1, 1),
285        vec4(0.0f, 4.0f, 0.50f, noise_offset.x),
286        vec4(0.0f, 4.0f, 0.50f, noise_offset.y),
287        vec4(0.0f, 0.5f, 0.05f, noise_noise),
288        vec4(0.0f, 4.0f, 0.25f, noise_retrace.x),
289        vec4(0.0f, 8.0f, 0.50f, noise_retrace.y),
290        vec4(0.0f, 4.0f, 0.25f, noise_retrace.z),
291        vec4(0),
292    vec4(0), /* ghost */
293        vec4(-0.5f, 0.5f, 0.01f, postfx_ghost1.x),
294        vec4(-0.5f, 0.5f, 0.01f, postfx_ghost1.y),
295        vec4(-2.0f, 2.0f, 0.10f, postfx_ghost1.z),
296        vec4(-1.0f, 1.0f, 0.05f, postfx_ghost1.w),
297        vec4(-0.5f, 0.5f, 0.01f, postfx_ghost2.x),
298        vec4(-0.5f, 0.5f, 0.01f, postfx_ghost2.y),
299        vec4(-2.0f, 2.0f, 0.10f, postfx_ghost2.z),
300        vec4(-1.0f, 1.0f, 0.05f, postfx_ghost2.w),
301    vec4(0), /* moire */
302        vec4( 0.5f, 1.0f, 0.05f, postfx_moire_h.x),
303        vec4(-0.5f, 0.5f, 0.05f, postfx_moire_h.y),
304        vec4( 0.0f, 4.0f, 0.50f, postfx_moire_h.z),
305        vec4( 0.0f, 4.0f, 0.50f, postfx_moire_h.w),
306        vec4( 0.5f, 1.0f, 0.05f, postfx_moire_v.x),
307        vec4(-0.5f, 0.5f, 0.05f, postfx_moire_v.y),
308        vec4( 0.0f, 4.0f, 0.50f, postfx_moire_v.z),
309        vec4( 0.0f, 4.0f, 0.50f, postfx_moire_v.w),
310    vec4(0), /* scanline */
311        vec4( 0.5f, 1.0f, 0.05f, postfx_scanline_h.x),
312        vec4(-0.5f, 0.5f, 0.05f, postfx_scanline_h.y),
313        vec4( 0.0f, 4.0f, 0.50f, postfx_scanline_h.z),
314        vec4( 0.0f, 4.0f, 0.50f, postfx_scanline_h.w),
315        vec4( 0.5f, 1.0f, 0.05f, postfx_scanline_v.x),
316        vec4(-0.5f, 0.5f, 0.05f, postfx_scanline_v.y),
317        vec4( 0.0f, 4.0f, 0.50f, postfx_scanline_v.z),
318        vec4( 0.0f, 4.0f, 0.50f, postfx_scanline_v.w),
319    vec4(0)
320    };
321
322void Render::UpdateVar()
323{
324    int k = 1; /* main */
325    ratio_2d = vec2(setup_var[k].w, setup_var[k + 1].w); k += 2;
326    border = vec2(setup_var[k].w, setup_var[k + 1].w) * ratio_2d; k += 2;
327    k += 5; /* remanency */
328    m_shader_remanency = (setup_var[k].w == 1) ? true : false; k++;
329    buffer = vec2(setup_var[k].w, setup_var[k + 1].w); k += 2;
330    remanency = vec2(setup_var[k].w, setup_var[k + 1].w); k += 2;
331    k += 4; /* glow */
332    m_shader_glow = (setup_var[k].w == 1) ? true : false; k++;
333    glow_mix = vec2(setup_var[k].w, setup_var[k + 1].w); k += 2;
334    glow_large = vec2(setup_var[k].w, setup_var[k + 1].w); k += 2;
335    glow_small = vec2(setup_var[k].w, setup_var[k + 1].w); k += 2;
336    k += 2; /* blur */
337    m_shader_blur = (setup_var[k].w == 1) ? true : false; k++;
338    blur = vec2(setup_var[k].w, setup_var[k + 1].w); k += 2;
339    k += 6; /* screen */
340    m_shader_postfx = (setup_var[k].w == 1) ? true : false; k++;
341    postfx_deform = vec2(setup_var[k].w, setup_var[k + 1].w); k += 2;
342    postfx_corner = vec2(setup_var[k].w, setup_var[k + 1].w); k += 2;
343    postfx_vignetting = setup_var[k].w; k++;
344    k += 3; /* color */
345    color_filter = vec3(setup_var[k].w, setup_var[k + 1].w, setup_var[k + 2].w); k += 3;
346    color_color = vec3(setup_var[k].w, setup_var[k + 1].w, setup_var[k + 2].w); k += 3;
347    postfx_aberration = setup_var[k].w; k++;
348    k += 2; /* noise */
349    m_shader_noise = (setup_var[k].w == 1) ? true : false; k++;
350    noise_offset = vec2(setup_var[k].w, setup_var[k + 1].w); k += 2;
351    noise_noise = setup_var[k].w; k++;
352    noise_retrace = vec3(setup_var[k].w, setup_var[k + 1].w, setup_var[k + 2].w); k += 3;
353    k += 2; /* ghost */
354    postfx_ghost1 = vec4(setup_var[k].w, setup_var[k + 1].w, setup_var[k + 2].w, setup_var[k + 3].w); k += 4;
355    postfx_ghost2 = vec4(setup_var[k].w, setup_var[k + 1].w, setup_var[k + 2].w, setup_var[k + 3].w); k += 4;
356    k += 1; /* moire */
357    postfx_moire_h = vec4(setup_var[k].w, setup_var[k + 1].w, setup_var[k + 2].w, setup_var[k + 3].w); k += 4;
358    postfx_moire_v = vec4(setup_var[k].w, setup_var[k + 1].w, setup_var[k + 2].w, setup_var[k + 3].w); k += 4;
359    k += 1; /* scanline */
360    postfx_scanline_h = vec4(setup_var[k].w, setup_var[k + 1].w, setup_var[k + 2].w, setup_var[k + 3].w); k += 4;
361    postfx_scanline_v = vec4(setup_var[k].w, setup_var[k + 1].w, setup_var[k + 2].w, setup_var[k + 3].w); k += 4;
362
363    UpdateSize();
364}
365
366void Render::UpdateSize()
367{
368    screen_size = Video::GetSize();
369    //border.y = border.x; // enabled to get same border everywhere
370    canvas_char = (screen_size - border * 2) / (font_size * ratio_2d);
371    canvas_size = canvas_char * font_size * ratio_2d;
372
373    border = (screen_size - canvas_size) / 2;
374
375    caca_set_canvas_size(m_caca, canvas_char.x, canvas_char.y);
376
377    setup_p = (canvas_char - setup_size.xy) / 2;
378}
379
380int calc_item_length()
381{
382    int n = !setup_switch ? setup_option_n : setup_item_n;
383    for (int i = 0; i < n; i++)
384    {
385        int k = !setup_switch ? (i * (setup_item_n + 1)) : (setup_option_i * (setup_item_n + 1) + 1 + i);
386        if (setup_text[k][0] == '\0') return i - 1;
387    }
388    return n - 1;
389}
390
391Shader *shader_simple;
392Shader *shader_blur_h, *shader_blur_v, *shader_glow;
393Shader *shader_remanency, *shader_copper, *shader_color;
394Shader *shader_noise, *shader_postfx;
395// shader variables
396ShaderUniform shader_simple_texture;
397ShaderUniform shader_blur_h_texture,
398              shader_blur_h_radius,
399              shader_blur_v_texture,
400              shader_blur_v_radius;
401ShaderUniform shader_glow_glow,
402              shader_glow_source,
403              shader_glow_mix;
404ShaderUniform shader_remanency_source,
405              shader_remanency_buffer,
406              shader_remanency_mix;
407ShaderUniform shader_copper_texture,
408              shader_copper_screen_size,
409              shader_copper_time,
410              shader_copper_copper;
411ShaderUniform shader_color_texture,
412              shader_color_screen_size,
413              shader_color_filter,
414              shader_color_color,
415              shader_color_flash;
416ShaderUniform shader_noise_texture,
417              shader_noise_screen_size,
418              shader_noise_time,
419              shader_noise_offset,
420              shader_noise_noise,
421              shader_noise_retrace;
422ShaderUniform shader_postfx_texture,
423              shader_postfx_texture_2d,
424              shader_postfx_screen_size,
425              shader_postfx_time,
426              shader_postfx_deform,
427              shader_postfx_ghost1,
428              shader_postfx_ghost2,
429              shader_postfx_vignetting,
430              shader_postfx_aberration,
431              shader_postfx_moire_h,
432              shader_postfx_moire_v,
433              shader_postfx_scanline_h,
434              shader_postfx_scanline_v,
435              shader_postfx_corner,
436              shader_postfx_sync;
437
438FrameBuffer *fbo_back, *fbo_front, *fbo_buffer;
439FrameBuffer *fbo_blur_h, *fbo_blur_v, *fbo_tmp;
440
441TextRender *text_render;
442
443void Render::TraceQuad()
444{
445    glLoadIdentity();
446    glDrawArrays(GL_QUADS, 0, 4);
447}
448
449void Render::ShaderSimple(FrameBuffer *fbo_output, int n)
450{
451    shader_simple->Bind();
452    shader_simple->SetUniform(shader_simple_texture, fbo_output->GetTexture(), n);
453    TraceQuad();
454    shader_simple->Unbind();
455}
456
457int Render::InitDraw(void)
458{
459    glDepthMask(GL_TRUE);     // do not write z-buffer
460    glEnable(GL_CULL_FACE);   // disable cull face
461    glCullFace(GL_BACK);      // don't draw front face
462
463    /* Initialise framebuffer objects */
464    fbo_back = new FrameBuffer(screen_size);
465    fbo_front = new FrameBuffer(screen_size);
466    fbo_buffer = new FrameBuffer(screen_size);
467    fbo_blur_h = new FrameBuffer(screen_size);
468    fbo_blur_v = new FrameBuffer(screen_size);
469    fbo_tmp = new FrameBuffer(screen_size);
470    // shader simple
471    shader_simple = Shader::Create(lolfx_simple);
472    shader_simple_texture = shader_simple->GetUniformLocation("texture");
473    // shader glow
474    shader_glow = Shader::Create(lolfx_glow);
475    shader_glow_glow = shader_glow->GetUniformLocation("glow");
476    shader_glow_source = shader_glow->GetUniformLocation("source");
477    shader_glow_mix = shader_glow->GetUniformLocation("mix");
478    // shader blur horizontal
479    shader_blur_h = Shader::Create(lolfx_blurh);
480    shader_blur_h_texture = shader_blur_h->GetUniformLocation("texture");
481    shader_blur_h_radius = shader_blur_h->GetUniformLocation("radius");
482    // shader blur vertical
483    shader_blur_v = Shader::Create(lolfx_blurv);
484    shader_blur_v_texture = shader_blur_v->GetUniformLocation("texture");
485    shader_blur_v_radius = shader_blur_v->GetUniformLocation("radius");
486    // shader remanency
487    shader_remanency = Shader::Create(lolfx_remanency);
488    shader_remanency_source = shader_remanency->GetUniformLocation("source");
489    shader_remanency_buffer = shader_remanency->GetUniformLocation("buffer");
490    shader_remanency_mix = shader_remanency->GetUniformLocation("mix");
491    // shader copper
492    shader_copper = Shader::Create(lolfx_copper);
493    shader_copper_texture = shader_copper->GetUniformLocation("texture");
494    shader_copper_screen_size = shader_copper->GetUniformLocation("screen_size");
495    shader_copper_time = shader_copper->GetUniformLocation("time");
496    shader_copper_copper = shader_copper->GetUniformLocation("copper");
497    // shader color
498    shader_color = Shader::Create(lolfx_color);
499    shader_color_texture = shader_color->GetUniformLocation("texture");
500    shader_color_screen_size = shader_color->GetUniformLocation("screen_size");
501    shader_color_filter = shader_color->GetUniformLocation("filter");
502    shader_color_color = shader_color->GetUniformLocation("color");
503    shader_color_flash = shader_color->GetUniformLocation("flash");
504    // shader noise
505    shader_noise = Shader::Create(lolfx_noise);
506    shader_noise_texture = shader_noise->GetUniformLocation("texture");
507    shader_noise_screen_size = shader_noise->GetUniformLocation("screen_size");
508    shader_noise_time = shader_noise->GetUniformLocation("time");
509    shader_noise_offset = shader_noise->GetUniformLocation("offset");
510    shader_noise_noise = shader_noise->GetUniformLocation("noise");
511    shader_noise_retrace = shader_noise->GetUniformLocation("retrace");
512    // shader postfx
513    shader_postfx = Shader::Create(lolfx_postfx);
514    shader_postfx_texture = shader_postfx->GetUniformLocation("texture");
515    shader_postfx_texture_2d = shader_postfx->GetUniformLocation("texture_2d");
516    shader_postfx_screen_size = shader_postfx->GetUniformLocation("screen_size");
517    shader_postfx_time = shader_postfx->GetUniformLocation("time");
518    shader_postfx_deform = shader_postfx->GetUniformLocation("deform");
519    shader_postfx_ghost1 = shader_postfx->GetUniformLocation("ghost1");
520    shader_postfx_ghost2 = shader_postfx->GetUniformLocation("ghost2");
521    shader_postfx_vignetting = shader_postfx->GetUniformLocation("vignetting");
522    shader_postfx_aberration = shader_postfx->GetUniformLocation("aberration");
523    shader_postfx_moire_h = shader_postfx->GetUniformLocation("moire_h");
524    shader_postfx_moire_v = shader_postfx->GetUniformLocation("moire_v");
525    shader_postfx_scanline_h = shader_postfx->GetUniformLocation("scanline_h");
526    shader_postfx_scanline_v = shader_postfx->GetUniformLocation("scanline_v");
527    shader_postfx_corner = shader_postfx->GetUniformLocation("corner");
528    shader_postfx_sync = shader_postfx->GetUniformLocation("sync");
529
530    return true;
531}
532
533int Render::CreateGLWindow()
534{
535    UpdateSize();
536    InitDraw();
537    return true;
538}
539
540Render::Render(caca_canvas_t *caca)
541  : m_caca(caca),
542    m_ready(false),
543    m_pause(false),
544    m_polygon(true),
545    m_setup(true),
546    m_shader(true),
547    m_shader_glow(true),
548    m_shader_blur(true),
549    m_shader_remanency(true),
550    m_shader_copper(true),
551    m_shader_color(true),
552    m_shader_noise(true),
553    m_shader_postfx(true)
554{
555    text_render = new TextRender(m_caca, font_size);
556}
557
558void Render::TickGame(float seconds)
559{
560    Entity::TickGame(seconds);
561}
562
563void Render::Pause()
564{
565    m_pause=!m_pause;
566}
567
568void Render::TickDraw(float seconds)
569{
570    /* keyboard manager */
571    if (Input::WasReleased(Key::Escape))
572    {
573        Ticker::Shutdown();
574    }
575    if (Input::WasPressed(Key::F1))
576    {
577        m_setup = !m_setup;
578        if (m_setup) setup_n = calc_item_length();
579        sync_flag = true;
580        sync_angle = main_angle;
581    }
582    if (Input::WasPressed(Key::F2))
583    {
584        m_polygon = !m_polygon;
585        polygon_fillmode = (m_polygon)?GL_FILL:GL_LINE;
586        glPolygonMode(GL_FRONT, polygon_fillmode);
587    }
588   if (Input::WasPressed(Key::Tab))
589    {
590        if (m_setup)
591        {
592            setup_switch = !setup_switch;
593            setup_n = calc_item_length();
594            setup_cursor = (!setup_switch?setup_option_i:setup_item_i) - (!setup_switch?setup_option_p:setup_item_p);
595        }
596    }
597    if (Input::WasPressed(Key::Up))
598    {
599        if (m_setup)
600        {
601            if (!setup_switch)
602            {
603                if (setup_cursor > 0)
604                {
605                    setup_cursor--;
606                }
607                else
608                {
609                    if ((setup_option_p > 0) && setup_cursor == 0) setup_option_p--;
610                }
611                if (setup_option_i > 0)
612                {
613                    setup_option_i--;
614                }
615                else
616                {
617                    setup_option_i = setup_option_n - 1;
618                    setup_option_p = setup_option_n - setup_h;
619                    setup_cursor = setup_h - 1;
620                }
621                setup_item_i = 0;
622            }
623            else
624            {
625                setup_item_i--;
626                if (setup_item_i < 0) setup_item_i = setup_n;
627            }
628        }
629    }
630    if (Input::WasPressed(Key::Down))
631    {
632        if (m_setup)
633        {
634            if (!setup_switch)
635            {
636                if (setup_cursor < setup_h - 1)
637                {
638                    setup_cursor++;
639                }
640                else
641                {
642                    if ((setup_option_p + setup_h < setup_option_n) && setup_cursor == setup_h - 1) setup_option_p++;
643                }
644                if (setup_option_i < setup_option_n - 1)
645                {
646                    setup_option_i++;
647                }
648                else
649                {
650                    setup_option_i = 0;
651                    setup_option_p = 0;
652                    setup_cursor = 0;
653                }
654                setup_item_i = 0;
655            }
656            else
657            {
658                if (setup_cursor < setup_h - 1)
659                {
660                    setup_cursor++;
661                }
662                else
663                {
664                    if ((setup_item_p + setup_h < setup_n) && setup_cursor == setup_h - 1) setup_item_p++;
665                }
666                if (setup_item_i < setup_n)
667                {
668                    setup_item_i++;
669                }
670                else
671                {
672                    setup_item_i = 0;
673                    setup_item_p = 0;
674                    setup_cursor = 0;
675                }
676            }
677        }
678    }
679    if (Input::WasPressed(Key::PageUp))
680    {
681        if (m_setup)
682        {
683            if (!setup_switch)
684            {
685                setup_option_i -= setup_cursor;
686                setup_cursor = 0;
687                setup_item_i = 0;
688            }
689            else
690            {
691                setup_item_i -= setup_cursor;
692                setup_cursor = 0;
693            }
694        }
695    }
696    if (Input::WasPressed(Key::PageDown))
697    {
698        if (m_setup)
699        {
700            if (!setup_switch)
701            {
702                setup_option_i += setup_h - setup_cursor - 1;
703                setup_cursor = setup_h - 1;
704                setup_item_i = 0;
705            }
706            else
707            {
708                setup_item_i += setup_n - setup_cursor - 1;
709                setup_cursor = setup_h - 1;
710            }
711        }
712    }
713    if (Input::WasPressed(Key::Left))
714    {
715        if (m_setup && setup_switch)
716        {
717            setup_var[setup_item_key].w -= setup_var[setup_item_key].z;
718            if (setup_var[setup_item_key].w < setup_var[setup_item_key].x) setup_var[setup_item_key].w = setup_var[setup_item_key].x;
719            UpdateVar();
720        }
721    }
722    if (Input::WasPressed(Key::Right))
723    {
724        if (m_setup && setup_switch)
725        {
726            setup_var[setup_item_key].w += setup_var[setup_item_key].z;
727            if (setup_var[setup_item_key].w > setup_var[setup_item_key].y) setup_var[setup_item_key].w = setup_var[setup_item_key].y;
728            UpdateVar();
729        }
730    }
731    if (Input::WasPressed(Key::Home))
732    {
733        if (m_setup && setup_switch)
734        {
735            setup_var[setup_item_key].w = setup_var[setup_item_key].x;
736            UpdateVar();
737        }
738    }
739    if (Input::WasPressed(Key::End))
740    {
741        if (m_setup && setup_switch)
742        {
743            setup_var[setup_item_key].w = setup_var[setup_item_key].y;
744            UpdateVar();
745        }
746    }
747    if (Input::WasPressed(Key::Return))
748    {
749        flash_flag = true;
750        flash_angle = main_angle;
751    }
752
753    Entity::TickDraw(seconds);
754
755    if (!m_ready)
756    {
757        CreateGLWindow();
758        text_render->Init();
759        m_ready = true;
760    }
761
762    // timer
763    if (!m_pause)
764    {
765        timer += seconds;
766        main_angle = timer * 100.0f * PID;
767    }
768    if (sync_flag)
769    {
770        angle = (main_angle - sync_angle) * sync_speed;
771        sync_value = 1.0f - sinf(angle);
772        if (angle > 90.0f * PID)
773        {
774            sync_value = 0;
775            sync_flag = false;
776        }
777    }
778    if (beat_flag)
779    {
780        angle = (main_angle - beat_angle) * beat_speed;
781        beat_value = 1.0f - sinf(angle);
782        if (angle > 90.0f * PID)
783        {
784            beat_value = 0;
785            beat_flag = false;
786        }
787    }
788    if (flash_flag)
789    {
790        angle = (main_angle - flash_angle) * flash_speed;
791        flash_value = 1.0f - sinf(angle);
792        if (angle > 90.0f * PID)
793        {
794            flash_value = 0;
795            flash_flag = false;
796        }
797    }
798    if (fade_flag)
799    {
800        angle = (main_angle - fade_angle) * fade_speed;
801        fade_value = 1.0f - sinf(angle);
802        if (angle > 90.0f * PID)
803        {
804            fade_value = 0;
805            fade_flag = false;
806        }
807    }
808    /* draw setup */
809    if (m_setup)
810    {
811        /* background */
812        caca_set_color_argb(m_caca, setup_color.x, setup_color.y);
813        caca_fill_box(m_caca, setup_p.x, setup_p.y, setup_size.x + 1, setup_size.y,' ');
814        caca_draw_line(m_caca, setup_p.x + setup_size.z - 1, setup_p.y + 1, setup_p.x + setup_size.z - 1, setup_p.y + setup_size.y - 1,'|');
815        /* title */
816        caca_set_color_argb(m_caca, setup_color.y, setup_color.x);
817        caca_draw_line(m_caca, setup_p.x, setup_p.y, setup_p.x + setup_size.x, setup_p.y,' ');
818        caca_put_str(m_caca, setup_p.x + setup_size.x / 2 - 3, setup_p.y, "SETUP");
819        /* display option */
820        for (int i = 0; i < setup_h; i++)
821        {
822            int y = setup_p.y + 1 + i;
823            int k = (setup_option_p + i) * (setup_item_n + 1);
824            if (setup_option_i != setup_option_p + i || setup_switch)
825            {
826                caca_set_color_argb(m_caca, setup_color.x, setup_color.y);
827                caca_put_str(m_caca, setup_p.x + 1, y, setup_text[k]);
828            }
829            else
830            {
831                caca_set_color_argb(m_caca, setup_color.y, setup_color.x);
832                caca_draw_line(m_caca, setup_p.x, y, setup_p.x + setup_size.z - 2, y,' ');
833                caca_put_str(m_caca, setup_p.x + 1, y, setup_text[k]);
834            }
835        }
836        /* display item */
837        for (int i = 0; i < setup_h; i++)
838        {
839            int y = setup_p.y + 1 + i;
840            int k = setup_option_i * (setup_item_n + 1) + 1 + setup_item_p + i;
841            if (setup_item_i != setup_item_p + i || !setup_switch)
842            {
843                caca_set_color_argb(m_caca, setup_color.x, setup_color.y);
844                caca_put_str(m_caca, setup_p.x + setup_size.z + 1, y, setup_text[k]);
845            }
846            else
847            {
848                caca_set_color_argb(m_caca, setup_color.y, setup_color.x);
849                caca_draw_line(m_caca, setup_p.x + setup_size.z, y, setup_p.x + setup_size.x, y,' ');
850                caca_put_str(m_caca, setup_p.x + setup_size.z + 1, y, setup_text[k]);
851            }
852        }
853        /* display variable */
854        int y = setup_p.y + setup_size.y;
855        setup_item_key = setup_option_i * (setup_item_n + 1) + 1 + setup_item_i;
856        caca_set_color_argb(m_caca, setup_color.y, setup_color.x);
857        caca_draw_line(m_caca, setup_p.x, y, setup_p.x + setup_size.x, y,' ');
858        if (setup_switch)
859        {
860            int x = setup_p.x + 1;
861            int w = setup_size.x - 3 - 4;
862            int bar_w = w / (setup_var[setup_item_key].y - setup_var[setup_item_key].x);
863            int bar_x = bar_w * setup_var[setup_item_key].x;
864            if ((setup_var[setup_item_key].y - setup_var[setup_item_key].x) / setup_var[setup_item_key].z > 1)
865            {
866                caca_printf(m_caca, setup_p.x + setup_size.x - 4, y, "%4.2f", setup_var[setup_item_key].w);
867                caca_draw_line(m_caca, x, y, x - bar_x + bar_w * setup_var[setup_item_key].y, y,'.');
868                if (setup_var[setup_item_key].w != setup_var[setup_item_key].x) caca_draw_line(m_caca, x, y, x - bar_x + bar_w * setup_var[setup_item_key].w, y,'x');
869            }
870            else
871            {
872                if (setup_var[setup_item_key] != vec4(0))
873                {
874                    caca_put_str(m_caca, setup_p.x + setup_size.x - 3, y, (setup_var[setup_item_key].w == setup_var[setup_item_key].y)?"YES":" NO");
875                }
876            }
877        }
878    /* informations */
879    int w = caca_get_canvas_width(m_caca);
880    int h = caca_get_canvas_height(m_caca);
881    caca_set_color_argb(m_caca, 0xfff, 0x000);
882    caca_printf(m_caca, 0, 0, "%i*%i", w, h);
883    }
884
885    Draw2D();
886    Draw3D();
887
888}
889
890void Render::Draw2D()
891{
892    /* Draw text in an offline buffer */
893    text_render->Render();
894
895    if (m_shader)
896        fbo_back->Bind();
897
898    glViewport(0, 0, screen_size.x, screen_size.y);
899
900    /* Clear the back buffer */
901    glEnable(GL_BLEND);
902    glBlendFunc(GL_SRC_COLOR, GL_DST_ALPHA);
903
904    Video::SetClearColor(vec4(screen_color, 1.f));
905    Video::SetClearDepth(1.0f); // set depth buffer
906    Video::Clear(ClearMask::Color | ClearMask::Depth);
907
908    text_render->Blit(border, canvas_size);
909
910    //if (m_polygon) glEnable(GL_LINE_SMOOTH); else glDisable(GL_LINE_SMOOTH);
911    glLineWidth((m_polygon)?2.0f:1.0f);
912    fx_angle=main_angle-part_angle;
913    if (m_polygon)
914        glEnable(GL_TEXTURE_2D);
915
916    glMatrixMode(GL_PROJECTION);
917    mat4 m = mat4::ortho(0, screen_size.x, screen_size.y, 0, -1.f, 1.f);
918    glLoadMatrixf(&m[0][0]);
919    glMatrixMode(GL_MODELVIEW);
920}
921
922void Render::Draw3D()
923{
924    if (!m_shader)
925        return;
926
927    glDisable(GL_DEPTH_TEST);
928    glDisable(GL_BLEND);
929
930    glEnableClientState(GL_VERTEX_ARRAY);
931    glVertexPointer(4, GL_FLOAT, 0, fs_quad_vtx);
932
933    glEnableClientState(GL_TEXTURE_COORD_ARRAY);
934    glTexCoordPointer(2, GL_FLOAT, 0, fs_quad_tex);
935
936    if (m_shader_copper)
937    {
938        // shader copper
939        fbo_tmp->Bind();
940        shader_copper->Bind();
941        shader_copper->SetUniform(shader_copper_texture, fbo_back->GetTexture(), 0);
942        shader_copper->SetUniform(shader_copper_screen_size, (vec2)screen_size);
943        shader_copper->SetUniform(shader_copper_time, fx_angle);
944        shader_copper->SetUniform(shader_copper_copper, copper);
945        TraceQuad();
946        shader_color->Unbind();
947        fbo_tmp->Unbind();
948        // shader simple
949        fbo_back->Bind();
950        ShaderSimple(fbo_tmp, 0);
951        fbo_back->Unbind();
952    }
953
954    if (m_shader_remanency)
955    {
956        // shader remanency
957        fbo_tmp->Bind();
958        shader_remanency->Bind();
959        shader_remanency->SetUniform(shader_remanency_source, fbo_back->GetTexture(), 0);
960        shader_remanency->SetUniform(shader_remanency_buffer, fbo_buffer->GetTexture(), 1);
961        shader_remanency->SetUniform(shader_remanency_mix, remanency);
962        TraceQuad();
963        shader_remanency->Unbind();
964        fbo_tmp->Unbind();
965        // shader simple
966        fbo_back->Bind();
967        ShaderSimple(fbo_tmp, 0);
968        fbo_back->Unbind();
969        // save previous fbo
970        fbo_tmp->Bind();
971        shader_remanency->Bind();
972        shader_remanency->SetUniform(shader_remanency_source, fbo_front->GetTexture(), 0);
973        shader_remanency->SetUniform(shader_remanency_buffer, fbo_buffer->GetTexture(), 1);
974        shader_remanency->SetUniform(shader_remanency_mix, buffer);
975        TraceQuad();
976        shader_remanency->Unbind();
977        fbo_tmp->Unbind();
978        // shader simple
979        fbo_buffer->Bind();
980        ShaderSimple(fbo_tmp, 0);
981        fbo_buffer->Unbind();
982    }
983
984    // shader glow
985    if (m_shader_glow)
986    {
987        // shader blur horizontal
988        fbo_blur_h->Bind();
989        shader_blur_h->Bind();
990        shader_blur_h->SetUniform(shader_blur_h_texture, fbo_back->GetTexture(), 0);
991        shader_blur_h->SetUniform(shader_blur_h_radius, glow_large / screen_size.x);
992        TraceQuad();
993        shader_blur_h->Unbind();
994        fbo_blur_h->Unbind();
995        // shader blur vertical
996        fbo_blur_v->Bind();
997        shader_blur_v->Bind();
998        shader_blur_v->SetUniform(shader_blur_v_texture, fbo_blur_h->GetTexture(), 0);
999        shader_blur_v->SetUniform(shader_blur_v_radius, glow_large / screen_size.y);
1000        TraceQuad();
1001        shader_blur_v->Unbind();
1002        fbo_blur_v->Unbind();
1003        // shader blur horizontal
1004        fbo_blur_h->Bind();
1005        shader_blur_h->Bind();
1006        shader_blur_h->SetUniform(shader_blur_h_texture, fbo_blur_v->GetTexture(), 0);
1007        shader_blur_h->SetUniform(shader_blur_h_radius, glow_small / screen_size.x);
1008        TraceQuad();
1009        shader_blur_h->Unbind();
1010        fbo_blur_h->Unbind();
1011        // shader blur vertical
1012        fbo_blur_v->Bind();
1013        shader_blur_v->Bind();
1014        shader_blur_v->SetUniform(shader_blur_v_texture, fbo_blur_h->GetTexture(), 0);
1015        shader_blur_v->SetUniform(shader_blur_v_radius, glow_small / screen_size.y);
1016        TraceQuad();
1017        shader_blur_v->Unbind();
1018        fbo_blur_v->Unbind();
1019        // shader glow
1020        fbo_front->Bind();
1021        shader_glow->Bind();
1022        shader_glow->SetUniform(shader_glow_glow, fbo_blur_v->GetTexture(), 0);
1023        shader_glow->SetUniform(shader_glow_source, fbo_back->GetTexture(), 1);
1024        shader_glow->SetUniform(shader_glow_mix, glow_mix);
1025        TraceQuad();
1026        shader_glow->Unbind();
1027        fbo_front->Unbind();
1028    }
1029    else
1030    {
1031        // shader simple
1032        fbo_front->Bind();
1033        ShaderSimple(fbo_back, 0);
1034        fbo_front->Unbind();
1035    }
1036
1037    if (m_shader_color)
1038    {
1039        // shader color
1040        fbo_tmp->Bind();
1041        shader_color->Bind();
1042        shader_color->SetUniform(shader_color_texture, fbo_front->GetTexture(), 0);
1043        shader_color->SetUniform(shader_color_screen_size, (vec2)screen_size);
1044        shader_color->SetUniform(shader_color_filter, color_filter);
1045        shader_color->SetUniform(shader_color_color, color_color);
1046        shader_color->SetUniform(shader_color_flash, flash_value);
1047        TraceQuad();
1048        shader_color->Unbind();
1049        fbo_tmp->Unbind();
1050        // shader simple
1051        fbo_front->Bind();
1052        ShaderSimple(fbo_tmp, 0);
1053        fbo_front->Unbind();
1054    }
1055
1056    if (m_shader_noise)
1057    {
1058        // shader noise
1059        fbo_tmp->Bind();
1060        shader_noise->Bind();
1061        shader_noise->SetUniform(shader_noise_texture, fbo_front->GetTexture(), 0);
1062        shader_noise->SetUniform(shader_noise_screen_size, (vec2)screen_size);
1063        shader_noise->SetUniform(shader_noise_time, fx_angle);
1064        shader_noise->SetUniform(shader_noise_offset, noise_offset);
1065        shader_noise->SetUniform(shader_noise_noise, noise_noise);
1066        shader_noise->SetUniform(shader_noise_retrace, noise_retrace);
1067        TraceQuad();
1068        shader_noise->Unbind();
1069        fbo_tmp->Unbind();
1070        // shader simple
1071        fbo_front->Bind();
1072        ShaderSimple(fbo_tmp, 0);
1073        fbo_front->Unbind();
1074    }
1075
1076    if (m_shader_blur)
1077    {
1078        // shader blur horizontal
1079        fbo_tmp->Bind();
1080        shader_blur_h->Bind();
1081        shader_blur_h->SetUniform(shader_blur_h_texture, fbo_front->GetTexture(), 0);
1082        shader_blur_h->SetUniform(shader_blur_h_radius, blur / screen_size.x);
1083        TraceQuad();
1084        shader_blur_h->Unbind();
1085        fbo_tmp->Unbind();
1086        // shader blur vertical
1087        fbo_front->Bind();
1088        shader_blur_v->Bind();
1089        shader_blur_v->SetUniform(shader_blur_v_texture, fbo_tmp->GetTexture(), 0);
1090        shader_blur_v->SetUniform(shader_blur_v_radius, blur / screen_size.y);
1091        TraceQuad();
1092        shader_blur_v->Unbind();
1093        fbo_front->Unbind();
1094    }
1095
1096    if (m_shader_postfx)
1097    {
1098        // shader postfx
1099        shader_postfx->Bind();
1100        shader_postfx->SetUniform(shader_postfx_texture, fbo_front->GetTexture(), 0);
1101        shader_postfx->SetUniform(shader_postfx_screen_size, (vec2)screen_size);
1102        shader_postfx->SetUniform(shader_postfx_time, fx_angle);
1103        shader_postfx->SetUniform(shader_postfx_deform, postfx_deform);
1104        shader_postfx->SetUniform(shader_postfx_ghost1, postfx_ghost1);
1105        shader_postfx->SetUniform(shader_postfx_ghost2, postfx_ghost2);
1106        shader_postfx->SetUniform(shader_postfx_vignetting, postfx_vignetting);
1107        shader_postfx->SetUniform(shader_postfx_aberration, postfx_aberration);
1108        shader_postfx->SetUniform(shader_postfx_moire_h, postfx_moire_h);
1109        shader_postfx->SetUniform(shader_postfx_moire_v, postfx_moire_v);
1110        shader_postfx->SetUniform(shader_postfx_scanline_h, postfx_scanline_h);
1111        shader_postfx->SetUniform(shader_postfx_scanline_v, postfx_scanline_v);
1112        shader_postfx->SetUniform(shader_postfx_corner, postfx_corner);
1113        shader_postfx->SetUniform(shader_postfx_sync, (float)fabs(sync_value*cosf((main_angle-sync_angle)*6.0f)));
1114        TraceQuad();
1115        shader_postfx->Unbind();
1116    }
1117    else
1118    {
1119        // shader simple
1120        ShaderSimple(fbo_front, 0);
1121    }
1122
1123    glDisableClientState(GL_VERTEX_ARRAY);
1124    glDisableClientState(GL_TEXTURE_COORD_ARRAY);
1125}
1126
1127Render::~Render()
1128{
1129}
Note: See TracBrowser for help on using the repository browser.