]> gitweb.factorcode.org Git - factor.git/blobdiff - extra/spheres/spheres.factor
interpolate: split out format into a hook
[factor.git] / extra / spheres / spheres.factor
old mode 100755 (executable)
new mode 100644 (file)
index 1a8f41b..107bb68
@@ -1,9 +1,7 @@
-USING: kernel opengl opengl.demo-support opengl.gl opengl.textures
-opengl.shaders opengl.framebuffers opengl.capabilities multiline
-ui.gadgets accessors sequences ui.render ui math locals arrays
-generalizations combinators ui.gadgets.worlds
-literals ui.pixel-formats ;
-FROM: opengl.demo-support => rect-vertices ;
+USING: accessors arrays combinators kernel literals multiline
+opengl opengl.capabilities opengl.demo-support
+opengl.framebuffers opengl.gl opengl.shaders opengl.textures
+sequences ui ui.gadgets.worlds ui.pixel-formats ;
 IN: spheres
 
 STRING: plane-vertex-shader
@@ -33,7 +31,7 @@ main()
 {
     float distance_factor = (gl_FragCoord.z * 0.5 + 0.5);
     distance_factor = pow(distance_factor, 500.0)*0.5;
-    
+
     gl_FragColor = checker_color(object_position)
         ? mix(checker_color_1, checker_color_2, distance_factor)
         : mix(checker_color_2, checker_color_1, distance_factor);
@@ -53,9 +51,9 @@ main()
 {
     world_position = gl_ModelViewMatrix * vec4(center, 1);
     sphere_position = gl_Vertex.xyz;
-    
+
     gl_Position = gl_ProjectionMatrix * (world_position + vec4(sphere_position * radius, 0));
-    
+
     vcolor = surface_color;
     vradius = radius;
 }
@@ -74,7 +72,7 @@ sphere_color(vec3 point, vec3 normal)
     vec3 transformed_light_position = (gl_ModelViewMatrix * vec4(light_position, 1)).xyz;
     vec3 direction = normalize(transformed_light_position - point);
     float d = max(0.0, dot(normal, direction));
-    
+
     return ambient * vcolor + diffuse * vec4(d * vcolor.rgb, vcolor.a);
 }
 ;
@@ -117,11 +115,11 @@ TUPLE: spheres-world < demo-world
     reflection-framebuffer reflection-depthbuffer
     reflection-texture ;
 
-M: spheres-world near-plane ( gadget -- z )
+M: spheres-world near-plane
     drop 1.0 ;
-M: spheres-world far-plane ( gadget -- z )
+M: spheres-world far-plane
     drop 512.0 ;
-M: spheres-world distance-step ( gadget -- dz )
+M: spheres-world distance-step
     drop 0.5 ;
 
 : (reflection-dim) ( -- w h )
@@ -149,14 +147,14 @@ M: spheres-world distance-step ( gadget -- dz )
 
 : (make-reflection-depthbuffer) ( -- depthbuffer )
     gen-renderbuffer [
-        GL_RENDERBUFFER_EXT swap glBindRenderbufferEXT
-        GL_RENDERBUFFER_EXT GL_DEPTH_COMPONENT32 (reflection-dim) glRenderbufferStorageEXT
+        GL_RENDERBUFFER swap glBindRenderbuffer
+        GL_RENDERBUFFER GL_DEPTH_COMPONENT32 (reflection-dim) glRenderbufferStorage
     ] keep ;
 
 : (make-reflection-framebuffer) ( depthbuffer -- framebuffer )
     gen-framebuffer dup [
-        swap [ GL_FRAMEBUFFER_EXT GL_DEPTH_ATTACHMENT_EXT GL_RENDERBUFFER_EXT ] dip
-        glFramebufferRenderbufferEXT
+        swap [ GL_DRAW_FRAMEBUFFER GL_DEPTH_ATTACHMENT GL_RENDERBUFFER ] dip
+        glFramebufferRenderbuffer
     ] with-framebuffer ;
 
 : (plane-program) ( -- program )
@@ -175,6 +173,9 @@ M: spheres-world distance-step ( gadget -- dz )
 M: spheres-world begin-world
     "2.0" { "GL_ARB_shader_objects" } require-gl-version-or-extensions
     { "GL_EXT_framebuffer_object" } require-gl-extensions
+    GL_DEPTH_TEST glEnable
+    GL_VERTEX_ARRAY glEnableClientState
+    0.15 0.15 1.0 1.0 glClearColor
     20.0 10.0 20.0 set-demo-orientation
     (plane-program) >>plane-program
     (solid-sphere-program) >>solid-sphere-program
@@ -194,20 +195,17 @@ M: spheres-world end-world
         [ plane-program>> [ delete-gl-program ] when* ]
     } cleave ;
 
-M: spheres-world pref-dim* ( gadget -- dim )
-    drop { 640 480 } ;
-
 :: (draw-sphere) ( program center radius -- )
     program "center" glGetAttribLocation center first3 glVertexAttrib3f
     program "radius" glGetAttribLocation radius glVertexAttrib1f
-    { -1.0 -1.0 } { 1.0 1.0 } rect-vertices ;
-    
+    { -1.0 -1.0 } { 2.0 2.0 } gl-fill-rect ;
+
 :: (draw-colored-sphere) ( program center radius surfacecolor -- )
     program "surface_color" glGetAttribLocation surfacecolor first4 glVertexAttrib4f
     program center radius (draw-sphere) ;
 
 : sphere-scene ( gadget -- )
-    GL_DEPTH_BUFFER_BIT GL_COLOR_BUFFER_BIT bitor glClear
+    flags{ GL_DEPTH_BUFFER_BIT GL_COLOR_BUFFER_BIT } glClear
     [
         solid-sphere-program>> [
             {
@@ -242,14 +240,14 @@ M: spheres-world pref-dim* ( gadget -- dim )
 
 : (reflection-face) ( gadget face -- )
     swap reflection-texture>> [
-        GL_FRAMEBUFFER_EXT
-        GL_COLOR_ATTACHMENT0_EXT
-    ] 2dip 0 glFramebufferTexture2DEXT
+        GL_DRAW_FRAMEBUFFER
+        GL_COLOR_ATTACHMENT0
+    ] 2dip 0 glFramebufferTexture2D
     check-framebuffer ;
 
 : (draw-reflection-texture) ( gadget -- )
     dup reflection-framebuffer>> [ {
-        [ drop 0 0 (reflection-dim) glViewport ]
+        [ drop { 0 0 } (reflection-dim) 2array gl-viewport ]
         [
             GL_PROJECTION glMatrixMode
             glPushMatrix glLoadIdentity
@@ -276,16 +274,14 @@ M: spheres-world pref-dim* ( gadget -- dim )
           glPopMatrix 90.0 1.0 0.0 0.0 glRotatef ]
         [ sphere-scene ]
         [
-            [ 0 0 ] dip dim>> first2 glViewport
+            [ { 0 0 } ] dip dim>> gl-viewport
             GL_PROJECTION glMatrixMode
             glPopMatrix
         ]
     } cleave ] with-framebuffer ;
 
 M: spheres-world draw-world*
-    GL_DEPTH_TEST glEnable
-    GL_SCISSOR_TEST glDisable
-    0.15 0.15 1.0 1.0 glClearColor {
+    {
         [ (draw-reflection-texture) ]
         [ demo-world-set-matrix ]
         [ sphere-scene ]
@@ -299,17 +295,13 @@ M: spheres-world draw-world*
         ]
     } cleave ;
 
-: spheres-window ( -- )
-    [
-        f T{ world-attributes
-            { world-class spheres-world }
-            { title "Spheres" }
-            { pixel-format-attributes {
-                windowed
-                double-buffered
-                T{ depth-bits { value 16 } }
-            } }
-        } open-window
-    ] with-ui ;
-
-MAIN: spheres-window
+MAIN-WINDOW: spheres-window {
+        { world-class spheres-world }
+        { title "Spheres" }
+        { pixel-format-attributes {
+            windowed
+            double-buffered
+            T{ depth-bits { value 16 } }
+        } }
+        { pref-dim { 640 480 } }
+    } ;