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

Last change on this file since 1694 was 1694, checked in by rez, 10 years ago

added parametrable hue/brightness/contrast to postfx and found a pretty decent scanline scheme

File size: 19.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_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
55float timer_key = 0;       // key timer
56float timer_key_repeat = 0.25f;// key repeat delay
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 = 1.5f;  // 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 = 1.0f;   // 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/* window variable */
88ivec2 border;              // border width
89/* text variable */
90ivec2 ratio_2d(2,4);       // 2d ratio
91ivec2 map_size(256,256);   // texture map size
92ivec2 font_size(8,8);      // font size
93ivec2 canvas_char(0,0);    // canvas char number
94ivec2 canvas_size(0,0);    // caca size
95/* common variable */
96float value, angle, radius, scale, speed;
97/* shader variable */
98vec2 buffer(0.75f,0.25f);       // [new frame mix,old frame mix]
99vec2 remanency(0.25f,0.75f);    // remanency [source mix,buffer mix]
100vec2 blur(0.25f,0.75f);         // glow radius [normal,deform]
101vec2 glow_mix(0.5f,0.5f);       // glow mix [source mix,glow mix]
102vec2 glow_large(2.0f,2.0f);     // large glow radius [normal,deform]
103vec2 glow_small(1.0f,1.0f);     // small glow radius [normal,deform]
104//vec3 radial(2.0f,0.8f,0);     // radial [mix,strength,color mode]
105//---------------------------------[IDEAS] http://www.youtube.com/watch?v=d1qEP2vMe-I
106float postfx_deform = 0.625f;   // deformation ratio
107vec3 postfx_filter(0.875f,0.75f,1.0f);// color filter [red,green,blue]
108vec3 postfx_color(1.75f,1.75f,0.5f);  // color modifier [brightness,contrast,gray]
109vec3 postfx_retrace(0.025f,2.0f,4.0f);// retrace [color,length,speed]
110vec2 postfx_offset(3.0f,3.0f);  // random line [horizontal,vertical]
111float postfx_noise = 0.125f;    // noise
112float postfx_aberration = 3.0f; // chromatic aberration
113bool postfx_moire = true;       // moire
114vec4 postfx_moire_h(0.75f,-0.25f,0.0f,1.0f);
115vec4 postfx_moire_v(0.75f,-0.25f,1.0f,1.5f);
116bool postfx_scanline = true;    // scanline
117vec4 postfx_scanline_h(0.75f, 0.25f,0.0f,2.0f);// vertical scanline [base,variable,repeat x,repeat y]
118vec4 postfx_scanline_v(0.75f,-0.25f,2.0f,0.0f);// horizontal scanline [base,variable,repeat x,repeat y]
119
120Shader *shader_simple;
121Shader *shader_blur_h, *shader_blur_v;
122Shader *shader_remanency, *shader_glow, *shader_radial, *shader_postfx;
123// shader variables
124ShaderUniform shader_simple_texture;
125ShaderUniform shader_blur_h_texture,
126              shader_blur_h_radius,
127              shader_blur_v_texture,
128              shader_blur_v_radius;
129ShaderUniform shader_remanency_source,
130              shader_remanency_buffer,
131              shader_remanency_mix;
132ShaderUniform shader_glow_glow,
133              shader_glow_source,
134              shader_glow_mix;
135ShaderUniform shader_radial_texture,
136              shader_radial_screen_size,
137              shader_radial_time,
138              shader_radial_value1,
139              shader_radial_value2,
140              shader_radial_color;
141ShaderUniform shader_postfx_texture,
142              shader_postfx_texture_2d,
143              shader_postfx_screen_size,
144              shader_postfx_time,
145              shader_postfx_deform,
146              shader_postfx_filter,
147              shader_postfx_color,
148              shader_postfx_retrace,
149              shader_postfx_offset,
150              shader_postfx_noise,
151              shader_postfx_aberration,
152              shader_postfx_moire,
153              shader_postfx_moire_h,
154              shader_postfx_moire_v,
155              shader_postfx_scanline,
156              shader_postfx_scanline_h,
157              shader_postfx_scanline_v,
158              shader_postfx_flash,
159              shader_postfx_sync;
160
161FrameBuffer *fbo_back, *fbo_front, *fbo_buffer;
162FrameBuffer *fbo_blur_h, *fbo_blur_v, *fbo_ping, *fbo_pong;
163
164TextRender *text_render;
165
166void fs_quad()
167{
168    glLoadIdentity();
169    glDrawArrays(GL_QUADS, 0, 4);
170}
171
172void draw_shader_simple(FrameBuffer *fbo_output, int n)
173{
174    shader_simple->Bind();
175    shader_simple->SetTexture(shader_simple_texture, fbo_output->GetTexture(), n);
176    fs_quad();
177    shader_simple->Unbind();
178}
179
180void rectangle(int x, int y, int w, int h)
181{
182    glLoadIdentity();
183    glBegin(GL_QUADS);
184        glVertex2i(x+w, y  );
185        glVertex2i(x  , y  );
186        glVertex2i(x  , y+h);
187        glVertex2i(x+w, y+h);
188    glEnd();
189}
190
191int Render::InitDraw(void)
192{
193    glDepthMask(GL_TRUE);     // do not write z-buffer
194    glEnable(GL_CULL_FACE);   // disable cull face
195    glCullFace(GL_BACK);      // don't draw front face
196
197    /* Initialise framebuffer objects */
198    fbo_back = new FrameBuffer(screen_size);
199    fbo_front = new FrameBuffer(screen_size);
200    fbo_buffer = new FrameBuffer(screen_size);
201    fbo_blur_h = new FrameBuffer(screen_size);
202    fbo_blur_v = new FrameBuffer(screen_size);
203    fbo_ping = new FrameBuffer(screen_size);
204    fbo_pong = new FrameBuffer(screen_size);
205    // shader simple
206    shader_simple = Shader::Create(lolfx_simple);
207    shader_simple_texture = shader_simple->GetUniformLocation("texture");
208    // shader blur horizontal
209    shader_blur_h = Shader::Create(lolfx_blurh);
210    shader_blur_h_texture = shader_blur_h->GetUniformLocation("texture");
211    shader_blur_h_radius = shader_blur_h->GetUniformLocation("radius");
212    // shader blur vertical
213    shader_blur_v = Shader::Create(lolfx_blurv);
214    shader_blur_v_texture = shader_blur_v->GetUniformLocation("texture");
215    shader_blur_v_radius = shader_blur_v->GetUniformLocation("radius");
216    // shader remanency
217    shader_remanency = Shader::Create(lolfx_remanency);
218    shader_remanency_source = shader_remanency->GetUniformLocation("source");
219    shader_remanency_buffer = shader_remanency->GetUniformLocation("buffer");
220    shader_remanency_mix = shader_remanency->GetUniformLocation("mix");
221    // shader glow
222    shader_glow = Shader::Create(lolfx_glow);
223    shader_glow_glow = shader_glow->GetUniformLocation("glow");
224    shader_glow_source = shader_glow->GetUniformLocation("source");
225    shader_glow_mix = shader_glow->GetUniformLocation("mix");
226    // shader radial
227    shader_radial = Shader::Create(lolfx_radial);
228    shader_radial_texture = shader_radial->GetUniformLocation("texture");
229    shader_radial_screen_size = shader_radial->GetUniformLocation("screen_size");
230    shader_radial_time = shader_radial->GetUniformLocation("time");
231    shader_radial_value1 = shader_radial->GetUniformLocation("value1");
232    shader_radial_value2 = shader_radial->GetUniformLocation("value2");
233    shader_radial_color = shader_radial->GetUniformLocation("color");
234    // shader postfx
235    shader_postfx = Shader::Create(lolfx_postfx);
236    shader_postfx_texture = shader_postfx->GetUniformLocation("texture");
237    shader_postfx_texture_2d = shader_postfx->GetUniformLocation("texture_2d");
238    shader_postfx_screen_size = shader_postfx->GetUniformLocation("screen_size");
239    shader_postfx_time = shader_postfx->GetUniformLocation("time");
240    shader_postfx_deform = shader_postfx->GetUniformLocation("deform");
241    shader_postfx_filter = shader_postfx->GetUniformLocation("filter");
242    shader_postfx_color = shader_postfx->GetUniformLocation("color");
243    shader_postfx_retrace = shader_postfx->GetUniformLocation("retrace");
244    shader_postfx_offset = shader_postfx->GetUniformLocation("offset");
245    shader_postfx_noise = shader_postfx->GetUniformLocation("noise");
246    shader_postfx_aberration = shader_postfx->GetUniformLocation("aberration");
247    shader_postfx_moire = shader_postfx->GetUniformLocation("moire");
248    shader_postfx_moire_h = shader_postfx->GetUniformLocation("moire_h");
249    shader_postfx_moire_v = shader_postfx->GetUniformLocation("moire_v");
250    shader_postfx_scanline = shader_postfx->GetUniformLocation("scanline");
251    shader_postfx_scanline_h = shader_postfx->GetUniformLocation("scanline_h");
252    shader_postfx_scanline_v = shader_postfx->GetUniformLocation("scanline_v");
253    shader_postfx_flash = shader_postfx->GetUniformLocation("flash");
254    shader_postfx_sync = shader_postfx->GetUniformLocation("sync");
255
256    return true;
257}
258
259int Render::CreateGLWindow()
260{
261    screen_size = Video::GetSize();
262    border = 18 * ratio_2d;
263    border.y = border.x; // enabled to get same border everywhere
264    canvas_char = (screen_size - border * 2) / (font_size * ratio_2d);
265    canvas_size = canvas_char * font_size * ratio_2d;
266
267    border = (screen_size - canvas_size) / 2;
268
269    caca_set_canvas_size(m_caca, canvas_char.x, canvas_char.y);
270
271    InitDraw();
272    return true;
273}
274
275Render::Render(caca_canvas_t *caca)
276  : m_caca(caca),
277    m_ready(false),
278    m_pause(false),
279    m_polygon(true),
280    m_shader(true),
281    m_shader_remanency(true),
282    m_shader_glow(true),
283    m_shader_blur(true),
284    m_shader_fx(true),
285    m_shader_postfx(true),
286    m_border(false)
287{
288    text_render = new TextRender(m_caca, font_size);
289}
290
291void Render::TickGame(float seconds)
292{
293    Entity::TickGame(seconds);
294}
295
296void Render::Pause()
297{
298    m_pause=!m_pause;
299}
300
301void Render::TickDraw(float seconds)
302{
303    /* keyboard manager */
304    if (Input::GetButtonState(27/*SDLK_ESCAPE*/))
305        Ticker::Shutdown();
306    //if (Input::GetButtonState(282/*SDLK_F1*/))
307    //    LEAULE();
308    if (Input::GetButtonState(283/*SDLK_F2*/)&&(timer-timer_key>timer_key_repeat))
309        {
310        m_polygon = !m_polygon;
311        polygon_fillmode = (m_polygon)?GL_FILL:GL_LINE;
312        glPolygonMode(GL_FRONT, polygon_fillmode);
313        timer_key = timer;
314        }
315    if (Input::GetButtonState(284/*SDLK_F3*/)&&(timer-timer_key>timer_key_repeat))
316        {
317        m_shader = !m_shader;
318        timer_key = timer;
319        }
320    if (Input::GetButtonState(285/*SDLK_F4*/)&&(timer-timer_key>timer_key_repeat))
321        {
322        m_shader_postfx = !m_shader_postfx;
323        timer_key = timer;
324        }
325    if (Input::GetButtonState(286/*SDLK_F5*/))
326        {
327        Pause();
328        }
329
330    Entity::TickDraw(seconds);
331
332    if (!m_ready)
333    {
334        CreateGLWindow();
335        text_render->Init();
336        m_ready = true;
337    }
338
339    // timer
340    if (!m_pause)
341    {
342        timer += seconds;
343        main_angle = timer * 100.0f * PID;
344    }
345    if (sync_flag)
346    {
347        angle=(main_angle-sync_angle)*sync_speed;
348        sync_value=1.0f-sinf(angle);
349        if (angle>90.0f*PID)
350        {
351            sync_value=0;
352            sync_flag=false;
353        }
354    }
355    if (beat_flag)
356    {
357        angle=(main_angle-beat_angle)*beat_speed;
358        beat_value=1.0f-sinf(angle);
359        if (angle>90.0f*PID)
360        {
361            beat_value=0;
362            beat_flag=false;
363        }
364    }
365    if (flash_flag)
366    {
367        angle=(main_angle-flash_angle)*flash_speed;
368        flash_value=1.0f-sinf(angle);
369        if (angle>90.0f*PID)
370        {
371            flash_value=0;
372            flash_flag=false;
373        }
374    }
375    if (fade_flag)
376    {
377        angle=(main_angle-fade_angle)*fade_speed;
378        fade_value=1.0f-sinf(angle);
379        if (angle>90.0f*PID)
380        {
381            fade_value=0;
382            fade_flag=false;
383        }
384    }
385
386    Draw2D();
387    Draw3D();
388}
389
390void Render::Draw2D()
391{
392    /* Draw text in an offline buffer */
393    text_render->Render();
394
395    if (m_shader)
396    {
397        fbo_back->Bind();
398    }
399
400    glViewport(0, 0, screen_size.x, screen_size.y);
401
402    /* Clear the back buffer */
403    glEnable(GL_BLEND);
404    glBlendFunc(GL_SRC_COLOR,GL_DST_ALPHA);
405    glClearColor(screen_color.r, screen_color.g, screen_color.b, 1.0f);
406    glClearDepth(1.0f); // set depth buffer
407    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
408
409    text_render->Blit(border, canvas_size);
410
411    //if (m_polygon) glEnable(GL_LINE_SMOOTH); else glDisable(GL_LINE_SMOOTH);
412    glLineWidth((m_polygon)?2.0f:1.0f);
413    fx_angle=main_angle-part_angle;
414    if (m_polygon)
415        glEnable(GL_TEXTURE_2D);
416
417    glMatrixMode(GL_PROJECTION);
418    mat4 m = mat4::ortho(0, screen_size.x, screen_size.y, 0, -1.f, 1.f);
419    glLoadMatrixf(&m[0][0]);
420    glMatrixMode(GL_MODELVIEW);
421    // draw border
422    if (m_border)
423    {
424        glDisable(GL_TEXTURE_2D);
425        glDisable(GL_BLEND);
426        glColor3f(1.0f, 1.0f, 1.0f);
427        rectangle(border.x - ratio_2d.x, border.y - ratio_2d.y, canvas_size.x + ratio_2d.x * 2, ratio_2d.y);
428        rectangle(border.x - ratio_2d.x, border.y, ratio_2d.x, canvas_size.y);
429        rectangle(border.x + canvas_size.x, border.y, ratio_2d.x, canvas_size.y);
430        rectangle(border.x - ratio_2d.x, border.y + canvas_size.y, canvas_size.x + ratio_2d.x * 2, ratio_2d.y);
431        glEnable(GL_BLEND);
432    }
433}
434
435void Render::Draw3D()
436{
437    if (!m_shader)
438        return;
439
440    glDisable(GL_DEPTH_TEST);
441    glDisable(GL_BLEND);
442
443    glEnableClientState(GL_VERTEX_ARRAY);
444    glVertexPointer(4, GL_FLOAT, 0, fs_quad_vtx);
445
446    glEnableClientState(GL_TEXTURE_COORD_ARRAY);
447    glTexCoordPointer(2, GL_FLOAT, 0, fs_quad_tex);
448
449    if (m_shader_remanency)
450    {
451        // shader remanency
452        fbo_ping->Bind();
453        shader_remanency->Bind();
454        shader_remanency->SetTexture(shader_remanency_source, fbo_back->GetTexture(), 0);
455        shader_remanency->SetTexture(shader_remanency_buffer, fbo_buffer->GetTexture(), 1);
456        shader_remanency->SetUniform(shader_remanency_mix, remanency);
457        fs_quad();
458        shader_remanency->Unbind();
459        fbo_ping->Unbind();
460        // shader simple
461        fbo_back->Bind();
462        draw_shader_simple(fbo_ping, 0);
463        fbo_back->Unbind();
464        // save previous fbo
465        fbo_ping->Bind();
466        shader_remanency->Bind();
467        shader_remanency->SetTexture(shader_remanency_source, fbo_front->GetTexture(), 0);
468        shader_remanency->SetTexture(shader_remanency_buffer, fbo_buffer->GetTexture(), 1);
469        shader_remanency->SetUniform(shader_remanency_mix, buffer);
470        fs_quad();
471        shader_remanency->Unbind();
472        fbo_ping->Unbind();
473        // shader simple
474        fbo_buffer->Bind();
475        draw_shader_simple(fbo_ping, 0);
476        fbo_buffer->Unbind();
477    }
478
479    // shader glow
480    if (m_shader_fx && m_shader_glow)
481    {
482        // shader blur horizontal
483        fbo_blur_h->Bind();
484        shader_blur_h->Bind();
485        shader_blur_h->SetTexture(shader_blur_h_texture, fbo_back->GetTexture(), 0);
486        shader_blur_h->SetUniform(shader_blur_h_radius, glow_large / screen_size.x);
487        fs_quad();
488        shader_blur_h->Unbind();
489        fbo_blur_h->Unbind();
490        // shader blur vertical
491        fbo_blur_v->Bind();
492        shader_blur_v->Bind();
493        shader_blur_v->SetTexture(shader_blur_v_texture, fbo_blur_h->GetTexture(), 0);
494        shader_blur_v->SetUniform(shader_blur_v_radius, glow_large / screen_size.y);
495        fs_quad();
496        shader_blur_v->Unbind();
497        fbo_blur_v->Unbind();
498        // shader blur horizontal
499        fbo_blur_h->Bind();
500        shader_blur_h->Bind();
501        shader_blur_h->SetTexture(shader_blur_h_texture, fbo_blur_v->GetTexture(), 0);
502        shader_blur_h->SetUniform(shader_blur_h_radius, glow_small / screen_size.x);
503        fs_quad();
504        shader_blur_h->Unbind();
505        fbo_blur_h->Unbind();
506        // shader blur vertical
507        fbo_blur_v->Bind();
508        shader_blur_v->Bind();
509        shader_blur_v->SetTexture(shader_blur_v_texture, fbo_blur_h->GetTexture(), 0);
510        shader_blur_v->SetUniform(shader_blur_v_radius, glow_small / screen_size.y);
511        fs_quad();
512        shader_blur_v->Unbind();
513        fbo_blur_v->Unbind();
514        // shader glow
515        fbo_front->Bind();
516        shader_glow->Bind();
517        shader_glow->SetTexture(shader_glow_glow, fbo_blur_v->GetTexture(), 0);
518        shader_glow->SetTexture(shader_glow_source, fbo_back->GetTexture(), 1);
519        shader_glow->SetUniform(shader_glow_mix, glow_mix);
520        fs_quad();
521        shader_glow->Unbind();
522        fbo_front->Unbind();
523    }
524    else
525    {
526        // shader simple
527        fbo_front->Bind();
528        draw_shader_simple(fbo_back, 0);
529        fbo_front->Unbind();
530    }
531
532    if (m_shader_fx && m_shader_blur)
533    {
534        // shader blur horizontal
535        fbo_ping->Bind();
536        shader_blur_h->Bind();
537        shader_blur_h->SetTexture(shader_blur_h_texture, fbo_front->GetTexture(), 0);
538        shader_blur_h->SetUniform(shader_blur_h_radius, blur / screen_size.x);
539        fs_quad();
540        shader_blur_h->Unbind();
541        fbo_ping->Unbind();
542        // shader blur vertical
543        fbo_front->Bind();
544        shader_blur_v->Bind();
545        shader_blur_v->SetTexture(shader_blur_v_texture, fbo_ping->GetTexture(), 0);
546        shader_blur_v->SetUniform(shader_blur_v_radius, blur / screen_size.y);
547        fs_quad();
548        shader_blur_v->Unbind();
549        fbo_front->Unbind();
550    }
551
552    if (m_shader_postfx)
553    {
554        // shader postfx
555        shader_postfx->Bind();
556        shader_postfx->SetTexture(shader_postfx_texture, fbo_front->GetTexture(), 0);
557        shader_postfx->SetUniform(shader_postfx_screen_size, (vec2)screen_size);
558        shader_postfx->SetUniform(shader_postfx_time, fx_angle);
559        shader_postfx->SetUniform(shader_postfx_deform, postfx_deform);
560        shader_postfx->SetUniform(shader_postfx_filter, postfx_filter);
561        shader_postfx->SetUniform(shader_postfx_color, postfx_color);
562        shader_postfx->SetUniform(shader_postfx_retrace, postfx_retrace);
563        shader_postfx->SetUniform(shader_postfx_offset, postfx_offset);
564        shader_postfx->SetUniform(shader_postfx_noise, postfx_noise);
565        shader_postfx->SetUniform(shader_postfx_aberration, postfx_aberration);
566        shader_postfx->SetUniform(shader_postfx_moire, postfx_moire);
567        shader_postfx->SetUniform(shader_postfx_moire_h, postfx_moire_h);
568        shader_postfx->SetUniform(shader_postfx_moire_v, postfx_moire_v);
569        shader_postfx->SetUniform(shader_postfx_scanline, postfx_scanline);
570        shader_postfx->SetUniform(shader_postfx_scanline_h, postfx_scanline_h);
571        shader_postfx->SetUniform(shader_postfx_scanline_v, postfx_scanline_v);
572        shader_postfx->SetUniform(shader_postfx_flash, flash_value);
573        shader_postfx->SetUniform(shader_postfx_sync, (float)fabs(beat_value*cosf((main_angle-beat_angle)*8.0f)));
574        fs_quad();
575        shader_postfx->Unbind();
576    }
577    else
578    {
579        // shader simple
580        draw_shader_simple(fbo_front, 0);
581    }
582
583    glDisableClientState(GL_VERTEX_ARRAY);
584    glDisableClientState(GL_TEXTURE_COORD_ARRAY);
585}
586
587Render::~Render()
588{
589}
Note: See TracBrowser for help on using the repository browser.