GithubHelp home page GithubHelp logo

vallentin / gltext Goto Github PK

View Code? Open in Web Editor NEW
154.0 154.0 17.0 30 KB

Cross-platform single header text rendering library for OpenGL

License: zlib License

C 100.00%
c computer-graphics cpp drawing graphics opengl rendering text

gltext's Introduction

Hello there!

vallentin.dev GitHub crates.io Stack Overflow Stack Exchange Twitter Reddit

I'm Christian Vallentin, a developer from Denmark with an MSc in Software Engineering.

gltext's People

Contributors

charlesgriffiths avatar vallentin avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

gltext's Issues

Support OpenGLES

Greetings,

Is it possible to support OpenGLES 3?

Thank you,
coast

Only Text Showing

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?

Monotype.

Is it possible for glyphs to become equal in width, in order to avoid dancing of FPS displays and counters ?

Black background

Hello MrVallentin,

Nice and useful library!
Just one quick question: how to set transparent background for text instead of black ?

Thanks,
Jev.

Text will not render

_gltText2DShader = glCreateProgram();
this line is generate valid handle(example 3). but in gltBeginDraw, glUseProgram's handle is 0(INVALID_HANDLE).

Missing backslash as one of the characters

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.

CMake sample for Ubuntu/Linux?

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:

  • I don't know what "glad" is (I'm new to opengl, and it doesn't exist on ubuntu as an apt package); it seems unnecessary on ubuntu as I eventually got it to build without it. Is this something that's mostly used in Windows?
  • glBindFragDataLocation is not in any header that I can find (I tried GL/glcorearb.h, but the declaration is behind an #ifdef; setting the #ifdef doesn't seem to unlock the declaration, so there's something I'm missing). The project still links (with a warning), so I'm not sure what it does. Commenting out the call to glBindFragDataLocation in gltext.h doesn't seem to affect the behavior of program.
  • Your README implies that only one of the files that include gltext.h needs to define GLT_IMPLEMENTATION, but since this results in the functions having private (static) linkage, from gltext.h:92, that means functions defined will only be available to functions in that compilation unit; therefore you need to set GLT_IMPLEMENTATION in every file that includes gltext.h, making the define unnecessary. It seems to me that you want the chack on line 86 to be for the existence of GLT_IMPLEMENTATION, and to have GLT_API be defined to nothing in this case (so that you get public linkage). At least, you need to do this if you want to have gltext.h turn into a libgltext.a that can be used by multiple files in your project.
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

Text will not render

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!

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.