1 USING: arrays bunny.model bunny.cel-shaded continuations
2 destructors kernel math multiline opengl opengl.shaders
3 opengl.framebuffers opengl.gl opengl.capabilities sequences
4 ui.gadgets combinators accessors ;
7 STRING: outlined-pass1-fragment-shader-main-source
14 gl_FragData[0] = cel_light();
15 gl_FragData[1] = vec4(normal, 1);
20 STRING: outlined-pass2-vertex-shader-source
26 gl_Position = ftransform();
27 coord = (gl_Vertex * vec4(0.5) + vec4(0.5)).xy;
32 STRING: outlined-pass2-fragment-shader-source
33 uniform sampler2D colormap, normalmap, depthmap;
34 uniform vec4 line_color;
37 const float DEPTH_RATIO_THRESHOLD = 1.001, SAMPLE_SPREAD = 1.0/512.0;
42 return texture2D(depthmap, c).x;
45 are_depths_border(vec3 depths)
47 return any(lessThan(depths, vec3(1.0/DEPTH_RATIO_THRESHOLD)))
48 || any(greaterThan(depths, vec3(DEPTH_RATIO_THRESHOLD)));
54 return texture2D(normalmap, c).xyz;
58 min6(float a, float b, float c, float d, float e, float f)
60 return min(min(min(min(min(a, b), c), d), e), f);
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);
71 vec3 normal1 = normal_sample(coord1),
72 normal2 = normal_sample(coord2),
73 normal3 = normal_sample(coord3),
74 normal4 = normal_sample(coord4);
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) {
82 vec4 depths = vec4(depth_sample(coord1),
85 depth_sample(coord4));
87 vec3 ratios1 = depths.xxx/depths.yzw, ratios2 = depths.yyz/depths.zww;
89 if (are_depths_border(ratios1) || are_depths_border(ratios2)) {
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),
101 return normal_border;
109 gl_FragColor = mix(texture2D(colormap, coord), line_color, border_factor(coord));
114 TUPLE: bunny-outlined
116 pass1-program pass2-program
117 color-texture normal-texture depth-texture
118 framebuffer framebuffer-dim ;
120 : outlining-supported? ( -- ? )
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 ;
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 ;
136 : pass2-program ( -- program )
137 outlined-pass2-vertex-shader-source
138 outlined-pass2-fragment-shader-source <simple-gl-program> ;
140 : <bunny-outlined> ( gadget -- draw )
141 outlining-supported? [
142 pass1-program pass2-program {
146 } bunny-outlined construct
149 : (framebuffer-texture) ( dim iformat xformat -- texture )
151 GL_TEXTURE0 glActiveTexture
152 gen-texture GL_TEXTURE_2D over glBindTexture
153 GL_TEXTURE_2D GL_TEXTURE_WRAP_S GL_CLAMP glTexParameteri
154 GL_TEXTURE_2D GL_TEXTURE_WRAP_T GL_CLAMP glTexParameteri
155 GL_TEXTURE_2D GL_TEXTURE_MAG_FILTER GL_NEAREST glTexParameteri
156 GL_TEXTURE_2D GL_TEXTURE_MIN_FILTER GL_NEAREST glTexParameteri
157 GL_TEXTURE_2D 0 r> r> first2 0 r> GL_UNSIGNED_BYTE f glTexImage2D ;
159 : (attach-framebuffer-texture) ( texture attachment -- )
161 GL_FRAMEBUFFER_EXT r> GL_TEXTURE_2D r> 0 glFramebufferTexture2DEXT
164 : (make-framebuffer) ( color-texture normal-texture depth-texture -- framebuffer )
165 3array gen-framebuffer dup [
166 swap GL_COLOR_ATTACHMENT0_EXT
167 GL_COLOR_ATTACHMENT1_EXT
168 GL_DEPTH_ATTACHMENT_EXT 3array [ (attach-framebuffer-texture) ] 2each
172 : dispose-framebuffer ( draw -- )
173 dup framebuffer-dim>> [
175 [ framebuffer>> [ delete-framebuffer ] when* ]
176 [ color-texture>> [ delete-texture ] when* ]
177 [ normal-texture>> [ delete-texture ] when* ]
178 [ depth-texture>> [ delete-texture ] when* ]
179 [ f >>framebuffer-dim drop ]
183 : remake-framebuffer-if-needed ( draw -- )
184 dup [ gadget>> dim>> ] [ framebuffer-dim>> ] bi =
186 [ dispose-framebuffer ] [ dup ] [ gadget>> dim>> ] tri {
188 GL_RGBA16F_ARB GL_RGBA (framebuffer-texture)
189 [ >>color-texture drop ] keep
191 GL_RGBA16F_ARB GL_RGBA (framebuffer-texture)
192 [ >>normal-texture drop ] keep
194 GL_DEPTH_COMPONENT32 GL_DEPTH_COMPONENT (framebuffer-texture)
195 [ >>depth-texture drop ] keep
198 [ (make-framebuffer) >>framebuffer ] [ >>framebuffer-dim ] bi
202 : clear-framebuffer ( -- )
203 GL_COLOR_ATTACHMENT0_EXT glDrawBuffer
204 0.15 0.15 0.15 1.0 glClearColor
205 GL_COLOR_BUFFER_BIT GL_DEPTH_BUFFER_BIT bitor glClear
206 GL_COLOR_ATTACHMENT1_EXT glDrawBuffer
207 0.0 0.0 0.0 0.0 glClearColor
208 GL_COLOR_BUFFER_BIT glClear ;
210 : (pass1) ( geom draw -- )
213 { GL_COLOR_ATTACHMENT0_EXT GL_COLOR_ATTACHMENT1_EXT } set-draw-buffers
214 pass1-program>> (draw-cel-shaded-bunny)
217 : (pass2) ( draw -- )
219 [ color-texture>> GL_TEXTURE_2D GL_TEXTURE0 bind-texture-unit ]
220 [ normal-texture>> GL_TEXTURE_2D GL_TEXTURE1 bind-texture-unit ]
221 [ depth-texture>> GL_TEXTURE_2D GL_TEXTURE2 bind-texture-unit ]
225 [ "colormap" glGetUniformLocation 0 glUniform1i ]
226 [ "normalmap" glGetUniformLocation 1 glUniform1i ]
227 [ "depthmap" glGetUniformLocation 2 glUniform1i ]
228 [ "line_color" glGetUniformLocation 0.1 0.0 0.1 1.0 glUniform4f ]
229 } cleave { -1.0 -1.0 } { 1.0 1.0 } rect-vertices
234 M: bunny-outlined draw-bunny
235 [ remake-framebuffer-if-needed ]
239 M: bunny-outlined dispose
240 [ pass1-program>> [ delete-gl-program ] when* ]
241 [ pass2-program>> [ delete-gl-program ] when* ]
242 [ dispose-framebuffer ] tri ;