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

Last change on this file since 1828 was 1828, checked in by rez, 9 years ago

fixed a minor detail

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