]> gitweb.factorcode.org Git - factor.git/blob - basis/opengl/opengl.factor
bb63cab67834b5792ed66d68cbe42a7a0b2082d2
[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 init io kernel math
7 math.functions math.parser namespaces opengl.gl sequences
8 sequences.generalizations specialized-arrays system 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 : throw-gl-error? ( -- ? )
44     os macosx? [
45         ! This is kind of terrible, but we are having
46         ! problems on Mac OS X 10.11 where the
47         ! default framebuffer seems to be initialized
48         ! asynchronously or something, so we should
49         ! just log these for now in (gl-error).
50         GL_FRAMEBUFFER glCheckFramebufferStatus
51         GL_FRAMEBUFFER_UNDEFINED = not
52     ] [ t ] if ;
53
54 : (gl-error) ( function -- )
55     gl-error-code [
56         throw-gl-error? [
57             <gl-error> throw
58         ] [
59             [
60                 [ number>string ] [ error>string ] bi ": " glue
61                 "OpenGL error: " prepend print flush drop
62             ] with-global
63         ] if
64     ] [ drop ] if* ;
65
66 : gl-error ( -- )
67     f (gl-error) ; inline
68
69 : do-enabled ( what quot -- )
70     over glEnable dip glDisable ; inline
71
72 : do-enabled-client-state ( what quot -- )
73     over glEnableClientState dip glDisableClientState ; inline
74
75 : words>values ( word/value-seq -- value-seq )
76     [ dup word? [ execute( -- x ) ] when ] map ;
77
78 : (all-enabled) ( seq quot -- )
79     [ dup [ glEnable ] each ] dip
80     dip
81     [ glDisable ] each ; inline
82
83 : (all-enabled-client-state) ( seq quot -- )
84     [ dup [ glEnableClientState ] each ] dip
85     dip
86     [ glDisableClientState ] each ; inline
87
88 MACRO: all-enabled ( seq quot -- quot )
89     [ words>values ] dip '[ _ _ (all-enabled) ] ;
90
91 MACRO: all-enabled-client-state ( seq quot -- quot )
92     [ words>values ] dip '[ _ _ (all-enabled-client-state) ] ;
93
94 : do-matrix ( quot -- )
95     glPushMatrix call glPopMatrix ; inline
96
97 : gl-material ( face pname params -- )
98     float-array{ } like glMaterialfv ;
99
100 : gl-vertex-pointer ( seq -- )
101     [ 2 GL_FLOAT 0 ] dip glVertexPointer ; inline
102
103 : gl-color-pointer ( seq -- )
104     [ 4 GL_FLOAT 0 ] dip glColorPointer ; inline
105
106 : gl-texture-coord-pointer ( seq -- )
107     [ 2 GL_FLOAT 0 ] dip glTexCoordPointer ; inline
108
109 : (line-vertices) ( a b -- vertices )
110     [ first2 [ 0.3 + ] bi@ ] bi@ 4 float-array{ } nsequence ;
111
112 : line-vertices ( a b -- )
113     (line-vertices) gl-vertex-pointer ;
114
115 : gl-line ( a b -- )
116     line-vertices GL_LINES 0 2 glDrawArrays ;
117
118 :: (rect-vertices) ( loc dim -- vertices )
119     ! We use GL_LINE_STRIP with a duplicated first vertex
120     ! instead of GL_LINE_LOOP to work around a bug in Apple's
121     ! X3100 driver.
122     loc first2 [ 0.3 + ] bi@ :> ( x y )
123     dim first2 [ 0.6 - ] bi@ :> ( w h )
124     [
125         x           y
126         x w +       y
127         x w +       y h +
128         x           y h +
129         x           y
130     ] float-array{ } output>sequence ;
131
132 : rect-vertices ( loc dim -- )
133     (rect-vertices) gl-vertex-pointer ;
134
135 : (gl-rect) ( -- )
136     GL_LINE_STRIP 0 5 glDrawArrays ;
137
138 : gl-rect ( loc dim -- )
139     rect-vertices (gl-rect) ;
140
141 :: (fill-rect-vertices) ( loc dim -- vertices )
142     loc first2 :> ( x y )
143     dim first2 :> ( w h )
144     [
145         x      y
146         x w +  y
147         x w +  y h +
148         x      y h +
149     ] float-array{ } output>sequence ;
150
151 : fill-rect-vertices ( loc dim -- )
152     (fill-rect-vertices) gl-vertex-pointer ;
153
154 : (gl-fill-rect) ( -- )
155     GL_QUADS 0 4 glDrawArrays ;
156
157 : gl-fill-rect ( loc dim -- )
158     fill-rect-vertices (gl-fill-rect) ;
159
160 : do-attribs ( bits quot -- )
161     swap glPushAttrib call glPopAttrib ; inline
162
163 : (gen-gl-object) ( quot -- id )
164     [ 1 { uint } ] dip with-out-parameters ; inline
165
166 : (delete-gl-object) ( id quot -- )
167     [ 1 swap uint <ref> ] dip call ; inline
168
169 : gen-gl-buffer ( -- id )
170     [ glGenBuffers ] (gen-gl-object) ;
171
172 : delete-gl-buffer ( id -- )
173     [ glDeleteBuffers ] (delete-gl-object) ;
174
175 :: with-gl-buffer ( binding id quot -- )
176     binding id glBindBuffer
177     quot [ binding 0 glBindBuffer ] finally ; inline
178
179 : with-array-element-buffers ( array-buffer element-buffer quot -- )
180     [ GL_ELEMENT_ARRAY_BUFFER ] 2dip '[
181         GL_ARRAY_BUFFER swap _ with-gl-buffer
182     ] with-gl-buffer ; inline
183
184 : gen-vertex-array ( -- id )
185     [ glGenVertexArrays ] (gen-gl-object) ;
186
187 : delete-vertex-array ( id -- )
188     [ glDeleteVertexArrays ] (delete-gl-object) ;
189
190 :: with-vertex-array ( id quot -- )
191     id glBindVertexArray
192     quot [ 0 glBindVertexArray ] finally ; inline
193
194 : <gl-buffer> ( target data hint -- id )
195     pick gen-gl-buffer [
196         [
197             [ [ byte-length ] keep ] dip glBufferData
198         ] with-gl-buffer
199     ] keep ;
200
201 : buffer-offset ( int -- alien )
202     <alien> ; inline
203
204 : bind-texture-unit ( id target unit -- )
205     glActiveTexture swap glBindTexture gl-error ;
206
207 : (set-draw-buffers) ( buffers -- )
208     [ length ] [ uint >c-array ] bi glDrawBuffers ;
209
210 MACRO: set-draw-buffers ( buffers -- quot )
211     words>values '[ _ (set-draw-buffers) ] ;
212
213 : gen-dlist ( -- id ) 1 glGenLists ;
214
215 : make-dlist ( type quot -- id )
216     [ gen-dlist ] 2dip '[ _ glNewList @ glEndList ] keep ; inline
217
218 : gl-translate ( point -- ) first2 0.0 glTranslated ;
219
220 : delete-dlist ( id -- ) 1 glDeleteLists ;
221
222 : with-translation ( loc quot -- )
223     [ [ gl-translate ] dip call ] do-matrix ; inline
224
225 : gl-scale ( m -- n )
226     gl-scale-factor get-global [ * ] when* ; inline
227
228 : gl-unscale ( m -- n )
229     gl-scale-factor get-global [ / ] when* ; inline
230
231 : gl-floor ( m -- n )
232     gl-scale floor gl-unscale ; inline
233
234 : gl-ceiling ( m -- n )
235     gl-scale ceiling gl-unscale ; inline
236
237 : gl-round ( m -- n )
238     gl-scale round gl-unscale ; inline
239
240 : fix-coordinates ( point1 point2 -- x1 y1 x2 y2 )
241     [ first2 [ gl-scale >fixnum ] bi@ ] bi@ ;
242
243 : gl-set-clip ( loc dim -- )
244     fix-coordinates glScissor ;
245
246 : gl-viewport ( loc dim -- )
247     fix-coordinates glViewport ;
248
249 : init-matrices ( -- )
250     ! Leaves with matrix mode GL_MODELVIEW
251     GL_PROJECTION glMatrixMode
252     glLoadIdentity
253     GL_MODELVIEW glMatrixMode
254     glLoadIdentity ;
255
256 STARTUP-HOOK: [ f gl-scale-factor set-global ]