USING: arrays combinators.lib kernel math math.functions math.vectors namespaces
- opengl opengl.gl sequences ui ui.gadgets ui.gestures ui.render ;
+ opengl opengl.gl sequences ui ui.gadgets ui.gestures ui.render accessors ;
IN: opengl.demo-support
: FOV 2.0 sqrt 1+ ; inline
: MOUSE-MOTION-SCALE 0.5 ; inline
: KEY-ROTATE-STEP 1.0 ; inline
-: DIMS { 640 480 } ; inline
-
-: FOV-RATIO ( -- fov ) DIMS dup first2 min v/n ;
SYMBOL: last-drag-loc
: <demo-gadget> ( yaw pitch distance -- gadget )
demo-gadget construct-gadget
- [ { set-demo-gadget-yaw set-demo-gadget-pitch set-demo-gadget-distance } set-slots ] keep ;
+ [ { (>>yaw) (>>pitch) (>>distance) } set-slots ] keep ;
GENERIC: far-plane ( gadget -- z )
GENERIC: near-plane ( gadget -- z )
M: demo-gadget distance-step ( gadget -- dz )
drop 1.0 64.0 / ;
+: fov-ratio ( gadget -- fov ) dim>> dup first2 min v/n ;
+
: yaw-demo-gadget ( yaw gadget -- )
[ [ demo-gadget-yaw + ] keep set-demo-gadget-yaw ] keep relayout-1 ;
[ [ demo-gadget-distance + ] keep set-demo-gadget-distance ] keep relayout-1 ;
M: demo-gadget pref-dim* ( gadget -- dim )
- drop DIMS ;
+ drop { 640 480 } ;
: -+ ( x -- -x x )
dup neg swap ;
: demo-gadget-frustum ( gadget -- -x x -y y near far )
- [ near-plane ] [ far-plane ] bi [
- drop FOV-RATIO swap FOV / v*n
+ [ near-plane ] [ far-plane ] [ fov-ratio ] tri [
+ nip swap FOV / v*n
first2 [ -+ ] bi@
- ] 2keep ;
+ ] 3keep drop ;
: demo-gadget-set-matrices ( gadget -- )
GL_COLOR_BUFFER_BIT GL_DEPTH_BUFFER_BIT bitor glClear
] [
GL_MODELVIEW glMatrixMode
glLoadIdentity
- [ >r 0.0 0.0 r> demo-gadget-distance neg glTranslatef ]
- [ demo-gadget-pitch 1.0 0.0 0.0 glRotatef ]
- [ demo-gadget-yaw 0.0 1.0 0.0 glRotatef ]
+ [ >r 0.0 0.0 r> distance>> neg glTranslatef ]
+ [ pitch>> 1.0 0.0 0.0 glRotatef ]
+ [ yaw>> 0.0 1.0 0.0 glRotatef ]
tri
] bi ;
USING: kernel opengl.demo-support opengl.gl opengl.shaders opengl.framebuffers
opengl multiline ui.gadgets accessors sequences ui.render ui math
-arrays.lib combinators ;
+arrays arrays.lib combinators ;
IN: spheres
STRING: plane-vertex-shader
}
;
-STRING: sphere-fragment-shader
+STRING: sphere-solid-color-fragment-shader
uniform vec3 light_position;
-varying float vradius;
-varying vec3 sphere_position;
-varying vec4 world_position, vcolor;
+varying vec4 vcolor;
-vec4 ambient = vec4(0.3, 0.2, 0.2, 1.0);
-vec4 diffuse = vec4(0.7, 0.8, 0.8, 1.0);
+const vec4 ambient = vec4(0.25, 0.2, 0.25, 1.0);
+const vec4 diffuse = vec4(0.75, 0.8, 0.75, 1.0);
vec4
-light(vec3 point, vec3 normal)
+sphere_color(vec3 point, vec3 normal)
{
vec3 transformed_light_position = (gl_ModelViewMatrix * vec4(light_position, 1)).xyz;
vec3 direction = normalize(transformed_light_position - point);
return ambient * vcolor + diffuse * vec4(d * vcolor.rgb, vcolor.a);
}
+;
+
+STRING: sphere-texture-fragment-shader
+uniform samplerCube surface_texture;
+
+vec4
+sphere_color(vec3 point, vec3 normal)
+{
+ vec3 reflect = reflect(normalize(point), normal);
+ return textureCube(surface_texture, reflect * gl_NormalMatrix);
+}
+;
+
+STRING: sphere-main-fragment-shader
+varying float vradius;
+varying vec3 sphere_position;
+varying vec4 world_position;
+
+vec4 sphere_color(vec3 point, vec3 normal);
void
main()
vec4 transformed_surface = gl_ProjectionMatrix * world_surface;
gl_FragDepth = (transformed_surface.z/transformed_surface.w + 1.0) * 0.5;
- gl_FragColor = light(world_surface.xyz, surface);
+ gl_FragColor = sphere_color(world_surface.xyz, surface);
}
;
TUPLE: spheres-gadget
- plane-program sphere-program
+ plane-program solid-sphere-program texture-sphere-program
reflection-framebuffer reflection-depthbuffer
reflection-texture ;
M: spheres-gadget distance-step ( gadget -- dz )
drop 0.5 ;
+: (reflection-dim) ( -- w h )
+ 1024 1024 ;
+
: (make-reflection-texture) ( -- texture )
gen-texture [
GL_TEXTURE_CUBE_MAP swap glBindTexture
GL_TEXTURE_CUBE_MAP_NEGATIVE_X
GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
GL_TEXTURE_CUBE_MAP_NEGATIVE_Z 6 narray
- [ 0 GL_RGBA8 1024 1024 0 GL_RGBA GL_UNSIGNED_BYTE f glTexImage2D ]
+ [ 0 GL_RGBA8 (reflection-dim) 0 GL_RGBA GL_UNSIGNED_BYTE f glTexImage2D ]
each
] keep ;
: (make-reflection-depthbuffer) ( -- depthbuffer )
gen-renderbuffer [
GL_RENDERBUFFER_EXT swap glBindRenderbufferEXT
- GL_RENDERBUFFER_EXT GL_DEPTH_COMPONENT32 1024 1024 glRenderbufferStorageEXT
+ GL_RENDERBUFFER_EXT GL_DEPTH_COMPONENT32 (reflection-dim) glRenderbufferStorageEXT
] keep ;
: (make-reflection-framebuffer) ( depthbuffer -- framebuffer )
glFramebufferRenderbufferEXT
] with-framebuffer ;
+: (plane-program) ( -- program )
+ plane-vertex-shader plane-fragment-shader <simple-gl-program> ;
+: (solid-sphere-program) ( -- program )
+ sphere-vertex-shader <vertex-shader> check-gl-shader
+ sphere-solid-color-fragment-shader <fragment-shader> check-gl-shader
+ sphere-main-fragment-shader <fragment-shader> check-gl-shader
+ 3array <gl-program> check-gl-program ;
+: (texture-sphere-program) ( -- program )
+ sphere-vertex-shader <vertex-shader> check-gl-shader
+ sphere-texture-fragment-shader <fragment-shader> check-gl-shader
+ sphere-main-fragment-shader <fragment-shader> check-gl-shader
+ 3array <gl-program> check-gl-program ;
+
M: spheres-gadget graft* ( gadget -- )
- plane-vertex-shader plane-fragment-shader
- <simple-gl-program> >>plane-program
- sphere-vertex-shader sphere-fragment-shader
- <simple-gl-program> >>sphere-program
+ (plane-program) >>plane-program
+ (solid-sphere-program) >>solid-sphere-program
+ (texture-sphere-program) >>texture-sphere-program
(make-reflection-texture) >>reflection-texture
(make-reflection-depthbuffer) [ >>reflection-depthbuffer ] keep
(make-reflection-framebuffer) >>reflection-framebuffer
[ reflection-framebuffer>> [ delete-framebuffer ] when* ]
[ reflection-depthbuffer>> [ delete-renderbuffer ] when* ]
[ reflection-texture>> [ delete-texture ] when* ]
- [ sphere-program>> [ delete-gl-program ] when* ]
+ [ solid-sphere-program>> [ delete-gl-program ] when* ]
+ [ texture-sphere-program>> [ delete-gl-program ] when* ]
[ plane-program>> [ delete-gl-program ] when* ]
} cleave ;
{ -1.0 -1.0 } { 1.0 1.0 } rect-vertices ;
: sphere-scene ( gadget -- )
- GL_DEPTH_BUFFER_BIT GL_COLOR_BUFFER_BIT bitor
- glClear
+ GL_DEPTH_BUFFER_BIT GL_COLOR_BUFFER_BIT bitor glClear
[
- sphere-program>> dup {
+ solid-sphere-program>> dup {
{ "light_position" [ 0.0 0.0 100.0 glUniform3f ] }
} [
- [ { 0.0 0.0 0.0 } 4.0 { 1.0 1.0 0.0 1.0 } (draw-sphere) ]
- [ { 7.0 0.0 0.0 } 1.0 { 1.0 0.0 0.0 1.0 } (draw-sphere) ]
- [ { -7.0 0.0 0.0 } 1.0 { 0.0 1.0 0.0 1.0 } (draw-sphere) ] tri
+ {
+ [ { 7.0 0.0 0.0 } 1.0 { 1.0 0.0 0.0 1.0 } (draw-sphere) ]
+ [ { -7.0 0.0 0.0 } 1.0 { 0.0 1.0 0.0 1.0 } (draw-sphere) ]
+ [ { 0.0 0.0 7.0 } 1.0 { 0.0 0.0 1.0 1.0 } (draw-sphere) ]
+ [ { 0.0 0.0 -7.0 } 1.0 { 1.0 1.0 0.0 1.0 } (draw-sphere) ]
+ [ { 0.0 7.0 0.0 } 1.0 { 1.0 0.0 1.0 1.0 } (draw-sphere) ]
+ [ { 0.0 -7.0 0.0 } 1.0 { 0.0 1.0 1.0 1.0 } (draw-sphere) ]
+ } cleave
] with-gl-program
] [
plane-program>> { } [
swap reflection-texture>> >r >r
GL_FRAMEBUFFER_EXT
GL_COLOR_ATTACHMENT0_EXT
- r> r> 0 glFramebufferTexture2DEXT ;
+ r> r> 0 glFramebufferTexture2DEXT
+ check-framebuffer ;
-M: spheres-gadget draw-gadget* ( gadget -- )
- GL_DEPTH_TEST glEnable
- 0.15 0.15 1.0 1.0 glClearColor {
+: (draw-reflection-texture) ( gadget -- )
+ dup reflection-framebuffer>> [ {
+ [ drop 0 0 (reflection-dim) glViewport ]
[
GL_PROJECTION glMatrixMode
glLoadIdentity
reflection-frustum glFrustum
GL_MODELVIEW glMatrixMode
glLoadIdentity
- glPushMatrix
- ]
- [
- dup reflection-framebuffer>> [ {
- [ GL_TEXTURE_CUBE_MAP_NEGATIVE_Z (reflection-face) ]
- [ sphere-scene ]
- [ GL_TEXTURE_CUBE_MAP_NEGATIVE_X (reflection-face)
- 90.0 0.0 1.0 0.0 glRotatef ]
- [ sphere-scene ]
- [ GL_TEXTURE_CUBE_MAP_POSITIVE_Z (reflection-face)
- 90.0 0.0 1.0 0.0 glRotatef ]
- [ sphere-scene ]
- [ GL_TEXTURE_CUBE_MAP_POSITIVE_X (reflection-face)
- 90.0 0.0 1.0 0.0 glRotatef ]
- [ sphere-scene ]
- [ GL_TEXTURE_CUBE_MAP_POSITIVE_Y (reflection-face)
- glPopMatrix glPushMatrix
- 90.0 1.0 0.0 0.0 glRotatef ]
- [ sphere-scene ]
- [ GL_TEXTURE_CUBE_MAP_NEGATIVE_Y (reflection-face)
- glPopMatrix
- -90.0 1.0 0.0 0.0 glRotatef ]
- [ sphere-scene ]
- } cleave ] with-framebuffer
+ 180.0 0.0 0.0 1.0 glRotatef
]
+ [ GL_TEXTURE_CUBE_MAP_NEGATIVE_Z (reflection-face) ]
+ [ sphere-scene ]
+ [ GL_TEXTURE_CUBE_MAP_POSITIVE_X (reflection-face)
+ 90.0 0.0 1.0 0.0 glRotatef ]
+ [ sphere-scene ]
+ [ GL_TEXTURE_CUBE_MAP_POSITIVE_Z (reflection-face)
+ 90.0 0.0 1.0 0.0 glRotatef glPushMatrix ]
+ [ sphere-scene ]
+ [ GL_TEXTURE_CUBE_MAP_NEGATIVE_X (reflection-face)
+ 90.0 0.0 1.0 0.0 glRotatef ]
+ [ sphere-scene ]
+ [ GL_TEXTURE_CUBE_MAP_NEGATIVE_Y (reflection-face)
+ glPopMatrix glPushMatrix -90.0 1.0 0.0 0.0 glRotatef ]
+ [ sphere-scene ]
+ [ GL_TEXTURE_CUBE_MAP_POSITIVE_Y (reflection-face)
+ glPopMatrix 90.0 1.0 0.0 0.0 glRotatef ]
+ [ sphere-scene ]
+ [ dim>> 0 0 rot first2 glViewport ]
+ } cleave ] with-framebuffer ;
+
+M: spheres-gadget draw-gadget* ( gadget -- )
+ GL_DEPTH_TEST glEnable
+ GL_SCISSOR_TEST glDisable
+ 0.15 0.15 1.0 1.0 glClearColor {
+ [ (draw-reflection-texture) ]
[ demo-gadget-set-matrices ]
[ sphere-scene ]
+ [
+ { texture-sphere-program>> reflection-texture>> } get-slots
+ GL_TEXTURE_CUBE_MAP GL_TEXTURE0 bind-texture-unit
+ dup {
+ { "surface_texture" [ 0 glUniform1i ] }
+ } [
+ { 0.0 0.0 0.0 } 4.0 { 1.0 0.0 0.0 1.0 } (draw-sphere)
+ ] with-gl-program
+ ]
} cleave ;
: spheres-window ( -- )