]> gitweb.factorcode.org Git - factor.git/blob - basis/opengl/opengl.factor
Merge branch 'inlinec' of git://github.com/jedahu/factor
[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 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
10 specialized-arrays.float specialized-arrays.uint
11 tools.annotations tools.annotations.private compiler.units ;
12 IN: opengl
13
14 : gl-color ( color -- ) >rgba-components glColor4d ; inline
15
16 : gl-clear-color ( color -- ) >rgba-components glClearColor ;
17
18 : gl-clear ( color -- )
19     gl-clear-color GL_COLOR_BUFFER_BIT glClear ;
20
21 : error>string ( n -- string )
22     H{
23         { HEX: 0 "No error" }
24         { HEX: 0501 "Invalid value" }
25         { HEX: 0500 "Invalid enumerant" }
26         { HEX: 0502 "Invalid operation" }
27         { HEX: 0503 "Stack overflow" }
28         { HEX: 0504 "Stack underflow" }
29         { HEX: 0505 "Out of memory" }
30         { HEX: 0506 "Invalid framebuffer operation" }
31     } at "Unknown error" or ;
32
33 TUPLE: gl-error function code string ;
34
35 TUPLE: gl-error-log
36     { function word initial: t }
37     { error gl-error }
38     { timestamp timestamp } ;
39
40 gl-error-log [ V{ } clone ] initialize
41
42 : <gl-error> ( function code -- gl-error )
43     dup error>string \ gl-error boa ; inline
44
45 : <gl-error-log> ( function code -- gl-error-log )
46     [ dup ] dip <gl-error> now gl-error-log boa ;
47
48 : gl-error-code ( -- code/f )
49     glGetError dup 0 = [ drop f ] when ; inline
50
51 : (gl-error) ( function -- )
52     gl-error-code [ <gl-error> throw ] [ drop ] if* ;
53
54 : gl-error ( -- )
55     f (gl-error) ; inline
56
57 : log-gl-error ( function -- )
58     gl-error-code [ <gl-error-log> gl-error-log get push ] [ drop ] if* ;
59
60 : gl-function? ( word -- ? )
61     name>> { [ "glGetError" = not ] [ "gl" head? ] [ third LETTER? ] } 1&& ;
62
63 : gl-functions ( -- words )
64     "opengl.gl" vocab words [ gl-function? ] filter ;
65
66 : annotate-gl-functions ( quot -- )
67     [
68         [ gl-functions ] dip [ [ dup ] dip curry (annotate) ] curry each
69     ] with-compilation-unit ;
70
71 : reset-gl-functions ( -- )
72     [ gl-functions [ (reset) ] each ] with-compilation-unit ;
73
74 : clear-gl-error-log ( -- )
75     V{ } clone gl-error-log set ;
76
77 : throw-gl-errors ( -- )
78     [ '[ @ _ (gl-error) ] ] annotate-gl-functions ;
79
80 : log-gl-errors ( -- )
81     [ '[ @ _ log-gl-error ] ] annotate-gl-functions ;
82
83 : do-enabled ( what quot -- )
84     over glEnable dip glDisable ; inline
85
86 : do-enabled-client-state ( what quot -- )
87     over glEnableClientState dip glDisableClientState ; inline
88
89 : words>values ( word/value-seq -- value-seq )
90     [ ?execute ] map ;
91
92 : (all-enabled) ( seq quot -- )
93     over [ glEnable ] each dip [ glDisable ] each ; inline
94
95 : (all-enabled-client-state) ( seq quot -- )
96     [ dup [ glEnableClientState ] each ] dip
97     dip
98     [ glDisableClientState ] each ; inline
99
100 MACRO: all-enabled ( seq quot -- )
101     [ words>values ] dip '[ _ _ (all-enabled) ] ;
102
103 MACRO: all-enabled-client-state ( seq quot -- )
104     [ words>values ] dip '[ _ _ (all-enabled-client-state) ] ;
105
106 : do-matrix ( quot -- )
107     glPushMatrix call glPopMatrix ; inline
108
109 : gl-material ( face pname params -- )
110     float-array{ } like glMaterialfv ;
111
112 : gl-vertex-pointer ( seq -- )
113     [ 2 GL_FLOAT 0 ] dip glVertexPointer ; inline
114
115 : gl-color-pointer ( seq -- )
116     [ 4 GL_FLOAT 0 ] dip glColorPointer ; inline
117
118 : gl-texture-coord-pointer ( seq -- )
119     [ 2 GL_FLOAT 0 ] dip glTexCoordPointer ; inline
120
121 : line-vertices ( a b -- )
122     [ first2 [ 0.5 + ] bi@ ] bi@ 4 float-array{ } nsequence
123     gl-vertex-pointer ;
124
125 : gl-line ( a b -- )
126     line-vertices GL_LINES 0 2 glDrawArrays ;
127
128 :: (rect-vertices) ( loc dim -- vertices )
129     #! We use GL_LINE_STRIP with a duplicated first vertex
130     #! instead of GL_LINE_LOOP to work around a bug in Apple's
131     #! X3100 driver.
132     loc first2 :> y :> x
133     dim first2 :> h :> w
134     [
135         x 0.5 +     y 0.5 +
136         x w + 0.3 - y 0.5 +
137         x w + 0.3 - y h + 0.3 -
138         x           y h + 0.3 -
139         x 0.5 +     y 0.5 +
140     ] float-array{ } output>sequence ;
141
142 : rect-vertices ( loc dim -- )
143     (rect-vertices) gl-vertex-pointer ;
144
145 : (gl-rect) ( -- )
146     GL_LINE_STRIP 0 5 glDrawArrays ;
147
148 : gl-rect ( loc dim -- )
149     rect-vertices (gl-rect) ;
150
151 :: (fill-rect-vertices) ( loc dim -- vertices )
152     loc first2 :> y :> x
153     dim first2 :> h :> w
154     [
155         x      y
156         x w +  y
157         x w +  y h +
158         x      y h +
159     ] float-array{ } output>sequence ;
160
161 : fill-rect-vertices ( loc dim -- )
162     (fill-rect-vertices) gl-vertex-pointer ;
163
164 : (gl-fill-rect) ( -- )
165     GL_QUADS 0 4 glDrawArrays ;
166
167 : gl-fill-rect ( loc dim -- )
168     fill-rect-vertices (gl-fill-rect) ;
169
170 : do-attribs ( bits quot -- )
171     swap glPushAttrib call glPopAttrib ; inline
172
173 : (gen-gl-object) ( quot -- id )
174     [ 1 0 <uint> ] dip keep *uint ; inline
175
176 : (delete-gl-object) ( id quot -- )
177     [ 1 swap <uint> ] dip call ; inline
178
179 : gen-gl-buffer ( -- id )
180     [ glGenBuffers ] (gen-gl-object) ;
181
182 : delete-gl-buffer ( id -- )
183     [ glDeleteBuffers ] (delete-gl-object) ;
184
185 :: with-gl-buffer ( binding id quot -- )
186     binding id glBindBuffer
187     quot [ binding 0 glBindBuffer ] [ ] cleanup ; inline
188
189 : with-array-element-buffers ( array-buffer element-buffer quot -- )
190     [ GL_ELEMENT_ARRAY_BUFFER ] 2dip '[
191         GL_ARRAY_BUFFER swap _ with-gl-buffer
192     ] with-gl-buffer ; inline
193
194 : gen-vertex-array ( -- id )
195     [ glGenVertexArrays ] (gen-gl-object) ;
196
197 : delete-vertex-array ( id -- )
198     [ glDeleteVertexArrays ] (delete-gl-object) ;
199
200 :: with-vertex-array ( id quot -- )
201     id glBindVertexArray
202     quot [ 0 glBindVertexArray ] [ ] cleanup ; inline
203
204 : <gl-buffer> ( target data hint -- id )
205     pick gen-gl-buffer [
206         [
207             [ [ byte-length ] keep ] dip glBufferData
208         ] with-gl-buffer
209     ] keep ;
210
211 : buffer-offset ( int -- alien )
212     <alien> ; inline
213
214 : bind-texture-unit ( id target unit -- )
215     glActiveTexture swap glBindTexture gl-error ;
216
217 : (set-draw-buffers) ( buffers -- )
218     [ length ] [ >uint-array ] bi glDrawBuffers ;
219
220 MACRO: set-draw-buffers ( buffers -- )
221     words>values '[ _ (set-draw-buffers) ] ;
222
223 : gen-dlist ( -- id ) 1 glGenLists ;
224
225 : make-dlist ( type quot -- id )
226     [ gen-dlist ] 2dip '[ _ glNewList @ glEndList ] keep ; inline
227
228 : gl-translate ( point -- ) first2 0.0 glTranslated ;
229
230 : delete-dlist ( id -- ) 1 glDeleteLists ;
231
232 : with-translation ( loc quot -- )
233     [ [ gl-translate ] dip call ] do-matrix ; inline
234
235 : fix-coordinates ( point1 point2 -- x1 y2 x2 y2 )
236     [ first2 [ >fixnum ] bi@ ] bi@ ;
237
238 : gl-set-clip ( loc dim -- )
239     fix-coordinates glScissor ;
240
241 : gl-viewport ( loc dim -- )
242     fix-coordinates glViewport ;
243
244 : init-matrices ( -- )
245     #! Leaves with matrix mode GL_MODELVIEW
246     GL_PROJECTION glMatrixMode
247     glLoadIdentity
248     GL_MODELVIEW glMatrixMode
249     glLoadIdentity ;