| Home | zeGraph lib | Lua lib | Custom lib | Tutorials | Notes | XML Script | C-Talk | Z-Script |
Lesson 1
Lesson 2
Lesson 3
Lesson 4
Lesson 5
Lesson 6
Lesson 7
Lesson 8
Lesson 9
Lesson 10
Lesson 11
Lesson 12
Lesson 13
Lesson 14
Lesson 15
Lesson 16
Lesson 17
Lesson 18
Lesson 19

Lesson 6 Text and Font

The OpenGL library does not support text rendering directly. zeGraph uses Freetype (zeFreetype) to produce texture-mapped text (zeText), overcoming the difficulty of having nice-looking text in a scene. Let's see how easy it is to draw text. Note that when you copy the copy to your text editor, the characters "<" and ">" will be changed to "&lt;" and "&gt;" because "<" and ">" are reserved characters of HTML.

require("register")

render, scene, node, font, text
    = zeGrf.new("render" ,"scene", "node", "font", "text")

render:add(scene)
scene:set{node = node}
node:add(text)
text:font(font)
text:fontsize(32)
text:set{color = {0, 0, 1, 1}}

--Use tags to create subscript and symbolic character.
text:text("CO<sub>2</sub> (<sym>956</sym>atm)") 

node:translate(-120, 0, 0)

render:tofile("text.png")

text.png

The bottom-left corner of a text is at (0, 0) on the x-y plane. The text can be re-positioned by node translation. You can also use the set() function to layout the text on any 3D plane and make it looks slanted. Three pairs of tags are implemented for creating subscript, superscript, and symbol characters: <sub></sub>, <sup></sup>, and <sym></sym>. You can use any language (e.g., Chinese and Japanese) in a text as long as you have a font that supports the language. Any character can be treated as a symbol character and rendered by putting its unicode (in decimal) between <sym> and </sym>. For a Greek character, you can use its name between the tags. A symbol character can appear as subscript or superscript as well.

Note that you can use the <sym> and </sym> tags to produce markers for 2D plot. Replacing

text:text("CO<sub>2</sub> (<sym>956</sym>atm)")

in the example with

text:text("<sym>9786</sym><sym>9788</sym><sym>9827</sym>")

You will get the image below.

The zeText object uses the normal Arial font if no font file is set. To generate small size characters, you may consider use the bold face of a font because the anti-aliased characters may appear too light.