]> gitweb.factorcode.org Git - factor.git/blob - basis/opengl/opengl.factor
use radix literals
[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 ascii calendar
6 combinators.short-circuit continuations kernel libc math macros
7 namespaces math.vectors math.parser opengl.gl combinators
8 combinators.smart arrays sequences splitting words byte-arrays
9 assocs vocabs colors colors.constants accessors generalizations
10 sequences.generalizations locals fry specialized-arrays ;
11 FROM: alien.c-types => float ;
12 SPECIALIZED-ARRAY: float
13 SPECIALIZED-ARRAY: uint
14 IN: opengl
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         { 0x0 "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 function code string ;
36
37 : <gl-error> ( function code -- gl-error )
38     dup error>string \ gl-error boa ; inline
39
40 : gl-error-code ( -- code/f )
41     glGetError dup 0 = [ drop f ] when ; inline
42
43 : (gl-error) ( function -- )
44     gl-error-code [ <gl-error> throw ] [ drop ] if* ;
45
46 : gl-error ( -- )
47     f (gl-error) ; inline
48
49 : do-enabled ( what quot -- )
50     over glEnable dip glDisable ; inline
51
52 : do-enabled-client-state ( what quot -- )
53     over glEnableClientState dip glDisableClientState ; inline
54
55 : words>values ( word/value-seq -- value-seq )
56     [ ?execute ] map ;
57
58 : (all-enabled) ( seq quot -- )
59     [ dup [ glEnable ] each ] dip
60     dip
61     [ glDisable ] each ; inline
62
63 : (all-enabled-client-state) ( seq quot -- )
64     [ dup [ glEnableClientState ] each ] dip
65     dip
66     [ glDisableClientState ] each ; inline
67
68 MACRO: all-enabled ( seq quot -- )
69     [ words>values ] dip '[ _ _ (all-enabled) ] ;
70
71 MACRO: all-enabled-client-state ( seq quot -- )
72     [ words>values ] dip '[ _ _ (all-enabled-client-state) ] ;
73
74 : do-matrix ( quot -- )
75     glPushMatrix call glPopMatrix ; inline
76
77 : gl-material ( face pname params -- )
78     float-array{ } like glMaterialfv ;
79
80 : gl-vertex-pointer ( seq -- )
81     [ 2 GL_FLOAT 0 ] dip glVertexPointer ; inline
82
83 : gl-color-pointer ( seq -- )
84     [ 4 GL_FLOAT 0 ] dip glColorPointer ; inline
85
86 : gl-texture-coord-pointer ( seq -- )
87     [ 2 GL_FLOAT 0 ] dip glTexCoordPointer ; inline
88
89 : line-vertices ( a b -- )
90     [ first2 [ 0.5 + ] bi@ ] bi@ 4 float-array{ } nsequence
91     gl-vertex-pointer ;
92
93 : gl-line ( a b -- )
94     line-vertices GL_LINES 0 2 glDrawArrays ;
95
96 :: (rect-vertices) ( loc dim -- vertices )
97     #! We use GL_LINE_STRIP with a duplicated first vertex
98     #! instead of GL_LINE_LOOP to work around a bug in Apple's
99     #! X3100 driver.
100     loc first2 :> ( x y )
101     dim first2 :> ( w h )
102     [
103         x 0.5 +     y 0.5 +
104         x w + 0.3 - y 0.5 +
105         x w + 0.3 - y h + 0.3 -
106         x           y h + 0.3 -
107         x 0.5 +     y 0.5 +
108     ] float-array{ } output>sequence ;
109
110 : rect-vertices ( loc dim -- )
111     (rect-vertices) gl-vertex-pointer ;
112
113 : (gl-rect) ( -- )
114     GL_LINE_STRIP 0 5 glDrawArrays ;
115
116 : gl-rect ( loc dim -- )
117     rect-vertices (gl-rect) ;
118
119 :: (fill-rect-vertices) ( loc dim -- vertices )
120     loc first2 :> ( x y )
121     dim first2 :> ( w h )
122     [
123         x      y
124         x w +  y
125         x w +  y h +
126         x      y h +
127     ] float-array{ } output>sequence ;
128
129 : fill-rect-vertices ( loc dim -- )
130     (fill-rect-vertices) gl-vertex-pointer ;
131
132 : (gl-fill-rect) ( -- )
133     GL_QUADS 0 4 glDrawArrays ;
134
135 : gl-fill-rect ( loc dim -- )
136     fill-rect-vertices (gl-fill-rect) ;
137
138 : do-attribs ( bits quot -- )
139     swap glPushAttrib call glPopAttrib ; inline
140
141 : (gen-gl-object) ( quot -- id )
142     [ 1 { uint } ] dip with-out-parameters ; inline
143
144 : (delete-gl-object) ( id quot -- )
145     [ 1 swap uint <ref> ] dip call ; inline
146
147 : gen-gl-buffer ( -- id )
148     [ glGenBuffers ] (gen-gl-object) ;
149
150 : delete-gl-buffer ( id -- )
151     [ glDeleteBuffers ] (delete-gl-object) ;
152
153 :: with-gl-buffer ( binding id quot -- )
154     binding id glBindBuffer
155     quot [ binding 0 glBindBuffer ] [ ] cleanup ; inline
156
157 : with-array-element-buffers ( array-buffer element-buffer quot -- )
158     [ GL_ELEMENT_ARRAY_BUFFER ] 2dip '[
159         GL_ARRAY_BUFFER swap _ with-gl-buffer
160     ] with-gl-buffer ; inline
161
162 : gen-vertex-array ( -- id )
163     [ glGenVertexArrays ] (gen-gl-object) ;
164
165 : delete-vertex-array ( id -- )
166     [ glDeleteVertexArrays ] (delete-gl-object) ;
167
168 :: with-vertex-array ( id quot -- )
169     id glBindVertexArray
170     quot [ 0 glBindVertexArray ] [ ] cleanup ; inline
171
172 : <gl-buffer> ( target data hint -- id )
173     pick gen-gl-buffer [
174         [
175             [ [ byte-length ] keep ] dip glBufferData
176         ] with-gl-buffer
177     ] keep ;
178
179 : buffer-offset ( int -- alien )
180     <alien> ; inline
181
182 : bind-texture-unit ( id target unit -- )
183     glActiveTexture swap glBindTexture gl-error ;
184
185 : (set-draw-buffers) ( buffers -- )
186     [ length ] [ uint >c-array ] bi glDrawBuffers ;
187
188 MACRO: set-draw-buffers ( buffers -- )
189     words>values '[ _ (set-draw-buffers) ] ;
190
191 : gen-dlist ( -- id ) 1 glGenLists ;
192
193 : make-dlist ( type quot -- id )
194     [ gen-dlist ] 2dip '[ _ glNewList @ glEndList ] keep ; inline
195
196 : gl-translate ( point -- ) first2 0.0 glTranslated ;
197
198 : delete-dlist ( id -- ) 1 glDeleteLists ;
199
200 : with-translation ( loc quot -- )
201     [ [ gl-translate ] dip call ] do-matrix ; inline
202
203 : fix-coordinates ( point1 point2 -- x1 y1 x2 y2 )
204     [ first2 [ >fixnum ] bi@ ] bi@ ;
205
206 : gl-set-clip ( loc dim -- )
207     fix-coordinates glScissor ;
208
209 : gl-viewport ( loc dim -- )
210     fix-coordinates glViewport ;
211
212 : init-matrices ( -- )
213     #! Leaves with matrix mode GL_MODELVIEW
214     GL_PROJECTION glMatrixMode
215     glLoadIdentity
216     GL_MODELVIEW glMatrixMode
217     glLoadIdentity ;