]> gitweb.factorcode.org Git - factor.git/blob - basis/opengl/opengl.factor
Merge branch 'master' into experimental (untested!)
[factor.git] / basis / opengl / opengl.factor
1 ! Copyright (C) 2005, 2008 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 continuations kernel libc math macros
6 namespaces math.vectors math.constants math.functions
7 math.parser opengl.gl opengl.glu combinators arrays sequences
8 splitting words byte-arrays assocs colors accessors
9 generalizations locals specialized-arrays.float
10 specialized-arrays.uint ;
11 IN: opengl
12
13 : color>raw ( object -- r g b a )
14     >rgba { [ red>> ] [ green>> ] [ blue>> ] [ alpha>> ] } cleave ; inline
15
16 : gl-color ( color -- ) color>raw glColor4d ; inline
17
18 : gl-clear-color ( color -- ) color>raw glClearColor ;
19
20 : gl-clear ( color -- )
21     gl-clear-color GL_COLOR_BUFFER_BIT glClear ;
22
23 : gl-error ( -- )
24     glGetError dup zero? [
25         "GL error: " over gluErrorString append throw
26     ] unless drop ;
27
28 : do-enabled ( what quot -- )
29     over glEnable dip glDisable ; inline
30
31 : do-enabled-client-state ( what quot -- )
32     over glEnableClientState dip glDisableClientState ; inline
33
34 : words>values ( word/value-seq -- value-seq )
35     [ dup word? [ execute ] when ] map ;
36
37 : (all-enabled) ( seq quot -- )
38     over [ glEnable ] each dip [ glDisable ] each ; inline
39
40 : (all-enabled-client-state) ( seq quot -- )
41     [ dup [ glEnableClientState ] each ] dip
42     dip
43     [ glDisableClientState ] each ; inline
44
45 MACRO: all-enabled ( seq quot -- )
46     [ words>values ] dip [ (all-enabled) ] 2curry ;
47
48 MACRO: all-enabled-client-state ( seq quot -- )
49     [ words>values ] dip [ (all-enabled-client-state) ] 2curry ;
50
51 : do-matrix ( mode quot -- )
52     swap [ glMatrixMode glPushMatrix call ] keep
53     glMatrixMode glPopMatrix ; inline
54
55 : gl-material ( face pname params -- )
56     float-array{ } like underlying>> glMaterialfv ;
57
58 : gl-vertex-pointer ( seq -- )
59     [ 2 GL_FLOAT 0 ] dip underlying>> glVertexPointer ; inline
60
61 : gl-color-pointer ( seq -- )
62     [ 4 GL_FLOAT 0 ] dip underlying>> glColorPointer ; inline
63
64 : gl-texture-coord-pointer ( seq -- )
65     [ 2 GL_FLOAT 0 ] dip underlying>> glTexCoordPointer ; inline
66
67 : line-vertices ( a b -- )
68     [ first2 [ 0.5 + ] bi@ ] bi@ 4 float-array{ } nsequence
69     gl-vertex-pointer ;
70
71 : gl-line ( a b -- )
72     line-vertices GL_LINES 0 2 glDrawArrays ;
73
74 : (rect-vertices) ( dim -- vertices )
75     #! We use GL_LINE_STRIP with a duplicated first vertex
76     #! instead of GL_LINE_LOOP to work around a bug in Apple's
77     #! X3100 driver.
78     {
79         [ drop 0.5 0.5 ]
80         [ first 0.3 - 0.5 ]
81         [ [ first 0.3 - ] [ second 0.3 - ] bi ]
82         [ second 0.3 - 0.5 swap ]
83         [ drop 0.5 0.5 ]
84     } cleave 10 float-array{ } nsequence ;
85
86 : rect-vertices ( dim -- )
87     (rect-vertices) gl-vertex-pointer ;
88
89 : (gl-rect) ( -- )
90     GL_LINE_STRIP 0 5 glDrawArrays ;
91
92 : gl-rect ( dim -- )
93     rect-vertices (gl-rect) ;
94
95 : (fill-rect-vertices) ( dim -- vertices )
96     {
97         [ drop 0 0 ]
98         [ first 0 ]
99         [ first2 ]
100         [ second 0 swap ]
101     } cleave 8 float-array{ } nsequence ;
102
103 : fill-rect-vertices ( dim -- )
104     (fill-rect-vertices) gl-vertex-pointer ;
105
106 : (gl-fill-rect) ( -- )
107     GL_QUADS 0 4 glDrawArrays ;
108
109 : gl-fill-rect ( dim -- )
110     fill-rect-vertices (gl-fill-rect) ;
111
112 : circle-steps ( steps -- angles )
113     dup length v/n 2 pi * v*n ;
114
115 : unit-circle ( angles -- points1 points2 )
116     [ [ sin ] map ] [ [ cos ] map ] bi ;
117
118 : adjust-points ( points1 points2 -- points1' points2' )
119     [ [ 1 + 0.5 * ] map ] bi@ ;
120
121 : scale-points ( loc dim points1 points2 -- points )
122     zip [ v* ] with map [ v+ ] with map ;
123
124 : circle-points ( loc dim steps -- points )
125     circle-steps unit-circle adjust-points scale-points ;
126
127 : close-path ( points -- points' )
128     dup first suffix ;
129
130 : circle-vertices ( loc dim steps -- vertices )
131     #! We use GL_LINE_STRIP with a duplicated first vertex
132     #! instead of GL_LINE_LOOP to work around a bug in Apple's
133     #! X3100 driver.
134     circle-points close-path concat >float-array ;
135
136 : fill-circle-vertices ( loc dim steps -- vertices )
137     circle-points concat >float-array ;
138
139 : (gen-gl-object) ( quot -- id )
140     [ 1 0 <uint> ] dip keep *uint ; inline
141
142 : gen-texture ( -- id )
143     [ glGenTextures ] (gen-gl-object) ;
144
145 : gen-gl-buffer ( -- id )
146     [ glGenBuffers ] (gen-gl-object) ;
147
148 : (delete-gl-object) ( id quot -- )
149     [ 1 swap <uint> ] dip call ; inline
150
151 : delete-texture ( id -- )
152     [ glDeleteTextures ] (delete-gl-object) ;
153
154 : delete-gl-buffer ( id -- )
155     [ glDeleteBuffers ] (delete-gl-object) ;
156
157 : with-gl-buffer ( binding id quot -- )
158     -rot dupd glBindBuffer
159     [ slip ] [ 0 glBindBuffer ] [ ] cleanup ; inline
160
161 : with-array-element-buffers ( array-buffer element-buffer quot -- )
162     -rot GL_ELEMENT_ARRAY_BUFFER swap [
163         swap GL_ARRAY_BUFFER -rot with-gl-buffer
164     ] with-gl-buffer ; inline
165
166 : <gl-buffer> ( target data hint -- id )
167     pick gen-gl-buffer [ [
168         [ dup byte-length swap ] dip glBufferData
169     ] with-gl-buffer ] keep ;
170
171 : buffer-offset ( int -- alien )
172     <alien> ; inline
173
174 : bind-texture-unit ( id target unit -- )
175     glActiveTexture swap glBindTexture gl-error ;
176
177 : (set-draw-buffers) ( buffers -- )
178     [ length ] [ >uint-array underlying>> ] bi glDrawBuffers ;
179
180 MACRO: set-draw-buffers ( buffers -- )
181     words>values [ (set-draw-buffers) ] curry ;
182
183 : do-attribs ( bits quot -- )
184     swap glPushAttrib call glPopAttrib ; inline
185
186 : gl-look-at ( eye focus up -- )
187     [ first3 ] tri@ gluLookAt ;
188
189 TUPLE: sprite loc dim dim2 dlist texture ;
190
191 : <sprite> ( loc dim dim2 -- sprite )
192     f f sprite boa ;
193
194 : sprite-size2 ( sprite -- w h ) dim2>> first2 ;
195
196 : sprite-width ( sprite -- w ) dim>> first ;
197
198 : gray-texture ( sprite pixmap -- id )
199     gen-texture [
200         GL_TEXTURE_BIT [
201             GL_TEXTURE_2D swap glBindTexture
202             [
203                 [ GL_TEXTURE_2D 0 GL_RGBA ] dip
204                 sprite-size2 0 GL_LUMINANCE_ALPHA
205                 GL_UNSIGNED_BYTE
206             ] dip glTexImage2D
207         ] do-attribs
208     ] keep ;
209     
210 : gen-dlist ( -- id ) 1 glGenLists ;
211
212 : make-dlist ( type quot -- id )
213     gen-dlist [ rot glNewList call glEndList ] keep ; inline
214
215 : init-texture ( -- )
216     GL_TEXTURE_2D GL_TEXTURE_MAG_FILTER GL_LINEAR glTexParameteri
217     GL_TEXTURE_2D GL_TEXTURE_MIN_FILTER GL_LINEAR glTexParameteri
218     GL_TEXTURE_2D GL_TEXTURE_WRAP_S GL_CLAMP glTexParameterf
219     GL_TEXTURE_2D GL_TEXTURE_WRAP_T GL_CLAMP glTexParameterf ;
220
221 : gl-translate ( point -- ) first2 0.0 glTranslated ;
222
223 : rect-texture-coords ( -- )
224     float-array{ 0 0 1 0 1 1 0 1 } gl-texture-coord-pointer ;
225
226 : draw-sprite ( sprite -- )
227     GL_TEXTURE_COORD_ARRAY [
228         dup loc>> gl-translate
229         GL_TEXTURE_2D over texture>> glBindTexture
230         init-texture rect-texture-coords
231         dim2>> fill-rect-vertices
232         (gl-fill-rect)
233         GL_TEXTURE_2D 0 glBindTexture
234     ] do-enabled-client-state ;
235
236 : make-sprite-dlist ( sprite -- id )
237     GL_MODELVIEW [
238         GL_COMPILE [ draw-sprite ] make-dlist
239     ] do-matrix ;
240
241 : init-sprite ( texture sprite -- )
242     swap >>texture
243     dup make-sprite-dlist >>dlist drop ;
244
245 : delete-dlist ( id -- ) 1 glDeleteLists ;
246
247 : free-sprite ( sprite -- )
248     [ dlist>> delete-dlist ]
249     [ texture>> delete-texture ] bi ;
250
251 : free-sprites ( sprites -- )
252     [ nip [ free-sprite ] when* ] assoc-each ;
253
254 : with-translation ( loc quot -- )
255     GL_MODELVIEW [ [ gl-translate ] dip call ] do-matrix ; inline
256
257 : fix-coordinates ( point1 point2 -- x1 y2 x2 y2 )
258     [ first2 [ >fixnum ] bi@ ] bi@ ;
259
260 : gl-set-clip ( loc dim -- )
261     fix-coordinates glScissor ;
262
263 : gl-viewport ( loc dim -- )
264     fix-coordinates glViewport ;
265
266 : init-matrices ( -- )
267     GL_PROJECTION glMatrixMode
268     glLoadIdentity
269     GL_MODELVIEW glMatrixMode
270     glLoadIdentity ;