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 opengl.glu 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 ;
20 glGetError dup zero? [
21 "GL error: " over gluErrorString append throw
24 : do-enabled ( what quot -- )
25 over glEnable dip glDisable ; inline
27 : do-enabled-client-state ( what quot -- )
28 over glEnableClientState dip glDisableClientState ; inline
30 : words>values ( word/value-seq -- value-seq )
33 : (all-enabled) ( seq quot -- )
34 over [ glEnable ] each dip [ glDisable ] each ; inline
36 : (all-enabled-client-state) ( seq quot -- )
37 [ dup [ glEnableClientState ] each ] dip
39 [ glDisableClientState ] each ; inline
41 MACRO: all-enabled ( seq quot -- )
42 [ words>values ] dip '[ _ _ (all-enabled) ] ;
44 MACRO: all-enabled-client-state ( seq quot -- )
45 [ words>values ] dip '[ _ _ (all-enabled-client-state) ] ;
47 : do-matrix ( mode quot -- )
48 swap [ glMatrixMode glPushMatrix call ] keep
49 glMatrixMode glPopMatrix ; inline
51 : gl-material ( face pname params -- )
52 float-array{ } like glMaterialfv ;
54 : gl-vertex-pointer ( seq -- )
55 [ 2 GL_FLOAT 0 ] dip glVertexPointer ; inline
57 : gl-color-pointer ( seq -- )
58 [ 4 GL_FLOAT 0 ] dip glColorPointer ; inline
60 : gl-texture-coord-pointer ( seq -- )
61 [ 2 GL_FLOAT 0 ] dip glTexCoordPointer ; inline
63 : line-vertices ( a b -- )
64 [ first2 [ 0.5 + ] bi@ ] bi@ 4 float-array{ } nsequence
68 line-vertices GL_LINES 0 2 glDrawArrays ;
70 :: (rect-vertices) ( loc dim -- vertices )
71 #! We use GL_LINE_STRIP with a duplicated first vertex
72 #! instead of GL_LINE_LOOP to work around a bug in Apple's
79 x w + 0.3 - y h + 0.3 -
82 ] float-array{ } output>sequence ;
84 : rect-vertices ( loc dim -- )
85 (rect-vertices) gl-vertex-pointer ;
88 GL_LINE_STRIP 0 5 glDrawArrays ;
90 : gl-rect ( loc dim -- )
91 rect-vertices (gl-rect) ;
93 :: (fill-rect-vertices) ( loc dim -- vertices )
101 ] float-array{ } output>sequence ;
103 : fill-rect-vertices ( loc dim -- )
104 (fill-rect-vertices) gl-vertex-pointer ;
106 : (gl-fill-rect) ( -- )
107 GL_QUADS 0 4 glDrawArrays ;
109 : gl-fill-rect ( loc dim -- )
110 fill-rect-vertices (gl-fill-rect) ;
112 : do-attribs ( bits quot -- )
113 swap glPushAttrib call glPopAttrib ; inline
115 : (gen-gl-object) ( quot -- id )
116 [ 1 0 <uint> ] dip keep *uint ; inline
118 : gen-gl-buffer ( -- id )
119 [ glGenBuffers ] (gen-gl-object) ;
121 : (delete-gl-object) ( id quot -- )
122 [ 1 swap <uint> ] dip call ; inline
124 : delete-gl-buffer ( id -- )
125 [ glDeleteBuffers ] (delete-gl-object) ;
127 :: with-gl-buffer ( binding id quot -- )
128 binding id glBindBuffer
129 quot [ binding 0 glBindBuffer ] [ ] cleanup ; inline
131 : with-array-element-buffers ( array-buffer element-buffer quot -- )
132 [ GL_ELEMENT_ARRAY_BUFFER ] 2dip '[
133 GL_ARRAY_BUFFER swap _ with-gl-buffer
134 ] with-gl-buffer ; inline
136 : <gl-buffer> ( target data hint -- id )
139 [ [ byte-length ] keep ] dip glBufferData
143 : buffer-offset ( int -- alien )
146 : bind-texture-unit ( id target unit -- )
147 glActiveTexture swap glBindTexture gl-error ;
149 : (set-draw-buffers) ( buffers -- )
150 [ length ] [ >uint-array ] bi glDrawBuffers ;
152 MACRO: set-draw-buffers ( buffers -- )
153 words>values '[ _ (set-draw-buffers) ] ;
155 : gl-look-at ( eye focus up -- )
156 [ first3 ] tri@ gluLookAt ;
158 : gen-dlist ( -- id ) 1 glGenLists ;
160 : make-dlist ( type quot -- id )
161 [ gen-dlist ] 2dip '[ _ glNewList @ glEndList ] keep ; inline
163 : gl-translate ( point -- ) first2 0.0 glTranslated ;
165 : delete-dlist ( id -- ) 1 glDeleteLists ;
167 : with-translation ( loc quot -- )
168 GL_MODELVIEW [ [ gl-translate ] dip call ] do-matrix ; inline
170 : fix-coordinates ( point1 point2 -- x1 y2 x2 y2 )
171 [ first2 [ >fixnum ] bi@ ] bi@ ;
173 : gl-set-clip ( loc dim -- )
174 fix-coordinates glScissor ;
176 : gl-viewport ( loc dim -- )
177 fix-coordinates glViewport ;
179 : init-matrices ( -- )
180 GL_PROJECTION glMatrixMode
182 GL_MODELVIEW glMatrixMode