I'm Christian Vallentin, a developer from Denmark with an MSc in Software Engineering.
vallentin / gltext Goto Github PK
View Code? Open in Web Editor NEWCross-platform single header text rendering library for OpenGL
License: zlib License
Cross-platform single header text rendering library for OpenGL
License: zlib License
So a month ago I had a project where I rendered circles and lines then rendered text, all in a loop and it worked. Now when build the program it works for a few frames and then everything turns black except for the text. I am using the exact same code and libraries is there anything you can think of that would be causing this?
is or will there be any way to use custom fonts?
Is it possible for glyphs to become equal in width, in order to avoid dancing of FPS displays and counters ?
Adding "glEnable(GL_DEPTH_TEST)" (when using GLEW) causes the text to not be rendered. This can be recreated by running the "simple.c" example, and adding the function call:
...
//Used GLEW here but should be the same for GLAD
if (glewInit())
{
fprintf(stderr, "Failed to load OpenGL functions and extensions\n");
glfwTerminate();
return EXIT_FAILURE;
}
glfwSwapInterval(1);
//Can insert here]
glEnable(GL_DEPTH_TEST);
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
if (!gltInit())
{
fprintf(stderr, "Failed to initialize glText\n");
glfwTerminate();
return EXIT_FAILURE;
}
...
I found this can be fixed/hacked around by tweaking the vertex shader source to set the Z coordinate to -1.0:
static const GLchar* _gltText2DVertexShaderSource =
"#version 330 core\n"
"\n"
"in vec2 position;\n"
"in vec2 texCoord;\n"
"\n"
"uniform mat4 mvp;\n"
"\n"
"out vec2 fTexCoord;\n"
"\n"
"void main()\n"
"{\n"
" fTexCoord = texCoord;\n"
" \n"
" // Updated gl_Position to account for depth test\n"
" vec4 ndc_pos = mvp * vec4(position, 0.0, 1.0);\n"
" gl_Position = vec4(ndc_pos.x, ndc_pos.y, -1.0, 1.0);\n"
"}\n";
Is this the intended behavior?
Hello MrVallentin,
Nice and useful library!
Just one quick question: how to set transparent background for text instead of black ?
Thanks,
Jev.
I've pasted code from "example" from readme. Is it possible to fix?
I have a GLFW project and was trying to call these two functions, but Im receiving E0135 errors for both of them.
I have a C++ project using OpenGL 3.3 / GLFW / glad to render graphics and i am trying to run your example but have no text showing when i run my program. I am using Visual Studio 2019 and the gltext.h is included properly. I get no errors whatsoever.
Could there be a problem with another graphics draw call cutting the text out? Thanks in advance!
Would be nice to have support for umlauts: ü, ä, ö, Ü, Ä and Ö. And to be fair, maybe for å and Å also.
Hi! I'm struggling on what to use in the last parameter of the function.
Cheers,
Solipa
Greetings,
Is it possible to support OpenGLES 3?
Thank you,
coast
_gltText2DShader = glCreateProgram();
this line is generate valid handle(example 3). but in gltBeginDraw, glUseProgram's handle is 0(INVALID_HANDLE).
You have 83 listed characters. One of the charactes is a quotation, but to use that quote, you have to use an escape character known as backslash. It looks as such --> \"
Now the problem is, you are missing the backslash as one of the charactes. You have forward slash listed / . But you do not have backslash listed. Understandably, if you have a single backslash \ , then it would act as an escape character for anything after it.
To fix this, you would need to escape another backslash.. as such \\.
This would also raise the glyph count from 83, to 84.
I could add in these two fixes, however that leaves the Font Glyph Data, and I am unsure how that part works. Is it HEX that is read as binary, and the binary is what is shown in openGL as dots on the screen for that particular character ? If so, what HEX code would need to be added, and where ?
EDIT : I just read this here ISSUE #10. I'll see if I can dig further. It would be nice if you could add the backslash, since you know what you are doing. haha
The baked in font data, is packed into _gltFontGlyphRects which stores the rectangle information of each glyph, and _gltFontGlyphData which stores the pixels of each font in relation to the rectangle bounds. By packed I mean it quite literally, as bit packing is used minimize the amount of data. For instance, the pixels are only 2-bit, i.e. 00 is transparent, 01 is white, 10 is black.
You can see all the logic for unpacking the data, along with creating the font in the subsequent _gltCreateText2DFontTexture() function. However, if we were to introduce a new function, e.g. gltCreateFont(image_data, glyph_rects), then the logic needed to implement that function is essentially already found in _gltCreateText2DFontTexture() if you ignore all the unpacking.
EDIT 2 : Well I have gone over the font data, breaking it down into binary. But I am still not seeing the pattern of how the rectangle and data work together. I also realized there has been no activity on this project since 2020. I see comments as of 2021, but no actual updates and 12+ open issues. So I am going to assume this project is now abandoned.
For some reason when I go to use ' for words like I'm it does it double for example
I updated the glyph to include this static const char *_gltFontGlyphCharacters = " abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.,!?-+/():;%&`*#=[]'@*\"";
but it does it incorrectly ideas on how to solve this?
I had some difficulty getting your simple.c to build & run on ubuntu; however, after some hacking I came up with these changes (I'm not sure how to push a branch up to your repository).
Major issues I had:
4 files changed, 36 insertions(+), 13 deletions(-)
CMakeLists.txt | 19 +++++++++++++++++++
examples/gltext.c | 7 +++++++
examples/simple.c | 15 ++++++---------
gltext.h | 8 ++++----
new file CMakeLists.txt
@@ -0,0 +1,19 @@
+cmake_minimum_required(VERSION 3.12...3.18 FATAL_ERROR)
+project (Test)
+
+set(CMAKE_C_STANDARD 99)
+
+find_package(OpenGL REQUIRED)
+find_package(glfw3 3.3 REQUIRED)
+
+set(CMAKE_EXPORT_COMPILE_COMMANDS Y)
+
+add_executable (Test examples/simple.c examples/gltext.c)
+target_include_directories(Test PUBLIC .)
+
+target_link_libraries(Test
+ PUBLIC
+ m
+ glfw
+ OpenGL::GL
+)
new file examples/gltext.c
@@ -0,0 +1,7 @@
+// For building implementations for gltext functions
+#include <GL/gl.h>
+
+#include <GLES3/gl3.h>
+
+#define GLT_IMPLEMENTATION
+#include "gltext.h"
modified examples/simple.c
@@ -11,13 +11,10 @@
#include <math.h>
-#include <glad/glad.h> /* https://github.com/Dav1dde/glad */
#include <GLFW/glfw3.h> /* https://github.com/glfw/glfw */
-#define GLT_IMPLEMENTATION
#include "gltext.h" /* https://github.com/vallentin/glText */
-
int main(int argc, char *argv[])
{
if (!glfwInit())
@@ -52,12 +49,12 @@ int main(int argc, char *argv[])
glfwShowWindow(window);
glfwMakeContextCurrent(window);
- if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
- {
- fprintf(stderr, "Failed to load OpenGL functions and extensions\n");
- glfwTerminate();
- return EXIT_FAILURE;
- }
+ /* if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) */
+ /* { */
+ /* fprintf(stderr, "Failed to load OpenGL functions and extensions\n"); */
+ /* glfwTerminate(); */
+ /* return EXIT_FAILURE; */
+ /* } */
glfwSwapInterval(1);
modified gltext.h
@@ -83,13 +83,13 @@ extern "C" {
#define GLT_NULL 0
#define GLT_NULL_HANDLE 0
-#ifdef GLT_IMPORTS
+#ifndef GLT_IMPLEMENTATION
# define GLT_API extern
#else
# ifndef GLT_STATIC
# define GLT_STATIC
# endif
-# define GLT_API static
+# define GLT_API
#endif
#define GLT_LEFT 0
@@ -956,7 +956,7 @@ GLT_API GLboolean _gltCreateText2DShader(void)
glBindAttribLocation(_gltText2DShader, _GLT_TEXT2D_POSITION_LOCATION, "position");
glBindAttribLocation(_gltText2DShader, _GLT_TEXT2D_TEXCOORD_LOCATION, "texCoord");
- glBindFragDataLocation(_gltText2DShader, 0, "fragColor");
+ // glBindFragDataLocation(_gltText2DShader, 0, "fragColor");
glLinkProgram(_gltText2DShader);
@@ -1336,4 +1336,4 @@ GLT_API GLboolean _gltCreateText2DFontTexture(void)
}
#endif
-#endif
\ No newline at end of file
+#endif
why i cann't assigned mvp multiple matrix from 3d model?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.