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