Coder Social home page Coder Social logo

Comments (3)

Miouyouyou avatar Miouyouyou commented on May 23, 2024

I'll try to write a quick one.

While I have a library to display TTF fonts, it's horrid, so let me see if I can hack around freetype-gl to display some nice text on the screen, using this example.

The whole ideas, anyway, to display TTF fonts on the screen are "simple" :

  1. You either render a complete sentence into a texture and apply this texture to a rectangle on the screen. (SDL-TTF can help in that regard).
  2. Or you generate a texture "atlas", which is composed of :
  • A texture where the characters are packed near to each other (with at least 1 pixel, or if possible, 2 transparent pixels on each side, to avoid texture filtering issues)
  • A data file telling you where the characters are on the texture, and their properties

Then to render a sentence :

  • You prepare and send to the GPU a shader that will display "textured rectangles"
  • You send the font texture to the GPU
  • You prepare a memory buffer with rectangles that will be sent to the GPU
  • You start with a virtual pen, at a specific point (x, y).
  • For each character :
    • You look up the props of that character in your data file
    • Add a rectangle to your rectangles memory buffer, with the size and the texture coordinates of the character
    • Advance your pen by the "advance x" property of the character
    • When encountering a carriage return, you go back to the beginning (x = line_start_x) and increase (or decrease depending on your matrix) the pen 'y'.
  • You send the buffer to the GPU
  • Then draw it.

The only issue with this method is how the fonts are stored in the texture. If you generate a simple texture, with bitmap fonts provided by Freetype, and then display the texture parts (characters) using their actual size (meaning display a 7x15 pixels character as 7x15 pixels on the screen), then no 'filtering' will occur and no visual artifact will happen.
If you display them a little bigger or a little smaller than needed, then filtering will happen and things can get ugly.

SDF (Signed Distance Field) fonts textures alleviate this issue by putting a range of pixels around the characters that define how close they are from the actual character borders. Then using a simple shader, and setting up bilinear texture filtering on the texture, for each sampled pixel value :

  • a simple color is written on the framebuffer if the pixel value (distance) is equal or higher than a given threshold.
  • a 'transparent' pixel is written on the framebuffer if the pixel value is lower than the given threshold
    That gives very crisp and nice results.

Unfortunately, most SDF atlas generators fail to provide a quick and easy way to use them in any C project... Though freetype-gl seems to be the best way for now. Mapbox also have a SDF atlas generator but it's for Javascript (WebGL) projects.

I've generated an atlas with its 'makefont' utility, and I'm trying to get it displayed on the screen... If that fails, I'll try to port my horrid library that still 'kind of work' for simple text purposes.

from simple-gl-evdev.

avafinger avatar avafinger commented on May 23, 2024

I'll try to write a quick one.
That would be awesome. Very nice of you.

I think you mean rendering 2D text something like this:
http://www.opengl-tutorial.org/intermediate-tutorials/tutorial-11-2d-text/

I was thinking of something similar to what the Kodi does, but it looks like a really complex task.

BTW, I tested your example Linux_DRM_OpenGLES.c and it works fine on Kernel 4.4.y and 5.x, so I thought, well, maybe you have something for DRM / GBM or have something in mind.

Thank you.

Cheers.

from simple-gl-evdev.

Miouyouyou avatar Miouyouyou commented on May 23, 2024

Ah, sorry about this one. I admit, I completely forgot about it.

Were you able to find another project ?

from simple-gl-evdev.

Related Issues (1)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.