Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (780)
Games in Android Showcase (233)
games submitted by our members
Games in WIP (856)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  LWJGL stb bindings  (Read 28186 times)
0 Members and 1 Guest are viewing this topic.
Offline Spasi
« Posted 2015-05-21 20:32:42 »

The latest LWJGL 3 nightly build (#49) now features bindings to the stb collection of "single-file public domain libraries for C/C++". Not everything is supported, only libraries that make sense in Java and are lightweight enough for LWJGL.

The main motivation for adding these bindings was to allow LWJGL users to get rid of any dependencies to AWT (e.g. for loading fonts or images). There are many other options of course, but stb is a solution that was very easy to support and should cover most needs. It is developed by Sean Barrett (works at RAD Game Tools), written in pure C, with functionality focused on game development.

LWJGL includes the following bindings (under the org.lwjgl.stb package):

STBEasyFont (demo)

A quick-and-dirty solution for getting some text rendered in the simplest possible way. It provides a function that converts an ASCII string to an array of GL_QUADS, which you can then render any way you like. It's essentially a monospaced bitmap font.

It's obviously very inefficient, but you can cache the generated geometry.

STBTruetype (simple demo - advanced demo)

Parses .ttf files, returns font/glyph metrics and rasterizes fonts to packed textures.

The API is simple, but this is an advanced library; line metrics, glyph metrics, kerning, pixel snapping, oversampling for small test sizes, subpixel positioning, everything you need is here. Make sure to read this guide on oversampling. The "advanced demo" above is an LWJGL port of the "oversample" test in the stb repository.


Can be used to pack rectangular textures into an atlas. It implements the Skyline Bottom-Left algorithm and is used internally by STBTruetype for the glyph packing.

STBImage (demo)

An image loading library. Supports the following image file formats:

  • JPEG baseline & progressive (12 bpc/arithmetic not supported, same as stock IJG lib)
  • PNG 1/2/4/8/16-bit-per-channel
  • TGA (not sure what subset, if a subset)
  • BMP non-1bpp, non-RLE
  • PSD (composited view only, no extra channels, 8/16 bit-per-channel)
  • GIF (*desired_channels always reports as 4-channel)
  • HDR (radiance rgbE format)
  • PIC (Softimage PIC)
  • PNM (PPM and PGM binary only)


High-quality image resizing. It's sRGB-aware, alpha-aware, supports different edge wrap modes and a variety of filters (including cubic and Mitchell-Netrevalli).

If you're using glGenerateMipmap or aren't doing gamma correct mipmap generation, you really really want to use this.


Simple library for writing image files. Supports:

  • PNG
  • BMP
  • TGA
  • HDR

It is meant for saving screenshots to disk, so it is optimized for encoding speed, not compression ratio.


A real-time DXT1/DXT5 compressor. If you need to compress textures at runtime, this should provide better quality than the compression done by OpenGL drivers.

STBVorbis (demo)

An Ogg Vorbis audio decoder. Seeking is not properly implemented yet, only "rewind" works.


Computes Perlin's revised noise function (3D input, 1D output). I'm not sure how this performs in terms of speed and quality; if anyone tests it, please post here.

General notes

Try to avoid the "filename" APIs (in STBImage, STBImageWrite and STBVorbis), they do not support unicode paths atm (on Windows). Better use standard Java IO to load the files in memory and use the corresponding APIs.

I haven't had the time to test everything, let me know if you encounter any issues. You can see a video of some of the above here (GIF, 7.43 MB).
Offline NegativeZero

JGO Kernel

Medals: 333
Exp: 1 month or less

Zero but not.

« Reply #1 - Posted 2015-07-01 06:25:25 »

@Spasi, all the Github links from this page are now 404'ing.

Offline Spasi
« Reply #2 - Posted 2015-07-01 08:13:21 »

Thanks, fixed.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline ags1

JGO Kernel

Medals: 367
Projects: 7

Make code not war!

« Reply #3 - Posted 2015-07-02 20:20:59 »

Awesome stuff. Much appreciated.

Offline Spasi
« Reply #4 - Posted 2015-07-16 08:33:29 »

The latest nightly build includes an updated font rasterizer, as described here: How the stb_truetype Anti-Aliased Software Rasterizer v2 Works
Offline zeejfps

Senior Newbie

Exp: 2 years

« Reply #5 - Posted 2015-12-27 23:01:16 »

Most of the examples for the TrueType usage seem to be using an outdated API that more closely resembles the C version. Also it uses the fixed pipeline for drawing the quads. Is there better documentation that I can find or better examples?
Offline Spasi
« Reply #6 - Posted 2015-12-28 01:47:25 »

Most of the examples for the TrueType usage seem to be using an outdated API that more closely resembles the C version.

There are functions in stb_truetype that can be used to render text in a few different ways. Which ones you use depends on the level of quality, performance and customization you're interested in. There's no outdated API afaik. The Truetype demo uses the simple stbtt_BakeFontBitmap/stbtt_GetBakedQuad and the TruetypeOversample demo uses the higher quality stbtt_PackFontRanges/stbtt_GetPackedQuad. Neither demo handles codepoint kerning (not all fonts come with kerning information), but it's easy to add.

Also it uses the fixed pipeline for drawing the quads.

The purpose of these demos is to provide sample usage of the stb_truetype API. How to best render the quads is a generic OpenGL question and not specific to font rendering. They're just simple quads with a position and a texcoord. The answer will depend on the application and how much caching (of character sequences) it is able to do.

Is there better documentation that I can find or better examples?

There's plenty of javadoc in the STBTruetype class. Also, reading this will be useful.
Offline zeejfps

Senior Newbie

Exp: 2 years

« Reply #7 - Posted 2015-12-28 07:53:27 »

Thanks for the reply. I realized I am an idiot, my lwjgl wasn't the latest nightly build so the API differed.
Offline DesertCoockie
« Reply #8 - Posted 2018-03-23 19:23:43 »

Links are dead! Please update them, thanks  Wink
Offline Spasi
« Reply #9 - Posted 2018-03-23 19:36:54 »

Thanks, fixed.
Pages: [1]
  ignore  |  Print  

hadezbladez (718 views)
2018-11-16 13:46:03

hadezbladez (359 views)
2018-11-16 13:41:33

hadezbladez (703 views)
2018-11-16 13:35:35

hadezbladez (179 views)
2018-11-16 13:32:03

EgonOlsen (2371 views)
2018-06-10 19:43:48

EgonOlsen (2505 views)
2018-06-10 19:43:44

EgonOlsen (1466 views)
2018-06-10 19:43:20

DesertCoockie (2132 views)
2018-05-13 18:23:11

nelsongames (1928 views)
2018-04-24 18:15:36

nelsongames (2601 views)
2018-04-24 18:14:32
Deployment and Packaging
by mudlee
2018-08-22 18:09:50

Java Gaming Resources
by gouessej
2018-08-22 08:19:41

Deployment and Packaging
by gouessej
2018-08-22 08:04:08

Deployment and Packaging
by gouessej
2018-08-22 08:03:45

Deployment and Packaging
by philfrei
2018-08-20 02:33:38

Deployment and Packaging
by philfrei
2018-08-20 02:29:55

Deployment and Packaging
by philfrei
2018-08-19 23:56:20

Deployment and Packaging
by philfrei
2018-08-19 23:54:46 is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!