Changeset 1499


Ignore:
Timestamp:
Jun 19, 2012, 7:33:35 AM (8 years ago)
Author:
sam
Message:

neercs: move TextRender to its own file.

Location:
trunk/neercs
Files:
2 added
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/neercs/Makefile.am

    r1485 r1499  
    88    \
    99    video/render.cpp video/render.h \
     10    video/text-render.cpp video/text-render.h \
    1011    video/blurh.lolfx video/blurv.lolfx video/glow.lolfx \
    1112    video/postfx.lolfx video/radial.lolfx video/simple.lolfx \
  • trunk/neercs/neercs.vcxproj

    r1467 r1499  
    3030    <ClInclude Include="neercs.h" />
    3131    <ClInclude Include="video\render.h" />
     32    <ClInclude Include="video\text-render.h" />
    3233  </ItemGroup>
    3334  <ItemGroup>
    3435    <ClCompile Include="neercs.cpp" />
    3536    <ClCompile Include="video\render.cpp" />
     37    <ClCompile Include="video\text-render.cpp" />
    3638  </ItemGroup>
    3739  <ItemGroup>
  • trunk/neercs/neercs.vcxproj.filters

    r1467 r1499  
    66      <Filter>video</Filter>
    77    </ClCompile>
     8    <ClCompile Include="video\text-render.cpp">
     9      <Filter>video</Filter>
     10    </ClCompile>
    811  </ItemGroup>
    912  <ItemGroup>
    1013    <ClInclude Include="neercs.h" />
    1114    <ClInclude Include="video\render.h">
     15      <Filter>video</Filter>
     16    </ClInclude>
     17    <ClInclude Include="video\text-render.h">
    1218      <Filter>video</Filter>
    1319    </ClInclude>
  • trunk/neercs/video/render.cpp

    r1498 r1499  
    3030#include "../neercs.h"
    3131#include "render.h"
     32#include "text-render.h"
    3233
    3334extern char const *lolfx_blurh;
     
    3738extern char const *lolfx_radial;
    3839extern char const *lolfx_simple;
    39 extern char const *lolfx_text;
    4040
    4141#define DEBUG 1            // debug flag //#if defined(_DEBUG)
     
    164164FrameBuffer *fbo_blur_h, *fbo_blur_v, *fbo_ping, *fbo_pong;
    165165
    166 /*
    167  * Text rendering interface
    168  */
    169 struct TextRender
    170 {
    171     TextRender(caca_canvas_t *caca, ivec2 font_size)
    172       : m_caca(caca),
    173         m_font_size(font_size),
    174         m_canvas_size(caca_get_canvas_width(m_caca),
    175                       caca_get_canvas_height(m_caca)),
    176         m_cells(m_canvas_size.x * m_canvas_size.y)
    177     {
    178         for (int j = 0; j < m_canvas_size.y; j++)
    179         for (int i = 0; i < m_canvas_size.x; i++)
    180             m_vertices << vec2(i, j);
    181     }
    182 
    183     void Init(ivec2 screen_size)
    184     {
    185         m_font = new TileSet("neercs/video/resource/map.png",
    186                              ivec2(256, 256), ivec2(1));
    187 
    188         m_shader = Shader::Create(lolfx_text);
    189         m_coord = m_shader->GetAttribLocation("in_Position",
    190                                               VertexUsage::Position, 0);
    191         m_color = m_shader->GetAttribLocation("in_Attr",
    192                                               VertexUsage::Color, 0);
    193         m_char = m_shader->GetAttribLocation("in_Char",
    194                                              VertexUsage::Color, 1);
    195         m_texture = m_shader->GetUniformLocation("in_Texture");
    196         m_transform = m_shader->GetUniformLocation("in_Transform");
    197         m_vdecl
    198           = new VertexDeclaration(VertexStream<vec2>(VertexUsage::Position),
    199                                   VertexStream<uint32_t>(VertexUsage::Color),
    200                                   VertexStream<uint32_t>(VertexUsage::Color));
    201 
    202         m_vbo1 = new VertexBuffer(m_vertices.Bytes());
    203         void *vertices = m_vbo1->Lock(0, 0);
    204         memcpy(vertices, &m_vertices[0], m_vertices.Bytes());
    205         m_vbo1->Unlock();
    206 
    207         m_vbo2 = new VertexBuffer(m_cells * sizeof(int32_t));
    208         m_vbo3 = new VertexBuffer(m_cells * sizeof(int32_t));
    209 
    210         m_fbo = new FrameBuffer(screen_size);
    211     }
    212 
    213     void Render(ivec2 fbo_size)
    214     {
    215         /* Transform matrix for the scene:
    216          *  - translate to the centre of the glyph
    217          *  - scale by 2.f * font_size / fbo_size
    218          *  - translate to the upper left corner */
    219         mat4 xform = mat4::translate(-1.f, 1.f, 0.f)
    220                    * mat4::scale(vec3(2.f * m_font_size / (fbo_size), 1.f)
    221                                   * vec3(1.f, -1.f, 1.f))
    222                    * mat4::translate(0.5f, 0.5f, 0.f);
    223 
    224         /* Upload libcaca canvas contents to the vertex buffers */
    225         uint32_t *colors = (uint32_t *)m_vbo2->Lock(0, 0);
    226         for (int j = 0; j < m_canvas_size.y; j++)
    227         for (int i = 0; i < m_canvas_size.x; i++)
    228         {
    229             uint32_t attr = caca_get_attr(m_caca, i, j);
    230             uint16_t fg = caca_attr_to_rgb12_fg(attr);
    231             uint16_t bg = caca_attr_to_rgb12_bg(attr);
    232             caca_set_color_argb(m_caca, fg, bg);
    233             attr = caca_get_attr(m_caca, -1, -1);
    234             caca_put_attr(m_caca, i, j, attr);
    235             uint32_t a2 = caca_get_attr(m_caca, i, j);
    236         }
    237         memcpy(colors, caca_get_canvas_attrs(m_caca),
    238                m_cells * sizeof(uint32_t));
    239         m_vbo2->Unlock();
    240 
    241         uint32_t *chars = (uint32_t *)m_vbo3->Lock(0, 0);
    242         memcpy(chars, caca_get_canvas_chars(m_caca),
    243                m_cells * sizeof(uint32_t));
    244         m_vbo3->Unlock();
    245 
    246         m_fbo->Bind();
    247         glDisable(GL_DEPTH_TEST);
    248         glEnable(GL_POINT_SPRITE);
    249         //glEnable(GL_VERTEX_PROGRAM_POINT_SIZE);
    250         glDisable(GL_POINT_SMOOTH);
    251         glPointSize((float)max(m_font_size.x, m_font_size.y));
    252         m_shader->Bind();
    253         m_font->Bind();
    254         m_shader->SetUniform(m_texture, 0);
    255         m_shader->SetUniform(m_transform, xform);
    256         m_vdecl->SetStream(m_vbo1, m_coord);
    257         m_vdecl->SetStream(m_vbo2, m_color);
    258         m_vdecl->SetStream(m_vbo3, m_char);
    259         m_vdecl->Bind();
    260         m_vdecl->DrawElements(MeshPrimitive::Points, 0, m_cells);
    261         m_vdecl->Unbind();
    262         m_font->Unbind();
    263         m_shader->Unbind();
    264         glDisable(GL_POINT_SPRITE);
    265         m_fbo->Unbind();
    266     }
    267 
    268     void Blit(ivec2 fbo_size, ivec2 pos, ivec2 size)
    269     {
    270         glDisable(GL_BLEND);
    271         glEnable(GL_TEXTURE_2D);
    272         glBindTexture(GL_TEXTURE_2D, m_fbo->GetTexture());
    273         glColor3f(1.0f, 1.0f, 1.0f);
    274 
    275         vec2 tc = (vec2)m_canvas_size * m_font_size / fbo_size;
    276 
    277         glLoadIdentity();
    278         glBegin(GL_QUADS);
    279             glTexCoord2f(tc.x, 1.0f);
    280             glVertex2i(pos.x + size.x, pos.y);
    281             glTexCoord2f(0.0f, 1.0f);
    282             glVertex2i(pos.x, pos.y);
    283             glTexCoord2f(0.0f, 1.0f - tc.y);
    284             glVertex2i(pos.x, pos.y + size.y);
    285             glTexCoord2f(tc.x, 1.0f - tc.y);
    286             glVertex2i(pos.x + size.x, pos.y + size.y);
    287         glEnd();
    288     }
    289 
    290 private:
    291     caca_canvas_t *m_caca;
    292     ivec2 m_font_size, m_canvas_size;
    293     int m_cells;
    294 
    295     Array<vec2> m_vertices;
    296     TileSet *m_font;
    297     Shader *m_shader;
    298     ShaderAttrib m_coord, m_color, m_char;
    299     ShaderUniform m_texture, m_transform;
    300     VertexDeclaration *m_vdecl;
    301     VertexBuffer *m_vbo1, *m_vbo2, *m_vbo3;
    302     FrameBuffer *m_fbo;
    303 };
    304 
    305166TextRender *text_render;
    306167
Note: See TracChangeset for help on using the changeset viewer.