项目作者: vallentin

项目描述 :
Cross-platform single header text rendering library for OpenGL
高级语言: C
项目地址: git://github.com/vallentin/glText.git
创建时间: 2016-05-12T18:33:40Z
项目社区:https://github.com/vallentin/glText

开源协议:zlib License

下载


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

  1. // Initialize glText
  2. gltInit();
  3. // Creating text
  4. GLTtext *text = gltCreateText();
  5. gltSetText(text, "Hello World!");
  6. // Begin text drawing (this for instance calls glUseProgram)
  7. gltBeginDraw();
  8. // Draw any amount of text between begin and end
  9. gltColor(1.0f, 1.0f, 1.0f, 1.0f);
  10. gltDrawText2D(text, x, y, scale);
  11. // Finish drawing text
  12. gltEndDraw();
  13. // Deleting text
  14. gltDeleteText(text);
  15. // Destroy glText
  16. gltTerminate();

Implementation

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

  1. #define GLT_IMPLEMENTATION
  2. #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.

  1. #define GLT_MANUAL_VIEWPORT
  2. #include "gltext.h"

Then when the viewport is resized manually call:

  1. gltViewport(width, height)

Manual Model, View, Projection Matrix

The example uses LinearAlgebra.

  1. GLfloat fontScale = 0.01f;
  2. GLfloat x = 0.0f;
  3. GLfloat y = 0.0f;
  4. x -= gltGetTextWidth(text, fontScale) * 0.5f;
  5. y -= gltGetTextHeight(text, fontScale) * 0.5f;
  6. mat4 proj = mat4::perspective(70.0f, viewportWidth, viewportHeight, 0.1f, 10.0f);
  7. mat4 view = ...;
  8. mat4 model = mat4::identity;
  9. model.translate(x, y + gltGetTextHeight(text, fontScale));
  10. model.scale(fontScale, -fontScale);
  11. mat4 mvp = proj * view * model;
  12. gltDrawText(text, (GLfloat*)&mvp);

Aligned Text

  1. // Where horizontal is either:
  2. // - GLT_LEFT (default)
  3. // - GLT_CENTER
  4. // - GLT_RIGHT
  5. // Where vertical is either:
  6. // - GLT_TOP (default)
  7. // - GLT_CENTER
  8. // - GLT_BOTTOM
  9. 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.