1 ! Copyright (C) 2005, 2008 Slava Pestov.
2 ! Portions copyright (C) 2007 Eduardo Cavazos.
3 ! Portions copyright (C) 2008 Joe Groff.
4 ! See http://factorcode.org/license.txt for BSD license.
6 USING: alien alien.c-types continuations kernel libc math macros
7 namespaces math.vectors math.constants math.functions
8 math.parser opengl.gl opengl.glu combinators arrays sequences
9 splitting words byte-arrays assocs colors accessors ;
13 : coordinates ( point1 point2 -- x1 y2 x2 y2 )
16 : fix-coordinates ( point1 point2 -- x1 y2 x2 y2 )
17 [ first2 [ >fixnum ] bi@ ] bi@ ;
19 : gl-color ( color -- ) first4 glColor4d ; inline
21 : gl-clear-color ( color -- )
24 : gl-clear ( color -- )
25 gl-clear-color GL_COLOR_BUFFER_BIT glClear ;
27 : color>raw ( object -- r g b a )
28 >rgba { [ red>> ] [ green>> ] [ blue>> ] [ alpha>> ] } cleave ;
30 : set-color ( object -- ) color>raw glColor4d ;
31 : set-clear-color ( object -- ) color>raw glClearColor ;
34 glGetError dup zero? [
35 "GL error: " over gluErrorString append throw
38 : do-state ( mode quot -- )
39 swap glBegin call glEnd ; inline
41 : do-enabled ( what quot -- )
42 over glEnable dip glDisable ; inline
43 : do-enabled-client-state ( what quot -- )
44 over glEnableClientState dip glDisableClientState ; inline
46 : words>values ( word/value-seq -- value-seq )
47 [ dup word? [ execute ] [ ] if ] map ;
49 : (all-enabled) ( seq quot -- )
50 over [ glEnable ] each dip [ glDisable ] each ; inline
51 : (all-enabled-client-state) ( seq quot -- )
52 [ dup [ glEnableClientState ] each ] dip
54 [ glDisableClientState ] each ; inline
56 MACRO: all-enabled ( seq quot -- )
57 >r words>values r> [ (all-enabled) ] 2curry ;
58 MACRO: all-enabled-client-state ( seq quot -- )
59 >r words>values r> [ (all-enabled-client-state) ] 2curry ;
61 : do-matrix ( mode quot -- )
62 swap [ glMatrixMode glPushMatrix call ] keep
63 glMatrixMode glPopMatrix ; inline
65 : gl-vertex ( point -- )
67 { 2 [ first2 glVertex2d ] }
68 { 3 [ first3 glVertex3d ] }
69 { 4 [ first4 glVertex4d ] }
72 : gl-normal ( normal -- ) first3 glNormal3d ;
74 : gl-material ( face pname params -- )
75 >c-float-array glMaterialfv ;
78 GL_LINES [ gl-vertex gl-vertex ] do-state ;
80 : gl-fill-rect ( loc ext -- )
83 : gl-rect ( loc ext -- )
84 GL_FRONT_AND_BACK GL_LINE glPolygonMode
85 >r { 0.5 0.5 } v+ r> { 0.5 0.5 } v- gl-fill-rect
86 GL_FRONT_AND_BACK GL_FILL glPolygonMode ;
88 : (gl-poly) ( points state -- )
89 [ [ gl-vertex ] each ] do-state ;
91 : gl-fill-poly ( points -- )
92 dup length 2 > GL_POLYGON GL_LINES ? (gl-poly) ;
94 : gl-poly ( points -- )
95 GL_LINE_LOOP (gl-poly) ;
97 : circle-steps ( steps -- angles )
98 dup length v/n 2 pi * v*n ;
100 : unit-circle ( angles -- points1 points2 )
101 [ [ sin ] map ] [ [ cos ] map ] bi ;
103 : adjust-points ( points1 points2 -- points1' points2' )
104 [ [ 1 + 0.5 * ] map ] bi@ ;
106 : scale-points ( loc dim points1 points2 -- points )
107 zip [ v* ] with map [ v+ ] with map ;
109 : circle-points ( loc dim steps -- points )
110 circle-steps unit-circle adjust-points scale-points ;
112 : gl-circle ( loc dim steps -- )
113 circle-points gl-poly ;
115 : gl-fill-circle ( loc dim steps -- )
116 circle-points gl-fill-poly ;
118 : prepare-gradient ( direction dim -- v1 v2 )
119 tuck v* [ v- ] keep ;
121 : gl-gradient ( direction colors dim -- )
123 swap >r prepare-gradient r>
124 [ length dup 1- v/n ] keep [
125 >r >r 2dup r> r> set-color v*n
126 dup gl-vertex v+ gl-vertex
130 : (gen-gl-object) ( quot -- id )
131 >r 1 0 <uint> r> keep *uint ; inline
132 : gen-texture ( -- id )
133 [ glGenTextures ] (gen-gl-object) ;
134 : gen-gl-buffer ( -- id )
135 [ glGenBuffers ] (gen-gl-object) ;
137 : (delete-gl-object) ( id quot -- )
138 >r 1 swap <uint> r> call ; inline
139 : delete-texture ( id -- )
140 [ glDeleteTextures ] (delete-gl-object) ;
141 : delete-gl-buffer ( id -- )
142 [ glDeleteBuffers ] (delete-gl-object) ;
144 : with-gl-buffer ( binding id quot -- )
145 -rot dupd glBindBuffer
146 [ slip ] [ 0 glBindBuffer ] [ ] cleanup ; inline
148 : with-array-element-buffers ( array-buffer element-buffer quot -- )
149 -rot GL_ELEMENT_ARRAY_BUFFER swap [
150 swap GL_ARRAY_BUFFER -rot with-gl-buffer
151 ] with-gl-buffer ; inline
153 : <gl-buffer> ( target data hint -- id )
154 pick gen-gl-buffer [ [
155 >r dup byte-length swap r> glBufferData
156 ] with-gl-buffer ] keep ;
158 : buffer-offset ( int -- alien )
161 : bind-texture-unit ( id target unit -- )
162 glActiveTexture swap glBindTexture gl-error ;
164 : (set-draw-buffers) ( buffers -- )
165 dup length swap >c-uint-array glDrawBuffers ;
167 MACRO: set-draw-buffers ( buffers -- )
168 words>values [ (set-draw-buffers) ] curry ;
170 : do-attribs ( bits quot -- )
171 swap glPushAttrib call glPopAttrib ; inline
173 : gl-look-at ( eye focus up -- )
174 [ first3 ] tri@ gluLookAt ;
176 TUPLE: sprite loc dim dim2 dlist texture ;
178 : <sprite> ( loc dim dim2 -- sprite )
181 : sprite-size2 ( sprite -- w h ) dim2>> first2 ;
183 : sprite-width ( sprite -- w ) dim>> first ;
185 : gray-texture ( sprite pixmap -- id )
188 GL_TEXTURE_2D swap glBindTexture
189 >r >r GL_TEXTURE_2D 0 GL_RGBA r>
190 sprite-size2 0 GL_LUMINANCE_ALPHA
191 GL_UNSIGNED_BYTE r> glTexImage2D
195 : gen-dlist ( -- id ) 1 glGenLists ;
197 : make-dlist ( type quot -- id )
198 gen-dlist [ rot glNewList call glEndList ] keep ; inline
200 : init-texture ( -- )
201 GL_TEXTURE_2D GL_TEXTURE_MAG_FILTER GL_LINEAR glTexParameteri
202 GL_TEXTURE_2D GL_TEXTURE_MIN_FILTER GL_LINEAR glTexParameteri
203 GL_TEXTURE_2D GL_TEXTURE_WRAP_S GL_CLAMP glTexParameterf
204 GL_TEXTURE_2D GL_TEXTURE_WRAP_T GL_CLAMP glTexParameterf ;
206 : gl-translate ( point -- ) first2 0.0 glTranslated ;
210 : top-left drop 0 0 glTexCoord2i 0.0 0.0 glVertex2d ; inline
212 : top-right 1 0 glTexCoord2i first 0.0 glVertex2d ; inline
214 : bottom-left 0 1 glTexCoord2i second 0.0 swap glVertex2d ; inline
216 : bottom-right 1 1 glTexCoord2i gl-vertex ; inline
220 : four-sides ( dim -- )
221 dup top-left dup top-right dup bottom-right bottom-left ;
223 : draw-sprite ( sprite -- )
224 dup loc>> gl-translate
225 GL_TEXTURE_2D over texture>> glBindTexture
227 GL_QUADS [ dim2>> four-sides ] do-state
228 GL_TEXTURE_2D 0 glBindTexture ;
230 : rect-vertices ( lower-left upper-right -- )
232 over first2 glVertex2d
233 dup first pick second glVertex2d
234 dup first2 glVertex2d
235 swap first swap second glVertex2d
238 : make-sprite-dlist ( sprite -- id )
240 GL_COMPILE [ draw-sprite ] make-dlist
243 : init-sprite ( texture sprite -- )
245 dup make-sprite-dlist >>dlist drop ;
247 : delete-dlist ( id -- ) 1 glDeleteLists ;
249 : free-sprite ( sprite -- )
250 [ dlist>> delete-dlist ]
251 [ texture>> delete-texture ] bi ;
253 : free-sprites ( sprites -- )
254 [ nip [ free-sprite ] when* ] assoc-each ;
256 : with-translation ( loc quot -- )
257 GL_MODELVIEW [ >r gl-translate r> call ] do-matrix ; inline
259 : gl-set-clip ( loc dim -- )
260 fix-coordinates glScissor ;
262 : gl-viewport ( loc dim -- )
263 fix-coordinates glViewport ;
265 : init-matrices ( -- )
266 GL_PROJECTION glMatrixMode
268 GL_MODELVIEW glMatrixMode