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

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

remanency lolfx added

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