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

Last change on this file since 1803 was 1803, checked in by sam, 7 years ago

input: switch all key names to camel case, like our other enums.

File size: 33.4 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_blurh;
35extern char const *lolfx_blurv;
36extern char const *lolfx_remanency;
37extern char const *lolfx_glow;
38extern char const *lolfx_postfx;
39extern char const *lolfx_radial;
40extern char const *lolfx_simple;
41
42#define PID M_PI/180.0f    // pi ratio
43#define CR 1.0f/256.0f     // color ratio
44
45/*
46 * Various variables
47 */
48
49int active = true;         // window active flag
50float nearplane = 0.1f;    // nearplane
51float farplane = 1000.0f;  // farplane
52int polygon_fillmode = GL_FILL; // fill mode
53/* timer variable */
54float timer = 0;           // timer
55/* window variable */
56ivec2 screen_size;         // screen size
57vec3 screen_color = CR * vec3(32, 32, 32); // screen color
58/* object variable */
59float main_angle = 0.0f;   // main angle
60float part_angle = 0.0f;   // part angle
61float fx_angle;            // current angle
62/* fs_quad variable */
63float 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};
64float fs_quad_tex[] = {0, 1.0f, 0, 0, 1.0f, 0, 1.0f, 1.0f};
65/* flash variable */
66bool flash_flag = false;   // flag
67float flash_angle = 0;     // angle
68float flash_value = 0;     // value
69float flash_speed = 1.5f;  // speed
70/* fade variable */
71bool fade_flag = false;    // flag
72float fade_angle = 0;      // angle
73float fade_value = 0;      // value
74float fade_speed = 0.2f;   // speed
75/* sync variable */
76bool sync_flag = false;    // flagsh
77float sync_angle = 0;      // angle
78float sync_value = 0;      // value
79float sync_speed = 1.0f;   // speed
80/* beat variable */
81bool beat_flag = false;    // flag
82float beat_angle = 0;      // angle
83float beat_value = 0;      // value
84float beat_speed = 2.0f;   // speed
85/* common variable */
86float value, angle, radius, scale, speed;
87/* shader variable */
88vec2 buffer(0.7f,0.3f);         // [new frame mix,old frame mix]
89vec2 remanency(0.3f,0.7f);      // remanency [source mix,buffer mix]
90vec2 glow_mix(0.6f,0.4f);       // glow mix [source mix,glow mix]
91vec2 glow_large(2.0f,2.0f);     // large glow radius [center,corner]
92vec2 glow_small(1.0f,1.0f);     // small glow radius [center,corner]
93vec2 blur(0.25f,0.5f);          // glow radius [center,corner]
94vec2 postfx_deform(0.7f,0.54f); // deformation [ratio,zoom]
95vec3 postfx_filter(0.8f,0.9f,0.4f);   // color filter [red,green,blue]
96vec3 postfx_color(1.8f,1.5f,0.5f);    // color modifier [brightness,contrast,grayscale]
97vec2 postfx_corner(0.75f,0.95f);      // corner [radius,blur]
98float postfx_vignetting = -0.5f;      // vignetting strength
99vec3 postfx_retrace(0.05f,2.0f,4.0f); // retrace [strength,length,speed]
100vec2 postfx_offset(3.0f,3.0f);  // random line [horizontal,vertical]
101float postfx_noise = 0.15f;     // noise
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]
109/* window variable */
110ivec2 border;              // border width
111/* text variable */
112ivec2 ratio_2d(2,4);       // 2d ratio
113ivec2 map_size(256,256);   // texture map size
114ivec2 font_size(8,8);      // font size
115ivec2 canvas_char(0,0);    // canvas char number
116ivec2 canvas_size(0,0);    // caca size
117/* setup variable */
118bool setup_switch = false;      // switch [option/item]
119int setup_option = 0;           // selected option
120int setup_option_n = 10;        // option number
121int setup_item = 0;             // selected item
122int setup_item_n = 8;           // item number
123int setup_item_key = 0;         // item array key
124int setup_n = 0;                // contextual option/item number
125ivec2 setup_p(1,1);             // position [x,y]
126ivec3 setup_size(30,0,12);      // size [w,h,split]
127ivec2 setup_color(0x678,0x234); // color [foreground,background]
128//vec3 radial(2.0f,0.8f,0);     // radial [mix,strength,color mode]
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        "",
183        "",
184    "noise",
185        "offset h",
186        "offset v",
187        "noise",
188        "aberration",
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.1f, blur.x),
252        vec4(0.0f, 2.0f, 0.1f, 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.1f, postfx_filter.x),
269        vec4(0.0f, 1.0f, 0.1f, postfx_filter.y),
270        vec4(0.0f, 1.0f, 0.1f, postfx_filter.z),
271        vec4(0.0f, 4.0f, 0.1f, postfx_color.x),
272        vec4(0.0f, 4.0f, 0.1f, postfx_color.y),
273        vec4(0.0f, 1.5f, 0.1f, postfx_color.z),
274        vec4(0),
275        vec4(0),
276    vec4(0), /* noise */
277        vec4(0.0f, 4.0f, 0.50f, postfx_offset.x),
278        vec4(0.0f, 4.0f, 0.50f, postfx_offset.y),
279        vec4(0.0f, 1.0f, 0.05f, postfx_noise),
280        vec4(0.0f, 8.0f, 0.50f, postfx_aberration),
281        vec4(0.0f, 0.2f, 0.01f, postfx_retrace.x),
282        vec4(0.0f, 8.0f, 0.50f, postfx_retrace.y),
283        vec4(0.0f, 4.0f, 0.25f, postfx_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    postfx_filter = vec3(setup_var[k].w, setup_var[k + 1].w, setup_var[k + 2].w); k += 3;
337    postfx_color = vec3(setup_var[k].w, setup_var[k + 1].w, setup_var[k + 2].w); k += 3;
338    k += 3; /* noise */
339    postfx_offset = vec2(setup_var[k].w, setup_var[k + 1].w); k += 2;
340    postfx_noise = setup_var[k].w; k++;
341    postfx_aberration = setup_var[k].w; k++;
342    postfx_retrace = vec3(setup_var[k].w, setup_var[k + 1].w, setup_var[k + 2].w); k += 3;
343    k += 2; /* ghost */
344    postfx_ghost1 = vec4(setup_var[k].w, setup_var[k + 1].w, setup_var[k + 2].w, setup_var[k + 3].w); k += 4;
345    postfx_ghost2 = vec4(setup_var[k].w, setup_var[k + 1].w, setup_var[k + 2].w, setup_var[k + 3].w); k += 4;
346    k += 1; /* moire */
347    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;
348    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;
349    k++; /* scanline */
350    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;
351    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;
352}
353
354int calc_item_length()
355{
356    int n = !setup_switch ? setup_option_n : setup_item_n;
357    int v = 0;
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_blur_h, *shader_blur_v;
368Shader *shader_remanency, *shader_glow, *shader_radial, *shader_postfx;
369// shader variables
370ShaderUniform shader_simple_texture;
371ShaderUniform shader_remanency_source,
372              shader_remanency_buffer,
373              shader_remanency_mix;
374ShaderUniform shader_glow_glow,
375              shader_glow_source,
376              shader_glow_mix;
377ShaderUniform shader_blur_h_texture,
378              shader_blur_h_radius,
379              shader_blur_v_texture,
380              shader_blur_v_radius;
381ShaderUniform shader_radial_texture,
382              shader_radial_screen_size,
383              shader_radial_time,
384              shader_radial_value1,
385              shader_radial_value2,
386              shader_radial_color;
387ShaderUniform shader_postfx_texture,
388              shader_postfx_texture_2d,
389              shader_postfx_screen_size,
390              shader_postfx_time,
391              shader_postfx_deform,
392              shader_postfx_ghost1,
393              shader_postfx_ghost2,
394              shader_postfx_filter,
395              shader_postfx_color,
396              shader_postfx_corner,
397              shader_postfx_vignetting,
398              shader_postfx_retrace,
399              shader_postfx_offset,
400              shader_postfx_noise,
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_flash,
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 radial
475    shader_radial = Shader::Create(lolfx_radial);
476    shader_radial_texture = shader_radial->GetUniformLocation("texture");
477    shader_radial_screen_size = shader_radial->GetUniformLocation("screen_size");
478    shader_radial_time = shader_radial->GetUniformLocation("time");
479    shader_radial_value1 = shader_radial->GetUniformLocation("value1");
480    shader_radial_value2 = shader_radial->GetUniformLocation("value2");
481    shader_radial_color = shader_radial->GetUniformLocation("color");
482    // shader postfx
483    shader_postfx = Shader::Create(lolfx_postfx);
484    shader_postfx_texture = shader_postfx->GetUniformLocation("texture");
485    shader_postfx_texture_2d = shader_postfx->GetUniformLocation("texture_2d");
486    shader_postfx_screen_size = shader_postfx->GetUniformLocation("screen_size");
487    shader_postfx_time = shader_postfx->GetUniformLocation("time");
488    shader_postfx_deform = shader_postfx->GetUniformLocation("deform");
489    shader_postfx_ghost1 = shader_postfx->GetUniformLocation("ghost1");
490    shader_postfx_ghost2 = shader_postfx->GetUniformLocation("ghost2");
491    shader_postfx_filter = shader_postfx->GetUniformLocation("filter");
492    shader_postfx_color = shader_postfx->GetUniformLocation("color");
493    shader_postfx_corner = shader_postfx->GetUniformLocation("corner");
494    shader_postfx_vignetting = shader_postfx->GetUniformLocation("vignetting");
495    shader_postfx_retrace = shader_postfx->GetUniformLocation("retrace");
496    shader_postfx_offset = shader_postfx->GetUniformLocation("offset");
497    shader_postfx_noise = shader_postfx->GetUniformLocation("noise");
498    shader_postfx_aberration = shader_postfx->GetUniformLocation("aberration");
499    shader_postfx_moire_h = shader_postfx->GetUniformLocation("moire_h");
500    shader_postfx_moire_v = shader_postfx->GetUniformLocation("moire_v");
501    shader_postfx_scanline_h = shader_postfx->GetUniformLocation("scanline_h");
502    shader_postfx_scanline_v = shader_postfx->GetUniformLocation("scanline_v");
503    shader_postfx_flash = shader_postfx->GetUniformLocation("flash");
504    shader_postfx_sync = shader_postfx->GetUniformLocation("sync");
505
506    return true;
507}
508
509int Render::CreateGLWindow()
510{
511    screen_size = Video::GetSize();
512    border = 18 * ratio_2d;
513    border.y = border.x; // enabled to get same border everywhere
514    canvas_char = (screen_size - border * 2) / (font_size * ratio_2d);
515    canvas_size = canvas_char * font_size * ratio_2d;
516
517    border = (screen_size - canvas_size) / 2;
518
519    caca_set_canvas_size(m_caca, canvas_char.x, canvas_char.y);
520
521    setup_size.y = ((setup_option_n > setup_item_n) ? setup_option_n : setup_item_n) + 1;
522    setup_p = (canvas_char - setup_size.xy) / 2;
523
524    setup_n = calc_item_length();
525
526    InitDraw();
527    return true;
528}
529
530Render::Render(caca_canvas_t *caca)
531  : m_caca(caca),
532    m_ready(false),
533    m_pause(false),
534    m_polygon(true),
535    m_setup(true),
536    m_shader(true),
537    m_shader_remanency(true),
538    m_shader_glow(true),
539    m_shader_blur(true),
540    m_shader_postfx(true)
541{
542    text_render = new TextRender(m_caca, font_size);
543}
544
545void Render::TickGame(float seconds)
546{
547    Entity::TickGame(seconds);
548}
549
550void Render::Pause()
551{
552    m_pause=!m_pause;
553}
554
555void Render::TickDraw(float seconds)
556{
557    /* keyboard manager */
558    if (Input::WasReleased(Key::Escape))
559    {
560        Ticker::Shutdown();
561    }
562    if (Input::WasPressed(Key::F1))
563    {
564        m_setup = !m_setup;
565        sync_flag = true;
566        sync_angle = main_angle;
567    }
568    if (Input::WasPressed(Key::F2))
569    {
570        m_polygon = !m_polygon;
571        polygon_fillmode = (m_polygon)?GL_FILL:GL_LINE;
572        glPolygonMode(GL_FRONT, polygon_fillmode);
573    }
574    if (Input::WasPressed(Key::F3))
575    {
576        m_shader_glow = !m_shader_glow;
577        m_shader_blur = !m_shader_blur;
578    }
579    if (Input::WasPressed(Key::F4))
580    {
581        m_shader_postfx = !m_shader_postfx;
582    }
583    if (Input::WasPressed(Key::F5))
584    {
585        Pause();
586    }
587   if (Input::WasPressed(Key::Tab))
588    {
589        if (m_setup)
590        {
591            setup_switch = !setup_switch;
592            setup_n = calc_item_length();
593        }
594    }
595    if (Input::WasPressed(Key::Up))
596    {
597        if (m_setup)
598        {
599            if (!setup_switch)
600            {
601                setup_option--;
602                if (setup_option < 0) setup_option = setup_n;
603                setup_item = 0;
604            }
605            else
606            {
607                setup_item--;
608                if (setup_item < 0) setup_item = setup_n;
609            }
610        }
611    }
612    if (Input::WasPressed(Key::Down))
613    {
614        if (m_setup)
615        {
616            if (!setup_switch)
617            {
618                setup_option++;
619                if (setup_option > setup_n) setup_option = 0;
620                setup_item = 0;
621            }
622            else
623            {
624                setup_item++;
625                if (setup_item > setup_n) setup_item = 0;
626            }
627        }
628    }
629    if (Input::WasPressed(Key::PageUp))
630    {
631        if (m_setup)
632        {
633            if (!setup_switch)
634            {
635                setup_option = 0;
636            }
637            else
638            {
639                setup_item = 0;
640            }
641        }
642    }
643    if (Input::WasPressed(Key::PageDown))
644    {
645        if (m_setup)
646        {
647            if (!setup_switch)
648            {
649                setup_option = setup_n;
650                setup_item = 0;
651            }
652            else
653            {
654                setup_item = setup_n;
655            }
656        }
657    }
658    if (Input::WasPressed(Key::Left))
659    {
660        if (m_setup && setup_switch)
661        {
662            setup_var[setup_item_key].w -= setup_var[setup_item_key].z;
663            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;
664            Render::UpdateVar();
665        }
666    }
667    if (Input::WasPressed(Key::Right))
668    {
669        if (m_setup && setup_switch)
670        {
671            setup_var[setup_item_key].w += setup_var[setup_item_key].z;
672            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;
673            Render::UpdateVar();
674        }
675    }
676    if (Input::WasPressed(Key::Return))
677    {
678        sync_flag = true;
679        sync_angle = main_angle;
680    }
681
682    Entity::TickDraw(seconds);
683
684    if (!m_ready)
685    {
686        CreateGLWindow();
687        text_render->Init();
688        m_ready = true;
689    }
690
691    // timer
692    if (!m_pause)
693    {
694        timer += seconds;
695        main_angle = timer * 100.0f * PID;
696    }
697    if (sync_flag)
698    {
699        angle=(main_angle-sync_angle)*sync_speed;
700        sync_value=1.0f-sinf(angle);
701        if (angle>90.0f*PID)
702        {
703            sync_value=0;
704            sync_flag=false;
705        }
706    }
707    if (beat_flag)
708    {
709        angle=(main_angle-beat_angle)*beat_speed;
710        beat_value=1.0f-sinf(angle);
711        if (angle>90.0f*PID)
712        {
713            beat_value=0;
714            beat_flag=false;
715        }
716    }
717    if (flash_flag)
718    {
719        angle=(main_angle-flash_angle)*flash_speed;
720        flash_value=1.0f-sinf(angle);
721        if (angle>90.0f*PID)
722        {
723            flash_value=0;
724            flash_flag=false;
725        }
726    }
727    if (fade_flag)
728    {
729        angle=(main_angle-fade_angle)*fade_speed;
730        fade_value=1.0f-sinf(angle);
731        if (angle>90.0f*PID)
732        {
733            fade_value=0;
734            fade_flag=false;
735        }
736    }
737    /* draw setup */
738    if (m_setup)
739    {
740        /* background */
741        caca_set_color_argb(m_caca, setup_color.x, setup_color.y);
742        caca_fill_box(m_caca, setup_p.x, setup_p.y, setup_size.x + 1, setup_size.y,' ');
743        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,'|');
744        /* title */
745        caca_set_color_argb(m_caca, setup_color.y, setup_color.x);
746        caca_draw_line(m_caca, setup_p.x, setup_p.y, setup_p.x + setup_size.x, setup_p.y,' ');
747        caca_put_str(m_caca, setup_p.x + setup_size.x / 2 - 3, setup_p.y, "SETUP");
748        /* display option */
749        for (int i = 0; i < setup_option_n; i++)
750        {
751            int y = setup_p.y + 1 + i;
752            int k = i * (setup_item_n + 1);
753            if (setup_option != i || setup_switch)
754            {
755                caca_set_color_argb(m_caca, setup_color.x, setup_color.y);
756                caca_put_str(m_caca, setup_p.x + 1, y, setup_text[k]);
757            }
758            else
759            {
760                caca_set_color_argb(m_caca, setup_color.y, setup_color.x);
761                caca_draw_line(m_caca, setup_p.x, y, setup_p.x + setup_size.z - 2, y,' ');
762                caca_put_str(m_caca, setup_p.x + 1, y, setup_text[k]);
763            }
764        }
765        /* display item */
766        for (int i = 0; i < setup_item_n; i++)
767        {
768            int y = setup_p.y + 1 + i;
769            int k = setup_option * (setup_item_n + 1) + 1 + i;
770            if (setup_item != i || !setup_switch)
771            {
772                caca_set_color_argb(m_caca, setup_color.x, setup_color.y);
773                caca_put_str(m_caca, setup_p.x + setup_size.z + 1, y, setup_text[k]);
774            }
775            else
776            {
777                caca_set_color_argb(m_caca, setup_color.y, setup_color.x);
778                caca_draw_line(m_caca, setup_p.x + setup_size.z, y, setup_p.x + setup_size.x, y,' ');
779                caca_put_str(m_caca, setup_p.x + setup_size.z + 1, y, setup_text[k]);
780            }
781        }
782        /* display variable */
783        int y = setup_p.y + setup_size.y;
784        setup_item_key = setup_option * (setup_item_n + 1) + 1 + setup_item;
785        caca_set_color_argb(m_caca, setup_color.y, setup_color.x);
786        caca_draw_line(m_caca, setup_p.x, y, setup_p.x + setup_size.x, y,' ');
787        if (setup_switch)
788        {
789            int x = setup_p.x + 1;
790            int w = setup_size.x - 3 - 4;
791            int bar_w = w / (setup_var[setup_item_key].y - setup_var[setup_item_key].x);
792            int bar_x = bar_w * setup_var[setup_item_key].x;
793            if ((setup_var[setup_item_key].y - setup_var[setup_item_key].x) / setup_var[setup_item_key].z > 2)
794            {
795                caca_printf(m_caca, setup_p.x + setup_size.x - 4, y, "%4.2f", setup_var[setup_item_key].w);
796                caca_draw_line(m_caca, x, y, x - bar_x + bar_w * setup_var[setup_item_key].y, y,'.');
797                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');
798            }
799            else
800            {
801                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");
802            }
803        }
804    }
805
806    Draw2D();
807    Draw3D();
808
809}
810
811void Render::Draw2D()
812{
813    /* Draw text in an offline buffer */
814    text_render->Render();
815
816    if (m_shader)
817        fbo_back->Bind();
818
819    glViewport(0, 0, screen_size.x, screen_size.y);
820
821    /* Clear the back buffer */
822    glEnable(GL_BLEND);
823    glBlendFunc(GL_SRC_COLOR, GL_DST_ALPHA);
824
825    Video::SetClearColor(vec4(screen_color, 1.f));
826    Video::SetClearDepth(1.0f); // set depth buffer
827    Video::Clear(ClearMask::Color | ClearMask::Depth);
828
829    text_render->Blit(border, canvas_size);
830
831    //if (m_polygon) glEnable(GL_LINE_SMOOTH); else glDisable(GL_LINE_SMOOTH);
832    glLineWidth((m_polygon)?2.0f:1.0f);
833    fx_angle=main_angle-part_angle;
834    if (m_polygon)
835        glEnable(GL_TEXTURE_2D);
836
837    glMatrixMode(GL_PROJECTION);
838    mat4 m = mat4::ortho(0, screen_size.x, screen_size.y, 0, -1.f, 1.f);
839    glLoadMatrixf(&m[0][0]);
840    glMatrixMode(GL_MODELVIEW);
841}
842
843void Render::Draw3D()
844{
845    if (!m_shader)
846        return;
847
848    glDisable(GL_DEPTH_TEST);
849    glDisable(GL_BLEND);
850
851    glEnableClientState(GL_VERTEX_ARRAY);
852    glVertexPointer(4, GL_FLOAT, 0, fs_quad_vtx);
853
854    glEnableClientState(GL_TEXTURE_COORD_ARRAY);
855    glTexCoordPointer(2, GL_FLOAT, 0, fs_quad_tex);
856
857    if (m_shader_remanency)
858    {
859        // shader remanency
860        fbo_ping->Bind();
861        shader_remanency->Bind();
862        shader_remanency->SetUniform(shader_remanency_source, fbo_back->GetTexture(), 0);
863        shader_remanency->SetUniform(shader_remanency_buffer, fbo_buffer->GetTexture(), 1);
864        shader_remanency->SetUniform(shader_remanency_mix, remanency);
865        fs_quad();
866        shader_remanency->Unbind();
867        fbo_ping->Unbind();
868        // shader simple
869        fbo_back->Bind();
870        draw_shader_simple(fbo_ping, 0);
871        fbo_back->Unbind();
872        // save previous fbo
873        fbo_ping->Bind();
874        shader_remanency->Bind();
875        shader_remanency->SetUniform(shader_remanency_source, fbo_front->GetTexture(), 0);
876        shader_remanency->SetUniform(shader_remanency_buffer, fbo_buffer->GetTexture(), 1);
877        shader_remanency->SetUniform(shader_remanency_mix, buffer);
878        fs_quad();
879        shader_remanency->Unbind();
880        fbo_ping->Unbind();
881        // shader simple
882        fbo_buffer->Bind();
883        draw_shader_simple(fbo_ping, 0);
884        fbo_buffer->Unbind();
885    }
886
887    // shader glow
888    if (m_shader_glow)
889    {
890        // shader blur horizontal
891        fbo_blur_h->Bind();
892        shader_blur_h->Bind();
893        shader_blur_h->SetUniform(shader_blur_h_texture, fbo_back->GetTexture(), 0);
894        shader_blur_h->SetUniform(shader_blur_h_radius, glow_large / screen_size.x);
895        fs_quad();
896        shader_blur_h->Unbind();
897        fbo_blur_h->Unbind();
898        // shader blur vertical
899        fbo_blur_v->Bind();
900        shader_blur_v->Bind();
901        shader_blur_v->SetUniform(shader_blur_v_texture, fbo_blur_h->GetTexture(), 0);
902        shader_blur_v->SetUniform(shader_blur_v_radius, glow_large / screen_size.y);
903        fs_quad();
904        shader_blur_v->Unbind();
905        fbo_blur_v->Unbind();
906        // shader blur horizontal
907        fbo_blur_h->Bind();
908        shader_blur_h->Bind();
909        shader_blur_h->SetUniform(shader_blur_h_texture, fbo_blur_v->GetTexture(), 0);
910        shader_blur_h->SetUniform(shader_blur_h_radius, glow_small / screen_size.x);
911        fs_quad();
912        shader_blur_h->Unbind();
913        fbo_blur_h->Unbind();
914        // shader blur vertical
915        fbo_blur_v->Bind();
916        shader_blur_v->Bind();
917        shader_blur_v->SetUniform(shader_blur_v_texture, fbo_blur_h->GetTexture(), 0);
918        shader_blur_v->SetUniform(shader_blur_v_radius, glow_small / screen_size.y);
919        fs_quad();
920        shader_blur_v->Unbind();
921        fbo_blur_v->Unbind();
922        // shader glow
923        fbo_front->Bind();
924        shader_glow->Bind();
925        shader_glow->SetUniform(shader_glow_glow, fbo_blur_v->GetTexture(), 0);
926        shader_glow->SetUniform(shader_glow_source, fbo_back->GetTexture(), 1);
927        shader_glow->SetUniform(shader_glow_mix, glow_mix);
928        fs_quad();
929        shader_glow->Unbind();
930        fbo_front->Unbind();
931    }
932    else
933    {
934        // shader simple
935        fbo_front->Bind();
936        draw_shader_simple(fbo_back, 0);
937        fbo_front->Unbind();
938    }
939
940    if (m_shader_blur)
941    {
942        // shader blur horizontal
943        fbo_ping->Bind();
944        shader_blur_h->Bind();
945        shader_blur_h->SetUniform(shader_blur_h_texture, fbo_front->GetTexture(), 0);
946        shader_blur_h->SetUniform(shader_blur_h_radius, blur / screen_size.x);
947        fs_quad();
948        shader_blur_h->Unbind();
949        fbo_ping->Unbind();
950        // shader blur vertical
951        fbo_front->Bind();
952        shader_blur_v->Bind();
953        shader_blur_v->SetUniform(shader_blur_v_texture, fbo_ping->GetTexture(), 0);
954        shader_blur_v->SetUniform(shader_blur_v_radius, blur / screen_size.y);
955        fs_quad();
956        shader_blur_v->Unbind();
957        fbo_front->Unbind();
958    }
959
960    if (m_shader_postfx)
961    {
962        // shader postfx
963        shader_postfx->Bind();
964        shader_postfx->SetUniform(shader_postfx_texture, fbo_front->GetTexture(), 0);
965        shader_postfx->SetUniform(shader_postfx_screen_size, (vec2)screen_size);
966        shader_postfx->SetUniform(shader_postfx_time, fx_angle);
967        shader_postfx->SetUniform(shader_postfx_deform, postfx_deform);
968        shader_postfx->SetUniform(shader_postfx_ghost1, postfx_ghost1);
969        shader_postfx->SetUniform(shader_postfx_ghost2, postfx_ghost2);
970        shader_postfx->SetUniform(shader_postfx_filter, postfx_filter);
971        shader_postfx->SetUniform(shader_postfx_color, postfx_color);
972        shader_postfx->SetUniform(shader_postfx_corner, postfx_corner);
973        shader_postfx->SetUniform(shader_postfx_vignetting, postfx_vignetting);
974        shader_postfx->SetUniform(shader_postfx_retrace, postfx_retrace);
975        shader_postfx->SetUniform(shader_postfx_offset, postfx_offset);
976        shader_postfx->SetUniform(shader_postfx_noise, postfx_noise);
977        shader_postfx->SetUniform(shader_postfx_aberration, postfx_aberration);
978        shader_postfx->SetUniform(shader_postfx_moire_h, postfx_moire_h);
979        shader_postfx->SetUniform(shader_postfx_moire_v, postfx_moire_v);
980        shader_postfx->SetUniform(shader_postfx_scanline_h, postfx_scanline_h);
981        shader_postfx->SetUniform(shader_postfx_scanline_v, postfx_scanline_v);
982        shader_postfx->SetUniform(shader_postfx_flash, flash_value);
983        shader_postfx->SetUniform(shader_postfx_sync, (float)fabs(sync_value*cosf((main_angle-sync_angle)*6.0f)));
984        fs_quad();
985        shader_postfx->Unbind();
986    }
987    else
988    {
989        // shader simple
990        draw_shader_simple(fbo_front, 0);
991    }
992
993    glDisableClientState(GL_VERTEX_ARRAY);
994    glDisableClientState(GL_TEXTURE_COORD_ARRAY);
995}
996
997Render::~Render()
998{
999}
Note: See TracBrowser for help on using the repository browser.