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

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

NEERCS: fixed values for ghost picture

File size: 39.7 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.6f,0.4f);      // remanency [source mix,buffer mix]
92vec2 glow_mix(0.7f,0.3f);       // glow mix [source mix,glow mix]
93vec2 glow_large(3.0f,0.0f);     // large glow radius [center,corner]
94vec2 glow_small(1.5f,0.0f);     // small glow radius [center,corner]
95vec2 blur(0.5f,0.0f);           // 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]
98vec4 color_color(1.5f,1.2f,0.1f,0.35f);         // color modifier [brightness,contrast,level,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 = 4.0f;       // chromatic aberration
105vec4 postfx_ghost1(1.0f,0.0f,0.0f,-0.25f);      // ghost picture 1 [position x,position y,position z,strength]
106vec4 postfx_ghost2(1.5f,0.0f,0.0f,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,shift]
108vec4 postfx_moire_v(0.75f,-0.25f,1.0f,1.5f);    // horizontal moire [base,variable,repeat,shift]
109vec4 postfx_scanline_h(0.75f,0.0f,0.0f,0.0f);   // vertical scanline [base,variable,repeat,shift]
110vec4 postfx_scanline_v(0.75f,-0.25f,2.0f,0.0f); // horizontal scanline [base,variable,repeat,shift]
111vec3 postfx_corner(0.0f,0.75f,0.95f);           // corner [width,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 = vec2(3,1) * ratio_2d * font_size; // 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 width",
177        "corner radius",
178        "corner blur",
179        "vignetting",
180        "",
181    "color",
182        "filter red",
183        "filter green",
184        "filter blue",
185        "brightness",
186        "contrast",
187        "level",
188        "grayscale",
189        "aberration",
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",
212        "h shift",
213        "v base",
214        "v variable",
215        "v repeat",
216        "v shift",
217    "scanline",
218        "h base",
219        "h variable",
220        "h repeat",
221        "h shift",
222        "v base",
223        "v variable",
224        "v repeat",
225        "v shift"
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, 16, 1, border.x / ratio_2d.x / font_size.x),
233        vec4( 0, 16, 1, border.y / ratio_2d.y / font_size.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, 4.0f, 0.10f, postfx_corner.x),
270        vec4(0.0f, 1.0f, 0.05f, postfx_corner.y),
271        vec4(0.0f, 1.0f, 0.05f, postfx_corner.z),
272        vec4(0.0f, 1.0f, 0.10f, postfx_vignetting),
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(-1.0f, 1.0f, 0.05f, color_color.z),
281        vec4( 0.0f, 1.0f, 0.05f, color_color.w),
282        vec4( 0.0f, 8.0f, 0.50f, postfx_aberration),
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(-2.0f, 2.0f, 0.10f, postfx_ghost1.x),
294        vec4(-2.0f, 2.0f, 0.10f, 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(-2.0f, 2.0f, 0.10f, postfx_ghost2.x),
298        vec4(-2.0f, 2.0f, 0.10f, 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 * font_size; 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 = vec3(setup_var[k].w, setup_var[k + 1].w, setup_var[k + 2].w); k += 3;
343    postfx_vignetting = setup_var[k].w; k++;
344    k += 2; /* color */
345    color_filter = vec3(setup_var[k].w, setup_var[k + 1].w, setup_var[k + 2].w); k += 3;
346    color_color = vec4(setup_var[k].w, setup_var[k + 1].w, setup_var[k + 2].w, setup_var[k + 3].w); k += 4;
347    postfx_aberration = setup_var[k].w; k++;
348    k += 1; /* 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_char = max(canvas_char, ivec2(1, 1));
372    canvas_size = canvas_char * font_size * ratio_2d;
373
374    border = (screen_size - canvas_size) / 2;
375
376    caca_set_canvas_size(m_caca, canvas_char.x, canvas_char.y);
377
378    setup_p = (canvas_char - setup_size.xy) / 2;
379}
380
381int calc_item_length()
382{
383    int n = !setup_switch ? setup_option_n : setup_item_n;
384    for (int i = 0; i < n; i++)
385    {
386        int k = !setup_switch ? (i * (setup_item_n + 1)) : (setup_option_i * (setup_item_n + 1) + 1 + i);
387        if (setup_text[k][0] == '\0') return i - 1;
388    }
389    return n - 1;
390}
391
392Shader *shader_simple;
393Shader *shader_blur_h, *shader_blur_v, *shader_glow;
394Shader *shader_remanency, *shader_copper, *shader_color;
395Shader *shader_noise, *shader_postfx;
396// shader variables
397ShaderUniform shader_simple_texture;
398ShaderUniform shader_blur_h_texture,
399              shader_blur_h_radius,
400              shader_blur_v_texture,
401              shader_blur_v_radius;
402ShaderUniform shader_glow_glow,
403              shader_glow_source,
404              shader_glow_mix;
405ShaderUniform shader_remanency_source,
406              shader_remanency_buffer,
407              shader_remanency_mix;
408ShaderUniform shader_copper_texture,
409              shader_copper_screen_size,
410              shader_copper_time,
411              shader_copper_copper;
412ShaderUniform shader_color_texture,
413              shader_color_screen_size,
414              shader_color_filter,
415              shader_color_color,
416              shader_color_flash;
417ShaderUniform shader_noise_texture,
418              shader_noise_screen_size,
419              shader_noise_time,
420              shader_noise_offset,
421              shader_noise_noise,
422              shader_noise_retrace;
423ShaderUniform shader_postfx_texture,
424              shader_postfx_texture_2d,
425              shader_postfx_screen_size,
426              shader_postfx_time,
427              shader_postfx_deform,
428              shader_postfx_ghost1,
429              shader_postfx_ghost2,
430              shader_postfx_vignetting,
431              shader_postfx_aberration,
432              shader_postfx_moire_h,
433              shader_postfx_moire_v,
434              shader_postfx_scanline_h,
435              shader_postfx_scanline_v,
436              shader_postfx_corner,
437              shader_postfx_sync,
438              shader_postfx_beat;
439
440FrameBuffer *fbo_back, *fbo_front, *fbo_buffer;
441FrameBuffer *fbo_blur_h, *fbo_blur_v, *fbo_tmp;
442
443TextRender *text_render;
444
445void Render::TraceQuad()
446{
447    glLoadIdentity();
448    glDrawArrays(GL_QUADS, 0, 4);
449}
450
451void Render::ShaderSimple(FrameBuffer *fbo_output, int n)
452{
453    shader_simple->Bind();
454    shader_simple->SetUniform(shader_simple_texture, fbo_output->GetTexture(), n);
455    TraceQuad();
456    shader_simple->Unbind();
457}
458
459int Render::InitDraw(void)
460{
461    glDepthMask(GL_TRUE);     // do not write z-buffer
462    glEnable(GL_CULL_FACE);   // disable cull face
463    glCullFace(GL_BACK);      // don't draw front face
464
465    /* Initialise framebuffer objects */
466    fbo_back = new FrameBuffer(screen_size);
467    fbo_front = new FrameBuffer(screen_size);
468    fbo_buffer = new FrameBuffer(screen_size);
469    fbo_blur_h = new FrameBuffer(screen_size);
470    fbo_blur_v = new FrameBuffer(screen_size);
471    fbo_tmp = new FrameBuffer(screen_size);
472    // shader simple
473    shader_simple = Shader::Create(lolfx_simple);
474    shader_simple_texture = shader_simple->GetUniformLocation("texture");
475    // shader glow
476    shader_glow = Shader::Create(lolfx_glow);
477    shader_glow_glow = shader_glow->GetUniformLocation("glow");
478    shader_glow_source = shader_glow->GetUniformLocation("source");
479    shader_glow_mix = shader_glow->GetUniformLocation("mix");
480    // shader blur horizontal
481    shader_blur_h = Shader::Create(lolfx_blurh);
482    shader_blur_h_texture = shader_blur_h->GetUniformLocation("texture");
483    shader_blur_h_radius = shader_blur_h->GetUniformLocation("radius");
484    // shader blur vertical
485    shader_blur_v = Shader::Create(lolfx_blurv);
486    shader_blur_v_texture = shader_blur_v->GetUniformLocation("texture");
487    shader_blur_v_radius = shader_blur_v->GetUniformLocation("radius");
488    // shader remanency
489    shader_remanency = Shader::Create(lolfx_remanency);
490    shader_remanency_source = shader_remanency->GetUniformLocation("source");
491    shader_remanency_buffer = shader_remanency->GetUniformLocation("buffer");
492    shader_remanency_mix = shader_remanency->GetUniformLocation("mix");
493    // shader copper
494    shader_copper = Shader::Create(lolfx_copper);
495    shader_copper_texture = shader_copper->GetUniformLocation("texture");
496    shader_copper_screen_size = shader_copper->GetUniformLocation("screen_size");
497    shader_copper_time = shader_copper->GetUniformLocation("time");
498    shader_copper_copper = shader_copper->GetUniformLocation("copper");
499    // shader color
500    shader_color = Shader::Create(lolfx_color);
501    shader_color_texture = shader_color->GetUniformLocation("texture");
502    shader_color_screen_size = shader_color->GetUniformLocation("screen_size");
503    shader_color_filter = shader_color->GetUniformLocation("filter");
504    shader_color_color = shader_color->GetUniformLocation("color");
505    shader_color_flash = shader_color->GetUniformLocation("flash");
506    // shader noise
507    shader_noise = Shader::Create(lolfx_noise);
508    shader_noise_texture = shader_noise->GetUniformLocation("texture");
509    shader_noise_screen_size = shader_noise->GetUniformLocation("screen_size");
510    shader_noise_time = shader_noise->GetUniformLocation("time");
511    shader_noise_offset = shader_noise->GetUniformLocation("offset");
512    shader_noise_noise = shader_noise->GetUniformLocation("noise");
513    shader_noise_retrace = shader_noise->GetUniformLocation("retrace");
514    // shader postfx
515    shader_postfx = Shader::Create(lolfx_postfx);
516    shader_postfx_texture = shader_postfx->GetUniformLocation("texture");
517    shader_postfx_texture_2d = shader_postfx->GetUniformLocation("texture_2d");
518    shader_postfx_screen_size = shader_postfx->GetUniformLocation("screen_size");
519    shader_postfx_time = shader_postfx->GetUniformLocation("time");
520    shader_postfx_deform = shader_postfx->GetUniformLocation("deform");
521    shader_postfx_ghost1 = shader_postfx->GetUniformLocation("ghost1");
522    shader_postfx_ghost2 = shader_postfx->GetUniformLocation("ghost2");
523    shader_postfx_vignetting = shader_postfx->GetUniformLocation("vignetting");
524    shader_postfx_aberration = shader_postfx->GetUniformLocation("aberration");
525    shader_postfx_moire_h = shader_postfx->GetUniformLocation("moire_h");
526    shader_postfx_moire_v = shader_postfx->GetUniformLocation("moire_v");
527    shader_postfx_scanline_h = shader_postfx->GetUniformLocation("scanline_h");
528    shader_postfx_scanline_v = shader_postfx->GetUniformLocation("scanline_v");
529    shader_postfx_corner = shader_postfx->GetUniformLocation("corner");
530    shader_postfx_sync = shader_postfx->GetUniformLocation("sync");
531    shader_postfx_beat = shader_postfx->GetUniformLocation("beat");
532
533    return true;
534}
535
536int Render::CreateGLWindow()
537{
538    UpdateSize();
539    InitDraw();
540    return true;
541}
542
543Render::Render(caca_canvas_t *caca)
544  : m_caca(caca),
545    m_ready(false),
546    m_pause(false),
547    m_polygon(true),
548    m_setup(true),
549    m_shader(true),
550    m_shader_glow(true),
551    m_shader_blur(true),
552    m_shader_remanency(true),
553    m_shader_copper(false),
554    m_shader_color(true),
555    m_shader_noise(true),
556    m_shader_postfx(true)
557{
558    text_render = new TextRender(m_caca, font_size);
559}
560
561void Render::TickGame(float seconds)
562{
563    Entity::TickGame(seconds);
564
565    /* draw setup */
566    if (m_setup)
567    {
568        /* background */
569        caca_set_color_argb(m_caca, setup_color.x, setup_color.y);
570        caca_fill_box(m_caca, setup_p.x, setup_p.y, setup_size.x + 1, setup_size.y,' ');
571        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,'|');
572        /* title */
573        caca_set_color_argb(m_caca, setup_color.y, setup_color.x);
574        caca_draw_line(m_caca, setup_p.x, setup_p.y, setup_p.x + setup_size.x, setup_p.y,' ');
575        caca_put_str(m_caca, setup_p.x + setup_size.x / 2 - 3, setup_p.y, "SETUP");
576        /* display option */
577        for (int i = 0; i < setup_h; i++)
578        {
579            int y = setup_p.y + 1 + i;
580            int k = (setup_option_p + i) * (setup_item_n + 1);
581            if (setup_option_i != setup_option_p + i || setup_switch)
582            {
583                caca_set_color_argb(m_caca, setup_color.x, setup_color.y);
584                caca_put_str(m_caca, setup_p.x + 1, y, setup_text[k]);
585            }
586            else
587            {
588                caca_set_color_argb(m_caca, setup_color.y, setup_color.x);
589                caca_draw_line(m_caca, setup_p.x, y, setup_p.x + setup_size.z - 2, y,' ');
590                caca_put_str(m_caca, setup_p.x + 1, y, setup_text[k]);
591            }
592        }
593        /* display item */
594        for (int i = 0; i < setup_h; i++)
595        {
596            int y = setup_p.y + 1 + i;
597            int k = setup_option_i * (setup_item_n + 1) + 1 + setup_item_p + i;
598            if (setup_item_i != setup_item_p + i || !setup_switch)
599            {
600                caca_set_color_argb(m_caca, setup_color.x, setup_color.y);
601                caca_put_str(m_caca, setup_p.x + setup_size.z + 1, y, setup_text[k]);
602            }
603            else
604            {
605                caca_set_color_argb(m_caca, setup_color.y, setup_color.x);
606                caca_draw_line(m_caca, setup_p.x + setup_size.z, y, setup_p.x + setup_size.x, y,' ');
607                caca_put_str(m_caca, setup_p.x + setup_size.z + 1, y, setup_text[k]);
608            }
609        }
610        /* display variable */
611        int y = setup_p.y + setup_size.y;
612        setup_item_key = setup_option_i * (setup_item_n + 1) + 1 + setup_item_i;
613        caca_set_color_argb(m_caca, setup_color.y, setup_color.x);
614        caca_draw_line(m_caca, setup_p.x, y, setup_p.x + setup_size.x, y,' ');
615        if (setup_switch)
616        {
617            int x = setup_p.x + 1;
618            int w = setup_size.x - 3 - 4;
619            int bar_w = w / (setup_var[setup_item_key].y - setup_var[setup_item_key].x);
620            int bar_x = bar_w * setup_var[setup_item_key].x;
621            if ((setup_var[setup_item_key].y - setup_var[setup_item_key].x) / setup_var[setup_item_key].z > 1)
622            {
623                /* Work around a bug in libcaca */
624                if (setup_p.x + setup_size.x - 4 < caca_get_canvas_width(m_caca))
625                    caca_printf(m_caca, setup_p.x + setup_size.x - 4, y, "%4.2f", setup_var[setup_item_key].w);
626                caca_draw_line(m_caca, x, y, x - bar_x + bar_w * setup_var[setup_item_key].y, y,'.');
627                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');
628            }
629            else
630            {
631                if (setup_var[setup_item_key] != vec4(0))
632                {
633                    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");
634                }
635            }
636        }
637
638        /* informations */
639        int w = caca_get_canvas_width(m_caca);
640        int h = caca_get_canvas_height(m_caca);
641        caca_set_color_argb(m_caca, 0xfff, 0x000);
642        caca_printf(m_caca, 0, 0, "%i*%i", w, h);
643    }
644}
645
646void Render::Pause()
647{
648    m_pause=!m_pause;
649}
650
651void Render::TickDraw(float seconds)
652{
653    /* keyboard manager */
654    if (Input::WasReleased(Key::Escape))
655    {
656        Ticker::Shutdown();
657    }
658    if (Input::WasPressed(Key::F1))
659    {
660        m_setup = !m_setup;
661        if (m_setup) setup_n = calc_item_length();
662        sync_flag = true;
663        sync_angle = main_angle;
664    }
665    if (Input::WasPressed(Key::F2))
666    {
667        m_shader_glow = !m_shader_glow;
668        m_shader_blur = !m_shader_blur;
669        m_shader_remanency = !m_shader_remanency;
670        m_shader_copper = !m_shader_copper;
671        m_shader_color = !m_shader_color;
672        m_shader_noise = !m_shader_noise;
673        m_shader_postfx = !m_shader_postfx;
674        //m_polygon = !m_polygon;
675        //polygon_fillmode = (m_polygon)?GL_FILL:GL_LINE;
676        //glPolygonMode(GL_FRONT, polygon_fillmode);
677    }
678   if (Input::WasPressed(Key::Tab))
679    {
680        if (m_setup)
681        {
682            setup_switch = !setup_switch;
683            setup_n = calc_item_length();
684            setup_cursor = (!setup_switch?setup_option_i:setup_item_i) - (!setup_switch?setup_option_p:setup_item_p);
685        }
686    }
687    if (Input::WasPressed(Key::Up))
688    {
689        if (m_setup)
690        {
691            if (!setup_switch)
692            {
693                if (setup_cursor > 0)
694                {
695                    setup_cursor--;
696                }
697                else
698                {
699                    if (setup_cursor == 0) setup_option_p--;
700                }
701                if (setup_option_i > 0)
702                {
703                    setup_option_i--;
704                }
705                else
706                {
707                    setup_option_i = setup_option_n - 1;
708                    setup_option_p = setup_option_n - setup_h;
709                    setup_cursor = setup_h - 1;
710                }
711                setup_item_i = 0;
712                setup_item_p = 0;
713            }
714            else
715            {
716                if (setup_cursor > 0)
717                {
718                    setup_cursor--;
719                }
720                else
721                {
722                    if (setup_cursor == 0) setup_item_p--;
723                }
724                if (setup_item_i > 0)
725                {
726                    setup_item_i--;
727                }
728                else
729                {
730                    setup_item_i = setup_n;
731                    setup_item_p = (setup_n < setup_h) ? 0 : setup_n - setup_h + 1;
732                    setup_cursor = (setup_n < setup_h) ? setup_n : setup_h - 1;
733                }
734            }
735        }
736    }
737    if (Input::WasPressed(Key::Down))
738    {
739        if (m_setup)
740        {
741            if (!setup_switch)
742            {
743                if (setup_cursor < setup_h - 1)
744                {
745                    setup_cursor++;
746                }
747                else
748                {
749                    if (setup_cursor == setup_h - 1) setup_option_p++;
750                }
751                if (setup_option_i < setup_option_n - 1)
752                {
753                    setup_option_i++;
754                }
755                else
756                {
757                    setup_option_i = 0;
758                    setup_option_p = 0;
759                    setup_cursor = 0;
760                }
761                setup_item_i = 0;
762                setup_item_p = 0;
763            }
764            else
765            {
766                if (setup_cursor < setup_h - 1)
767                {
768                    setup_cursor++;
769                }
770                else
771                {
772                    if (setup_cursor == setup_h - 1) setup_item_p++;
773                }
774                if (setup_item_i < setup_n)
775                {
776                    setup_item_i++;
777                }
778                else
779                {
780                    setup_item_i = 0;
781                    setup_item_p = 0;
782                    setup_cursor = 0;
783                }
784            }
785        }
786    }
787    if (Input::WasPressed(Key::PageUp))
788    {
789        if (m_setup)
790        {
791            if (!setup_switch)
792            {
793                setup_option_i -= setup_cursor;
794                setup_cursor = 0;
795                setup_item_i = 0;
796            }
797            else
798            {
799                setup_item_i -= setup_cursor;
800                setup_cursor = 0;
801            }
802        }
803    }
804    if (Input::WasPressed(Key::PageDown))
805    {
806        if (m_setup)
807        {
808            if (!setup_switch)
809            {
810                setup_option_i += setup_h - setup_cursor - 1;
811                setup_cursor = setup_h - 1;
812                setup_item_i = 0;
813            }
814            else
815            {
816                setup_item_i += (setup_n < setup_h) ? setup_n - setup_cursor : setup_h - setup_cursor - 1;
817                setup_cursor = (setup_n < setup_h) ? setup_n : setup_h - 1;
818            }
819        }
820    }
821    if (Input::WasPressed(Key::Left))
822    {
823        if (m_setup && setup_switch)
824        {
825            setup_var[setup_item_key].w -= setup_var[setup_item_key].z;
826            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;
827            UpdateVar();
828        }
829    }
830    if (Input::WasPressed(Key::Right))
831    {
832        if (m_setup && setup_switch)
833        {
834            setup_var[setup_item_key].w += setup_var[setup_item_key].z;
835            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;
836            UpdateVar();
837        }
838    }
839    if (Input::WasPressed(Key::Home))
840    {
841        if (m_setup && setup_switch)
842        {
843            setup_var[setup_item_key].w = setup_var[setup_item_key].x;
844            UpdateVar();
845        }
846    }
847    if (Input::WasPressed(Key::End))
848    {
849        if (m_setup && setup_switch)
850        {
851            setup_var[setup_item_key].w = setup_var[setup_item_key].y;
852            UpdateVar();
853        }
854    }
855    if (Input::WasPressed(Key::Return))
856    {
857        beat_flag = true;
858        beat_angle = main_angle;
859        //flash_flag = true;
860        //flash_angle = main_angle;
861    }
862
863    Entity::TickDraw(seconds);
864
865    if (!m_ready)
866    {
867        CreateGLWindow();
868        text_render->Init();
869        m_ready = true;
870    }
871
872    // timer
873    if (!m_pause)
874    {
875        timer += seconds;
876        main_angle = timer * 100.0f * PID;
877    }
878    if (sync_flag)
879    {
880        angle = (main_angle - sync_angle) * sync_speed;
881        sync_value = 1.0f - sinf(angle);
882        if (angle > 90.0f * PID)
883        {
884            sync_value = 0;
885            sync_flag = false;
886        }
887    }
888    if (beat_flag)
889    {
890        angle = (main_angle - beat_angle) * beat_speed;
891        beat_value = 1.0f - sinf(angle);
892        if (angle > 90.0f * PID)
893        {
894            beat_value = 0;
895            beat_flag = false;
896        }
897    }
898    if (flash_flag)
899    {
900        angle = (main_angle - flash_angle) * flash_speed;
901        flash_value = 1.0f - sinf(angle);
902        if (angle > 90.0f * PID)
903        {
904            flash_value = 0;
905            flash_flag = false;
906        }
907    }
908    if (fade_flag)
909    {
910        angle = (main_angle - fade_angle) * fade_speed;
911        fade_value = 1.0f - sinf(angle);
912        if (angle > 90.0f * PID)
913        {
914            fade_value = 0;
915            fade_flag = false;
916        }
917    }
918
919    Draw2D();
920    Draw3D();
921
922}
923
924void Render::Draw2D()
925{
926    /* Draw text in an offline buffer */
927    text_render->Render();
928
929    if (m_shader)
930        fbo_back->Bind();
931
932    glViewport(0, 0, screen_size.x, screen_size.y);
933
934    /* Clear the back buffer */
935    glEnable(GL_BLEND);
936    glBlendFunc(GL_SRC_COLOR, GL_DST_ALPHA);
937
938    Video::SetClearColor(vec4(screen_color, 1.f));
939    Video::SetClearDepth(1.0f); // set depth buffer
940    Video::Clear(ClearMask::Color | ClearMask::Depth);
941
942    text_render->Blit(border, canvas_size);
943
944    //if (m_polygon) glEnable(GL_LINE_SMOOTH); else glDisable(GL_LINE_SMOOTH);
945    glLineWidth((m_polygon)?2.0f:1.0f);
946    fx_angle=main_angle-part_angle;
947    if (m_polygon)
948        glEnable(GL_TEXTURE_2D);
949
950    glMatrixMode(GL_PROJECTION);
951    mat4 m = mat4::ortho(0, screen_size.x, screen_size.y, 0, -1.f, 1.f);
952    glLoadMatrixf(&m[0][0]);
953    glMatrixMode(GL_MODELVIEW);
954}
955
956void Render::Draw3D()
957{
958    if (!m_shader)
959        return;
960
961    glDisable(GL_DEPTH_TEST);
962    glDisable(GL_BLEND);
963
964    glEnableClientState(GL_VERTEX_ARRAY);
965    glVertexPointer(4, GL_FLOAT, 0, fs_quad_vtx);
966
967    glEnableClientState(GL_TEXTURE_COORD_ARRAY);
968    glTexCoordPointer(2, GL_FLOAT, 0, fs_quad_tex);
969
970    if (m_shader_copper)
971    {
972        // shader copper
973        fbo_tmp->Bind();
974        shader_copper->Bind();
975        shader_copper->SetUniform(shader_copper_texture, fbo_back->GetTexture(), 0);
976        shader_copper->SetUniform(shader_copper_screen_size, (vec2)screen_size);
977        shader_copper->SetUniform(shader_copper_time, fx_angle);
978        shader_copper->SetUniform(shader_copper_copper, copper);
979        TraceQuad();
980        shader_color->Unbind();
981        fbo_tmp->Unbind();
982        // shader simple
983        fbo_back->Bind();
984        ShaderSimple(fbo_tmp, 0);
985        fbo_back->Unbind();
986    }
987
988    if (m_shader_remanency)
989    {
990        // shader remanency
991        fbo_tmp->Bind();
992        shader_remanency->Bind();
993        shader_remanency->SetUniform(shader_remanency_source, fbo_back->GetTexture(), 0);
994        shader_remanency->SetUniform(shader_remanency_buffer, fbo_buffer->GetTexture(), 1);
995        shader_remanency->SetUniform(shader_remanency_mix, remanency);
996        TraceQuad();
997        shader_remanency->Unbind();
998        fbo_tmp->Unbind();
999        // shader simple
1000        fbo_back->Bind();
1001        ShaderSimple(fbo_tmp, 0);
1002        fbo_back->Unbind();
1003        // save previous fbo
1004        fbo_tmp->Bind();
1005        shader_remanency->Bind();
1006        shader_remanency->SetUniform(shader_remanency_source, fbo_front->GetTexture(), 0);
1007        shader_remanency->SetUniform(shader_remanency_buffer, fbo_buffer->GetTexture(), 1);
1008        shader_remanency->SetUniform(shader_remanency_mix, buffer);
1009        TraceQuad();
1010        shader_remanency->Unbind();
1011        fbo_tmp->Unbind();
1012        // shader simple
1013        fbo_buffer->Bind();
1014        ShaderSimple(fbo_tmp, 0);
1015        fbo_buffer->Unbind();
1016    }
1017
1018    // shader glow
1019    if (m_shader_glow)
1020    {
1021        // shader blur horizontal
1022        fbo_blur_h->Bind();
1023        shader_blur_h->Bind();
1024        shader_blur_h->SetUniform(shader_blur_h_texture, fbo_back->GetTexture(), 0);
1025        shader_blur_h->SetUniform(shader_blur_h_radius, glow_large / screen_size.x);
1026        TraceQuad();
1027        shader_blur_h->Unbind();
1028        fbo_blur_h->Unbind();
1029        // shader blur vertical
1030        fbo_blur_v->Bind();
1031        shader_blur_v->Bind();
1032        shader_blur_v->SetUniform(shader_blur_v_texture, fbo_blur_h->GetTexture(), 0);
1033        shader_blur_v->SetUniform(shader_blur_v_radius, glow_large / screen_size.y);
1034        TraceQuad();
1035        shader_blur_v->Unbind();
1036        fbo_blur_v->Unbind();
1037        // shader blur horizontal
1038        fbo_blur_h->Bind();
1039        shader_blur_h->Bind();
1040        shader_blur_h->SetUniform(shader_blur_h_texture, fbo_blur_v->GetTexture(), 0);
1041        shader_blur_h->SetUniform(shader_blur_h_radius, glow_small / screen_size.x);
1042        TraceQuad();
1043        shader_blur_h->Unbind();
1044        fbo_blur_h->Unbind();
1045        // shader blur vertical
1046        fbo_blur_v->Bind();
1047        shader_blur_v->Bind();
1048        shader_blur_v->SetUniform(shader_blur_v_texture, fbo_blur_h->GetTexture(), 0);
1049        shader_blur_v->SetUniform(shader_blur_v_radius, glow_small / screen_size.y);
1050        TraceQuad();
1051        shader_blur_v->Unbind();
1052        fbo_blur_v->Unbind();
1053        // shader glow
1054        fbo_front->Bind();
1055        shader_glow->Bind();
1056        shader_glow->SetUniform(shader_glow_glow, fbo_blur_v->GetTexture(), 0);
1057        shader_glow->SetUniform(shader_glow_source, fbo_back->GetTexture(), 1);
1058        shader_glow->SetUniform(shader_glow_mix, glow_mix);
1059        TraceQuad();
1060        shader_glow->Unbind();
1061        fbo_front->Unbind();
1062    }
1063    else
1064    {
1065        // shader simple
1066        fbo_front->Bind();
1067        ShaderSimple(fbo_back, 0);
1068        fbo_front->Unbind();
1069    }
1070
1071    if (m_shader_color)
1072    {
1073        // shader color
1074        fbo_tmp->Bind();
1075        shader_color->Bind();
1076        shader_color->SetUniform(shader_color_texture, fbo_front->GetTexture(), 0);
1077        shader_color->SetUniform(shader_color_screen_size, (vec2)screen_size);
1078        shader_color->SetUniform(shader_color_filter, color_filter);
1079        shader_color->SetUniform(shader_color_color, color_color);
1080        shader_color->SetUniform(shader_color_flash, flash_value);
1081        TraceQuad();
1082        shader_color->Unbind();
1083        fbo_tmp->Unbind();
1084        // shader simple
1085        fbo_front->Bind();
1086        ShaderSimple(fbo_tmp, 0);
1087        fbo_front->Unbind();
1088    }
1089
1090    if (m_shader_noise)
1091    {
1092        // shader noise
1093        fbo_tmp->Bind();
1094        shader_noise->Bind();
1095        shader_noise->SetUniform(shader_noise_texture, fbo_front->GetTexture(), 0);
1096        shader_noise->SetUniform(shader_noise_screen_size, (vec2)screen_size);
1097        shader_noise->SetUniform(shader_noise_time, fx_angle);
1098        shader_noise->SetUniform(shader_noise_offset, noise_offset);
1099        shader_noise->SetUniform(shader_noise_noise, noise_noise);
1100        shader_noise->SetUniform(shader_noise_retrace, noise_retrace);
1101        TraceQuad();
1102        shader_noise->Unbind();
1103        fbo_tmp->Unbind();
1104        // shader simple
1105        fbo_front->Bind();
1106        ShaderSimple(fbo_tmp, 0);
1107        fbo_front->Unbind();
1108    }
1109
1110    if (m_shader_blur)
1111    {
1112        // shader blur horizontal
1113        fbo_tmp->Bind();
1114        shader_blur_h->Bind();
1115        shader_blur_h->SetUniform(shader_blur_h_texture, fbo_front->GetTexture(), 0);
1116        shader_blur_h->SetUniform(shader_blur_h_radius, blur / screen_size.x);
1117        TraceQuad();
1118        shader_blur_h->Unbind();
1119        fbo_tmp->Unbind();
1120        // shader blur vertical
1121        fbo_front->Bind();
1122        shader_blur_v->Bind();
1123        shader_blur_v->SetUniform(shader_blur_v_texture, fbo_tmp->GetTexture(), 0);
1124        shader_blur_v->SetUniform(shader_blur_v_radius, blur / screen_size.y);
1125        TraceQuad();
1126        shader_blur_v->Unbind();
1127        fbo_front->Unbind();
1128    }
1129
1130    if (m_shader_postfx)
1131    {
1132        // shader postfx
1133        shader_postfx->Bind();
1134        shader_postfx->SetUniform(shader_postfx_texture, fbo_front->GetTexture(), 0);
1135        shader_postfx->SetUniform(shader_postfx_screen_size, (vec2)screen_size);
1136        shader_postfx->SetUniform(shader_postfx_time, fx_angle);
1137        shader_postfx->SetUniform(shader_postfx_deform, postfx_deform);
1138        shader_postfx->SetUniform(shader_postfx_ghost1, postfx_ghost1);
1139        shader_postfx->SetUniform(shader_postfx_ghost2, postfx_ghost2);
1140        shader_postfx->SetUniform(shader_postfx_vignetting, postfx_vignetting);
1141        shader_postfx->SetUniform(shader_postfx_aberration, postfx_aberration);
1142        shader_postfx->SetUniform(shader_postfx_moire_h, postfx_moire_h);
1143        shader_postfx->SetUniform(shader_postfx_moire_v, postfx_moire_v);
1144        shader_postfx->SetUniform(shader_postfx_scanline_h, postfx_scanline_h);
1145        shader_postfx->SetUniform(shader_postfx_scanline_v, postfx_scanline_v);
1146        shader_postfx->SetUniform(shader_postfx_corner, postfx_corner);
1147        shader_postfx->SetUniform(shader_postfx_sync, (float)fabs(sync_value*cosf((main_angle-sync_angle)*6.0f)));
1148        shader_postfx->SetUniform(shader_postfx_beat, (float)fabs(beat_value*cosf((main_angle-beat_angle)*6.0f)));
1149        TraceQuad();
1150        shader_postfx->Unbind();
1151    }
1152    else
1153    {
1154        // shader simple
1155        ShaderSimple(fbo_front, 0);
1156    }
1157
1158    glDisableClientState(GL_VERTEX_ARRAY);
1159    glDisableClientState(GL_TEXTURE_COORD_ARRAY);
1160}
1161
1162Render::~Render()
1163{
1164}
Note: See TracBrowser for help on using the repository browser.