Processing 2. Креативное программирование
.pdf, 2D
pg = createGraphics( 64, 64, JAVA2D );
background( 255 ); imageMode( CENTER );
x = 0; y = 0;
}
, draw() - PGraphics. image(). -x y .
void draw()
{
pg.beginDraw(); pg.background( 255, 0, 0, 8 ); pg.smooth();
for ( int i = 0; i < 8; i++ ) { pg.stroke( random( 255 ), 0, 0 );
pg.line( random( pg.width ), random( pg.height ), random( pg.width ), random( pg.height ) );
}
pg.endDraw();
image( pg, x, y );
x += random( 4, 16 ); if ( x > width ) {
x = 0;
y += random( 32, 64 ); if ( y > height ) {
y = 0;
fill( 255, 32 ); noStroke();
rect( 0, 0, width, height );
}
}
}
mousePressed() .50 .
void mousePressed()
{
if ( random( 100 ) < 50 ) {
60
2
background( 0 );
}else {
background( 255 );
}
}
- . ,.
createGraphics() , . - , ., - . ,P2D. PGraphics pg.beginDraw(). pg.endDraw(). , , .Processing,. , PGraphics,pg.line(), - pg.rect(). pg.width pg.height. , background() ., PGraphics.
61
, 2D
, , , ,. , setup() PGraphics. PGraphics image(), .
62
3
3D:
:
f3D
fpolygon soup
f2D 3D
f3D
2, , 2D,. 3D.
.
, -.
1, Processing 2,Processing. -.
3D:
, - OpenGL.. 2,, 2D.
Sketch | Import Library | OpenGL:
import processing.opengl.*;
, - setup(). , size(), 2.
float depth; float zSpeed;
void setup()
{
size( 640, 480, OPENGL );
depth = 0; zSpeed = -1;
}
draw() depth. 0 10000.
void draw()
{
depth += zSpeed;
if ( depth <= -1000 || depth >= 0 ) { zSpeed *= -1;
}
}
. -, .draw().
background( 255 );
noFill(); stroke( 0 );
for ( int i = 0; i < 10; i++ ) { pushMatrix();
64
3
translate( 0, 0, -i * 100 ); rect( 0, 0, width, height ); popMatrix();
}
draw() ., . .
pushMatrix();
translate( 0, 0, depth ); fill( 255, 0, 0 );
rect( 0, 0, 80, 80 ); fill( 0, 255, 0 );
rect( width-80, 0, 80, 80 ); fill( 255, 255, 0 );
rect( width-80, height-80, 80, 80 ); fill( 0, 255, 255 );
rect( 0, height-80, 80, 80 ); popMatrix();
, .
65
3D:
Processing size(), . OPENGL.OpenGL, .
, rect() ellipse()x y.z . -, translate().
translate() pushMatrix() popMatrix(),.
pushMatrix() .popMatrix(), , pushMatrix().
translate(), 3D, .x, y z , .depth z- translate(). : -(0, 0), , z.
3D
2D. Processing3D,. .
setup(). ,. OpenGL , -640 x 480 .OPENGL size().
draw(). pushMatrix(), popMatrix() translate() .rotateY(). box() sphere(). sphereDetail() .
background( 255 ); lights();
66
3
float angleY = radians( frameCount );
pushMatrix();
translate( width * 0.3, height* 0.3 ); rotateY( angleY );
fill( 0, 255, 255 ); box( 100 ); popMatrix();
pushMatrix();
translate( width * 0.5, height* 0.5 ); rotateY( angleY );
fill( 0, 255, 0 ); box( 100, 40, 50 ); popMatrix();
pushMatrix();
translate( width * 0.7, height * 0.3 ); rotateY( angleY );
fill( 255, 0, 0 ); sphereDetail( 30 ); sphere( 75 ); popMatrix();
pushMatrix();
translate( width * 0.3, height * 0.7 ); rotateY( angleY );
fill( 255, 255, 0 ); sphereDetail( 6 ); sphere( 75 ); popMatrix();
pushMatrix();
translate( width * 0.7, height * 0.7 ); rotateY( angleY );
fill( 255, 0, 255 ); sphereDetail( 4, 20 ); sphere( 75 ); popMatrix();
67
3D:
draw() . , :
box() , ., . ,.
sphere() : ., sphereDetail() sphere()., 30. -; - .,. , .
68
3
draw() angleY.3D- Y.. frameCount 1 ., Processing.
, , . Processing . ,.
, , OpenGLOpenGL 640 x 480 . setup(), lightMode lightDirection,. setup() .
lightMode = 0; lightDirection = 0;
draw() . lightMode, lightDirection, .
background( 0 );
switch ( lightMode ) { case 0:
noLights();
break; case 1:
lights();
break; case 2:
if ( lightDirection == 0 ) {
directionalLight( 255, 128, 0, 0, -1, 0 ); // UP
}else if ( lightDirection == 1 ) { directionalLight( 0, 255, 0, 1, 0, 0 ); // RIGHT
}else if ( lightDirection == 2 ) { directionalLight( 255, 0, 255, 0, 1, 0 ); // DOWN
69