Thune-GL User Guide

1   Overview

Thune-GL expands Thune with datatypes, functions and dialects for using OpenGL and OpenAL.

2   Datatypes

Datatype Examples
draw-list!  
raster!  
texture!  
font!  
shader!  
framebuffer!  

2.1   Draw List

A draw-list! value is a program of the draw list dialect. The value can be either a block containing a program or a compiled OpenGL display list version of a program.

The make signature:

(draw-list! block -- dlist)

The following equivalent statements will create a draw-list:

draw-list! [] make
[] draw-list

To compile a program into an OpenGL display list use draw-list.compile.

[font my-font
 text 50,100 "Hello!"] draw-list.compile :hello-dl

2.2   Raster

(raster! w,h,b -- raster)

2.3   Texture

(texture! raster -- tex)
(texture! coord  -- tex)
(texture! [raster!/coord! 'mipmap 'nearest 'linear 'repeat 'clamp] -- tex)

2.4   Font

(font! ["font.name" 20 "chars abc" 256,128] -- font)

2.5   Shader

(shader! spec -- shader!/context!)

Specification block:

vertex {...}
fragment {...}
default []

2.6   Framebuffer

(framebuffer! texture!/coord! -- fbo)

4   Functions

4.1   Graphics Functions

Word Stack Usage Function
draw (dlist -- ) Draw display list
text-size (font text -- coord) Pixel size of text in a given font
display.events ( -- blk) Block of input events
display.swap (color -- ) coord!/vec3!/none!
display.area ( -- coord) Size of display
display.snapshot ( -- raster) Grab snapshot of display
display.cursor (logic -- ) Show/hide mouse cursor
key-repeat (logic -- ) Enable/disable key repeat
key-code (key -- code) Maps char!/word! to OS key code
load.png (file -- raster)  
save.png (raster file -- )  
display (size -- )  
look-at (matrix vec3 -- )  
gl-extensions ( -- string) GL_EXTENSIONS
gl-max-textures ( -- count) GL_MAX_TEXTURE_UNITS
shadowmap (size -- framebuffer) Create shadowmap

4.2   Graphics Helpers

Word Stack Usage Function
draw-list (src -- dl)  
draw-list.compile (src -- dl)  
unit-matrix    
load.shader (file -- shader)  
load.tex (file -- texture)  
load.mip (file -- texture)  
load.tex.clamp (file -- texture)  
make-matrix (vec3 -- matrix)  

4.3   Audio Functions

Word Stack Usage Function
load.wav (file -- sound)  
play (sound/file -- ) Play sound or music file
stop (sound -- ) Stop sound

4.4   Math Functions

Word Stack Usage Function
lerp (val1 val2 t -- valt)  
curve-value (curve t -- valt)  

4.4.1   lerp

Does a linear interpolation between two values of the same type (decimal!, vec3!, or coord!).

This is the calculation used:

result = v1 + ((v2 - v1) * t)

Stack usage:

(v1 decimal!/vec3!/coord!
 v2 decimal!/vec3!/coord!
 t  decimal!
 -- decimal!/vec3!/coord!)

4.4.2   curve-value

Returns value on curve at time t. Currently only linear segments are implemented.

Stack usage:

(block!  decimal!/int! -- value)

Example:

[0.0  2.0
 0.5  7.0
 1.0  7.0
 8.0  2.0] 3 curve-value
= 5.57143

5   Draw List Dialect

5.1   General Instructions

Word Arguments Description
nop   No operation
clear   glClear()
call draw-list  
enable/F   glEnable()
disable/F   glDisable()
solid   Draw without textures
model   Draw with texture (will be changed)
decal texture Bind Texture with transparency
particle 'off/'add/'burn/'trans Set blending for drawing particles
camera camera-context Set GL viewport and matricies
light logic!/vec3!/block!  
light/N logic!/vec3!/block!  
lighting logic! Enable/disable GL_LIGHTING
color coord!/vec3!/int! glColor()
push   glPushMatrix()
pop   glPopMatrix()
translate vec3! glTranslatef()
rotate angle axis glRotatef()
scale vec3!/number! glScalef()
font font! Draw with font
text [coord!] text Draw text in current font
sphere radius slices,stacks Draw textured GLUT sphere
shader shader!/none!/0 Load shader
framebuffer framebuffer!/none!/0 Bind framebuffer
shadow-begin framebuffer!  
shadow-end    

5.2   Geometry Instructions

Word Arguments Description
colors block! Set color array
verts vector! Set vertex array
normals vector! Set normal array
uvs vector! Set texture UV array
attrib select! (int!) vector! Set user attribute array
points attr vector! Draw points
lines attr vector! Draw line primitives
line-strip attr vector! Draw line primitives
tris attr vector! Draw triangle primitives
tri-strip attr vector! Draw triangle primitives
tri-fan attr vector! Draw triangle primitives
quads attr vector! Draw quad primitives
quad-strip attr vector! Draw quad primitives

5.2.1   Geometry Primitives

The normal OpenGL primitives (lines, triangles, etc.) are specified with a primitive type, an attribute word, and an integer vector.

The attribute word has one character for each primitive attribute. The vertex attribute ('v') must be the last character in the word.

Attribute Id Character Description
Color c Index into colors vector
Normal n Index into normals vector
UV t Index into texture uvs vector
Vertex v Index into verts vector
User Attribute a Index into attrib vector

The UV attribute (t) may appear more than once in the word. Each occurance will set the UV of the next texture unit (0, 1, 2, etc.).

Some primitive examples:

tris v #[0  1  2
         3  4  5]

tri-fan cv #[0 0   1 1   2 2   2 3   1 4]

quads tnv #[0 0 0   1 0 1   2 0 2   3 0 3

5.2.2   Geometry Examples

Here is a triangle with colored verticies:

verts #[-1.0  0.0  0.0
         0.0  1.73 0.0
         1.0  0.0  0.0]

colors [255,0,0  0,255,0  0,0,255]

tris cv #[0 0   1 1   2 2]

Here is geometry for a textured cube:

/*
         5___3
         |\4__\2
  X Y    | |   |
   \|_Z  7\|_1_|
           6   0
*/

verts #[
    -2.0 -2.0  2.0
     2.0 -2.0  2.0
    -2.0  2.0  2.0
     2.0  2.0  2.0
    -2.0  2.0 -2.0
     2.0  2.0 -2.0
    -2.0 -2.0 -2.0
     2.0 -2.0 -2.0
]

normals #[
     1.0  0.0  0.0
     0.0  1.0  0.0
     0.0  0.0  1.0
    -1.0  0.0  0.0
     0.0 -1.0  0.0
     0.0  0.0 -1.0
]

uvs #[
    0.0  0.0
    0.0  1.0
    1.0  0.0
    1.0  1.0
]

quads tnv #[
    1 0 1   3 0 7   2 0 5   0 0 3   ; +X
    1 3 6   3 3 0   2 3 2   0 3 4   ; -X
    1 1 4   3 1 2   2 1 3   0 1 5   ; +Y
    1 4 0   3 4 6   2 4 7   0 4 1   ; -Y
    1 2 0   3 2 1   2 2 3   0 2 2   ; +Z
    1 5 4   3 5 5   2 5 7   0 5 6   ; -Z
]