]> gitweb.factorcode.org Git - factor.git/blob - extra/bunny/outlined/outlined.factor
FUEL: Fix bug whereby true display-stacks? could hang the listener.
[factor.git] / extra / bunny / outlined / outlined.factor
1 USING: arrays bunny.model bunny.cel-shaded continuations
2 destructors kernel math multiline opengl opengl.shaders
3 opengl.framebuffers opengl.gl opengl.demo-support fry
4 opengl.capabilities sequences ui.gadgets combinators accessors
5 macros ;
6 IN: bunny.outlined
7
8 STRING: outlined-pass1-fragment-shader-main-source
9 varying vec3 normal;
10 vec4 cel_light();
11
12 void
13 main()
14 {
15     gl_FragData[0] = cel_light();
16     gl_FragData[1] = vec4(normal, 1);
17 }
18
19 ;
20
21 STRING: outlined-pass2-vertex-shader-source
22 varying vec2 coord;
23
24 void
25 main()
26 {
27     gl_Position = ftransform();
28     coord = (gl_Vertex * vec4(0.5) + vec4(0.5)).xy;
29 }
30
31 ;
32
33 STRING: outlined-pass2-fragment-shader-source
34 uniform sampler2D colormap, normalmap, depthmap;
35 uniform vec4 line_color;
36 varying vec2 coord;
37
38 const float DEPTH_RATIO_THRESHOLD = 1.001, SAMPLE_SPREAD = 1.0/512.0;
39
40 float
41 depth_sample(vec2 c)
42 {
43     return texture2D(depthmap, c).x;
44 }
45 bool
46 are_depths_border(vec3 depths)
47 {
48     return any(lessThan(depths, vec3(1.0/DEPTH_RATIO_THRESHOLD)))
49         || any(greaterThan(depths, vec3(DEPTH_RATIO_THRESHOLD)));
50 }
51
52 vec3
53 normal_sample(vec2 c)
54 {
55     return texture2D(normalmap, c).xyz;
56 }
57
58 float
59 min6(float a, float b, float c, float d, float e, float f)
60 {
61     return min(min(min(min(min(a, b), c), d), e), f);
62 }
63
64 float
65 border_factor(vec2 c)
66 {
67     vec2 coord1 = c + vec2(-SAMPLE_SPREAD, -SAMPLE_SPREAD),
68          coord2 = c + vec2( SAMPLE_SPREAD, -SAMPLE_SPREAD),
69          coord3 = c + vec2(-SAMPLE_SPREAD,  SAMPLE_SPREAD),
70          coord4 = c + vec2( SAMPLE_SPREAD,  SAMPLE_SPREAD);
71     
72     vec3 normal1 = normal_sample(coord1),
73          normal2 = normal_sample(coord2),
74          normal3 = normal_sample(coord3),
75          normal4 = normal_sample(coord4);
76
77     if (dot(normal1, normal1) < 0.5
78         && dot(normal2, normal2) < 0.5
79         && dot(normal3, normal3) < 0.5
80         && dot(normal4, normal4) < 0.5) {
81         return 0.0;
82     } else {
83         vec4 depths = vec4(depth_sample(coord1),
84                            depth_sample(coord2),
85                            depth_sample(coord3),
86                            depth_sample(coord4));
87     
88         vec3 ratios1 = depths.xxx/depths.yzw, ratios2 = depths.yyz/depths.zww;
89     
90         if (are_depths_border(ratios1) || are_depths_border(ratios2)) {
91             return 1.0;
92         } else {
93             float normal_border = 1.0 - min6(
94                 dot(normal1, normal2),
95                 dot(normal1, normal3),
96                 dot(normal1, normal4),
97                 dot(normal2, normal3),
98                 dot(normal2, normal4),
99                 dot(normal3, normal4)
100             );
101     
102             return normal_border;
103         }
104     }
105 }
106
107 void
108 main()
109 {
110     gl_FragColor = mix(texture2D(colormap, coord), line_color, border_factor(coord));
111 }
112
113 ;
114
115 TUPLE: bunny-outlined
116     gadget
117     pass1-program pass2-program
118     color-texture normal-texture depth-texture
119     framebuffer framebuffer-dim ;
120
121 : outlining-supported? ( -- ? )
122     "2.0" {
123         "GL_ARB_shading_objects"
124         "GL_ARB_draw_buffers"
125         "GL_ARB_multitexture"
126     } has-gl-version-or-extensions? {
127         "GL_EXT_framebuffer_object"
128         "GL_ARB_texture_float"
129     } has-gl-extensions? and ;
130
131 : pass1-program ( -- program )
132     vertex-shader-source <vertex-shader> check-gl-shader
133     cel-shaded-fragment-shader-lib-source <fragment-shader> check-gl-shader
134     outlined-pass1-fragment-shader-main-source <fragment-shader> check-gl-shader
135     3array <gl-program> check-gl-program ;
136
137 : pass2-program ( -- program )
138     outlined-pass2-vertex-shader-source
139     outlined-pass2-fragment-shader-source <simple-gl-program> ;
140
141 : <bunny-outlined> ( gadget -- draw )
142     outlining-supported? [
143         pass1-program pass2-program f f f f f bunny-outlined boa
144     ] [ drop f ] if ;
145
146 : (framebuffer-texture) ( dim iformat xformat -- texture )
147     swapd >r >r >r
148     GL_TEXTURE0 glActiveTexture
149     gen-texture GL_TEXTURE_2D over glBindTexture
150     GL_TEXTURE_2D GL_TEXTURE_WRAP_S GL_CLAMP glTexParameteri
151     GL_TEXTURE_2D GL_TEXTURE_WRAP_T GL_CLAMP glTexParameteri
152     GL_TEXTURE_2D GL_TEXTURE_MAG_FILTER GL_NEAREST glTexParameteri
153     GL_TEXTURE_2D GL_TEXTURE_MIN_FILTER GL_NEAREST glTexParameteri
154     GL_TEXTURE_2D 0 r> r> first2 0 r> GL_UNSIGNED_BYTE f glTexImage2D ;
155
156 : (attach-framebuffer-texture) ( texture attachment -- )
157     swap >r >r
158     GL_FRAMEBUFFER_EXT r> GL_TEXTURE_2D r> 0 glFramebufferTexture2DEXT
159     gl-error ;
160
161 : (make-framebuffer) ( color-texture normal-texture depth-texture -- framebuffer )
162     3array gen-framebuffer dup [
163         swap GL_COLOR_ATTACHMENT0_EXT
164              GL_COLOR_ATTACHMENT1_EXT
165              GL_DEPTH_ATTACHMENT_EXT 3array [ (attach-framebuffer-texture) ] 2each
166         check-framebuffer
167     ] with-framebuffer ;
168
169 : dispose-framebuffer ( draw -- )
170     dup framebuffer-dim>> [
171         {
172             [ framebuffer>>    [ delete-framebuffer ] when* ]
173             [ color-texture>>  [ delete-texture ] when* ]
174             [ normal-texture>> [ delete-texture ] when* ]
175             [ depth-texture>>  [ delete-texture ] when* ]
176             [ f >>framebuffer-dim drop ]
177         } cleave
178     ] [ drop ] if ;
179
180 MACRO: (framebuffer-texture>>draw) ( iformat xformat setter -- )
181     '[ _ _ (framebuffer-texture) [ @ drop ] keep ] ;
182
183 : (make-framebuffer-textures) ( draw dim -- draw color normal depth )
184     {
185         [ drop ]
186         [ GL_RGBA16F_ARB GL_RGBA [ >>color-texture  ] (framebuffer-texture>>draw) ]
187         [ GL_RGBA16F_ARB GL_RGBA [ >>normal-texture ] (framebuffer-texture>>draw) ]
188         [
189             GL_DEPTH_COMPONENT32 GL_DEPTH_COMPONENT
190             [ >>depth-texture ] (framebuffer-texture>>draw)
191         ]
192     } 2cleave ;
193
194 : remake-framebuffer ( draw -- )
195     [ dispose-framebuffer ]
196     [ dup gadget>> dim>>
197         [ (make-framebuffer-textures) (make-framebuffer) >>framebuffer ]
198         [ >>framebuffer-dim drop ] bi
199     ] bi ;
200
201 : remake-framebuffer-if-needed ( draw -- )
202     dup [ gadget>> dim>> ] [ framebuffer-dim>> ] bi =
203     [ drop ] [ remake-framebuffer ] if ;
204
205 : clear-framebuffer ( -- )
206     GL_COLOR_ATTACHMENT0_EXT glDrawBuffer
207     0.15 0.15 0.15 1.0 glClearColor
208     GL_COLOR_BUFFER_BIT GL_DEPTH_BUFFER_BIT bitor glClear
209     GL_COLOR_ATTACHMENT1_EXT glDrawBuffer
210     0.0 0.0 0.0 0.0 glClearColor
211     GL_COLOR_BUFFER_BIT glClear ;
212
213 : (pass1) ( geom draw -- )
214     dup framebuffer>> [
215         clear-framebuffer
216         { GL_COLOR_ATTACHMENT0_EXT GL_COLOR_ATTACHMENT1_EXT } set-draw-buffers
217         pass1-program>> (draw-cel-shaded-bunny)
218     ] with-framebuffer ;
219
220 : (pass2) ( draw -- )
221     init-matrices {
222         [ color-texture>>  GL_TEXTURE_2D GL_TEXTURE0 bind-texture-unit ]
223         [ normal-texture>> GL_TEXTURE_2D GL_TEXTURE1 bind-texture-unit ]
224         [ depth-texture>>  GL_TEXTURE_2D GL_TEXTURE2 bind-texture-unit ]
225         [
226             pass2-program>> [
227                 {
228                     [ "colormap"   glGetUniformLocation 0 glUniform1i ]
229                     [ "normalmap"  glGetUniformLocation 1 glUniform1i ]
230                     [ "depthmap"   glGetUniformLocation 2 glUniform1i ]
231                     [ "line_color" glGetUniformLocation 0.1 0.0 0.1 1.0 glUniform4f ]
232                 } cleave { -1.0 -1.0 } { 1.0 1.0 } rect-vertices
233             ] with-gl-program
234         ]
235     } cleave ;
236
237 M: bunny-outlined draw-bunny
238     [ remake-framebuffer-if-needed ]
239     [ (pass1) ]
240     [ (pass2) ] tri ;
241
242 M: bunny-outlined dispose
243     [ pass1-program>> [ delete-gl-program ] when* ]
244     [ pass2-program>> [ delete-gl-program ] when* ]
245     [ dispose-framebuffer ] tri ;