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

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

copper.lolfx added

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