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

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

added smoothstep in postfx (more contrast!) and a magnificient LOLCUBE!

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