]> gitweb.factorcode.org Git - factor.git/blob - basis/opengl/opengl.factor
Various load-everything fixes
[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 opengl.glu
7 combinators 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 : gl-error ( -- )
20     glGetError dup zero? [
21         "GL error: " over gluErrorString append throw
22     ] unless drop ;
23
24 : do-enabled ( what quot -- )
25     over glEnable dip glDisable ; inline
26
27 : do-enabled-client-state ( what quot -- )
28     over glEnableClientState dip glDisableClientState ; inline
29
30 : words>values ( word/value-seq -- value-seq )
31     [ dup word? [ execute ] when ] map ;
32
33 : (all-enabled) ( seq quot -- )
34     over [ glEnable ] each dip [ glDisable ] each ; inline
35
36 : (all-enabled-client-state) ( seq quot -- )
37     [ dup [ glEnableClientState ] each ] dip
38     dip
39     [ glDisableClientState ] each ; inline
40
41 MACRO: all-enabled ( seq quot -- )
42     [ words>values ] dip '[ _ _ (all-enabled) ] ;
43
44 MACRO: all-enabled-client-state ( seq quot -- )
45     [ words>values ] dip '[ _ _ (all-enabled-client-state) ] ;
46
47 : do-matrix ( mode quot -- )
48     swap [ glMatrixMode glPushMatrix call ] keep
49     glMatrixMode glPopMatrix ; inline
50
51 : gl-material ( face pname params -- )
52     float-array{ } like glMaterialfv ;
53
54 : gl-vertex-pointer ( seq -- )
55     [ 2 GL_FLOAT 0 ] dip glVertexPointer ; inline
56
57 : gl-color-pointer ( seq -- )
58     [ 4 GL_FLOAT 0 ] dip glColorPointer ; inline
59
60 : gl-texture-coord-pointer ( seq -- )
61     [ 2 GL_FLOAT 0 ] dip glTexCoordPointer ; inline
62
63 : line-vertices ( a b -- )
64     [ first2 [ 0.5 + ] bi@ ] bi@ 4 float-array{ } nsequence
65     gl-vertex-pointer ;
66
67 : gl-line ( a b -- )
68     line-vertices GL_LINES 0 2 glDrawArrays ;
69
70 : (rect-vertices) ( dim -- vertices )
71     #! We use GL_LINE_STRIP with a duplicated first vertex
72     #! instead of GL_LINE_LOOP to work around a bug in Apple's
73     #! X3100 driver.
74     {
75         [ drop 0.5 0.5 ]
76         [ first 0.3 - 0.5 ]
77         [ [ first 0.3 - ] [ second 0.3 - ] bi ]
78         [ second 0.3 - 0.5 swap ]
79         [ drop 0.5 0.5 ]
80     } cleave 10 float-array{ } nsequence ;
81
82 : rect-vertices ( dim -- )
83     (rect-vertices) gl-vertex-pointer ;
84
85 : (gl-rect) ( -- )
86     GL_LINE_STRIP 0 5 glDrawArrays ;
87
88 : gl-rect ( dim -- )
89     rect-vertices (gl-rect) ;
90
91 : (fill-rect-vertices) ( dim -- vertices )
92     {
93         [ drop 0 0 ]
94         [ first 0 ]
95         [ first2 ]
96         [ second 0 swap ]
97     } cleave 8 float-array{ } nsequence ;
98
99 : fill-rect-vertices ( dim -- )
100     (fill-rect-vertices) gl-vertex-pointer ;
101
102 : (gl-fill-rect) ( -- )
103     GL_QUADS 0 4 glDrawArrays ;
104
105 : gl-fill-rect ( dim -- )
106     fill-rect-vertices (gl-fill-rect) ;
107
108 : do-attribs ( bits quot -- )
109     swap glPushAttrib call glPopAttrib ; inline
110
111 : (gen-gl-object) ( quot -- id )
112     [ 1 0 <uint> ] dip keep *uint ; inline
113
114 : gen-gl-buffer ( -- id )
115     [ glGenBuffers ] (gen-gl-object) ;
116
117 : (delete-gl-object) ( id quot -- )
118     [ 1 swap <uint> ] dip call ; inline
119
120 : delete-gl-buffer ( id -- )
121     [ glDeleteBuffers ] (delete-gl-object) ;
122
123 :: with-gl-buffer ( binding id quot -- )
124     binding id glBindBuffer
125     quot [ binding 0 glBindBuffer ] [ ] cleanup ; inline
126
127 : with-array-element-buffers ( array-buffer element-buffer quot -- )
128     [ GL_ELEMENT_ARRAY_BUFFER ] 2dip '[
129         GL_ARRAY_BUFFER swap _ with-gl-buffer
130     ] with-gl-buffer ; inline
131
132 : <gl-buffer> ( target data hint -- id )
133     pick gen-gl-buffer [
134         [
135             [ [ byte-length ] keep ] dip glBufferData
136         ] with-gl-buffer
137     ] keep ;
138
139 : buffer-offset ( int -- alien )
140     <alien> ; inline
141
142 : bind-texture-unit ( id target unit -- )
143     glActiveTexture swap glBindTexture gl-error ;
144
145 : (set-draw-buffers) ( buffers -- )
146     [ length ] [ >uint-array ] bi glDrawBuffers ;
147
148 MACRO: set-draw-buffers ( buffers -- )
149     words>values '[ _ (set-draw-buffers) ] ;
150
151 : gl-look-at ( eye focus up -- )
152     [ first3 ] tri@ gluLookAt ;
153
154 : gen-dlist ( -- id ) 1 glGenLists ;
155
156 : make-dlist ( type quot -- id )
157     [ gen-dlist ] 2dip '[ _ glNewList @ glEndList ] keep ; inline
158
159 : gl-translate ( point -- ) first2 0.0 glTranslated ;
160
161 : delete-dlist ( id -- ) 1 glDeleteLists ;
162
163 : with-translation ( loc quot -- )
164     GL_MODELVIEW [ [ gl-translate ] dip call ] do-matrix ; inline
165
166 : fix-coordinates ( point1 point2 -- x1 y2 x2 y2 )
167     [ first2 [ >fixnum ] bi@ ] bi@ ;
168
169 : gl-set-clip ( loc dim -- )
170     fix-coordinates glScissor ;
171
172 : gl-viewport ( loc dim -- )
173     fix-coordinates glViewport ;
174
175 : init-matrices ( -- )
176     GL_PROJECTION glMatrixMode
177     glLoadIdentity
178     GL_MODELVIEW glMatrixMode
179     glLoadIdentity ;