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 | |
---|
18 | using namespace std; |
---|
19 | using namespace lol; |
---|
20 | |
---|
21 | #include "../neercs.h" |
---|
22 | #include "render.h" |
---|
23 | #include "text-render.h" |
---|
24 | |
---|
25 | extern char const *lolfx_text; |
---|
26 | |
---|
27 | #define HAVE_SHADER_4 1 |
---|
28 | |
---|
29 | /* |
---|
30 | * Text rendering interface |
---|
31 | */ |
---|
32 | |
---|
33 | TextRender::TextRender(caca_canvas_t *caca, ivec2 font_size) |
---|
34 | : m_caca(caca), |
---|
35 | m_font_size(font_size), |
---|
36 | m_canvas_size(caca_get_canvas_width(m_caca), |
---|
37 | caca_get_canvas_height(m_caca)), |
---|
38 | m_fbo_size(m_font_size * m_canvas_size), |
---|
39 | m_cells(m_canvas_size.x * m_canvas_size.y) |
---|
40 | { |
---|
41 | } |
---|
42 | |
---|
43 | void TextRender::Init() |
---|
44 | { |
---|
45 | m_font = new TileSet("tools/neercs/video/resource/charset_amiga.png", |
---|
46 | ivec2(256, 256), ivec2(1)); |
---|
47 | |
---|
48 | m_shader = Shader::Create(lolfx_text); |
---|
49 | m_color = m_shader->GetAttribLocation("in_Attr", |
---|
50 | VertexUsage::Color, 0); |
---|
51 | m_char = m_shader->GetAttribLocation("in_Char", |
---|
52 | VertexUsage::Color, 1); |
---|
53 | #if !HAVE_SHADER_4 |
---|
54 | m_vertexid = m_shader->GetAttribLocation("in_VertexID", |
---|
55 | VertexUsage::Position, 0); |
---|
56 | #endif |
---|
57 | m_texture = m_shader->GetUniformLocation("u_Texture"); |
---|
58 | m_transform = m_shader->GetUniformLocation("u_Transform"); |
---|
59 | m_datasize = m_shader->GetUniformLocation("u_DataSize"); |
---|
60 | m_vdecl |
---|
61 | #if HAVE_SHADER_4 |
---|
62 | = new VertexDeclaration(VertexStream<uint32_t>(VertexUsage::Color), |
---|
63 | VertexStream<uint32_t>(VertexUsage::Color)); |
---|
64 | #else |
---|
65 | = new VertexDeclaration(VertexStream<u8vec4>(VertexUsage::Color), |
---|
66 | VertexStream<u8vec4>(VertexUsage::Color), |
---|
67 | VertexStream<float>(VertexUsage::Position)); |
---|
68 | #endif |
---|
69 | |
---|
70 | CreateBuffers(); |
---|
71 | } |
---|
72 | |
---|
73 | void TextRender::CreateBuffers() |
---|
74 | { |
---|
75 | #if HAVE_SHADER_4 |
---|
76 | m_vbo1 = new VertexBuffer(m_cells * sizeof(uint32_t)); |
---|
77 | m_vbo2 = new VertexBuffer(m_cells * sizeof(uint32_t)); |
---|
78 | #else |
---|
79 | m_vbo1 = new VertexBuffer(m_cells * sizeof(u8vec4)); |
---|
80 | m_vbo2 = new VertexBuffer(m_cells * sizeof(u8vec4)); |
---|
81 | m_vbo3 = new VertexBuffer(4 * m_cells * sizeof(float)); |
---|
82 | |
---|
83 | float *idx = (float *)m_vbo3->Lock(0, 0); |
---|
84 | for (int i = 0; i < m_cells; i++) |
---|
85 | idx[i] = (float)i; |
---|
86 | m_vbo3->Unlock(); |
---|
87 | #endif |
---|
88 | |
---|
89 | m_fbo = new FrameBuffer(m_fbo_size); |
---|
90 | } |
---|
91 | |
---|
92 | void TextRender::Render() |
---|
93 | { |
---|
94 | /* Handle canvas size changes */ |
---|
95 | ivec2 current_size(caca_get_canvas_width(m_caca), |
---|
96 | caca_get_canvas_height(m_caca)); |
---|
97 | if (current_size != m_canvas_size) |
---|
98 | { |
---|
99 | delete m_vbo1; |
---|
100 | delete m_vbo2; |
---|
101 | delete m_vbo3; |
---|
102 | delete m_fbo; |
---|
103 | |
---|
104 | m_canvas_size = current_size; |
---|
105 | m_fbo_size = m_font_size * m_canvas_size; |
---|
106 | m_cells = m_canvas_size.x * m_canvas_size.y; |
---|
107 | |
---|
108 | CreateBuffers(); |
---|
109 | } |
---|
110 | |
---|
111 | /* Transform matrix for the scene: |
---|
112 | * - translate to the centre of the glyph |
---|
113 | * - scale by 2.f * font_size / fbo_size |
---|
114 | * - translate to the lower left corner */ |
---|
115 | mat4 xform = mat4::translate(-1.f, -1.f + 2.0f * m_font_size.y |
---|
116 | * m_canvas_size.y / m_fbo_size.y, 0.f) |
---|
117 | * mat4::scale(vec3(2.f * m_font_size / m_fbo_size, 1.f) |
---|
118 | * vec3(1.f, -1.f, 1.f)) |
---|
119 | * mat4::translate(0.5f, 0.5f, 0.f); |
---|
120 | |
---|
121 | /* Upload libcaca canvas contents to the vertex buffers */ |
---|
122 | uint32_t *colors = (uint32_t *)m_vbo1->Lock(0, 0); |
---|
123 | for (int j = 0; j < m_canvas_size.y; j++) |
---|
124 | for (int i = 0; i < m_canvas_size.x; i++) |
---|
125 | { |
---|
126 | uint32_t attr = caca_get_attr(m_caca, i, j); |
---|
127 | uint16_t fg = caca_attr_to_rgb12_fg(attr); |
---|
128 | uint16_t bg = caca_attr_to_rgb12_bg(attr); |
---|
129 | caca_set_color_argb(m_caca, fg, bg); |
---|
130 | attr = caca_get_attr(m_caca, -1, -1); |
---|
131 | caca_put_attr(m_caca, i, j, attr); |
---|
132 | } |
---|
133 | memcpy(colors, caca_get_canvas_attrs(m_caca), |
---|
134 | m_cells * sizeof(uint32_t)); |
---|
135 | m_vbo1->Unlock(); |
---|
136 | |
---|
137 | uint32_t *chars = (uint32_t *)m_vbo2->Lock(0, 0); |
---|
138 | memcpy(chars, caca_get_canvas_chars(m_caca), |
---|
139 | m_cells * sizeof(uint32_t)); |
---|
140 | m_vbo2->Unlock(); |
---|
141 | |
---|
142 | m_fbo->Bind(); |
---|
143 | glViewport(0, 0, m_fbo_size.x, m_fbo_size.y); |
---|
144 | glDisable(GL_DEPTH_TEST); |
---|
145 | #if !defined HAVE_GLES_2X |
---|
146 | glEnable(GL_POINT_SPRITE); |
---|
147 | //glEnable(GL_VERTEX_PROGRAM_POINT_SIZE); |
---|
148 | glDisable(GL_POINT_SMOOTH); |
---|
149 | glEnable(GL_VERTEX_PROGRAM_POINT_SIZE); |
---|
150 | glTexEnvi(GL_POINT_SPRITE, GL_COORD_REPLACE, GL_TRUE); |
---|
151 | #endif |
---|
152 | m_shader->Bind(); |
---|
153 | m_font->Bind(); |
---|
154 | m_shader->SetUniform(m_texture, 0); |
---|
155 | m_shader->SetUniform(m_transform, xform); |
---|
156 | m_shader->SetUniform(m_datasize, vec2(m_canvas_size.x, |
---|
157 | max(m_font_size.x, m_font_size.y))); |
---|
158 | m_vdecl->SetStream(m_vbo1, m_color); |
---|
159 | m_vdecl->SetStream(m_vbo2, m_char); |
---|
160 | #if !HAVE_SHADER_4 |
---|
161 | m_vdecl->SetStream(m_vbo3, m_vertexid); |
---|
162 | #endif |
---|
163 | m_vdecl->Bind(); |
---|
164 | m_vdecl->DrawElements(MeshPrimitive::Points, 0, m_cells); |
---|
165 | m_vdecl->Unbind(); |
---|
166 | m_font->Unbind(); |
---|
167 | m_shader->Unbind(); |
---|
168 | #if !defined HAVE_GLES_2X |
---|
169 | glDisable(GL_POINT_SPRITE); |
---|
170 | #endif |
---|
171 | m_fbo->Unbind(); |
---|
172 | } |
---|
173 | |
---|
174 | void TextRender::Blit(ivec2 pos, ivec2 size) |
---|
175 | { |
---|
176 | /* FIXME: this is ugly! But we will get rid of it when we |
---|
177 | * do the Direct3D port, so don't worry too much. */ |
---|
178 | ShaderTexture t = m_fbo->GetTexture(); |
---|
179 | uint64_t const &x = *(uint64_t const *)&t; |
---|
180 | |
---|
181 | glDisable(GL_BLEND); |
---|
182 | glEnable(GL_TEXTURE_2D); |
---|
183 | glBindTexture(GL_TEXTURE_2D, (int)x); |
---|
184 | glColor3f(1.0f, 1.0f, 1.0f); |
---|
185 | |
---|
186 | vec2 tc = (vec2)m_canvas_size * m_font_size / m_fbo_size; |
---|
187 | |
---|
188 | glLoadIdentity(); |
---|
189 | glBegin(GL_QUADS); |
---|
190 | glTexCoord2f(tc.x, tc.y); |
---|
191 | glVertex2i(pos.x + size.x, pos.y); |
---|
192 | glTexCoord2f(0.0f, tc.y); |
---|
193 | glVertex2i(pos.x, pos.y); |
---|
194 | glTexCoord2f(0.0f, 0.0f); |
---|
195 | glVertex2i(pos.x, pos.y + size.y); |
---|
196 | glTexCoord2f(tc.x, 0.0f); |
---|
197 | glVertex2i(pos.x + size.x, pos.y + size.y); |
---|
198 | glEnd(); |
---|
199 | } |
---|
200 | |
---|