1 ! Copyright (C) 2005, 2009 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.
5 USING: alien alien.c-types continuations kernel libc math macros
6 namespaces math.vectors math.constants math.functions
7 math.parser opengl.gl opengl.glu combinators arrays sequences
8 splitting words byte-arrays assocs colors accessors
9 generalizations locals fry specialized-arrays.float
10 specialized-arrays.uint ;
13 : gl-color ( color -- ) >rgba-components glColor4d ; inline
15 : gl-clear-color ( color -- ) >rgba-components glClearColor ;
17 : gl-clear ( color -- )
18 gl-clear-color GL_COLOR_BUFFER_BIT glClear ;
21 glGetError dup zero? [
22 "GL error: " over gluErrorString append throw
25 : do-enabled ( what quot -- )
26 over glEnable dip glDisable ; inline
28 : do-enabled-client-state ( what quot -- )
29 over glEnableClientState dip glDisableClientState ; inline
31 : words>values ( word/value-seq -- value-seq )
32 [ dup word? [ execute ] when ] map ;
34 : (all-enabled) ( seq quot -- )
35 over [ glEnable ] each dip [ glDisable ] each ; inline
37 : (all-enabled-client-state) ( seq quot -- )
38 [ dup [ glEnableClientState ] each ] dip
40 [ glDisableClientState ] each ; inline
42 MACRO: all-enabled ( seq quot -- )
43 [ words>values ] dip '[ _ _ (all-enabled) ] ;
45 MACRO: all-enabled-client-state ( seq quot -- )
46 [ words>values ] dip '[ _ (all-enabled-client-state) ] ;
48 : do-matrix ( mode quot -- )
49 swap [ glMatrixMode glPushMatrix call ] keep
50 glMatrixMode glPopMatrix ; inline
52 : gl-material ( face pname params -- )
53 float-array{ } like glMaterialfv ;
55 : gl-vertex-pointer ( seq -- )
56 [ 2 GL_FLOAT 0 ] dip glVertexPointer ; inline
58 : gl-color-pointer ( seq -- )
59 [ 4 GL_FLOAT 0 ] dip glColorPointer ; inline
61 : gl-texture-coord-pointer ( seq -- )
62 [ 2 GL_FLOAT 0 ] dip glTexCoordPointer ; inline
64 : line-vertices ( a b -- )
65 [ first2 [ 0.5 + ] bi@ ] bi@ 4 float-array{ } nsequence
69 line-vertices GL_LINES 0 2 glDrawArrays ;
71 : (rect-vertices) ( dim -- vertices )
72 #! We use GL_LINE_STRIP with a duplicated first vertex
73 #! instead of GL_LINE_LOOP to work around a bug in Apple's
78 [ [ first 0.3 - ] [ second 0.3 - ] bi ]
79 [ second 0.3 - 0.5 swap ]
81 } cleave 10 float-array{ } nsequence ;
83 : rect-vertices ( dim -- )
84 (rect-vertices) gl-vertex-pointer ;
87 GL_LINE_STRIP 0 5 glDrawArrays ;
90 rect-vertices (gl-rect) ;
92 : (fill-rect-vertices) ( dim -- vertices )
98 } cleave 8 float-array{ } nsequence ;
100 : fill-rect-vertices ( dim -- )
101 (fill-rect-vertices) gl-vertex-pointer ;
103 : (gl-fill-rect) ( -- )
104 GL_QUADS 0 4 glDrawArrays ;
106 : gl-fill-rect ( dim -- )
107 fill-rect-vertices (gl-fill-rect) ;
109 : circle-steps ( steps -- angles )
110 dup length v/n 2 pi * v*n ;
112 : unit-circle ( angles -- points1 points2 )
113 [ [ sin ] map ] [ [ cos ] map ] bi ;
115 : adjust-points ( points1 points2 -- points1' points2' )
116 [ [ 1 + 0.5 * ] map ] bi@ ;
118 : scale-points ( loc dim points1 points2 -- points )
119 zip [ v* ] with map [ v+ ] with map ;
121 : circle-points ( loc dim steps -- points )
122 circle-steps unit-circle adjust-points scale-points ;
124 : close-path ( points -- points' )
127 : circle-vertices ( loc dim steps -- vertices )
128 #! We use GL_LINE_STRIP with a duplicated first vertex
129 #! instead of GL_LINE_LOOP to work around a bug in Apple's
131 circle-points close-path concat >float-array ;
133 : fill-circle-vertices ( loc dim steps -- vertices )
134 circle-points concat >float-array ;
136 : (gen-gl-object) ( quot -- id )
137 [ 1 0 <uint> ] dip keep *uint ; inline
139 : gen-texture ( -- id )
140 [ glGenTextures ] (gen-gl-object) ;
142 : gen-gl-buffer ( -- id )
143 [ glGenBuffers ] (gen-gl-object) ;
145 : (delete-gl-object) ( id quot -- )
146 [ 1 swap <uint> ] dip call ; inline
148 : delete-texture ( id -- )
149 [ glDeleteTextures ] (delete-gl-object) ;
151 : delete-gl-buffer ( id -- )
152 [ glDeleteBuffers ] (delete-gl-object) ;
154 :: with-gl-buffer ( binding id quot -- )
155 binding id glBindBuffer
156 quot [ binding 0 glBindBuffer ] [ ] cleanup ; inline
158 : with-array-element-buffers ( array-buffer element-buffer quot -- )
159 [ GL_ELEMENT_ARRAY_BUFFER ] 2dip '[
160 GL_ARRAY_BUFFER swap _ with-gl-buffer
161 ] with-gl-buffer ; inline
163 : <gl-buffer> ( target data hint -- id )
166 [ [ byte-length ] keep ] dip glBufferData
170 : buffer-offset ( int -- alien )
173 : bind-texture-unit ( id target unit -- )
174 glActiveTexture swap glBindTexture gl-error ;
176 : (set-draw-buffers) ( buffers -- )
177 [ length ] [ >uint-array ] bi glDrawBuffers ;
179 MACRO: set-draw-buffers ( buffers -- )
180 words>values '[ _ (set-draw-buffers) ] ;
182 : do-attribs ( bits quot -- )
183 swap glPushAttrib call glPopAttrib ; inline
185 : gl-look-at ( eye focus up -- )
186 [ first3 ] tri@ gluLookAt ;
188 :: make-texture ( dim pixmap format type -- id )
191 GL_TEXTURE_2D swap glBindTexture
204 : gen-dlist ( -- id ) 1 glGenLists ;
206 : make-dlist ( type quot -- id )
207 [ gen-dlist ] 2dip '[ _ glNewList @ glEndList ] keep ; inline
209 : init-texture ( -- )
210 GL_TEXTURE_2D GL_TEXTURE_MAG_FILTER GL_LINEAR glTexParameteri
211 GL_TEXTURE_2D GL_TEXTURE_MIN_FILTER GL_LINEAR glTexParameteri
212 GL_TEXTURE_2D GL_TEXTURE_WRAP_S GL_CLAMP glTexParameterf
213 GL_TEXTURE_2D GL_TEXTURE_WRAP_T GL_CLAMP glTexParameterf ;
215 : gl-translate ( point -- ) first2 0.0 glTranslated ;
217 : rect-texture-coords ( -- )
218 float-array{ 0 0 1 0 1 1 0 1 } gl-texture-coord-pointer ;
220 : delete-dlist ( id -- ) 1 glDeleteLists ;
222 : with-translation ( loc quot -- )
223 GL_MODELVIEW [ [ gl-translate ] dip call ] do-matrix ; inline
225 : fix-coordinates ( point1 point2 -- x1 y2 x2 y2 )
226 [ first2 [ >fixnum ] bi@ ] bi@ ;
228 : gl-set-clip ( loc dim -- )
229 fix-coordinates glScissor ;
231 : gl-viewport ( loc dim -- )
232 fix-coordinates glViewport ;
234 : init-matrices ( -- )
235 GL_PROJECTION glMatrixMode
237 GL_MODELVIEW glMatrixMode