source: trunk/neercs/video/render.cpp @ 1467

Last change on this file since 1467 was 1467, checked in by sam, 9 years ago

neercs: start working on direct rendering of libcaca canvases.

File size: 29.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
33extern char const *lolfx_blurh;
34extern char const *lolfx_blurv;
35extern char const *lolfx_glow;
36extern char const *lolfx_postfx;
37extern char const *lolfx_radial;
38extern char const *lolfx_simple;
39extern char const *lolfx_text;
40
41#define WIN32_LEAN_AND_MEAN
42#define DEBUG 1            // debug flag //#if defined(_DEBUG)
43#define SHADER 1           // shader flag
44#define FUCK 0             // test flag
45#define PI 3.1415926535f   // pi
46#define PID PI/180.0f      // pi ratio
47#define CR 1.0f/256.0f     // color ratio
48
49bool done = false;
50
51/*
52 * Various variables
53 */
54
55int keys[256];             // keyboard array
56int active = true;         // window active flag
57bool fullscreen = DEBUG?false:true;    // fullscreen flag
58bool paused = false;       // pause flag
59float nearplane = 0.1f;    // nearplane
60float farplane = 1000.0f;  // farplane
61bool polygon = true;       // polygon mode
62int polygon_fillmode = GL_FILL; // fill mode
63int ratio_2d = 4;          // 2d ratio
64/* window variable */
65int base_w = 800;          // base width
66int base_h = 600;          // base height
67int window_w = base_w;     // width
68int window_h = base_h;     // height
69ivec2 screen_size;         // screen size
70int window_color = 32;     // color depth
71vec3 screen_color;         // screen color
72/* object variable */
73float main_angle;          // main angle
74float part_angle;          // part angle
75float fx_angle;            // current angle
76/* text variable */
77char const *name = "cacaShell";
78/* fs_quad variable */
79float 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};
80float fs_quad_tex[] = {0, 1.0f, 0, 0, 1.0f, 0, 1.0f, 1.0f};
81/* flash variable */
82bool flash_flag = false;   // flag
83float flash_angle = 0;     // angle
84float flash_value = 0;     // value
85float flash_speed;         // speed
86/* fade variable */
87bool fade_flag = false;    // flag
88float fade_angle = 0;      // angle
89float fade_value = 0;      // value
90float fade_speed;          // speed
91/* debug variable */
92bool debug_flag = false;   // flag
93int debug_x;               // position x
94int debug_y;               // position y
95int debug_w;               // width
96int debug_h;               // height
97int debug_vtx[8];          // vertex array
98/* sync variable */
99bool sync_flag = false;    // flag
100float sync_angle = 0;      // angle
101float sync_value = 0;      // value
102float sync_speed;          // speed
103/* beat variable */
104bool beat_flag = false;    // flag
105float beat_angle = 0;      // angle
106float beat_value = 0;      // value
107float beat_speed;          // speed
108/* corner variable */
109const int corner_n = 10;   // polygon number
110int corner_w = 24;         // radius
111int corner_vtx[corner_n*6];// vertex array
112/* dos variable */
113bool dos_flag = false;     // flag
114int dos_w;                 // width
115int dos_h;                 // height
116int dos_m;                 // margin
117vec3 dos_color;            // color value
118int dos_vtx[8];            // vertex array
119int shell_vtx[8];          // vertex array
120float shell_tex[] = {1.0f, 0.96875f, 1.0f, 1.0f, 0.78125f, 1.0f, 0.78125f, 0.96875f};
121/* keyboard variable */
122int key_code = 0;          // keyboard code
123/* common variable */
124float value, angle, radius, scale, speed;
125TileSet *tex_map;
126/* shader variable */
127bool shader_flag = SHADER?true:false;
128bool shader_blur_flag = SHADER?true:false;
129bool shader_effect_flag = SHADER?true:false;
130bool shader_glow_flag = true;
131int glow_fbo_size = 2;     // glow fbo size
132int glow_color;            // 0 = color/1 = alpha
133float glow_smoothstep;     // glow smoothstep value
134float glow_mix_ratio1;     // glow mixing ratio
135float glow_mix_ratio2;     // source mixing ratio
136bool shader_radial_flag = false;
137float radial_value1;
138float radial_value2;
139float radial_color;        // color
140bool shader_postfx_flag = SHADER?true:false;
141bool postfx_scanline = true;
142float postfx_deform;       // deformation ratio
143#if SHADER
144Shader *shader_simple, *shader_blur_h, *shader_blur_v;
145Shader *shader_glow, *shader_radial, *shader_postfx;
146// shader variables
147ShaderUniform shader_simple_texture;
148ShaderUniform shader_blur_h_texture,
149              shader_blur_h_screen_size,
150              shader_blur_h_time,
151              shader_blur_h_value;
152ShaderUniform shader_blur_v_texture,
153              shader_blur_v_screen_size,
154              shader_blur_v_time,
155              shader_blur_v_value;
156ShaderUniform shader_glow_texture,
157              shader_glow_texture_prv,
158              shader_glow_screen_size,
159              shader_glow_time,
160              shader_glow_step,
161              shader_glow_value1,
162              shader_glow_value2;
163ShaderUniform shader_radial_texture,
164              shader_radial_screen_size,
165              shader_radial_time,
166              shader_radial_value1,
167              shader_radial_value2,
168              shader_radial_color;
169ShaderUniform shader_postfx_texture,
170              shader_postfx_texture_2d,
171              shader_postfx_screen_size,
172              shader_postfx_time,
173              shader_postfx_flash,
174              shader_postfx_value,
175              shader_postfx_deform,
176              shader_postfx_scanline,
177              shader_postfx_sync;
178
179FrameBuffer *fbo_back, *fbo_front, *fbo_blur_h, *fbo_blur_v;
180FrameBuffer *fbo_ping, *fbo_pong;
181#endif
182
183#if FUCK
184/*
185 * Text rendering interface
186 */
187struct TextRender
188{
189    TextRender(caca_canvas_t *caca)
190      : m_caca(caca),
191        m_width(caca_get_canvas_width(m_caca)),
192        m_height(caca_get_canvas_height(m_caca))
193    {
194        for (int j = 0; j < m_height; j++)
195        for (int i = 0; i < m_width; i++)
196            m_vertices << vec2(0.05 * i - 1.0, 0.5 - 0.09 * j);
197    }
198
199    void Init()
200    {
201        m_shader = Shader::Create(lolfx_text);
202        m_coord = m_shader->GetAttribLocation("in_Position",
203                                              VertexUsage::Position, 0);
204        m_color = m_shader->GetAttribLocation("in_Color",
205                                              VertexUsage::Color, 0);
206        m_char = m_shader->GetAttribLocation("in_Char",
207                                             VertexUsage::Color, 1);
208        m_texture = m_shader->GetUniformLocation("in_Texture");
209        m_vdecl
210          = new VertexDeclaration(VertexStream<vec2>(VertexUsage::Position),
211                                  VertexStream<int32_t>(VertexUsage::Color),
212                                  VertexStream<int32_t>(VertexUsage::Color));
213
214        m_vbo1 = new VertexBuffer(m_vertices.Bytes());
215        void *vertices = m_vbo1->Lock(0, 0);
216        memcpy(vertices, &m_vertices[0], m_vertices.Bytes());
217        m_vbo1->Unlock();
218
219        m_vbo2 = new VertexBuffer(m_width * m_height * sizeof(int32_t));
220        m_vbo3 = new VertexBuffer(m_width * m_height * sizeof(int32_t));
221    }
222
223    void Blit()
224    {
225        void *colors = m_vbo2->Lock(0, 0);
226        memcpy(colors, caca_get_canvas_attrs(m_caca),
227               m_width * m_height * sizeof(uint32_t));
228for (int j = 0; j < m_height; j++)
229for (int i = 0; i < m_width; i++)
230{
231    uint32_t attr = caca_get_attr(m_caca, i, j);
232//printf("%i %i: %08x -> ", i, j, attr);
233    uint16_t fg = caca_attr_to_rgb12_fg(attr);
234    uint16_t bg = caca_attr_to_rgb12_bg(attr);
235//printf("%04x %04x -> ", fg, bg);
236    caca_set_color_argb(m_caca, fg, bg);
237    attr = caca_get_attr(m_caca, -1, -1);
238//printf("%08x\n", attr);
239    caca_put_attr(m_caca, i, j, attr);
240}
241        m_vbo2->Unlock();
242
243        void *chars = m_vbo3->Lock(0, 0);
244        memcpy(chars, caca_get_canvas_chars(m_caca),
245               m_width * m_height * sizeof(uint32_t));
246for (int j = 0; j < m_height; j++)
247for (int i = 0; i < m_width; i++)
248{
249    uint32_t ch = caca_get_char(m_caca, i, j);
250//    printf("%i %i: %08x -> ", i, j, ch);
251}
252        m_vbo3->Unlock();
253
254        glDisable(GL_POINT_SMOOTH);
255        glPointSize(16.f);
256        m_shader->Bind();
257        tex_map->Bind();
258        m_shader->SetUniform(m_texture, 0);
259        m_vdecl->SetStream(m_vbo1, m_coord);
260        m_vdecl->SetStream(m_vbo2, m_color);
261        m_vdecl->SetStream(m_vbo3, m_char);
262        m_vdecl->Bind();
263        m_vdecl->DrawElements(MeshPrimitive::Points, 0, m_width * m_height);
264        m_vdecl->Unbind();
265        tex_map->Unbind();
266        m_shader->Unbind();
267    }
268
269private:
270    caca_canvas_t *m_caca;
271    int m_width, m_height;
272
273    Array<vec2> m_vertices;
274    Shader *m_shader;
275    ShaderAttrib m_coord, m_color, m_char;
276    ShaderUniform m_texture;
277    VertexDeclaration *m_vdecl;
278    VertexBuffer *m_vbo1, *m_vbo2, *m_vbo3;
279};
280
281TextRender *text_render;
282#endif
283
284void init_viewport(int type)
285{
286    glViewport(0, 0, screen_size.x, screen_size.y);
287    glMatrixMode(GL_PROJECTION);
288    glLoadIdentity();
289    mat4 m;
290    if (type == 0)
291        m = mat4::perspective(70, (float)screen_size.x, (float)screen_size.y,
292                              nearplane, farplane);
293    else
294        m = mat4::ortho(0, screen_size.x, screen_size.y, 0, -1.f, 1.f);
295    glLoadMatrixf(&m[0][0]);
296    glMatrixMode(GL_MODELVIEW);
297}
298
299#if SHADER
300void init_shader()
301{
302    glDisable(GL_BLEND);
303    glVertexPointer(4, GL_FLOAT, 0, fs_quad_vtx);
304    glTexCoordPointer(2, GL_FLOAT, 0, fs_quad_tex);
305}
306
307void fs_quad()
308{
309    glLoadIdentity();
310    glDrawArrays(GL_QUADS, 0, 4);
311}
312
313void draw_shader_simple(FrameBuffer *fbo_output, int n)
314{
315    shader_simple->Bind();
316    shader_simple->SetTexture(shader_simple_texture, fbo_output->GetTexture(), n);
317    fs_quad();
318    shader_simple->Unbind();
319}
320#endif
321
322void rectangle(int x, int y, int w, int h)
323{
324    glLoadIdentity();
325    glBegin(GL_QUADS);
326        glVertex2i(x+w, y  );
327        glVertex2i(x  , y  );
328        glVertex2i(x  , y+h);
329        glVertex2i(x+w, y+h);
330    glEnd();
331}
332
333void corner()
334{
335    float vertex[2+corner_n*2];
336    vertex[0] = 0;
337    vertex[1] = 0;
338    for (int i = 1; i < corner_n + 1; i++)
339    {
340        int j = i*2;
341        float a = PID*90.0f/(corner_n-1)*(i-1);
342        vertex[j  ] = corner_w-corner_w*cosf(a);
343        vertex[j+1] = corner_w-corner_w*sinf(a);
344    }
345    for (int i = 0; i < corner_n; i++)
346    {
347        int j = i*6;
348        int k = i*2;
349        corner_vtx[j  ] = (int)vertex[0];
350        corner_vtx[j+1] = (int)vertex[1];
351        corner_vtx[j+2] = (int)vertex[2+k];
352        corner_vtx[j+3] = (int)vertex[3+k];
353        corner_vtx[j+4] = (int)vertex[4+k];
354        corner_vtx[j+5] = (int)vertex[5+k];
355    }
356}
357
358void screen()
359{
360    part_angle = main_angle;
361    dos_flag = true;
362    dos_color = CR * vec3(48, 56, 64);
363    screen_color = dos_color;
364    /*-------------------------*/
365    shader_effect_flag = true;
366    shader_glow_flag = true;
367    glow_color = 1;
368    glow_smoothstep = 0;//.025f;
369    glow_mix_ratio1 = 0.5f;
370    glow_mix_ratio2 = 0.5f;
371    shader_radial_flag = false;
372    radial_value1 = 2.0f;
373    radial_value2 = 0.8f;
374    radial_color = 0;
375    postfx_deform = 0.5f;
376    /*-------------------------*/
377    flash_speed = 1.5f;
378    fade_speed = 0.2f;
379    sync_flag = false;
380    sync_value = 1.0f;
381    sync_speed = 1.0f;
382    beat_speed = 2.0f;
383    /*-------------------------*/
384    glClearColor(screen_color.r, screen_color.g, screen_color.b, 1.0f);
385}
386
387int InitGL(void)
388{
389    glClearDepth(1.0f);                                // set depth buffer
390    glDepthMask(GL_TRUE);                            // do not write z-buffer
391    glEnable(GL_CULL_FACE);                        // disable cull face
392    glCullFace(GL_BACK);                            // don't draw front face
393    glEnableClientState(GL_VERTEX_ARRAY);
394    glEnableClientState(GL_TEXTURE_COORD_ARRAY);
395    // load texture
396    tex_map = new TileSet("neercs/video/resource/map.png", ivec2(256, 256), ivec2(1));
397    // initialize some variable
398    screen();
399    /*-PATACODE----------------------------------------------------------------*/
400#if SHADER
401    if (shader_flag)
402    {
403        fbo_back = new FrameBuffer(screen_size);
404        fbo_front = new FrameBuffer(screen_size);
405        fbo_blur_h = new FrameBuffer(screen_size / glow_fbo_size);
406        fbo_blur_v = new FrameBuffer(screen_size / glow_fbo_size);
407        fbo_ping = new FrameBuffer(screen_size);
408        fbo_pong = new FrameBuffer(screen_size);
409        // shader simple
410        shader_simple = Shader::Create(lolfx_simple);
411        shader_simple_texture = shader_simple->GetUniformLocation("texture");
412        // shader blur horizontal
413        shader_blur_h = Shader::Create(lolfx_blurh);
414        shader_blur_h_texture = shader_blur_h->GetUniformLocation("texture");
415        shader_blur_h_screen_size = shader_blur_h->GetUniformLocation("screen_size");
416        shader_blur_h_time = shader_blur_h->GetUniformLocation("time");
417        shader_blur_h_value = shader_blur_h->GetUniformLocation("value");
418        // shader blur vertical
419        shader_blur_v = Shader::Create(lolfx_blurv);
420        shader_blur_v_texture = shader_blur_v->GetUniformLocation("texture");
421        shader_blur_v_screen_size = shader_blur_v->GetUniformLocation("screen_size");
422        shader_blur_v_time = shader_blur_v->GetUniformLocation("time");
423        shader_blur_v_value = shader_blur_v->GetUniformLocation("value");
424        // shader glow
425        shader_glow = Shader::Create(lolfx_glow);
426        shader_glow_texture = shader_glow->GetUniformLocation("texture");
427        shader_glow_texture_prv = shader_glow->GetUniformLocation("texture_prv");
428        shader_glow_screen_size = shader_glow->GetUniformLocation("screen_size");
429        shader_glow_time = shader_glow->GetUniformLocation("time");
430        shader_glow_step = shader_glow->GetUniformLocation("step");
431        shader_glow_value1 = shader_glow->GetUniformLocation("value1");
432        shader_glow_value2 = shader_glow->GetUniformLocation("value2");
433        // shader radial
434        shader_radial = Shader::Create(lolfx_radial);
435        shader_radial_texture = shader_radial->GetUniformLocation("texture");
436        shader_radial_screen_size = shader_radial->GetUniformLocation("screen_size");
437        shader_radial_time = shader_radial->GetUniformLocation("time");
438        shader_radial_value1 = shader_radial->GetUniformLocation("value1");
439        shader_radial_value2 = shader_radial->GetUniformLocation("value2");
440        shader_radial_color = shader_radial->GetUniformLocation("color");
441        // shader postfx
442        shader_postfx = Shader::Create(lolfx_postfx);
443        shader_postfx_texture = shader_postfx->GetUniformLocation("texture");
444        shader_postfx_texture_2d = shader_postfx->GetUniformLocation("texture_2d");
445        shader_postfx_screen_size = shader_postfx->GetUniformLocation("screen_size");
446        shader_postfx_time = shader_postfx->GetUniformLocation("time");
447        shader_postfx_flash = shader_postfx->GetUniformLocation("flash");
448        shader_postfx_value = shader_postfx->GetUniformLocation("value");
449        shader_postfx_deform = shader_postfx->GetUniformLocation("deform");
450        shader_postfx_scanline = shader_postfx->GetUniformLocation("scanline");
451        shader_postfx_sync = shader_postfx->GetUniformLocation("sync");
452    }
453#endif
454    /*-/PATACODE---------------------------------------------------------------*/
455    return true;
456}
457
458int CreateGLWindow(char const *title)
459{
460    screen_size = Video::GetSize();
461    //ratio_2d = (int)(screen_size.x/400); if(ratio_2d<2) ratio_2d = 2;
462    corner_w = 16*ratio_2d;
463    corner();
464    dos_w = 2*ratio_2d;
465    dos_h = 4*ratio_2d;
466    dos_m = 12*ratio_2d;
467    dos_vtx[0] = dos_w;
468    dos_vtx[1] = dos_h;
469    dos_vtx[2] = dos_w;
470    dos_vtx[3] = -dos_h;
471    dos_vtx[4] = -dos_w;
472    dos_vtx[5] = -dos_h;
473    dos_vtx[6] = -dos_w;
474    dos_vtx[7] = dos_h;
475    shell_vtx[0] = dos_m+29*ratio_2d;
476    shell_vtx[1] = dos_m+9*ratio_2d;
477    shell_vtx[2] = dos_m+29*ratio_2d;
478    shell_vtx[3] = dos_m+1*ratio_2d;
479    shell_vtx[4] = dos_m+1*ratio_2d;
480    shell_vtx[5] = dos_m+1*ratio_2d;
481    shell_vtx[6] = dos_m+1*ratio_2d;
482    shell_vtx[7] = dos_m+9*ratio_2d;
483    debug_w = 5*ratio_2d;
484    debug_h = 5*ratio_2d;
485    debug_x = 10*ratio_2d;
486    debug_y = 28*ratio_2d;
487    debug_vtx[0] = debug_w;
488    debug_vtx[1] = debug_h;
489    debug_vtx[2] = debug_w;
490    debug_vtx[3] = -debug_h;
491    debug_vtx[4] = -debug_w;
492    debug_vtx[5] = -debug_h;
493    debug_vtx[6] = -debug_w;
494    debug_vtx[7] = debug_h;
495    InitGL();
496    return true;
497}
498
499Render::Render(caca_canvas_t *caca)
500  : m_caca(caca)
501{
502#if FUCK
503    text_render = new TextRender(m_caca);
504#endif
505}
506
507void Render::TickGame(float seconds)
508{
509    Entity::TickGame(seconds);
510}
511
512void Render::TickDraw(float seconds)
513{
514    Entity::TickDraw(seconds);
515
516    static int todo = 1;
517    if (todo)
518    {
519        CreateGLWindow("LOL");
520        todo = 0;
521#if FUCK
522        text_render->Init();
523#endif
524    }
525
526    // timer
527    if(!paused)
528        main_angle += seconds * 100.0f * PID;
529    if(sync_flag)
530    {
531        angle=(main_angle-sync_angle)*sync_speed;
532        sync_value=1.0f-sinf(angle);
533        if(angle>90.0f*PID)
534        {
535            sync_value=0;
536            sync_flag=false;
537        }
538    }
539    if(beat_flag)
540    {
541        angle=(main_angle-beat_angle)*beat_speed;
542        beat_value=1.0f-sinf(angle);
543        if(angle>90.0f*PID)
544        {
545            beat_value=0;
546            beat_flag=false;
547        }
548    }
549    if(flash_flag)
550    {
551        angle=(main_angle-flash_angle)*flash_speed;
552        flash_value=1.0f-sinf(angle);
553        if(angle>90.0f*PID)
554        {
555            flash_value=0;
556            flash_flag=false;
557        }
558    }
559    if(fade_flag)
560    {
561        angle=(main_angle-fade_angle)*fade_speed;
562        fade_value=1.0f-sinf(angle);
563        if(angle>90.0f*PID)
564        {
565            fade_value=0;
566            fade_flag=false;
567        }
568    }
569
570    Draw2D();
571    Draw3D();
572#if FUCK
573    text_render->Blit();
574#endif
575}
576
577void Render::Draw2D()
578{
579    /*-PATACODE----------------------------------------------------------------*/
580#if SHADER
581    if(shader_flag)
582    {
583        fbo_back->Bind();
584        fbo_back->Clear(vec4(screen_color, 0.0f), 1.0f);
585    }
586    else
587    {
588        glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
589    }
590#else
591    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
592#endif
593
594    /*-/PATACODE---------------------------------------------------------------*/
595    glEnable(GL_BLEND);
596    //if(polygon) glEnable(GL_LINE_SMOOTH); else glDisable(GL_LINE_SMOOTH);
597    glLineWidth((polygon)?2.0f:1.0f);
598    fx_angle=main_angle-part_angle;
599    if(polygon) glEnable(GL_TEXTURE_2D);
600
601    init_viewport(1);
602
603    tex_map->Bind();
604    glEnable(GL_BLEND);
605    // draw dos
606    if(dos_flag)
607    {
608        glDisable(GL_TEXTURE_2D);
609        glDisable(GL_BLEND);
610        glColor3f(1.0f, 1.0f, 1.0f);
611        rectangle(dos_m, dos_m, screen_size.x-(int)(26.5f*ratio_2d)-dos_m*2, 10*ratio_2d);
612        rectangle(screen_size.x-(int)(25.5f*ratio_2d)-dos_m, dos_m, 11*ratio_2d, 10*ratio_2d);
613        rectangle(screen_size.x-(int)(13.5f*ratio_2d)-dos_m, dos_m, 11*ratio_2d, 10*ratio_2d);
614        rectangle(screen_size.x-(int)(1.5f*ratio_2d)-dos_m, dos_m, (int)(1.5f*ratio_2d), 10*ratio_2d);
615        rectangle(dos_m, dos_m+10*ratio_2d, 1*ratio_2d, screen_size.y-10*ratio_2d-dos_m*2);
616        rectangle(screen_size.x-1*ratio_2d-dos_m, dos_m+10*ratio_2d, 1*ratio_2d, screen_size.y-10*ratio_2d-dos_m*2);
617        rectangle(dos_m+1*ratio_2d, screen_size.y-1*ratio_2d-dos_m, screen_size.x-2*ratio_2d-dos_m*2, 1*ratio_2d);
618        glColor3f(dos_color.r, dos_color.g, dos_color.b);
619        rectangle(dos_m+1*ratio_2d, dos_m+1*ratio_2d, 28*ratio_2d, 8*ratio_2d);
620        rectangle(dos_m+(int)(30.5f*ratio_2d), dos_m+2*ratio_2d, screen_size.x-(int)(58.5f*ratio_2d)-dos_m*2, 2*ratio_2d);
621        rectangle(dos_m+(int)(30.5f*ratio_2d), dos_m+6*ratio_2d, screen_size.x-(int)(58.5f*ratio_2d)-dos_m*2, 2*ratio_2d);
622        rectangle(screen_size.x-(int)(24.5f*ratio_2d)-dos_m, dos_m+1*ratio_2d, 7*ratio_2d, 6*ratio_2d);
623        glColor3f(1.0f, 1.0f, 1.0f);
624        rectangle(screen_size.x-(int)(23.5f*ratio_2d)-dos_m, dos_m+2*ratio_2d, 5*ratio_2d, 4*ratio_2d);
625        glColor3f(0, 0, 0);
626        rectangle(screen_size.x-(int)(22.5f*ratio_2d)-dos_m, dos_m+3*ratio_2d, 7*ratio_2d, 6*ratio_2d);
627        rectangle(screen_size.x-(int)(12.5f*ratio_2d)-dos_m, dos_m+1*ratio_2d, 7*ratio_2d, 6*ratio_2d);
628        glColor3f(dos_color.r, dos_color.g, dos_color.b);
629        rectangle(screen_size.x-(int)(10.5f*ratio_2d)-dos_m, dos_m+2*ratio_2d, 7*ratio_2d, 7*ratio_2d);
630        glColor3f(1.0f, 1.0f, 1.0f);
631        rectangle(screen_size.x-(int)(9.5f*ratio_2d)-dos_m, dos_m+3*ratio_2d, 5*ratio_2d, 5*ratio_2d);
632        rectangle(screen_size.x-8*ratio_2d-dos_m, screen_size.y-9*ratio_2d-dos_m, 7*ratio_2d, 8*ratio_2d);
633        glColor3f(dos_color.r, dos_color.g, dos_color.b);
634        rectangle(screen_size.x-7*ratio_2d-dos_m, screen_size.y-8*ratio_2d-dos_m, 6*ratio_2d, 7*ratio_2d);
635        glColor3f(1.0f, 1.0f, 1.0f);
636        rectangle(screen_size.x-4*ratio_2d-dos_m, screen_size.y-8*ratio_2d-dos_m, 4*ratio_2d, 2*ratio_2d);
637        rectangle(screen_size.x-7*ratio_2d-dos_m, screen_size.y-5*ratio_2d-dos_m, 2*ratio_2d, 5*ratio_2d);
638        rectangle(screen_size.x-6*ratio_2d-dos_m, screen_size.y-7*ratio_2d-dos_m, 1*ratio_2d, 1*ratio_2d);
639        rectangle(screen_size.x-4*ratio_2d-dos_m, screen_size.y-5*ratio_2d-dos_m, 2*ratio_2d, 3*ratio_2d);
640        glEnable(GL_BLEND);
641        if(polygon) glEnable(GL_TEXTURE_2D);
642        glBlendFunc(GL_ONE, GL_ONE);
643        glVertexPointer(2, GL_INT, 0, shell_vtx);
644        glTexCoordPointer(2, GL_FLOAT, 0, shell_tex);
645        glDrawArrays(GL_QUADS, 0, 4);
646        // draw dos text
647        glVertexPointer(2, GL_INT, 0, dos_vtx);
648        float x = dos_m + (float)(dos_w + 2 * ratio_2d);
649        float y = dos_m + (float)(15 * ratio_2d);
650
651        /* Background colors */
652        glDisable(GL_TEXTURE_2D);
653        for (int j = 0; j < caca_get_canvas_height(m_caca); j++)
654        for (int i = 0; i < caca_get_canvas_width(m_caca); i++)
655        {
656            uint32_t att = caca_get_attr(m_caca, i, j);
657
658            glLoadIdentity();
659            glTranslated(x+i*(dos_w+2*ratio_2d), y+j*(dos_h+4*ratio_2d), 0);
660
661            uint16_t bg = caca_attr_to_rgb12_bg(att);
662            glColor3f((float)(bg >> 8) / 16.f,
663                      (float)((bg & 0xf0) >> 4) / 16.f,
664                      (float)(bg & 0xf) / 16.f);
665            glDrawArrays(GL_QUADS, 0, 4);
666        }
667        glEnable(GL_TEXTURE_2D);
668
669        /* Foreground characters */
670        for (int j = 0; j < caca_get_canvas_height(m_caca); j++)
671        for (int i = 0; i < caca_get_canvas_width(m_caca); i++)
672        {
673            uint32_t car = caca_get_char(m_caca, i, j);
674            uint32_t att = caca_get_attr(m_caca, i, j);
675
676            if (car <= 32 || car >= 127)
677                continue;
678
679            glLoadIdentity();
680            glTranslated(x+i*(dos_w+2*ratio_2d), y+j*(dos_h+4*ratio_2d), 0);
681
682            float l_w=(car%16)*0.03125f;
683            float l_h=(car-car%16)*0.001953125f;
684            float dos_tex[]={l_w+0.03125f, 0.5f+l_h+0.03125f, l_w+0.03125f, 0.5f+l_h, l_w, 0.5f+l_h, l_w, 0.5f+l_h+0.03125f};
685            glTexCoordPointer(2, GL_FLOAT, 0, dos_tex);
686            uint16_t fg = caca_attr_to_rgb12_fg(att);
687            glColor3f((float)(fg >> 8) / 16.f,
688                      (float)((fg & 0xf0) >> 4) / 16.f,
689                      (float)(fg & 0xf) / 16.f);
690            // FIXME: draw cursor!
691            //if(car==car_cursor) glColor3f(CR*(dos_color.r+64), CR*(dos_color.g+48), CR*(dos_color.b+48));
692            glDrawArrays(GL_QUADS, 0, 4);
693            //if(car==car_cursor) glColor3f(1.0f, 1.0f, 1.0f);
694        }
695    }
696    // draw corner
697    glDisable(GL_TEXTURE_2D);
698    glDisable(GL_BLEND);
699    glVertexPointer(2, GL_INT, 0, corner_vtx);
700    glLoadIdentity();
701    glColor3f(0, 0, 0);
702    glDrawArrays(GL_TRIANGLES, 0, corner_n*3);
703    glTranslated(screen_size.x, 0, 0);
704    glRotated(90.0f, 0, 0, 1.0f);
705    glDrawArrays(GL_TRIANGLES, 0, corner_n*3);
706    glTranslated(screen_size.y, 0, 0);
707    glRotated(90.0f, 0, 0, 1.0f);
708    glDrawArrays(GL_TRIANGLES, 0, corner_n*3);
709    glTranslated(screen_size.x, 0, 0);
710    glRotated(90.0f, 0, 0, 1.0f);
711    glDrawArrays(GL_TRIANGLES, 0, corner_n*3);
712    glEnable(GL_BLEND);
713    tex_map->Unbind();
714}
715
716void Render::Draw3D()
717{
718    //init_viewport(0);
719    /*-PATACODE----------------------------------------------------------------*/
720    glDisable(GL_DEPTH_TEST);
721
722#if SHADER
723    if (!shader_flag)
724        return;
725
726    init_shader();
727    fbo_back->Unbind();
728    if (shader_effect_flag && shader_blur_flag)
729    {
730        // shader blur horizontal
731        fbo_ping->Bind();
732        shader_blur_h->Bind();
733        shader_blur_h->SetTexture(shader_blur_h_texture, fbo_back->GetTexture(), 0);
734        shader_blur_h->SetUniform(shader_blur_h_screen_size, vec2(1.0f));
735        shader_blur_h->SetUniform(shader_blur_h_time, fx_angle);
736        shader_blur_h->SetUniform(shader_blur_h_value, 0.5f/screen_size.x);
737        fs_quad();
738        shader_blur_h->Unbind();
739        fbo_ping->Unbind();
740        // shader blur vertical
741        fbo_front->Bind();
742        shader_blur_v->Bind();
743        shader_blur_v->SetTexture(shader_blur_v_texture, fbo_ping->GetTexture(), 0);
744        shader_blur_v->SetUniform(shader_blur_v_screen_size, vec2(1.0f));
745        shader_blur_v->SetUniform(shader_blur_v_time, fx_angle);
746        shader_blur_v->SetUniform(shader_blur_v_value, 0.5f/screen_size.y);
747        fs_quad();
748        shader_blur_v->Unbind();
749    }
750    else
751    {
752        // shader simple
753        fbo_front->Bind();
754        draw_shader_simple(fbo_back, 0);
755    }
756    fbo_front->Unbind();
757    // shader glow
758    if(shader_effect_flag&&shader_glow_flag)
759    {
760        // shader blur horizontal
761        fbo_blur_h->Bind();
762        shader_blur_h->Bind();
763        shader_blur_h->SetTexture(shader_blur_h_texture, fbo_ping->GetTexture(), 0);
764        shader_blur_h->SetUniform(shader_blur_h_screen_size, vec2(1.0f / glow_fbo_size));
765        shader_blur_h->SetUniform(shader_blur_h_time, fx_angle);
766        shader_blur_h->SetUniform(shader_blur_h_value, 2.5f/screen_size.x);
767        fs_quad();
768        shader_blur_h->Unbind();
769        fbo_blur_h->Unbind();
770        // shader blur vertical
771        fbo_blur_v->Bind();
772        shader_blur_v->Bind();
773        shader_blur_v->SetTexture(shader_blur_v_texture, fbo_blur_h->GetTexture(), 0);
774        shader_blur_v->SetUniform(shader_blur_v_screen_size, vec2(1.0f / glow_fbo_size));
775        shader_blur_v->SetUniform(shader_blur_v_time, fx_angle);
776        shader_blur_v->SetUniform(shader_blur_h_value, 2.5f/screen_size.y);
777        fs_quad();
778        shader_blur_v->Unbind();
779        fbo_blur_v->Unbind();
780        // shader blur horizontal
781        fbo_blur_h->Bind();
782        shader_blur_h->Bind();
783        shader_blur_h->SetTexture(shader_blur_h_texture, fbo_blur_v->GetTexture(), 0);
784        shader_blur_h->SetUniform(shader_blur_h_screen_size, vec2(1.0f / glow_fbo_size));
785        shader_blur_h->SetUniform(shader_blur_h_time, fx_angle);
786        shader_blur_h->SetUniform(shader_blur_h_value, 1.0f/screen_size.x);
787        fs_quad();
788        shader_blur_h->Unbind();
789        fbo_blur_h->Unbind();
790        // shader blur vertical
791        fbo_blur_v->Bind();
792        shader_blur_v->Bind();
793        shader_blur_v->SetTexture(shader_blur_v_texture, fbo_blur_h->GetTexture(), 0);
794        shader_blur_v->SetUniform(shader_blur_v_screen_size, vec2(1.0f / glow_fbo_size));
795        shader_blur_v->SetUniform(shader_blur_v_time, fx_angle);
796        shader_blur_v->SetUniform(shader_blur_h_value, 1.0f/screen_size.y);
797        fs_quad();
798        shader_blur_v->Unbind();
799        fbo_blur_v->Unbind();
800        // shader glow
801        fbo_pong->Bind();
802        shader_glow->Bind();
803        shader_glow->SetTexture(shader_glow_texture, fbo_blur_v->GetTexture(), 0);
804        shader_glow->SetTexture(shader_glow_texture_prv, fbo_front->GetTexture(), 1);
805        shader_glow->SetUniform(shader_glow_screen_size, vec2(1.0f));
806        shader_glow->SetUniform(shader_glow_time, fx_angle);
807        shader_glow->SetUniform(shader_glow_step, glow_smoothstep);
808        shader_glow->SetUniform(shader_glow_value1, glow_mix_ratio1);
809        shader_glow->SetUniform(shader_glow_value2, glow_mix_ratio2);
810        fs_quad();
811        shader_glow->Unbind();
812    }
813    if(!shader_effect_flag)
814    {
815        // shader simple
816        fbo_pong->Bind();
817        draw_shader_simple(fbo_front, 0);
818    }
819    fbo_pong->Unbind();
820    if(shader_postfx_flag)
821    {
822        // shader postfx
823        shader_postfx->Bind();
824        shader_postfx->SetTexture(shader_postfx_texture, fbo_pong->GetTexture(), 0);
825        shader_postfx->SetUniform(shader_postfx_screen_size, (vec2)screen_size);
826        shader_postfx->SetUniform(shader_postfx_time, fx_angle);
827        shader_postfx->SetUniform(shader_postfx_flash, flash_value);
828        shader_postfx->SetUniform(shader_postfx_value, 4.0f);
829        shader_postfx->SetUniform(shader_postfx_deform, postfx_deform);
830        shader_postfx->SetUniform(shader_postfx_scanline, postfx_scanline);
831        shader_postfx->SetUniform(shader_postfx_sync, (float)fabs(beat_value*cosf((main_angle-beat_angle)*8.0f)));
832        fs_quad();
833        shader_postfx->Unbind();
834    }
835    else
836    {
837        // shader simple
838        draw_shader_simple(fbo_pong, 0);
839    }
840#endif
841    /*-/PATACODE---------------------------------------------------------------*/
842}
843
844Render::~Render()
845{
846}
847
Note: See TracBrowser for help on using the repository browser.