]> gitweb.factorcode.org Git - factor.git/blobdiff - extra/spheres/spheres.factor
Revert "interpolate: allow format directives to be used"
[factor.git] / extra / spheres / spheres.factor
old mode 100755 (executable)
new mode 100644 (file)
index 543c26a..107bb68
@@ -1,7 +1,7 @@
-USING: kernel opengl opengl.demo-support opengl.gl
-opengl.shaders opengl.framebuffers opengl.capabilities multiline
-ui.gadgets accessors sequences ui.render ui math locals arrays
-generalizations combinators ui.gadgets.worlds ;
+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
@@ -31,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);
@@ -51,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;
 }
@@ -72,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);
 }
 ;
@@ -110,19 +110,16 @@ main()
 }
 ;
 
-TUPLE: spheres-gadget < demo-gadget
+TUPLE: spheres-world < demo-world
     plane-program solid-sphere-program texture-sphere-program
     reflection-framebuffer reflection-depthbuffer
-    reflection-texture initialized? ;
+    reflection-texture ;
 
-: <spheres-gadget> ( -- gadget )
-    20.0 10.0 20.0 spheres-gadget new-demo-gadget ;
-
-M: spheres-gadget near-plane ( gadget -- z )
+M: spheres-world near-plane
     drop 1.0 ;
-M: spheres-gadget far-plane ( gadget -- z )
+M: spheres-world far-plane
     drop 512.0 ;
-M: spheres-gadget distance-step ( gadget -- dz )
+M: spheres-world distance-step
     drop 0.5 ;
 
 : (reflection-dim) ( -- w h )
@@ -136,27 +133,28 @@ M: spheres-gadget distance-step ( gadget -- dz )
         GL_TEXTURE_CUBE_MAP GL_TEXTURE_WRAP_S GL_CLAMP glTexParameteri
         GL_TEXTURE_CUBE_MAP GL_TEXTURE_WRAP_T GL_CLAMP glTexParameteri
         GL_TEXTURE_CUBE_MAP GL_TEXTURE_WRAP_R GL_CLAMP glTexParameteri
-        GL_TEXTURE_CUBE_MAP_POSITIVE_X
-        GL_TEXTURE_CUBE_MAP_POSITIVE_Y
-        GL_TEXTURE_CUBE_MAP_POSITIVE_Z
-        GL_TEXTURE_CUBE_MAP_NEGATIVE_X
-        GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
-        GL_TEXTURE_CUBE_MAP_NEGATIVE_Z 6 narray
+        ${
+            GL_TEXTURE_CUBE_MAP_POSITIVE_X
+            GL_TEXTURE_CUBE_MAP_POSITIVE_Y
+            GL_TEXTURE_CUBE_MAP_POSITIVE_Z
+            GL_TEXTURE_CUBE_MAP_NEGATIVE_X
+            GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
+            GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
+        }
         [ 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 (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 >r
-        GL_FRAMEBUFFER_EXT GL_DEPTH_ATTACHMENT_EXT GL_RENDERBUFFER_EXT r>
-        glFramebufferRenderbufferEXT
+        swap [ GL_DRAW_FRAMEBUFFER GL_DEPTH_ATTACHMENT GL_RENDERBUFFER ] dip
+        glFramebufferRenderbuffer
     ] with-framebuffer ;
 
 : (plane-program) ( -- program )
@@ -172,22 +170,22 @@ M: spheres-gadget distance-step ( gadget -- dz )
     sphere-main-fragment-shader <fragment-shader> check-gl-shader
     3array <gl-program> check-gl-program ;
 
-M: spheres-gadget graft* ( gadget -- )
-    dup find-gl-context
+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
     (texture-sphere-program) >>texture-sphere-program
     (make-reflection-texture) >>reflection-texture
     (make-reflection-depthbuffer) [ >>reflection-depthbuffer ] keep
     (make-reflection-framebuffer) >>reflection-framebuffer
-    t >>initialized?
     drop ;
 
-M: spheres-gadget ungraft* ( gadget -- )
-    f >>initialized?
-    dup find-gl-context
+M: spheres-world end-world
     {
         [ reflection-framebuffer>> [ delete-framebuffer ] when* ]
         [ reflection-depthbuffer>> [ delete-renderbuffer ] when* ]
@@ -197,20 +195,17 @@ M: spheres-gadget ungraft* ( gadget -- )
         [ plane-program>> [ delete-gl-program ] when* ]
     } cleave ;
 
-M: spheres-gadget 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>> [
             {
@@ -244,18 +239,18 @@ M: spheres-gadget pref-dim* ( gadget -- dim )
     [ drop dup [ -+ ] bi@ ] 2keep ;
 
 : (reflection-face) ( gadget face -- )
-    swap reflection-texture>> >r >r
-    GL_FRAMEBUFFER_EXT
-    GL_COLOR_ATTACHMENT0_EXT
-    r> r> 0 glFramebufferTexture2DEXT
+    swap reflection-texture>> [
+        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
-            glLoadIdentity
+            glPushMatrix glLoadIdentity
             reflection-frustum glFrustum
             GL_MODELVIEW glMatrixMode
             glLoadIdentity
@@ -278,15 +273,17 @@ M: spheres-gadget pref-dim* ( gadget -- dim )
         [ 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 ]
+        [
+            [ { 0 0 } ] dip dim>> gl-viewport
+            GL_PROJECTION glMatrixMode
+            glPopMatrix
+        ]
     } cleave ] with-framebuffer ;
 
-: (draw-gadget) ( gadget -- )
-    GL_DEPTH_TEST glEnable
-    GL_SCISSOR_TEST glDisable
-    0.15 0.15 1.0 1.0 glClearColor {
+M: spheres-world draw-world*
+    {
         [ (draw-reflection-texture) ]
-        [ demo-gadget-set-matrices ]
+        [ demo-world-set-matrix ]
         [ sphere-scene ]
         [ reflection-texture>> GL_TEXTURE_CUBE_MAP GL_TEXTURE0 bind-texture-unit ]
         [
@@ -298,10 +295,13 @@ M: spheres-gadget pref-dim* ( gadget -- dim )
         ]
     } cleave ;
 
-M: spheres-gadget draw-gadget* ( gadget -- )
-    dup initialized?>> [ (draw-gadget) ] [ drop ] if ;
-
-: spheres-window ( -- )
-    [ <spheres-gadget> "Spheres" 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 } }
+    } ;