change spheres to use new world api

db4
Joe Groff 2009-05-03 17:14:49 -05:00
parent 474735a60c
commit 4e8df4a190
2 changed files with 68 additions and 65 deletions

View File

@ -1,6 +1,6 @@
USING: arrays kernel math math.functions math.order math.vectors USING: arrays kernel math math.functions math.order math.vectors
namespaces opengl opengl.gl sequences ui ui.gadgets ui.gestures namespaces opengl opengl.gl sequences ui ui.gadgets ui.gestures
ui.render accessors combinators ; ui.gadgets.worlds ui.render accessors combinators ;
IN: opengl.demo-support IN: opengl.demo-support
: FOV ( -- x ) 2.0 sqrt 1+ ; inline : FOV ( -- x ) 2.0 sqrt 1+ ; inline
@ -9,62 +9,61 @@ CONSTANT: KEY-ROTATE-STEP 10.0
SYMBOL: last-drag-loc SYMBOL: last-drag-loc
TUPLE: demo-gadget < gadget yaw pitch distance ; TUPLE: demo-world < world yaw pitch distance ;
: new-demo-gadget ( yaw pitch distance class -- gadget ) : set-demo-orientation ( world yaw pitch distance -- world )
new [ >>yaw ] [ >>pitch ] [ >>distance ] tri* ;
swap >>distance
swap >>pitch
swap >>yaw ; inline
GENERIC: far-plane ( gadget -- z ) GENERIC: far-plane ( gadget -- z )
GENERIC: near-plane ( gadget -- z ) GENERIC: near-plane ( gadget -- z )
GENERIC: distance-step ( gadget -- dz ) GENERIC: distance-step ( gadget -- dz )
M: demo-gadget far-plane ( gadget -- z ) M: demo-world far-plane ( gadget -- z )
drop 4.0 ; drop 4.0 ;
M: demo-gadget near-plane ( gadget -- z ) M: demo-world near-plane ( gadget -- z )
drop 1.0 64.0 / ; drop 1.0 64.0 / ;
M: demo-gadget distance-step ( gadget -- dz ) M: demo-world distance-step ( gadget -- dz )
drop 1.0 64.0 / ; drop 1.0 64.0 / ;
: fov-ratio ( gadget -- fov ) dim>> dup first2 min v/n ; : fov-ratio ( gadget -- fov ) dim>> dup first2 min v/n ;
: yaw-demo-gadget ( yaw gadget -- ) : yaw-demo-world ( yaw gadget -- )
[ + ] with change-yaw relayout-1 ; [ + ] with change-yaw relayout-1 ;
: pitch-demo-gadget ( pitch gadget -- ) : pitch-demo-world ( pitch gadget -- )
[ + ] with change-pitch relayout-1 ; [ + ] with change-pitch relayout-1 ;
: zoom-demo-gadget ( distance gadget -- ) : zoom-demo-world ( distance gadget -- )
[ + ] with change-distance relayout-1 ; [ + ] with change-distance relayout-1 ;
M: demo-gadget pref-dim* ( gadget -- dim ) M: demo-world focusable-child* ( world -- gadget )
drop t ;
M: demo-world pref-dim* ( gadget -- dim )
drop { 640 480 } ; drop { 640 480 } ;
: -+ ( x -- -x x ) : -+ ( x -- -x x )
[ neg ] keep ; [ neg ] keep ;
: demo-gadget-frustum ( gadget -- -x x -y y near far ) : demo-world-frustum ( gadget -- -x x -y y near far )
[ near-plane ] [ far-plane ] [ fov-ratio ] tri [ [ near-plane ] [ far-plane ] [ fov-ratio ] tri [
nip swap FOV / v*n nip swap FOV / v*n
first2 [ -+ ] bi@ first2 [ -+ ] bi@
] 3keep drop ; ] 3keep drop ;
: demo-gadget-set-matrices ( gadget -- ) M: demo-world begin-world
GL_PROJECTION glMatrixMode
glLoadIdentity
demo-world-frustum glFrustum ;
: demo-world-set-matrix ( gadget -- )
GL_COLOR_BUFFER_BIT GL_DEPTH_BUFFER_BIT bitor glClear GL_COLOR_BUFFER_BIT GL_DEPTH_BUFFER_BIT bitor glClear
[ GL_MODELVIEW glMatrixMode
GL_PROJECTION glMatrixMode glLoadIdentity
glLoadIdentity [ [ 0.0 0.0 ] dip distance>> neg glTranslatef ]
demo-gadget-frustum glFrustum [ pitch>> 1.0 0.0 0.0 glRotatef ]
] [ [ yaw>> 0.0 1.0 0.0 glRotatef ]
GL_MODELVIEW glMatrixMode tri ;
glLoadIdentity
[ [ 0.0 0.0 ] dip distance>> neg glTranslatef ]
[ pitch>> 1.0 0.0 0.0 glRotatef ]
[ yaw>> 0.0 1.0 0.0 glRotatef ]
tri
] bi ;
: reset-last-drag-rel ( -- ) : reset-last-drag-rel ( -- )
{ 0 0 } last-drag-loc set-global ; { 0 0 } last-drag-loc set-global ;
@ -94,16 +93,16 @@ M: demo-gadget pref-dim* ( gadget -- dim )
swap first swap second glVertex2d swap first swap second glVertex2d
] do-state ; ] do-state ;
demo-gadget H{ demo-world H{
{ T{ key-down f f "LEFT" } [ KEY-ROTATE-STEP neg swap yaw-demo-gadget ] } { T{ key-down f f "LEFT" } [ KEY-ROTATE-STEP neg swap yaw-demo-world ] }
{ T{ key-down f f "RIGHT" } [ KEY-ROTATE-STEP swap yaw-demo-gadget ] } { T{ key-down f f "RIGHT" } [ KEY-ROTATE-STEP swap yaw-demo-world ] }
{ T{ key-down f f "DOWN" } [ KEY-ROTATE-STEP neg swap pitch-demo-gadget ] } { T{ key-down f f "DOWN" } [ KEY-ROTATE-STEP neg swap pitch-demo-world ] }
{ T{ key-down f f "UP" } [ KEY-ROTATE-STEP swap pitch-demo-gadget ] } { T{ key-down f f "UP" } [ KEY-ROTATE-STEP swap pitch-demo-world ] }
{ T{ key-down f f "=" } [ dup distance-step neg swap zoom-demo-gadget ] } { T{ key-down f f "=" } [ dup distance-step neg swap zoom-demo-world ] }
{ T{ key-down f f "-" } [ dup distance-step swap zoom-demo-gadget ] } { T{ key-down f f "-" } [ dup distance-step swap zoom-demo-world ] }
{ T{ button-down f f 1 } [ drop reset-last-drag-rel ] } { T{ button-down f f 1 } [ drop reset-last-drag-rel ] }
{ T{ drag f 1 } [ drag-yaw-pitch rot [ pitch-demo-gadget ] keep yaw-demo-gadget ] } { T{ drag f 1 } [ drag-yaw-pitch rot [ pitch-demo-world ] keep yaw-demo-world ] }
{ mouse-scroll [ scroll-direction get second over distance-step * swap zoom-demo-gadget ] } { mouse-scroll [ scroll-direction get second over distance-step * swap zoom-demo-world ] }
} set-gestures } set-gestures

View File

@ -1,7 +1,8 @@
USING: kernel opengl opengl.demo-support opengl.gl opengl.textures USING: kernel opengl opengl.demo-support opengl.gl opengl.textures
opengl.shaders opengl.framebuffers opengl.capabilities multiline opengl.shaders opengl.framebuffers opengl.capabilities multiline
ui.gadgets accessors sequences ui.render ui math locals arrays ui.gadgets accessors sequences ui.render ui math locals arrays
generalizations combinators ui.gadgets.worlds ; generalizations combinators ui.gadgets.worlds method-chains
literals ui.pixel-formats ;
IN: spheres IN: spheres
STRING: plane-vertex-shader STRING: plane-vertex-shader
@ -110,19 +111,16 @@ main()
} }
; ;
TUPLE: spheres-gadget < demo-gadget TUPLE: spheres-world < demo-world
plane-program solid-sphere-program texture-sphere-program plane-program solid-sphere-program texture-sphere-program
reflection-framebuffer reflection-depthbuffer reflection-framebuffer reflection-depthbuffer
reflection-texture initialized? ; reflection-texture ;
: <spheres-gadget> ( -- gadget ) M: spheres-world near-plane ( gadget -- z )
20.0 10.0 20.0 spheres-gadget new-demo-gadget ;
M: spheres-gadget near-plane ( gadget -- z )
drop 1.0 ; drop 1.0 ;
M: spheres-gadget far-plane ( gadget -- z ) M: spheres-world far-plane ( gadget -- z )
drop 512.0 ; drop 512.0 ;
M: spheres-gadget distance-step ( gadget -- dz ) M: spheres-world distance-step ( gadget -- dz )
drop 0.5 ; drop 0.5 ;
: (reflection-dim) ( -- w h ) : (reflection-dim) ( -- w h )
@ -136,12 +134,14 @@ 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_S GL_CLAMP glTexParameteri
GL_TEXTURE_CUBE_MAP GL_TEXTURE_WRAP_T 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 GL_TEXTURE_WRAP_R GL_CLAMP glTexParameteri
GL_TEXTURE_CUBE_MAP_POSITIVE_X {
GL_TEXTURE_CUBE_MAP_POSITIVE_Y $ GL_TEXTURE_CUBE_MAP_POSITIVE_X
GL_TEXTURE_CUBE_MAP_POSITIVE_Z $ GL_TEXTURE_CUBE_MAP_POSITIVE_Y
GL_TEXTURE_CUBE_MAP_NEGATIVE_X $ GL_TEXTURE_CUBE_MAP_POSITIVE_Z
GL_TEXTURE_CUBE_MAP_NEGATIVE_Y $ GL_TEXTURE_CUBE_MAP_NEGATIVE_X
GL_TEXTURE_CUBE_MAP_NEGATIVE_Z 6 narray $ 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 ] [ 0 GL_RGBA8 (reflection-dim) 0 GL_RGBA GL_UNSIGNED_BYTE f glTexImage2D ]
each each
] keep ; ] keep ;
@ -171,22 +171,19 @@ M: spheres-gadget distance-step ( gadget -- dz )
sphere-main-fragment-shader <fragment-shader> check-gl-shader sphere-main-fragment-shader <fragment-shader> check-gl-shader
3array <gl-program> check-gl-program ; 3array <gl-program> check-gl-program ;
M: spheres-gadget graft* ( gadget -- ) AFTER: spheres-world begin-world
dup find-gl-context
"2.0" { "GL_ARB_shader_objects" } require-gl-version-or-extensions "2.0" { "GL_ARB_shader_objects" } require-gl-version-or-extensions
{ "GL_EXT_framebuffer_object" } require-gl-extensions { "GL_EXT_framebuffer_object" } require-gl-extensions
20.0 10.0 20.0 set-demo-orientation
(plane-program) >>plane-program (plane-program) >>plane-program
(solid-sphere-program) >>solid-sphere-program (solid-sphere-program) >>solid-sphere-program
(texture-sphere-program) >>texture-sphere-program (texture-sphere-program) >>texture-sphere-program
(make-reflection-texture) >>reflection-texture (make-reflection-texture) >>reflection-texture
(make-reflection-depthbuffer) [ >>reflection-depthbuffer ] keep (make-reflection-depthbuffer) [ >>reflection-depthbuffer ] keep
(make-reflection-framebuffer) >>reflection-framebuffer (make-reflection-framebuffer) >>reflection-framebuffer
t >>initialized?
drop ; drop ;
M: spheres-gadget ungraft* ( gadget -- ) M: spheres-world end-world
f >>initialized?
dup find-gl-context
{ {
[ reflection-framebuffer>> [ delete-framebuffer ] when* ] [ reflection-framebuffer>> [ delete-framebuffer ] when* ]
[ reflection-depthbuffer>> [ delete-renderbuffer ] when* ] [ reflection-depthbuffer>> [ delete-renderbuffer ] when* ]
@ -196,7 +193,7 @@ M: spheres-gadget ungraft* ( gadget -- )
[ plane-program>> [ delete-gl-program ] when* ] [ plane-program>> [ delete-gl-program ] when* ]
} cleave ; } cleave ;
M: spheres-gadget pref-dim* ( gadget -- dim ) M: spheres-world pref-dim* ( gadget -- dim )
drop { 640 480 } ; drop { 640 480 } ;
:: (draw-sphere) ( program center radius -- ) :: (draw-sphere) ( program center radius -- )
@ -280,12 +277,12 @@ M: spheres-gadget pref-dim* ( gadget -- dim )
[ dim>> 0 0 rot first2 glViewport ] [ dim>> 0 0 rot first2 glViewport ]
} cleave ] with-framebuffer ; } cleave ] with-framebuffer ;
: (draw-gadget) ( gadget -- ) M: spheres-world draw-world*
GL_DEPTH_TEST glEnable GL_DEPTH_TEST glEnable
GL_SCISSOR_TEST glDisable GL_SCISSOR_TEST glDisable
0.15 0.15 1.0 1.0 glClearColor { 0.15 0.15 1.0 1.0 glClearColor {
[ (draw-reflection-texture) ] [ (draw-reflection-texture) ]
[ demo-gadget-set-matrices ] [ demo-world-set-matrix ]
[ sphere-scene ] [ sphere-scene ]
[ reflection-texture>> GL_TEXTURE_CUBE_MAP GL_TEXTURE0 bind-texture-unit ] [ reflection-texture>> GL_TEXTURE_CUBE_MAP GL_TEXTURE0 bind-texture-unit ]
[ [
@ -297,10 +294,17 @@ M: spheres-gadget pref-dim* ( gadget -- dim )
] ]
} cleave ; } cleave ;
M: spheres-gadget draw-gadget* ( gadget -- )
dup initialized?>> [ (draw-gadget) ] [ drop ] if ;
: spheres-window ( -- ) : spheres-window ( -- )
[ <spheres-gadget> "Spheres" open-window ] with-ui ; [
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: spheres-window