Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tutorial for OpenGL? #13

Open
aqilc opened this issue Apr 30, 2021 · 4 comments
Open

Tutorial for OpenGL? #13

aqilc opened this issue Apr 30, 2021 · 4 comments

Comments

@aqilc
Copy link

aqilc commented Apr 30, 2021

I'm a beginner in OpenGL, I don't use XLib at all, and this looks like a really nice alternative for the very heavy FreeType. I don't want to spend days/weeks learning XLib to figure out how to port either, so if you could provide a tutorial/demo for graphical OpenGL C programs, it would be extremely appreciated!!

@tomolt
Copy link
Owner

tomolt commented May 2, 2021

This is something I wanted to do for a while, I just haven't gotten around to it yet.
But for the meantime, I can at least give you these tips:

  • To display a glyph, you have to first draw it into a buffer in CPU memory with sft_render() and then upload that buffer into a GL Texture in GPU memory. You will also have to keep some additional information for each glyph around in CPU memory that tells you how big to render it and where to put the following glyphs etc. Simply storing the glyph's SFT_GMetrics should suffice, since all of that data is in there.
  • To get things off the ground, you can initially put each glyph in a separate texture. Once it's working, you can optimize it by
    putting them all next to each other into a single GL_TEXTURE_2D (this is called a texture atlas).
    Alternatively, you could use a single GL_TEXTURE_2D_ARRAY, which is often easier to work with for these sorts of things.
  • To upload libschrift-generated images to GL Textures, you can use glTexImage2D() or glTexSubImage2D() with format=GL_RED and type=GL_UNSIGNED_BYTE.
  • OpenGL wants all its textures to have side lengths that are powers of two. The easiest way to achieve this with libschrift is
    to round up the glyph's width and height to powers of two before allocating the SFT_Image that you pass into sft_render().
  • Since OpenGL expects its textures to have their first row at the bottom and their last row at the top, you should not set SFT_DOWNWARD_Y.
  • To display text, you will of course need some kind of vertex and fragment shader (I'm assuming you're not using the old GL1.x fixed function pipeline since it's incredibly outdated). Since the texture is monochrome, it will have only a single color channel, which happens to be red. This means that your fragment shader will have to do something like gl_FragColor = texture(glyph_texture, my_texcoords).r * glyph_color;.

@blazer2k1
Copy link

blazer2k1 commented Jan 23, 2023

@tomolt Why not do a GLUT/FreeGLUT example so a demo can be crossplatform and not just X11?

@jeannekamikaze
Copy link

SDL would work fine if all you want is a cross-platform sample. I can type something up if you're interested.

@aqilc
Copy link
Author

aqilc commented Apr 28, 2024

Actually, I have enough skill now to try to display something using raw OpenGL + GLFW + GLEW. If I get the time, I'll PR another example! Sorry I didn't reply before, but it's a very cool library that could definitely replace Freetype and stb_truetype as a lightweight, fast rendering library that fills in the gap left by the two. If hinting were supported, I'd definitely use it it more of my projects!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants