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

Last change on this file since 2091 was 2091, checked in by sam, 8 years ago

neercs: use gl_VertexID instead of sending a whole new vertex array.

  • Property svn:keywords set to Id
File size: 5.1 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}
40
41void TextRender::Init()
42{
43    m_font = new TileSet("tools/neercs/video/resource/charset_amiga.png",
44                         ivec2(256, 256), ivec2(1));
45
46    m_shader = Shader::Create(lolfx_text);
47    m_color = m_shader->GetAttribLocation("in_Attr",
48                                          VertexUsage::Color, 0);
49    m_char = m_shader->GetAttribLocation("in_Char",
50                                         VertexUsage::Color, 1);
51    m_texture = m_shader->GetUniformLocation("u_Texture");
52    m_transform = m_shader->GetUniformLocation("u_Transform");
53    m_datasize = m_shader->GetUniformLocation("u_DataSize");
54    m_vdecl
55      = new VertexDeclaration(VertexStream<uint32_t>(VertexUsage::Color),
56                              VertexStream<uint32_t>(VertexUsage::Color));
57
58    CreateBuffers();
59}
60
61void TextRender::CreateBuffers()
62{
63    m_vbo2 = new VertexBuffer(m_cells * sizeof(int32_t));
64    m_vbo3 = new VertexBuffer(m_cells * sizeof(int32_t));
65
66    m_fbo = new FrameBuffer(m_fbo_size);
67}
68
69void TextRender::Render()
70{
71    /* Handle canvas size changes */
72    ivec2 current_size(caca_get_canvas_width(m_caca),
73                       caca_get_canvas_height(m_caca));
74    if (current_size != m_canvas_size)
75    {
76        delete m_vbo2;
77        delete m_vbo3;
78        delete m_fbo;
79
80        m_canvas_size = current_size;
81        m_fbo_size = m_font_size * m_canvas_size;
82        m_cells = m_canvas_size.x * m_canvas_size.y;
83
84        CreateBuffers();
85    }
86
87    /* Transform matrix for the scene:
88     *  - translate to the centre of the glyph
89     *  - scale by 2.f * font_size / fbo_size
90     *  - translate to the lower left corner */
91    mat4 xform = mat4::translate(-1.f, -1.f + 2.0f * m_font_size.y
92                                        * m_canvas_size.y / m_fbo_size.y, 0.f)
93               * mat4::scale(vec3(2.f * m_font_size / m_fbo_size, 1.f)
94                              * vec3(1.f, -1.f, 1.f))
95               * mat4::translate(0.5f, 0.5f, 0.f);
96
97    /* Upload libcaca canvas contents to the vertex buffers */
98    uint32_t *colors = (uint32_t *)m_vbo2->Lock(0, 0);
99    for (int j = 0; j < m_canvas_size.y; j++)
100    for (int i = 0; i < m_canvas_size.x; i++)
101    {
102        uint32_t attr = caca_get_attr(m_caca, i, j);
103        uint16_t fg = caca_attr_to_rgb12_fg(attr);
104        uint16_t bg = caca_attr_to_rgb12_bg(attr);
105        caca_set_color_argb(m_caca, fg, bg);
106        attr = caca_get_attr(m_caca, -1, -1);
107        caca_put_attr(m_caca, i, j, attr);
108    }
109    memcpy(colors, caca_get_canvas_attrs(m_caca),
110           m_cells * sizeof(uint32_t));
111    m_vbo2->Unlock();
112
113    uint32_t *chars = (uint32_t *)m_vbo3->Lock(0, 0);
114    memcpy(chars, caca_get_canvas_chars(m_caca),
115           m_cells * sizeof(uint32_t));
116    m_vbo3->Unlock();
117
118    m_fbo->Bind();
119    glViewport(0, 0, m_fbo_size.x, m_fbo_size.y);
120    glDisable(GL_DEPTH_TEST);
121#if !defined HAVE_GLES_2X
122    glEnable(GL_POINT_SPRITE);
123    //glEnable(GL_VERTEX_PROGRAM_POINT_SIZE);
124    glDisable(GL_POINT_SMOOTH);
125#endif
126    glEnable(GL_VERTEX_PROGRAM_POINT_SIZE);
127    glTexEnvi(GL_POINT_SPRITE, GL_COORD_REPLACE, GL_TRUE);
128    m_shader->Bind();
129    m_font->Bind();
130    m_shader->SetUniform(m_texture, 0);
131    m_shader->SetUniform(m_transform, xform);
132    m_shader->SetUniform(m_datasize, vec2(m_canvas_size.x,
133                                          max(m_font_size.x, m_font_size.y)));
134    m_vdecl->SetStream(m_vbo2, m_color);
135    m_vdecl->SetStream(m_vbo3, m_char);
136    m_vdecl->Bind();
137    m_vdecl->DrawElements(MeshPrimitive::Points, 0, m_cells);
138    m_vdecl->Unbind();
139    m_font->Unbind();
140    m_shader->Unbind();
141#if !defined HAVE_GLES_2X
142    glDisable(GL_POINT_SPRITE);
143#endif
144    m_fbo->Unbind();
145}
146
147void TextRender::Blit(ivec2 pos, ivec2 size)
148{
149    /* FIXME: this is ugly! But we will get rid of it when we
150     * do the Direct3D port, so don't worry too much. */
151    ShaderTexture t = m_fbo->GetTexture();
152    uint64_t const &x = *(uint64_t const *)&t;
153
154    glDisable(GL_BLEND);
155    glEnable(GL_TEXTURE_2D);
156    glBindTexture(GL_TEXTURE_2D, (int)x);
157    glColor3f(1.0f, 1.0f, 1.0f);
158
159    vec2 tc = (vec2)m_canvas_size * m_font_size / m_fbo_size;
160
161    glLoadIdentity();
162    glBegin(GL_QUADS);
163        glTexCoord2f(tc.x, tc.y);
164        glVertex2i(pos.x + size.x, pos.y);
165        glTexCoord2f(0.0f, tc.y);
166        glVertex2i(pos.x, pos.y);
167        glTexCoord2f(0.0f, 0.0f);
168        glVertex2i(pos.x, pos.y + size.y);
169        glTexCoord2f(tc.x, 0.0f);
170        glVertex2i(pos.x + size.x, pos.y + size.y);
171    glEnd();
172}
173
Note: See TracBrowser for help on using the repository browser.