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