/glText

Cross-platform single header text rendering library for OpenGL

Primary LanguageCzlib LicenseZlib

glText

Build Status Release Supported OpenGL 3.3 License

glText is a simple cross-platform single header text rendering library for OpenGL. glText requires no additional files (such as fonts or textures) for drawing text, everything comes pre-packed in the header.

simple example

The above screenshot is of the simple.c example.

Example

// Initialize glText
gltInit();

// Creating text
GLTtext *text = gltCreateText();
gltSetText(text, "Hello World!");

// Begin text drawing (this for instance calls glUseProgram)
gltBeginDraw();

// Draw any amount of text between begin and end
gltColor(1.0f, 1.0f, 1.0f, 1.0f);
gltDrawText2D(text, x, y, scale);

// Finish drawing text
gltEndDraw();

// Deleting text
gltDeleteText(text);

// Destroy glText
gltTerminate();

Implementation

In one C or C++ file, define GLT_IMPLEMENTATION prior to inclusion to create the implementation.

#define GLT_IMPLEMENTATION
#include "gltext.h"

Optimization

Each time gltDraw*() functions are called, glGetIntegerv(GL_VIEWPORT, ...) is called. To avoid this and optimize that call away, define GLT_MANUAL_VIEWPORT before including gltext.h.

#define GLT_MANUAL_VIEWPORT
#include "gltext.h"

Then when the viewport is resized manually call:

gltViewport(width, height)

Manual Model, View, Projection Matrix

The example uses LinearAlgebra.

GLfloat fontScale = 0.01f;

GLfloat x = 0.0f;
GLfloat y = 0.0f;

x -= gltGetTextWidth(text, fontScale) * 0.5f;
y -= gltGetTextHeight(text, fontScale) * 0.5f;

mat4 proj = mat4::perspective(70.0f, viewportWidth, viewportHeight, 0.1f, 10.0f);

mat4 view = ...;

mat4 model = mat4::identity;
model.translate(x, y + gltGetTextHeight(text, fontScale));
model.scale(fontScale, -fontScale);

mat4 mvp = proj * view * model;

gltDrawText(text, (GLfloat*)&mvp);

Aligned Text

// Where horizontal is either:
// - GLT_LEFT (default)
// - GLT_CENTER
// - GLT_RIGHT

// Where vertical is either:
// - GLT_TOP (default)
// - GLT_CENTER
// - GLT_BOTTOM

gltDrawText2DAligned(text, x, y, scale, horizontal, vertical);

No Dependencies

glText has no external dependencies besides OpenGL and the standard C libraries. By default glText uses stdlib.h, string.h and stdint.h.

If GLT_DEBUG is defined assert.h is needed. If GLT_DEBUG_PRINT is defined stdio.h is needed.

Reporting Bugs & Requests

Feel free to use the issue tracker, for reporting bugs, submitting patches or requesting features.

Before submitting bugs, make sure that you're using the latest version of glText.