From 1c091ed24b04cc85f639616af2e0ba80d71aef1b Mon Sep 17 00:00:00 2001 From: Eduardo Cavazos Date: Mon, 28 Jul 2008 00:48:29 -0500 Subject: [PATCH 1/6] processing.shapes: Factor out shape drawing code. It is not specific to processing. --- extra/processing/shapes/shapes.factor | 112 ++++++++++++++++++++++++++ 1 file changed, 112 insertions(+) create mode 100644 extra/processing/shapes/shapes.factor diff --git a/extra/processing/shapes/shapes.factor b/extra/processing/shapes/shapes.factor new file mode 100644 index 0000000000..6f680a87e6 --- /dev/null +++ b/extra/processing/shapes/shapes.factor @@ -0,0 +1,112 @@ + +USING: kernel namespaces arrays sequences grouping + alien.c-types + math math.vectors math.geometry.rect + opengl.gl opengl.glu opengl generalizations vars + combinators.cleave ; + +IN: processing.shapes + +! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +VAR: fill-color +VAR: stroke-color + +{ 0 0 0 1 } stroke-color set-global +{ 1 1 1 1 } fill-color set-global + +! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +: fill-mode ( -- ) + GL_FRONT_AND_BACK GL_FILL glPolygonMode + fill-color> first4 glColor4d ; + +! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +: stroke-mode ( -- ) + GL_FRONT_AND_BACK GL_LINE glPolygonMode + stroke-color> first4 glColor4d ; + +! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +: gl-vertex-2d ( vertex -- ) first2 glVertex2d ; + +: gl-vertices-2d ( vertices -- ) [ gl-vertex-2d ] each ; + +! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +: point* ( x y -- ) stroke-mode GL_POINTS [ glVertex2d ] do-state ; +: point ( point -- ) stroke-mode GL_POINTS [ gl-vertex-2d ] do-state ; +: points ( points -- ) stroke-mode GL_POINTS [ gl-vertices-2d ] do-state ; + +! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +: line** ( x y x y -- ) + stroke-mode GL_LINES [ glVertex2d glVertex2d ] do-state ; + +: line* ( a b -- ) stroke-mode GL_LINES [ [ gl-vertex-2d ] bi@ ] do-state ; + +: lines ( seq -- ) stroke-mode GL_LINES [ gl-vertices-2d ] do-state ; + +: line ( seq -- ) lines ; + +! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +: triangles ( seq -- ) + [ fill-mode GL_TRIANGLES [ gl-vertices-2d ] do-state ] + [ stroke-mode GL_TRIANGLES [ gl-vertices-2d ] do-state ] bi ; + +: triangle ( seq -- ) triangles ; + +: triangle* ( a b c -- ) 3array triangles ; + +: triangle** ( x y x y x y -- ) 6 narray 2 group triangles ; + +! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +: polygon ( seq -- ) + [ fill-mode GL_POLYGON [ gl-vertices-2d ] do-state ] + [ stroke-mode GL_POLYGON [ gl-vertices-2d ] do-state ] bi ; + +! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +: rectangle ( loc dim -- ) + + { top-left top-right bottom-right bottom-left } + 1arr + polygon ; + +: rectangle* ( x y width height -- ) [ 2array ] 2bi@ rectangle ; + +! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +: gl-translate-2d ( pos -- ) first2 0 glTranslated ; + +: gl-scale-2d ( xy -- ) first2 1 glScaled ; + +! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +: gl-ellipse ( center dim -- ) + glPushMatrix + [ gl-translate-2d ] [ gl-scale-2d ] bi* + gluNewQuadric + dup 0 0.5 20 1 gluDisk + gluDeleteQuadric + glPopMatrix ; + +! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +: gl-get-line-width ( -- width ) + GL_LINE_WIDTH 0 tuck glGetDoublev *double ; + +: ellipse ( center dim -- ) + GL_FRONT_AND_BACK GL_FILL glPolygonMode + [ stroke-color> gl-color gl-ellipse ] + [ fill-color> gl-color gl-get-line-width 2 * dup 2array v- gl-ellipse ] 2bi ; + +! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +: circle ( center size -- ) dup 2array ellipse ; + +! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + From d6ad62ebf031a247b97ad0b47667df1006de6230 Mon Sep 17 00:00:00 2001 From: Eduardo Cavazos Date: Mon, 28 Jul 2008 00:49:34 -0500 Subject: [PATCH 2/6] golden-section: Use processing.shapes --- extra/golden-section/golden-section.factor | 24 ++++++++-------------- 1 file changed, 9 insertions(+), 15 deletions(-) diff --git a/extra/golden-section/golden-section.factor b/extra/golden-section/golden-section.factor index 8ae8bccc25..a83dc988fd 100644 --- a/extra/golden-section/golden-section.factor +++ b/extra/golden-section/golden-section.factor @@ -1,21 +1,14 @@ -USING: kernel namespaces math math.constants math.functions arrays sequences +USING: kernel namespaces math math.constants math.functions math.order + arrays sequences opengl opengl.gl opengl.glu ui ui.render ui.gadgets ui.gadgets.theme - ui.gadgets.slate colors accessors combinators.cleave ; + ui.gadgets.slate colors accessors combinators.cleave + processing.shapes ; IN: golden-section ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -: disk ( radius center -- ) - glPushMatrix - gl-translate - dup 0 glScalef - gluNewQuadric [ 0 1 20 20 gluDisk ] [ gluDeleteQuadric ] bi - glPopMatrix ; - -! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! - ! omega(i) = 2*pi*i*(phi-1) ! x(i) = 0.5*i*cos(omega(i)) @@ -34,12 +27,13 @@ IN: golden-section : radius ( i -- radius ) pi * 720 / sin 10 * ; -: color ( i -- color ) 360.0 / dup 0.25 1 4array ; +: color ( i -- i ) dup 360.0 / dup 0.25 1 4array >fill-color ; -: rim ( i -- ) [ drop black gl-color ] [ radius 1.5 * ] [ center ] tri disk ; -: inner ( i -- ) [ color gl-color ] [ radius ] [ center ] tri disk ; +: line-width ( i -- i ) dup radius 0.5 * 1 max glLineWidth ; -: dot ( i -- ) [ rim ] [ inner ] bi ; +: draw ( i -- ) [ center ] [ radius 1.5 * 2 * ] bi circle ; + +: dot ( i -- ) color line-width draw ; : golden-section ( -- ) 720 [ dot ] each ; From 7a3a0d3677b523442b11b605f888ba7bb91fb6f6 Mon Sep 17 00:00:00 2001 From: Eduardo Cavazos Date: Mon, 28 Jul 2008 00:50:08 -0500 Subject: [PATCH 3/6] boids: Up the initial boids count to 100 --- extra/boids/boids.factor | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/extra/boids/boids.factor b/extra/boids/boids.factor index ab624a606b..8c045ee270 100644 --- a/extra/boids/boids.factor +++ b/extra/boids/boids.factor @@ -220,7 +220,7 @@ cond ; ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -: init-boids ( -- ) 50 random-boids >boids ; +: init-boids ( -- ) 100 random-boids >boids ; : init-world-size ( -- ) { 100 100 } >world-size ; From 86a881f1f2b8facdb37258610f87298abffa0963 Mon Sep 17 00:00:00 2001 From: Eduardo Cavazos Date: Mon, 28 Jul 2008 00:50:22 -0500 Subject: [PATCH 4/6] boids.ui: Use processing.shapes --- extra/boids/ui/ui.factor | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/extra/boids/ui/ui.factor b/extra/boids/ui/ui.factor index 064eda841b..f380441960 100755 --- a/extra/boids/ui/ui.factor +++ b/extra/boids/ui/ui.factor @@ -1,6 +1,7 @@ USING: combinators.short-circuit kernel namespaces math + math.trig math.functions math.vectors math.parser @@ -21,7 +22,8 @@ USING: combinators.short-circuit kernel namespaces ui.gestures assocs.lib vars rewrite-closures boids accessors math.geometry.rect - newfx ; + newfx + processing.shapes ; IN: boids.ui @@ -29,17 +31,22 @@ IN: boids.ui ! draw-boid ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -: point-a ( boid -- a ) pos>> ; - -: point-b ( boid -- b ) [ pos>> ] [ vel>> normalize* 20 v*n ] bi v+ ; - -: boid-points ( boid -- point-a point-b ) [ point-a ] [ point-b ] bi ; - -: draw-boid ( boid -- ) boid-points gl-line ; +: draw-boid ( boid -- ) + glPushMatrix + dup pos>> gl-translate-2d + vel>> first2 rect> arg rad>deg 0 0 1 glRotated + { { 0 5 } { 0 -5 } { 20 0 } } triangle + glPopMatrix ; : draw-boids ( -- ) boids> [ draw-boid ] each ; -: display ( -- ) black gl-color draw-boids ; +: boid-color ( -- color ) { 1.0 0 0 0.3 } ; + +: display ( -- ) + white gl-clear + boid-color >fill-color + 2 glLineWidth + draw-boids ; ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! From d2352a15e6b5e039d4e249b590ce37b4ea9e8c7c Mon Sep 17 00:00:00 2001 From: Eduardo Cavazos Date: Mon, 28 Jul 2008 01:41:10 -0500 Subject: [PATCH 5/6] processing.shapes: Use 'gl-color' in a couple of places --- extra/processing/shapes/shapes.factor | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/extra/processing/shapes/shapes.factor b/extra/processing/shapes/shapes.factor index 6f680a87e6..16530c5414 100644 --- a/extra/processing/shapes/shapes.factor +++ b/extra/processing/shapes/shapes.factor @@ -19,13 +19,13 @@ VAR: stroke-color : fill-mode ( -- ) GL_FRONT_AND_BACK GL_FILL glPolygonMode - fill-color> first4 glColor4d ; + fill-color> gl-color ; ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! : stroke-mode ( -- ) GL_FRONT_AND_BACK GL_LINE glPolygonMode - stroke-color> first4 glColor4d ; + stroke-color> gl-color ; ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! From 4f10ed4aaf1dd545c2e44a8266e27f8ebe0c12e8 Mon Sep 17 00:00:00 2001 From: Eduardo Cavazos Date: Mon, 28 Jul 2008 01:41:49 -0500 Subject: [PATCH 6/6] boids.ui: Add workaround for display glitch --- extra/boids/ui/ui.factor | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/extra/boids/ui/ui.factor b/extra/boids/ui/ui.factor index f380441960..38dd9b4f78 100755 --- a/extra/boids/ui/ui.factor +++ b/extra/boids/ui/ui.factor @@ -36,6 +36,7 @@ IN: boids.ui dup pos>> gl-translate-2d vel>> first2 rect> arg rad>deg 0 0 1 glRotated { { 0 5 } { 0 -5 } { 20 0 } } triangle + fill-mode glPopMatrix ; : draw-boids ( -- ) boids> [ draw-boid ] each ; @@ -43,9 +44,7 @@ IN: boids.ui : boid-color ( -- color ) { 1.0 0 0 0.3 } ; : display ( -- ) - white gl-clear boid-color >fill-color - 2 glLineWidth draw-boids ; ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!