draw-world* ( world -- )
Factor documentation > Factor handbook > UI framework > Implementing new gadgets > Customizing gadget appearance > Implementing custom drawing logic > Subclassing worlds
Prev:resize-world ( world -- )


Vocabulary
ui.gadgets.worlds

Inputs and outputs
worlda world


Word description
Called when world needs to be redrawn. The world's OpenGL context is current when this method is called.

Definition
IN: ui.gadgets.worlds

GENERIC: draw-world* ( world -- )


Methods
USING: accessors gpu.demos.bunny kernel ui.gadgets.worlds ;

M: bunny-world draw-world*
dup bunny>> dup bunny-loaded? [
dup bunny-state-filled?
[ drop ] [ fill-bunny-state ] if draw-sobeled-bunny
] [ drop draw-loading ] if ;


USING: accessors bunny bunny.model kernel math
opengl.demo-support opengl.gl sequences ui.gadgets.worlds ;

M: bunny-world draw-world*
dup draw-seq>> empty?
[ drop ] [
0.15 0.15 0.15 1.0 glClearColor
GL_DEPTH_BUFFER_BIT GL_COLOR_BUFFER_BIT bitor glClear
dup demo-world-set-matrix GL_MODELVIEW glMatrixMode
0.02 -0.105 0.0 glTranslatef [ geom>> ] [ get-draw ] bi
draw-bunny
] if ;


USING: accessors alien alien.c-types alien.data chipmunk.demo
chipmunk.ffi classes.struct kernel locals opengl.gl sequences
ui.gadgets.worlds ;

M:: chipmunk-world draw-world* ( world -- )
1 1 1 0 glClearColor GL_COLOR_BUFFER_BIT glClear
GL_PROJECTION glMatrixMode
glLoadIdentity -320 320 -240 240 -1 1 glOrtho
0.5 0.5 0 glTranslatef GL_VERTEX_ARRAY glEnableClientState
world space>> :> space 3 glPointSize 0 0 0 glColor3f
GL_POINTS glBegin space bodies>>
[ num>> ] [ arr>> swap void* <c-direct-array> ] bi
[ cpBody memory>struct p>> [ x>> ] [ y>> ] bi glVertex2f ]
each glEnd 2 glPointSize 1 0 0 glColor3f GL_POINTS glBegin
space arbiters>>
[ num>> ] [ arr>> swap void* <c-direct-array> ] bi [
cpArbiter memory>struct
[ numContacts>> ]
[ contacts>> >c-ptr swap cpContact <c-direct-array> ]
bi [ p>> [ x>> ] [ y>> ] bi glVertex2f ] each
] each glEnd ;


USING: accessors destructors fluids gpu.effects.blur
gpu.effects.step gpu.state gpu.util kernel locals make
math.vectors sequences
specialized-arrays.instances.alien.c-types.float
ui.gadgets.worlds ;

M:: fluids-world draw-world* ( world -- )
world particles>>
[ [ p>> [ first , ] [ second , ] bi ] each ] curry
float-array{ } make :> verts [
verts world texture>> 30.0 world dim>> { 4 4 } v/
blended-point-sprite-batch
&dispose blend-state new set-gpu-state
gaussian-blur &dispose world ramp>> world dim>>
step-texture &dispose world dim>> draw-texture
] with-destructors ;


USING: model-viewer ui.gadgets.worlds ;

M: model-world draw-world* draw-model ;


USING: kernel ui.gadgets.worlds ui.gadgets.worlds.null ;

M: null-world draw-world* drop ;


USING: accessors gpu.demos.raytrace gpu.render kernel
ui.gadgets.worlds ;

M: raytrace-world draw-world*
{
{ "primitive-mode" [ drop triangle-strip-mode ] }
{ "indexes" [ drop T{ index-range { count 4 } } ] }
{ "uniforms" [ <sphere-uniforms> ] }
{ "vertex-array" [ vertex-array>> ] }
} <render-set> render ;


USING: accessors combinators kernel opengl opengl.demo-support
opengl.gl opengl.shaders spheres ui.gadgets.worlds ;

M: spheres-world draw-world*
{
[ (draw-reflection-texture) ]
[ demo-world-set-matrix ]
[ sphere-scene ]
[
reflection-texture>> GL_TEXTURE_CUBE_MAP
GL_TEXTURE0 bind-texture-unit
]
[
texture-sphere-program>> [
[
"surface_texture" glGetUniformLocation
0 glUniform1i
] [ { 0.0 0.0 0.0 } 4.0 (draw-sphere) ] bi
] with-gl-program
]
} cleave ;


USING: accessors combinators grid-meshes kernel opengl
opengl.gl opengl.shaders sequences terrain ui.gadgets.worlds ;

M: terrain-world draw-world*
{
[ set-modelview-matrix ]
[
terrain-texture>> GL_TEXTURE_2D GL_TEXTURE0
bind-texture-unit
]
[
sky-texture>> GL_TEXTURE_2D GL_TEXTURE1
bind-texture-unit
]
[
GL_DEPTH_TEST glDisable dup sky-program>> [
[
nip "sky" glGetUniformLocation
1 glUniform1i
] [
"sky_gradient" glGetUniformLocation
swap sky-gradient glUniform1f
] [
"sky_theta" glGetUniformLocation
swap sky-theta glUniform1f
] 2tri { -1.0 -1.0 } { 2.0 2.0 } gl-fill-rect
] with-gl-program
]
[
GL_DEPTH_TEST glEnable dup terrain-program>> [
[
"heightmap" glGetUniformLocation
0 glUniform1i
] [
"component_scale" glGetUniformLocation
COMPONENT-SCALE first4 glUniform4f
] bi terrain-mesh>> draw-grid-mesh
] with-gl-program
]
} cleave gl-error ;


USING: game.debug.tests ui.gadgets.worlds ;

M: tests-world draw-world* draw-debug-tests ;


USING: accessors cache combinators kernel opengl.textures
ui.gadgets.worlds ui.render ;

M: world draw-world*
check-extensions {
[ init-gl ]
[ draw-gadget ]
[ text-handle>> [ purge-cache ] when* ]
[ images>> [ purge-cache ] when* ]
} cleave ;