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

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

fixed rand() function

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