Processing 2. Креативное программирование
.pdf3D:
}else if ( lightDirection == 3 ) { directionalLight( 0, 255, 255, -1, 0, 0 ); // LEFT
}
break; case 3:
ambientLight( 0, 255, 255 ); break;
case 4:
pointLight( 255, 255, 0, 100, height*0.3, 100 ); break;
case 5:
spotLight( 128, 255, 128, 800, 20, 300, -1, .25, 0, PI, 2 );
break;
default:
noLights();
}
.rotateY() rotateX() .
pushMatrix();
translate( width/2, height/2, 0 );
pushMatrix();
rotateY( radians( frameCount ) ); fill( 255 );
noStroke(); sphere( 100 ); popMatrix();
pushMatrix();
rotateZ( radians( frameCount ) ); rotateX( radians( frameCount/2 ) ); fill( 255 );
noStroke(); box( 150 ); popMatrix();
popMatrix();
70
3
- . . , , 1,
Processing 2.
void keyPressed()
{
switch ( key ) { case 'n':
lightMode = 0; // no lights break;
case 'l':
lightMode = 1; // lights break;
case 'd':
lightMode = 2; // directional light break;
case 'a':
lightMode = 3; // ambient light break;
case 'p':
lightMode = 4; // point light break;
case 's':
lightMode = 5; // spot light break;
}
if ( key == CODED ) { switch ( keyCode ) {
case UP: lightDirection = 0; break;
case RIGHT: lightDirection = 1; break;
case DOWN: lightDirection = 2; break;
case LEFT: lightDirection = 3; break;
}
}
}
71
3D:
,N, L, D, A, P S. - ,.
- lights(). ,Processing . noLights(). , , - noLights().
directionalLight().. , . - r, g b .x, y z. -11.
ambientLight() .. . r, g b ..
pointLight() . ., - x, y z -.
72
3
spotLight(). . , - x, y z . 7, 8 9 x, y z,, directionalLight(). - . , . -.
polygon soup
3D , Processing :. , ,3D- . ,
.
, OpenGL640 x 480 . -. , -..
void cylinder( int numSegments, float h, float r )
{
float angle = 360.0 / (float)numSegments;
//top beginShape();
for ( int i = 0; i < numSegments; i++ ) { float x = cos( radians( angle * i ) ) * r; float y = sin( radians( angle * i ) ) * r; vertex( x, y, -h/2 );
}
endShape( CLOSE );
//side
beginShape( |
QUAD_STRIP ); |
|
for ( int |
i |
= 0; i < numSegments + 1; i++ ) { |
float x |
= |
cos( radians( angle * i ) ) * r; |
float y |
= |
sin( radians( angle * i ) ) * r; |
vertex( |
x, y, -h/2 ); |
|
vertex( |
x, y, h/2 ); |
|
} |
|
|
73
3D:
endShape();
// bottom beginShape();
for ( int i = 0; i < numSegments; i++ ) { float x = cos( radians( angle * i ) ) * r; float y = sin( radians( angle * i ) ) * r; vertex( x, y, h/2 );
}
endShape( CLOSE );
}
, , . draw():
background( 255 );
pushMatrix();
translate( width*.3, height*.3, 0 ); rotateY( radians( frameCount ) ); fill( 255, 0, 0 );
cylinder( 30, 100, 50 ); popMatrix();
pushMatrix();
translate( width*.7, height*.5, 0 ); rotateY( radians( frameCount ) ); fill( 255, 255, 0 );
cylinder( 4, 200, 50 ); popMatrix();
pushMatrix();
translate( width*.3, height*.7, 0 ); rotateY( radians( frameCount ) ); fill( 0, 0, 255 );
cylinder( 3, 200, 30 ); popMatrix();
74
3
, , . cylinder(),.
Polygon soup , , , - . ,, , 3D , ..
.. -.
- QUAD_STRIPbeginShape(). -..
75
3D:
3D- ,Hemesh . -, . http://hemesh.wblut.com/.
2D 3D
ၘၖၧၩၠၙၤၡၯၝ. , . ,.
, , OpenGL 640 x 480 .. -n. 3D-. :
import processing.opengl.*;
float n;
void setup()
{
size( 640, 480, OPENGL ); n = 0.0f;
}
- . n 0.01, .draw() , .
hint( ENABLE_DEPTH_TEST ); n += 0.01;
background( 255 ); lights();
noStroke();
fill( 255, 128, 0 );
pushMatrix();
for ( int i = 0; i < 17; i++ ) { for ( int j = 0; j < 13; j++ ) {
pushMatrix();
fill( i * 15, 0, j * 19 );
76
3
translate( i * 40, j * 40 );
rotateY( radians( i * 10 + frameCount ) ); rotateZ( radians( i * 10 + frameCount ) ); box( noise( i, j, n ) * 40 ); popMatrix();
}
}
popMatrix();
draw() . z., , .
noLights();
fill( 0 ); stroke( 0 );
rect( 320, 40, 200, 200 );
hint( DISABLE_DEPTH_TEST ); fill( 255 );
rect( 320, 240, 200, 200 );
, . , , , .
77
3D:
hint() . . ENABLE_DEPTH_TEST draw() z. DISABLE_ DEPTH_TEST z,.
... .
OpenGL 640 x 480. , - . -draw(). , beginShape(), for .
background( 255 ); lights();
pushMatrix();
translate( width/2, height/2, 0 ); rotateY( radians( frameCount ) );
pushMatrix();
rotateZ( radians( frameCount ) );
fill( 255, 0, 0 );
beginShape( TRIANGLE_STRIP ); for ( int i = 0; i < 20; i++ ) {
float x1 = cos( radians( i * 10 ) ) * 100; float y1 = sin( radians( i * 10 ) ) * 100;
float x2 = cos( radians( i * 10 + 5 ) ) * ( 180 - i * 4 ); float y2 = sin( radians( i * 10 + 5 ) ) * ( 180 - i * 4 );
vertex( x1, y1, 0 ); vertex( x2, y2, 50 + i );
}
endShape();
popMatrix();
78
3
, - . , QUAD_STRIP beginShape() x, y z .
pushMatrix();
translate( 0, 0, -100 );
rotateZ( radians( -frameCount ) );
fill( 255, 255, 0 );
beginShape( QUAD_STRIP );
for ( int i = 0; i < 20; i++ ) {
float x1 = cos( radians( i * 10 ) ) * ( 100 + i * 5 ); float y1 = sin( radians( i * 10 ) ) * ( 100 + i * 5 ); float x2 = cos( radians( i * 10 + 5 ) ) * 180;
float y2 = sin( radians( i * 10 + 5 ) ) * 180;
vertex( x1, y1, 0 );
vertex( x2, y2, 80 + i * 2 );
}
endShape();
popMatrix();
popMatrix();
, ., - .
79