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