update bunny to use world api; clean up projection matrix and viewport discipline in demos

db4
Joe Groff 2009-05-03 22:02:50 -05:00
parent 4504907736
commit 8925773558
4 changed files with 50 additions and 30 deletions

View File

@ -1,58 +1,67 @@
USING: accessors arrays bunny.cel-shaded bunny.fixed-pipeline USING: accessors arrays bunny.cel-shaded bunny.fixed-pipeline
bunny.model bunny.outlined destructors kernel math opengl.demo-support bunny.model bunny.outlined destructors kernel math opengl.demo-support
opengl.gl sequences ui ui.gadgets ui.gadgets.worlds ui.gestures opengl.gl sequences ui ui.gadgets ui.gadgets.worlds ui.gestures
ui.render words ; ui.render words ui.pixel-formats ;
IN: bunny IN: bunny
TUPLE: bunny-gadget < demo-gadget model-triangles geom draw-seq draw-n ; TUPLE: bunny-world < demo-world model-triangles geom draw-seq draw-n ;
: <bunny-gadget> ( -- bunny-gadget ) : get-draw ( gadget -- draw )
0.0 0.0 0.375 bunny-gadget new-demo-gadget
maybe-download read-model >>model-triangles ;
: bunny-gadget-draw ( gadget -- draw )
[ draw-n>> ] [ draw-seq>> ] bi nth ; [ draw-n>> ] [ draw-seq>> ] bi nth ;
: bunny-gadget-next-draw ( gadget -- ) : next-draw ( gadget -- )
dup [ draw-seq>> ] [ draw-n>> ] bi dup [ draw-seq>> ] [ draw-n>> ] bi
1+ swap length mod 1+ swap length mod
>>draw-n relayout-1 ; >>draw-n relayout-1 ;
M: bunny-gadget graft* ( gadget -- ) : make-draws ( gadget -- draw-seq )
dup find-gl-context
GL_DEPTH_TEST glEnable
dup model-triangles>> <bunny-geom> >>geom
dup
[ <bunny-fixed-pipeline> ] [ <bunny-fixed-pipeline> ]
[ <bunny-cel-shaded> ] [ <bunny-cel-shaded> ]
[ <bunny-outlined> ] tri 3array [ <bunny-outlined> ] tri 3array
sift >>draw-seq sift ;
M: bunny-world begin-world
GL_DEPTH_TEST glEnable
0.0 0.0 0.375 set-demo-orientation
maybe-download read-model
[ >>model-triangles ] [ <bunny-geom> >>geom ] bi
dup make-draws >>draw-seq
0 >>draw-n 0 >>draw-n
drop ; drop ;
M: bunny-gadget ungraft* ( gadget -- ) M: bunny-world end-world
dup find-gl-context dup find-gl-context
[ geom>> [ dispose ] when* ] [ geom>> [ dispose ] when* ]
[ draw-seq>> [ [ dispose ] when* ] each ] bi ; [ draw-seq>> [ [ dispose ] when* ] each ] bi ;
M: bunny-gadget draw-gadget* ( gadget -- ) M: bunny-world draw-world*
dup draw-seq>> empty? [ drop ] [ dup draw-seq>> empty? [ drop ] [
0.15 0.15 0.15 1.0 glClearColor 0.15 0.15 0.15 1.0 glClearColor
GL_DEPTH_BUFFER_BIT GL_COLOR_BUFFER_BIT bitor glClear GL_DEPTH_BUFFER_BIT GL_COLOR_BUFFER_BIT bitor glClear
dup demo-gadget-set-matrices dup demo-world-set-matrix
GL_MODELVIEW glMatrixMode GL_MODELVIEW glMatrixMode
0.02 -0.105 0.0 glTranslatef 0.02 -0.105 0.0 glTranslatef
[ geom>> ] [ bunny-gadget-draw ] bi draw-bunny [ geom>> ] [ get-draw ] bi draw-bunny
] if ; ] if ;
M: bunny-gadget pref-dim* ( gadget -- dim ) M: bunny-world pref-dim* ( gadget -- dim )
drop { 640 480 } ; drop { 640 480 } ;
bunny-gadget H{ bunny-world H{
{ T{ key-down f f "TAB" } [ bunny-gadget-next-draw ] } { T{ key-down f f "TAB" } [ next-draw ] }
} set-gestures } set-gestures
: bunny-window ( -- ) : bunny-window ( -- )
[ <bunny-gadget> "Bunny" open-window ] with-ui ; [
f T{ world-attributes
{ world-class bunny-world }
{ title "Bunny" }
{ pixel-format-attributes {
windowed
double-buffered
T{ depth-bits { value 16 } }
} }
} open-window
] with-ui ;
MAIN: bunny-window MAIN: bunny-window

View File

@ -216,7 +216,11 @@ MACRO: (framebuffer-texture>>draw) ( iformat xformat setter -- )
] with-framebuffer ; ] with-framebuffer ;
: (pass2) ( draw -- ) : (pass2) ( draw -- )
init-matrices { GL_PROJECTION glMatrixMode
glPushMatrix glLoadIdentity
GL_MODELVIEW glMatrixMode
glLoadIdentity
{
[ color-texture>> GL_TEXTURE_2D GL_TEXTURE0 bind-texture-unit ] [ color-texture>> GL_TEXTURE_2D GL_TEXTURE0 bind-texture-unit ]
[ normal-texture>> GL_TEXTURE_2D GL_TEXTURE1 bind-texture-unit ] [ normal-texture>> GL_TEXTURE_2D GL_TEXTURE1 bind-texture-unit ]
[ depth-texture>> GL_TEXTURE_2D GL_TEXTURE2 bind-texture-unit ] [ depth-texture>> GL_TEXTURE_2D GL_TEXTURE2 bind-texture-unit ]
@ -230,7 +234,9 @@ MACRO: (framebuffer-texture>>draw) ( iformat xformat setter -- )
} cleave { -1.0 -1.0 } { 1.0 1.0 } rect-vertices } cleave { -1.0 -1.0 } { 1.0 1.0 } rect-vertices
] with-gl-program ] with-gl-program
] ]
} cleave ; } cleave
GL_PROJECTION glMatrixMode
glPopMatrix ;
M: bunny-outlined draw-bunny M: bunny-outlined draw-bunny
[ remake-framebuffer-if-needed ] [ remake-framebuffer-if-needed ]

View File

@ -45,16 +45,17 @@ M: demo-world pref-dim* ( gadget -- dim )
: -+ ( x -- -x x ) : -+ ( x -- -x x )
[ neg ] keep ; [ neg ] keep ;
: demo-world-frustum ( gadget -- -x x -y y near far ) : demo-world-frustum ( world -- -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 ;
M: demo-world begin-world M: demo-world resize-world
GL_PROJECTION glMatrixMode GL_PROJECTION glMatrixMode
glLoadIdentity glLoadIdentity
demo-world-frustum glFrustum ; [ [ 0 0 ] dip dim>> first2 glViewport ]
[ demo-world-frustum glFrustum ] bi ;
: demo-world-set-matrix ( gadget -- ) : 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

View File

@ -171,7 +171,7 @@ M: spheres-world 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 ;
AFTER: spheres-world begin-world M: spheres-world begin-world
"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 20.0 10.0 20.0 set-demo-orientation
@ -251,7 +251,7 @@ M: spheres-world pref-dim* ( gadget -- dim )
[ drop 0 0 (reflection-dim) glViewport ] [ drop 0 0 (reflection-dim) glViewport ]
[ [
GL_PROJECTION glMatrixMode GL_PROJECTION glMatrixMode
glLoadIdentity glPushMatrix glLoadIdentity
reflection-frustum glFrustum reflection-frustum glFrustum
GL_MODELVIEW glMatrixMode GL_MODELVIEW glMatrixMode
glLoadIdentity glLoadIdentity
@ -274,7 +274,11 @@ M: spheres-world pref-dim* ( gadget -- dim )
[ GL_TEXTURE_CUBE_MAP_POSITIVE_Y (reflection-face) [ GL_TEXTURE_CUBE_MAP_POSITIVE_Y (reflection-face)
glPopMatrix 90.0 1.0 0.0 0.0 glRotatef ] glPopMatrix 90.0 1.0 0.0 0.0 glRotatef ]
[ sphere-scene ] [ sphere-scene ]
[ dim>> 0 0 rot first2 glViewport ] [
[ 0 0 ] dip dim>> first2 glViewport
GL_PROJECTION glMatrixMode
glPopMatrix
]
} cleave ] with-framebuffer ; } cleave ] with-framebuffer ;
M: spheres-world draw-world* M: spheres-world draw-world*