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.parser opengl.gl combinators
7 combinators.smart arrays sequences splitting words byte-arrays assocs
8 colors colors.constants accessors generalizations locals fry
9 specialized-arrays.float specialized-arrays.uint ;
12 : gl-color ( color -- ) >rgba-components glColor4d ; inline
14 : gl-clear-color ( color -- ) >rgba-components glClearColor ;
16 : gl-clear ( color -- )
17 gl-clear-color GL_COLOR_BUFFER_BIT glClear ;
19 : error>string ( n -- string )
22 { HEX: 0501 "Invalid value" }
23 { HEX: 0500 "Invalid enumerant" }
24 { HEX: 0502 "Invalid operation" }
25 { HEX: 0503 "Stack overflow" }
26 { HEX: 0504 "Stack underflow" }
27 { HEX: 0505 "Out of memory" }
28 { HEX: 0506 "Invalid framebuffer operation" }
29 } at "Unknown error" or ;
31 TUPLE: gl-error code string ;
34 glGetError dup 0 = [ drop ] [
35 dup error>string \ gl-error boa throw
38 : do-enabled ( what quot -- )
39 over glEnable dip glDisable ; inline
41 : do-enabled-client-state ( what quot -- )
42 over glEnableClientState dip glDisableClientState ; inline
44 : words>values ( word/value-seq -- value-seq )
47 : (all-enabled) ( seq quot -- )
48 over [ glEnable ] each dip [ glDisable ] each ; inline
50 : (all-enabled-client-state) ( seq quot -- )
51 [ dup [ glEnableClientState ] each ] dip
53 [ glDisableClientState ] each ; inline
55 MACRO: all-enabled ( seq quot -- )
56 [ words>values ] dip '[ _ _ (all-enabled) ] ;
58 MACRO: all-enabled-client-state ( seq quot -- )
59 [ words>values ] dip '[ _ _ (all-enabled-client-state) ] ;
61 : do-matrix ( quot -- )
62 glPushMatrix call glPopMatrix ; inline
64 : gl-material ( face pname params -- )
65 float-array{ } like glMaterialfv ;
67 : gl-vertex-pointer ( seq -- )
68 [ 2 GL_FLOAT 0 ] dip glVertexPointer ; inline
70 : gl-color-pointer ( seq -- )
71 [ 4 GL_FLOAT 0 ] dip glColorPointer ; inline
73 : gl-texture-coord-pointer ( seq -- )
74 [ 2 GL_FLOAT 0 ] dip glTexCoordPointer ; inline
76 : line-vertices ( a b -- )
77 [ first2 [ 0.5 + ] bi@ ] bi@ 4 float-array{ } nsequence
81 line-vertices GL_LINES 0 2 glDrawArrays ;
83 :: (rect-vertices) ( loc dim -- vertices )
84 #! We use GL_LINE_STRIP with a duplicated first vertex
85 #! instead of GL_LINE_LOOP to work around a bug in Apple's
92 x w + 0.3 - y h + 0.3 -
95 ] float-array{ } output>sequence ;
97 : rect-vertices ( loc dim -- )
98 (rect-vertices) gl-vertex-pointer ;
101 GL_LINE_STRIP 0 5 glDrawArrays ;
103 : gl-rect ( loc dim -- )
104 rect-vertices (gl-rect) ;
106 :: (fill-rect-vertices) ( loc dim -- vertices )
114 ] float-array{ } output>sequence ;
116 : fill-rect-vertices ( loc dim -- )
117 (fill-rect-vertices) gl-vertex-pointer ;
119 : (gl-fill-rect) ( -- )
120 GL_QUADS 0 4 glDrawArrays ;
122 : gl-fill-rect ( loc dim -- )
123 fill-rect-vertices (gl-fill-rect) ;
125 : do-attribs ( bits quot -- )
126 swap glPushAttrib call glPopAttrib ; inline
128 : (gen-gl-object) ( quot -- id )
129 [ 1 0 <uint> ] dip keep *uint ; inline
131 : gen-gl-buffer ( -- id )
132 [ glGenBuffers ] (gen-gl-object) ;
134 : (delete-gl-object) ( id quot -- )
135 [ 1 swap <uint> ] dip call ; inline
137 : delete-gl-buffer ( id -- )
138 [ glDeleteBuffers ] (delete-gl-object) ;
140 :: with-gl-buffer ( binding id quot -- )
141 binding id glBindBuffer
142 quot [ binding 0 glBindBuffer ] [ ] cleanup ; inline
144 : with-array-element-buffers ( array-buffer element-buffer quot -- )
145 [ GL_ELEMENT_ARRAY_BUFFER ] 2dip '[
146 GL_ARRAY_BUFFER swap _ with-gl-buffer
147 ] with-gl-buffer ; inline
149 : <gl-buffer> ( target data hint -- id )
152 [ [ byte-length ] keep ] dip glBufferData
156 : buffer-offset ( int -- alien )
159 : bind-texture-unit ( id target unit -- )
160 glActiveTexture swap glBindTexture gl-error ;
162 : (set-draw-buffers) ( buffers -- )
163 [ length ] [ >uint-array ] bi glDrawBuffers ;
165 MACRO: set-draw-buffers ( buffers -- )
166 words>values '[ _ (set-draw-buffers) ] ;
168 : gen-dlist ( -- id ) 1 glGenLists ;
170 : make-dlist ( type quot -- id )
171 [ gen-dlist ] 2dip '[ _ glNewList @ glEndList ] keep ; inline
173 : gl-translate ( point -- ) first2 0.0 glTranslated ;
175 : delete-dlist ( id -- ) 1 glDeleteLists ;
177 : with-translation ( loc quot -- )
178 [ [ gl-translate ] dip call ] do-matrix ; inline
180 : fix-coordinates ( point1 point2 -- x1 y2 x2 y2 )
181 [ first2 [ >fixnum ] bi@ ] bi@ ;
183 : gl-set-clip ( loc dim -- )
184 fix-coordinates glScissor ;
186 : gl-viewport ( loc dim -- )
187 fix-coordinates glViewport ;
189 : init-matrices ( -- )
190 #! Leaves with matrix mode GL_MODELVIEW
191 GL_PROJECTION glMatrixMode
193 GL_MODELVIEW glMatrixMode