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