Update Unicode docs
[factor/jcg.git] / extra / bunny / outlined / outlined.factor
blobc91a895ce112973dc0e92a95ec758e0927451184
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 locals ;
6 IN: bunny.outlined
8 STRING: outlined-pass1-fragment-shader-main-source
9 varying vec3 normal;
10 vec4 cel_light();
12 void
13 main()
15     gl_FragData[0] = cel_light();
16     gl_FragData[1] = vec4(normal, 1);
21 STRING: outlined-pass2-vertex-shader-source
22 varying vec2 coord;
24 void
25 main()
27     gl_Position = ftransform();
28     coord = (gl_Vertex * vec4(0.5) + vec4(0.5)).xy;
33 STRING: outlined-pass2-fragment-shader-source
34 uniform sampler2D colormap, normalmap, depthmap;
35 uniform vec4 line_color;
36 varying vec2 coord;
38 const float DEPTH_RATIO_THRESHOLD = 1.001, SAMPLE_SPREAD = 1.0/512.0;
40 float
41 depth_sample(vec2 c)
43     return texture2D(depthmap, c).x;
45 bool
46 are_depths_border(vec3 depths)
48     return any(lessThan(depths, vec3(1.0/DEPTH_RATIO_THRESHOLD)))
49         || any(greaterThan(depths, vec3(DEPTH_RATIO_THRESHOLD)));
52 vec3
53 normal_sample(vec2 c)
55     return texture2D(normalmap, c).xyz;
58 float
59 min6(float a, float b, float c, float d, float e, float f)
61     return min(min(min(min(min(a, b), c), d), e), f);
64 float
65 border_factor(vec2 c)
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);
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     }
107 void
108 main()
110     gl_FragColor = mix(texture2D(colormap, coord), line_color, border_factor(coord));
115 TUPLE: bunny-outlined
116     gadget
117     pass1-program pass2-program
118     color-texture normal-texture depth-texture
119     framebuffer framebuffer-dim ;
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 ;
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 ;
137 : pass2-program ( -- program )
138     outlined-pass2-vertex-shader-source
139     outlined-pass2-fragment-shader-source <simple-gl-program> ;
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 ;
146 :: (framebuffer-texture) ( dim iformat xformat -- texture )
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 iformat dim first2 0 xformat GL_UNSIGNED_BYTE f glTexImage2D ;
155 :: (attach-framebuffer-texture) ( texture attachment -- )
156     GL_FRAMEBUFFER_EXT attachment GL_TEXTURE_2D texture 0 glFramebufferTexture2DEXT
157     gl-error ;
159 : (make-framebuffer) ( color-texture normal-texture depth-texture -- framebuffer )
160     3array gen-framebuffer dup [
161         swap GL_COLOR_ATTACHMENT0_EXT
162              GL_COLOR_ATTACHMENT1_EXT
163              GL_DEPTH_ATTACHMENT_EXT 3array [ (attach-framebuffer-texture) ] 2each
164         check-framebuffer
165     ] with-framebuffer ;
167 : dispose-framebuffer ( draw -- )
168     dup framebuffer-dim>> [
169         {
170             [ framebuffer>>    [ delete-framebuffer ] when* ]
171             [ color-texture>>  [ delete-texture ] when* ]
172             [ normal-texture>> [ delete-texture ] when* ]
173             [ depth-texture>>  [ delete-texture ] when* ]
174             [ f >>framebuffer-dim drop ]
175         } cleave
176     ] [ drop ] if ;
178 MACRO: (framebuffer-texture>>draw) ( iformat xformat setter -- )
179     '[ _ _ (framebuffer-texture) [ @ drop ] keep ] ;
181 : (make-framebuffer-textures) ( draw dim -- draw color normal depth )
182     {
183         [ drop ]
184         [ GL_RGBA16F_ARB GL_RGBA [ >>color-texture  ] (framebuffer-texture>>draw) ]
185         [ GL_RGBA16F_ARB GL_RGBA [ >>normal-texture ] (framebuffer-texture>>draw) ]
186         [
187             GL_DEPTH_COMPONENT32 GL_DEPTH_COMPONENT
188             [ >>depth-texture ] (framebuffer-texture>>draw)
189         ]
190     } 2cleave ;
192 : remake-framebuffer ( draw -- )
193     [ dispose-framebuffer ]
194     [ dup gadget>> dim>>
195         [ (make-framebuffer-textures) (make-framebuffer) >>framebuffer ]
196         [ >>framebuffer-dim drop ] bi
197     ] bi ;
199 : remake-framebuffer-if-needed ( draw -- )
200     dup [ gadget>> dim>> ] [ framebuffer-dim>> ] bi =
201     [ drop ] [ remake-framebuffer ] if ;
203 : clear-framebuffer ( -- )
204     GL_COLOR_ATTACHMENT0_EXT glDrawBuffer
205     0.15 0.15 0.15 1.0 glClearColor
206     GL_COLOR_BUFFER_BIT GL_DEPTH_BUFFER_BIT bitor glClear
207     GL_COLOR_ATTACHMENT1_EXT glDrawBuffer
208     0.0 0.0 0.0 0.0 glClearColor
209     GL_COLOR_BUFFER_BIT glClear ;
211 : (pass1) ( geom draw -- )
212     dup framebuffer>> [
213         clear-framebuffer
214         { GL_COLOR_ATTACHMENT0_EXT GL_COLOR_ATTACHMENT1_EXT } set-draw-buffers
215         pass1-program>> (draw-cel-shaded-bunny)
216     ] with-framebuffer ;
218 : (pass2) ( draw -- )
219     init-matrices {
220         [ color-texture>>  GL_TEXTURE_2D GL_TEXTURE0 bind-texture-unit ]
221         [ normal-texture>> GL_TEXTURE_2D GL_TEXTURE1 bind-texture-unit ]
222         [ depth-texture>>  GL_TEXTURE_2D GL_TEXTURE2 bind-texture-unit ]
223         [
224             pass2-program>> [
225                 {
226                     [ "colormap"   glGetUniformLocation 0 glUniform1i ]
227                     [ "normalmap"  glGetUniformLocation 1 glUniform1i ]
228                     [ "depthmap"   glGetUniformLocation 2 glUniform1i ]
229                     [ "line_color" glGetUniformLocation 0.1 0.0 0.1 1.0 glUniform4f ]
230                 } cleave { -1.0 -1.0 } { 1.0 1.0 } rect-vertices
231             ] with-gl-program
232         ]
233     } cleave ;
235 M: bunny-outlined draw-bunny
236     [ remake-framebuffer-if-needed ]
237     [ (pass1) ]
238     [ (pass2) ] tri ;
240 M: bunny-outlined dispose
241     [ pass1-program>> [ delete-gl-program ] when* ]
242     [ pass2-program>> [ delete-gl-program ] when* ]
243     [ dispose-framebuffer ] tri ;