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

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

glsl letterbox and round corner & some new fancy animation!

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