flyx / OpenGLAda

Thick Ada binding for OpenGL and GLFW
flyx.github.io/OpenGLAda/
MIT License
95 stars 13 forks source link

How to render text via modern shader method? #84

Closed rogermc2 closed 6 years ago

rogermc2 commented 7 years ago

After considerable effort I remain unable to figure out how to obtain access to bitmap glyph image data:

face->glyph->bitmap.buffer

which I need for:

Texture_2D.Load_From_Data  (0, RGB, GL.Types.Int (Data.Width),
                                  GL.Types.Int (Data.Height), Red,
                                  Unsigned_Byte, Image_Address);

Image_Address being the address of a bitmapped character glyph. FTGL provides access to necessary character parameters, such as width and height, but doesn't seem to provide access to character glyphs which is available in C++ via the FTGlyphContainer function:

const FTGlyph* const Glyph(const unsigned int characterCode) const;

This seems to be an appropriate function for obtaining the glyph of a character for use in a modern OpenGL shader texture which I would like to provide by means of a FTGL function such as ~ function Get_Glyph (char : Character) return System.Address ~

The FTGL C interface seems designed only for the deprecated immediate pipeline use. Advice on solving this problem will be greatly appreciated.

The code I am trying to implement is based on https://learnopengl.com/#!In-Practice/Text-Rendering. My initial need is for bitmapped characters. The relevant code is:

  glTexImage2D
         (  GL_TEXTURE_2D,
            0,
            GL_RED,
            face->glyph->bitmap.width,
            face->glyph->bitmap.rows,
            0,
            GL_RED,
            GL_UNSIGNED_BYTE,
            face->glyph->bitmap.buffer
         );

I am close to completion but am stopped by my inability to access glyphs.

rogermc2 commented 7 years ago

https://sourceforge.net/p/ftgl/bugs/23/ indicates that immediate mode in my OpenGL context cannot work. However, the ftgl_test program does work. I've tried changing most parameters in my Initialize file without success.

rogermc2 commented 7 years ago

I think the solution may be to implement Freetype_GL in Ada. I started my attempt to write suitable bindings. See https://github.com/rougier/freetype-gl

flyx commented 7 years ago

I am aware that the FTGL binding is incomplete. IIRC I did not include bindings for FTGlyph because it depended on portions of the Freetype library which I also would have to wrap if I wanted to provide that interface. I may re-evaluate that if necessary, keep me updated how you're doing with Freetype_GL.

rogermc2 commented 7 years ago

After considerable effort I am unable to make a working implementation of Freetype_GL. The provided Freetype_GL C demos work OK and the code is not difficult to follow. I have tried to implement two of the demos; font and texture. Texture I also tried without using the Freetype_GL rendering routine but using the standard Open_GL process, just using Freetype_GL to try and load the font and associated data. Using the Freetype rendering routine, Vertex_Buffer_Render (Buffer_Ptr, Mode'Enum_Rep), that is, pragma Import (C, Vertex_Buffer_Render, "vertex_buffer_render"), my display code runs once then, on the second iteration, raises an INVALID_VALUE_ERROR exception in Clear_Background_Colour. Freetype_GL shaders are written in an earlier version of GSL than my machine recognizes so I have had to adapt the shaders to version 4.1. If I comment out Vertex_Buffer_Render the exception does not occur. I'm not clear on how to proceed from here. Thanks for any advice offerred.

rogermc2 commented 7 years ago

My version of Texture without using the Freetype_GL rendering routine runs without crashing however, no data is displayed in Texture's window. On each iteration of display, GL.Objects.Programs.Validate_Status returns false with an empty info log. I seem to recall this problem from the past but can't remember the cause or reason. I suspect this version has the best chance for success.

rogermc2 commented 7 years ago

Due to lack of success to date, I now have decided to return to my original example and write wrappers as needed directly to freestyle.

rogermc2 commented 7 years ago

Using freetype wrappers seems the best approach. At least it gives better visibility to the freetype methodology.

rogermc2 commented 7 years ago

SUCCESS at last! I have finally produced a one character example that works using freetype wrappers. Before issuing a PR, I intend to convert as many of the freetype data types to GL types and clean up as much as possible. My freetype solution should be a good candidate for inclusion in your next release.

flyx commented 7 years ago

Definitely! Looking forward to the PR.

flyx commented 6 years ago

So with the recently merged PR, we can close this issue, right?