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 } at "Unknown error" or ;
30 TUPLE: gl-error code string ;
33 glGetError dup 0 = [ drop ] [
34 dup error>string \ gl-error boa throw
37 : do-enabled ( what quot -- )
38 over glEnable dip glDisable ; inline
40 : do-enabled-client-state ( what quot -- )
41 over glEnableClientState dip glDisableClientState ; inline
43 : words>values ( word/value-seq -- value-seq )
46 : (all-enabled) ( seq quot -- )
47 over [ glEnable ] each dip [ glDisable ] each ; inline
49 : (all-enabled-client-state) ( seq quot -- )
50 [ dup [ glEnableClientState ] each ] dip
52 [ glDisableClientState ] each ; inline
54 MACRO: all-enabled ( seq quot -- )
55 [ words>values ] dip '[ _ _ (all-enabled) ] ;
57 MACRO: all-enabled-client-state ( seq quot -- )
58 [ words>values ] dip '[ _ _ (all-enabled-client-state) ] ;
60 : do-matrix ( quot -- )
61 glPushMatrix call glPopMatrix ; inline
63 : gl-material ( face pname params -- )
64 float-array{ } like glMaterialfv ;
66 : gl-vertex-pointer ( seq -- )
67 [ 2 GL_FLOAT 0 ] dip glVertexPointer ; inline
69 : gl-color-pointer ( seq -- )
70 [ 4 GL_FLOAT 0 ] dip glColorPointer ; inline
72 : gl-texture-coord-pointer ( seq -- )
73 [ 2 GL_FLOAT 0 ] dip glTexCoordPointer ; inline
75 : line-vertices ( a b -- )
76 [ first2 [ 0.5 + ] bi@ ] bi@ 4 float-array{ } nsequence
80 line-vertices GL_LINES 0 2 glDrawArrays ;
82 :: (rect-vertices) ( loc dim -- vertices )
83 #! We use GL_LINE_STRIP with a duplicated first vertex
84 #! instead of GL_LINE_LOOP to work around a bug in Apple's
91 x w + 0.3 - y h + 0.3 -
94 ] float-array{ } output>sequence ;
96 : rect-vertices ( loc dim -- )
97 (rect-vertices) gl-vertex-pointer ;
100 GL_LINE_STRIP 0 5 glDrawArrays ;
102 : gl-rect ( loc dim -- )
103 rect-vertices (gl-rect) ;
105 :: (fill-rect-vertices) ( loc dim -- vertices )
113 ] float-array{ } output>sequence ;
115 : fill-rect-vertices ( loc dim -- )
116 (fill-rect-vertices) gl-vertex-pointer ;
118 : (gl-fill-rect) ( -- )
119 GL_QUADS 0 4 glDrawArrays ;
121 : gl-fill-rect ( loc dim -- )
122 fill-rect-vertices (gl-fill-rect) ;
124 : do-attribs ( bits quot -- )
125 swap glPushAttrib call glPopAttrib ; inline
127 : (gen-gl-object) ( quot -- id )
128 [ 1 0 <uint> ] dip keep *uint ; inline
130 : gen-gl-buffer ( -- id )
131 [ glGenBuffers ] (gen-gl-object) ;
133 : (delete-gl-object) ( id quot -- )
134 [ 1 swap <uint> ] dip call ; inline
136 : delete-gl-buffer ( id -- )
137 [ glDeleteBuffers ] (delete-gl-object) ;
139 :: with-gl-buffer ( binding id quot -- )
140 binding id glBindBuffer
141 quot [ binding 0 glBindBuffer ] [ ] cleanup ; inline
143 : with-array-element-buffers ( array-buffer element-buffer quot -- )
144 [ GL_ELEMENT_ARRAY_BUFFER ] 2dip '[
145 GL_ARRAY_BUFFER swap _ with-gl-buffer
146 ] with-gl-buffer ; inline
148 : <gl-buffer> ( target data hint -- id )
151 [ [ byte-length ] keep ] dip glBufferData
155 : buffer-offset ( int -- alien )
158 : bind-texture-unit ( id target unit -- )
159 glActiveTexture swap glBindTexture gl-error ;
161 : (set-draw-buffers) ( buffers -- )
162 [ length ] [ >uint-array ] bi glDrawBuffers ;
164 MACRO: set-draw-buffers ( buffers -- )
165 words>values '[ _ (set-draw-buffers) ] ;
167 : gen-dlist ( -- id ) 1 glGenLists ;
169 : make-dlist ( type quot -- id )
170 [ gen-dlist ] 2dip '[ _ glNewList @ glEndList ] keep ; inline
172 : gl-translate ( point -- ) first2 0.0 glTranslated ;
174 : delete-dlist ( id -- ) 1 glDeleteLists ;
176 : with-translation ( loc quot -- )
177 [ [ gl-translate ] dip call ] do-matrix ; inline
179 : fix-coordinates ( point1 point2 -- x1 y2 x2 y2 )
180 [ first2 [ >fixnum ] bi@ ] bi@ ;
182 : gl-set-clip ( loc dim -- )
183 fix-coordinates glScissor ;
185 : gl-viewport ( loc dim -- )
186 fix-coordinates glViewport ;
188 : init-matrices ( -- )
189 #! Leaves with matrix mode GL_MODELVIEW
190 GL_PROJECTION glMatrixMode
192 GL_MODELVIEW glMatrixMode