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

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

remanency buffer now use ONLY ONE buffer / renamed font map, added a new one (dos) / fixed the ASCII logo

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