source: trunk/tools/neercs/video/text-render.cpp @ 1623

Last change on this file since 1623 was 1561, checked in by sam, 11 years ago

data: fix a lot of paths to assets.

  • Property svn:keywords set to Id
File size: 4.6 KB
Line 
1//
2// Neercs
3//
4
5#if defined HAVE_CONFIG_H
6#   include "config.h"
7#endif
8
9#include <cmath>
10#include <cstdio>
11#include <cstdlib>
12#include <ctime>
13#include <string>
14
15#include "core.h"
16#include "lolgl.h"
17
18using namespace std;
19using namespace lol;
20
21#include "../neercs.h"
22#include "render.h"
23#include "text-render.h"
24
25extern char const *lolfx_text;
26
27/*
28 * Text rendering interface
29 */
30
31TextRender::TextRender(caca_canvas_t *caca, ivec2 font_size)
32  : m_caca(caca),
33    m_font_size(font_size),
34    m_canvas_size(caca_get_canvas_width(m_caca),
35                  caca_get_canvas_height(m_caca)),
36    m_fbo_size(m_font_size * m_canvas_size),
37    m_cells(m_canvas_size.x * m_canvas_size.y)
38{
39    for (int j = 0; j < m_canvas_size.y; j++)
40    for (int i = 0; i < m_canvas_size.x; i++)
41        m_vertices << vec2(i, j);
42}
43
44void TextRender::Init()
45{
46    m_font = new TileSet("tools/neercs/video/resource/map.png",
47                         ivec2(256, 256), ivec2(1));
48
49    m_shader = Shader::Create(lolfx_text);
50    m_coord = m_shader->GetAttribLocation("in_Position",
51                                          VertexUsage::Position, 0);
52    m_color = m_shader->GetAttribLocation("in_Attr",
53                                          VertexUsage::Color, 0);
54    m_char = m_shader->GetAttribLocation("in_Char",
55                                         VertexUsage::Color, 1);
56    m_texture = m_shader->GetUniformLocation("in_Texture");
57    m_transform = m_shader->GetUniformLocation("in_Transform");
58    m_vdecl
59      = new VertexDeclaration(VertexStream<vec2>(VertexUsage::Position),
60                              VertexStream<uint32_t>(VertexUsage::Color),
61                              VertexStream<uint32_t>(VertexUsage::Color));
62
63    m_vbo1 = new VertexBuffer(m_vertices.Bytes());
64    void *vertices = m_vbo1->Lock(0, 0);
65    memcpy(vertices, &m_vertices[0], m_vertices.Bytes());
66    m_vbo1->Unlock();
67
68    m_vbo2 = new VertexBuffer(m_cells * sizeof(int32_t));
69    m_vbo3 = new VertexBuffer(m_cells * sizeof(int32_t));
70
71    m_fbo = new FrameBuffer(m_fbo_size);
72}
73
74void TextRender::Render()
75{
76    /* Transform matrix for the scene:
77     *  - translate to the centre of the glyph
78     *  - scale by 2.f * font_size / fbo_size
79     *  - translate to the lower left corner */
80    mat4 xform = mat4::translate(-1.f, -1.f + 2.0f * m_font_size.y
81                                        * m_canvas_size.y / m_fbo_size.y, 0.f)
82               * mat4::scale(vec3(2.f * m_font_size / m_fbo_size, 1.f)
83                              * vec3(1.f, -1.f, 1.f))
84               * mat4::translate(0.5f, 0.5f, 0.f);
85
86    /* Upload libcaca canvas contents to the vertex buffers */
87    uint32_t *colors = (uint32_t *)m_vbo2->Lock(0, 0);
88    for (int j = 0; j < m_canvas_size.y; j++)
89    for (int i = 0; i < m_canvas_size.x; i++)
90    {
91        uint32_t attr = caca_get_attr(m_caca, i, j);
92        uint16_t fg = caca_attr_to_rgb12_fg(attr);
93        uint16_t bg = caca_attr_to_rgb12_bg(attr);
94        caca_set_color_argb(m_caca, fg, bg);
95        attr = caca_get_attr(m_caca, -1, -1);
96        caca_put_attr(m_caca, i, j, attr);
97    }
98    memcpy(colors, caca_get_canvas_attrs(m_caca),
99           m_cells * sizeof(uint32_t));
100    m_vbo2->Unlock();
101
102    uint32_t *chars = (uint32_t *)m_vbo3->Lock(0, 0);
103    memcpy(chars, caca_get_canvas_chars(m_caca),
104           m_cells * sizeof(uint32_t));
105    m_vbo3->Unlock();
106
107    m_fbo->Bind();
108    glViewport(0, 0, m_fbo_size.x, m_fbo_size.y);
109    glDisable(GL_DEPTH_TEST);
110    glEnable(GL_POINT_SPRITE);
111    //glEnable(GL_VERTEX_PROGRAM_POINT_SIZE);
112    glDisable(GL_POINT_SMOOTH);
113    glPointSize((float)max(m_font_size.x, m_font_size.y));
114    m_shader->Bind();
115    m_font->Bind();
116    m_shader->SetUniform(m_texture, 0);
117    m_shader->SetUniform(m_transform, xform);
118    m_vdecl->SetStream(m_vbo1, m_coord);
119    m_vdecl->SetStream(m_vbo2, m_color);
120    m_vdecl->SetStream(m_vbo3, m_char);
121    m_vdecl->Bind();
122    m_vdecl->DrawElements(MeshPrimitive::Points, 0, m_cells);
123    m_vdecl->Unbind();
124    m_font->Unbind();
125    m_shader->Unbind();
126    glDisable(GL_POINT_SPRITE);
127    m_fbo->Unbind();
128}
129
130void TextRender::Blit(ivec2 pos, ivec2 size)
131{
132    glDisable(GL_BLEND);
133    glEnable(GL_TEXTURE_2D);
134    glBindTexture(GL_TEXTURE_2D, m_fbo->GetTexture());
135    glColor3f(1.0f, 1.0f, 1.0f);
136
137    vec2 tc = (vec2)m_canvas_size * m_font_size / m_fbo_size;
138
139    glLoadIdentity();
140    glBegin(GL_QUADS);
141        glTexCoord2f(tc.x, tc.y);
142        glVertex2i(pos.x + size.x, pos.y);
143        glTexCoord2f(0.0f, tc.y);
144        glVertex2i(pos.x, pos.y);
145        glTexCoord2f(0.0f, 0.0f);
146        glVertex2i(pos.x, pos.y + size.y);
147        glTexCoord2f(tc.x, 0.0f);
148        glVertex2i(pos.x + size.x, pos.y + size.y);
149    glEnd();
150}
151
Note: See TracBrowser for help on using the repository browser.