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 ascii calendar combinators.short-circuit
6 continuations kernel libc math macros namespaces math.vectors
7 math.parser opengl.gl combinators combinators.smart arrays
8 sequences splitting words byte-arrays assocs vocabs
9 colors colors.constants accessors generalizations locals fry
11 FROM: alien.c-types => float ;
12 SPECIALIZED-ARRAY: float
13 SPECIALIZED-ARRAY: uint
16 : gl-color ( color -- ) >rgba-components glColor4d ; inline
18 : gl-clear-color ( color -- ) >rgba-components glClearColor ;
20 : gl-clear ( color -- )
21 gl-clear-color GL_COLOR_BUFFER_BIT glClear ;
23 : error>string ( n -- string )
26 { HEX: 0501 "Invalid value" }
27 { HEX: 0500 "Invalid enumerant" }
28 { HEX: 0502 "Invalid operation" }
29 { HEX: 0503 "Stack overflow" }
30 { HEX: 0504 "Stack underflow" }
31 { HEX: 0505 "Out of memory" }
32 { HEX: 0506 "Invalid framebuffer operation" }
33 } at "Unknown error" or ;
35 TUPLE: gl-error function code string ;
37 : <gl-error> ( function code -- gl-error )
38 dup error>string \ gl-error boa ; inline
40 : gl-error-code ( -- code/f )
41 glGetError dup 0 = [ drop f ] when ; inline
43 : (gl-error) ( function -- )
44 gl-error-code [ <gl-error> throw ] [ drop ] if* ;
49 : do-enabled ( what quot -- )
50 over glEnable dip glDisable ; inline
52 : do-enabled-client-state ( what quot -- )
53 over glEnableClientState dip glDisableClientState ; inline
55 : words>values ( word/value-seq -- value-seq )
58 : (all-enabled) ( seq quot -- )
59 over [ glEnable ] each dip [ glDisable ] each ; inline
61 : (all-enabled-client-state) ( seq quot -- )
62 [ dup [ glEnableClientState ] each ] dip
64 [ glDisableClientState ] each ; inline
66 MACRO: all-enabled ( seq quot -- )
67 [ words>values ] dip '[ _ _ (all-enabled) ] ;
69 MACRO: all-enabled-client-state ( seq quot -- )
70 [ words>values ] dip '[ _ _ (all-enabled-client-state) ] ;
72 : do-matrix ( quot -- )
73 glPushMatrix call glPopMatrix ; inline
75 : gl-material ( face pname params -- )
76 float-array{ } like glMaterialfv ;
78 : gl-vertex-pointer ( seq -- )
79 [ 2 GL_FLOAT 0 ] dip glVertexPointer ; inline
81 : gl-color-pointer ( seq -- )
82 [ 4 GL_FLOAT 0 ] dip glColorPointer ; inline
84 : gl-texture-coord-pointer ( seq -- )
85 [ 2 GL_FLOAT 0 ] dip glTexCoordPointer ; inline
87 : line-vertices ( a b -- )
88 [ first2 [ 0.5 + ] bi@ ] bi@ 4 float-array{ } nsequence
92 line-vertices GL_LINES 0 2 glDrawArrays ;
94 :: (rect-vertices) ( loc dim -- vertices )
95 #! We use GL_LINE_STRIP with a duplicated first vertex
96 #! instead of GL_LINE_LOOP to work around a bug in Apple's
103 x w + 0.3 - y h + 0.3 -
106 ] float-array{ } output>sequence ;
108 : rect-vertices ( loc dim -- )
109 (rect-vertices) gl-vertex-pointer ;
112 GL_LINE_STRIP 0 5 glDrawArrays ;
114 : gl-rect ( loc dim -- )
115 rect-vertices (gl-rect) ;
117 :: (fill-rect-vertices) ( loc dim -- vertices )
125 ] float-array{ } output>sequence ;
127 : fill-rect-vertices ( loc dim -- )
128 (fill-rect-vertices) gl-vertex-pointer ;
130 : (gl-fill-rect) ( -- )
131 GL_QUADS 0 4 glDrawArrays ;
133 : gl-fill-rect ( loc dim -- )
134 fill-rect-vertices (gl-fill-rect) ;
136 : do-attribs ( bits quot -- )
137 swap glPushAttrib call glPopAttrib ; inline
139 : (gen-gl-object) ( quot -- id )
140 [ 1 0 <uint> ] dip keep *uint ; inline
142 : (delete-gl-object) ( id quot -- )
143 [ 1 swap <uint> ] dip call ; inline
145 : gen-gl-buffer ( -- id )
146 [ glGenBuffers ] (gen-gl-object) ;
148 : delete-gl-buffer ( id -- )
149 [ glDeleteBuffers ] (delete-gl-object) ;
151 :: with-gl-buffer ( binding id quot -- )
152 binding id glBindBuffer
153 quot [ binding 0 glBindBuffer ] [ ] cleanup ; inline
155 : with-array-element-buffers ( array-buffer element-buffer quot -- )
156 [ GL_ELEMENT_ARRAY_BUFFER ] 2dip '[
157 GL_ARRAY_BUFFER swap _ with-gl-buffer
158 ] with-gl-buffer ; inline
160 : gen-vertex-array ( -- id )
161 [ glGenVertexArrays ] (gen-gl-object) ;
163 : delete-vertex-array ( id -- )
164 [ glDeleteVertexArrays ] (delete-gl-object) ;
166 :: with-vertex-array ( id quot -- )
168 quot [ 0 glBindVertexArray ] [ ] cleanup ; inline
170 : <gl-buffer> ( target data hint -- id )
173 [ [ byte-length ] keep ] dip glBufferData
177 : buffer-offset ( int -- alien )
180 : bind-texture-unit ( id target unit -- )
181 glActiveTexture swap glBindTexture gl-error ;
183 : (set-draw-buffers) ( buffers -- )
184 [ length ] [ >uint-array ] bi glDrawBuffers ;
186 MACRO: set-draw-buffers ( buffers -- )
187 words>values '[ _ (set-draw-buffers) ] ;
189 : gen-dlist ( -- id ) 1 glGenLists ;
191 : make-dlist ( type quot -- id )
192 [ gen-dlist ] 2dip '[ _ glNewList @ glEndList ] keep ; inline
194 : gl-translate ( point -- ) first2 0.0 glTranslated ;
196 : delete-dlist ( id -- ) 1 glDeleteLists ;
198 : with-translation ( loc quot -- )
199 [ [ gl-translate ] dip call ] do-matrix ; inline
201 : fix-coordinates ( point1 point2 -- x1 y2 x2 y2 )
202 [ first2 [ >fixnum ] bi@ ] bi@ ;
204 : gl-set-clip ( loc dim -- )
205 fix-coordinates glScissor ;
207 : gl-viewport ( loc dim -- )
208 fix-coordinates glViewport ;
210 : init-matrices ( -- )
211 #! Leaves with matrix mode GL_MODELVIEW
212 GL_PROJECTION glMatrixMode
214 GL_MODELVIEW glMatrixMode