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