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