]> gitweb.factorcode.org Git - factor.git/blob - basis/opengl/opengl.factor
factor: Rename MACRO: and MACRO: to have quot as the output in stack effects.
[factor.git] / basis / opengl / opengl.factor
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 alien.data assocs colors
6 combinators.smart continuations fry init kernel locals macros
7 math namespaces opengl.gl sequences sequences.generalizations
8 specialized-arrays words ;
9 FROM: alien.c-types => float ;
10 SPECIALIZED-ARRAY: float
11 SPECIALIZED-ARRAY: uint
12 IN: opengl
13
14 SYMBOL: gl-scale-factor
15
16 : gl-color ( color -- ) >rgba-components glColor4d ; inline
17
18 : gl-clear-color ( color -- ) >rgba-components glClearColor ;
19
20 : gl-clear ( color -- )
21     gl-clear-color GL_COLOR_BUFFER_BIT glClear ;
22
23 : error>string ( n -- string )
24     H{
25         { 0x0000 "No error" }
26         { 0x0501 "Invalid value" }
27         { 0x0500 "Invalid enumerant" }
28         { 0x0502 "Invalid operation" }
29         { 0x0503 "Stack overflow" }
30         { 0x0504 "Stack underflow" }
31         { 0x0505 "Out of memory" }
32         { 0x0506 "Invalid framebuffer operation" }
33     } at "Unknown error" or ;
34
35 TUPLE: gl-error-tuple function code string ;
36
37 : <gl-error> ( function code -- gl-error )
38     dup error>string \ gl-error-tuple boa ; inline
39
40 : gl-error-code ( -- code/f )
41     glGetError dup 0 = [ drop f ] when ; inline
42
43 : (gl-error) ( function -- )
44     gl-error-code [ <gl-error> throw ] [ drop ] if* ;
45
46 : gl-error ( -- )
47     f (gl-error) ; inline
48
49 : do-enabled ( what quot -- )
50     over glEnable dip glDisable ; inline
51
52 : do-enabled-client-state ( what quot -- )
53     over glEnableClientState dip glDisableClientState ; inline
54
55 : words>values ( word/value-seq -- value-seq )
56     [ dup word? [ execute( -- x ) ] when ] map ;
57
58 : (all-enabled) ( seq quot -- )
59     [ dup [ glEnable ] each ] dip
60     dip
61     [ glDisable ] each ; inline
62
63 : (all-enabled-client-state) ( seq quot -- )
64     [ dup [ glEnableClientState ] each ] dip
65     dip
66     [ glDisableClientState ] each ; inline
67
68 MACRO: all-enabled ( seq quot -- quot )
69     [ words>values ] dip '[ _ _ (all-enabled) ] ;
70
71 MACRO: all-enabled-client-state ( seq quot -- quot )
72     [ words>values ] dip '[ _ _ (all-enabled-client-state) ] ;
73
74 : do-matrix ( quot -- )
75     glPushMatrix call glPopMatrix ; inline
76
77 : gl-material ( face pname params -- )
78     float-array{ } like glMaterialfv ;
79
80 : gl-vertex-pointer ( seq -- )
81     [ 2 GL_FLOAT 0 ] dip glVertexPointer ; inline
82
83 : gl-color-pointer ( seq -- )
84     [ 4 GL_FLOAT 0 ] dip glColorPointer ; inline
85
86 : gl-texture-coord-pointer ( seq -- )
87     [ 2 GL_FLOAT 0 ] dip glTexCoordPointer ; inline
88
89 : (line-vertices) ( a b -- vertices )
90     [ first2 [ 0.3 + ] bi@ ] bi@ 4 float-array{ } nsequence ;
91
92 : line-vertices ( a b -- )
93     (line-vertices) gl-vertex-pointer ;
94
95 : gl-line ( a b -- )
96     line-vertices GL_LINES 0 2 glDrawArrays ;
97
98 :: (rect-vertices) ( loc dim -- vertices )
99     #! We use GL_LINE_STRIP with a duplicated first vertex
100     #! instead of GL_LINE_LOOP to work around a bug in Apple's
101     #! X3100 driver.
102     loc first2 [ 0.3 + ] bi@ :> ( x y )
103     dim first2 [ 0.6 - ] bi@ :> ( w h )
104     [
105         x           y
106         x w +       y
107         x w +       y h +
108         x           y h +
109         x           y
110     ] float-array{ } output>sequence ;
111
112 : rect-vertices ( loc dim -- )
113     (rect-vertices) gl-vertex-pointer ;
114
115 : (gl-rect) ( -- )
116     GL_LINE_STRIP 0 5 glDrawArrays ;
117
118 : gl-rect ( loc dim -- )
119     rect-vertices (gl-rect) ;
120
121 :: (fill-rect-vertices) ( loc dim -- vertices )
122     loc first2 :> ( x y )
123     dim first2 :> ( w h )
124     [
125         x      y
126         x w +  y
127         x w +  y h +
128         x      y h +
129     ] float-array{ } output>sequence ;
130
131 : fill-rect-vertices ( loc dim -- )
132     (fill-rect-vertices) gl-vertex-pointer ;
133
134 : (gl-fill-rect) ( -- )
135     GL_QUADS 0 4 glDrawArrays ;
136
137 : gl-fill-rect ( loc dim -- )
138     fill-rect-vertices (gl-fill-rect) ;
139
140 : do-attribs ( bits quot -- )
141     swap glPushAttrib call glPopAttrib ; inline
142
143 : (gen-gl-object) ( quot -- id )
144     [ 1 { uint } ] dip with-out-parameters ; inline
145
146 : (delete-gl-object) ( id quot -- )
147     [ 1 swap uint <ref> ] dip call ; inline
148
149 : gen-gl-buffer ( -- id )
150     [ glGenBuffers ] (gen-gl-object) ;
151
152 : delete-gl-buffer ( id -- )
153     [ glDeleteBuffers ] (delete-gl-object) ;
154
155 :: with-gl-buffer ( binding id quot -- )
156     binding id glBindBuffer
157     quot [ binding 0 glBindBuffer ] [ ] cleanup ; inline
158
159 : with-array-element-buffers ( array-buffer element-buffer quot -- )
160     [ GL_ELEMENT_ARRAY_BUFFER ] 2dip '[
161         GL_ARRAY_BUFFER swap _ with-gl-buffer
162     ] with-gl-buffer ; inline
163
164 : gen-vertex-array ( -- id )
165     [ glGenVertexArrays ] (gen-gl-object) ;
166
167 : delete-vertex-array ( id -- )
168     [ glDeleteVertexArrays ] (delete-gl-object) ;
169
170 :: with-vertex-array ( id quot -- )
171     id glBindVertexArray
172     quot [ 0 glBindVertexArray ] [ ] cleanup ; inline
173
174 : <gl-buffer> ( target data hint -- id )
175     pick gen-gl-buffer [
176         [
177             [ [ byte-length ] keep ] dip glBufferData
178         ] with-gl-buffer
179     ] keep ;
180
181 : buffer-offset ( int -- alien )
182     <alien> ; inline
183
184 : bind-texture-unit ( id target unit -- )
185     glActiveTexture swap glBindTexture gl-error ;
186
187 : (set-draw-buffers) ( buffers -- )
188     [ length ] [ uint >c-array ] bi glDrawBuffers ;
189
190 MACRO: set-draw-buffers ( buffers -- quot )
191     words>values '[ _ (set-draw-buffers) ] ;
192
193 : gen-dlist ( -- id ) 1 glGenLists ;
194
195 : make-dlist ( type quot -- id )
196     [ gen-dlist ] 2dip '[ _ glNewList @ glEndList ] keep ; inline
197
198 : gl-translate ( point -- ) first2 0.0 glTranslated ;
199
200 : delete-dlist ( id -- ) 1 glDeleteLists ;
201
202 : with-translation ( loc quot -- )
203     [ [ gl-translate ] dip call ] do-matrix ; inline
204
205 : gl-scale ( m -- n )
206     gl-scale-factor get-global [ * ] when* ; inline
207
208 : gl-unscale ( m -- n )
209     gl-scale-factor get-global [ / ] when* ; inline
210
211 : fix-coordinates ( point1 point2 -- x1 y1 x2 y2 )
212     [ first2 [ gl-scale >fixnum ] bi@ ] bi@ ;
213
214 : gl-set-clip ( loc dim -- )
215     fix-coordinates glScissor ;
216
217 : gl-viewport ( loc dim -- )
218     fix-coordinates glViewport ;
219
220 : init-matrices ( -- )
221     #! Leaves with matrix mode GL_MODELVIEW
222     GL_PROJECTION glMatrixMode
223     glLoadIdentity
224     GL_MODELVIEW glMatrixMode
225     glLoadIdentity ;
226
227 [ f gl-scale-factor set-global ] "opengl" add-startup-hook