Untitled
processing
posted: Apr, 17th 2012 | jump to bottom
import javax.media.opengl.*;
import processing.opengl.*;
void setup(){
size( 640, 480, OPENGL );
smooth();
strokeWeight(4);
}
class Matrix4 {
float[] data = new float[16];
Matrix4( float m00, float m01, float m02, float m03,
float m10, float m11, float m12, float m13,
float m20, float m21, float m22, float m23,
float m30, float m31, float m32, float m33
){
data[0] = m00; data[1] = m01; data[2] = m02; data[3] = m03;
data[4] = m10; data[5] = m11; data[6] = m12; data[7] = m13;
data[8] = m20; data[9] = m21; data[10] = m22; data[11] = m23;
data[12] = m30; data[13] = m31; data[14] = m32; data[15] = m33;
}
Vec4 mult( Vec4 v ) {
Vec4 r = new Vec4();
r.x = v.x * get(0,0) + v.y * get(0,1) + v.z * get(0,2) + v.w * get(0,3);
r.y = v.x * get(1,0) + v.y * get(1,1) + v.z * get(1,2) + v.w * get(1,3);
r.z = v.x * get(2,0) + v.y * get(2,1) + v.z * get(2,2) + v.w * get(2,3);
r.w = v.x * get(3,0) + v.y * get(3,1) + v.z * get(3,2) + v.w * get(3,3);
return r;
}
float get( int y, int x ){
return data[ x + 4*y ];
}
}
Matrix4 rotXY( float ang ){ang *= PI / 180.0; return new Matrix4(cos( ang ),sin( ang ),0,0,-sin( ang ),cos( ang ),0,0,0,0,1,0,0,0,0,1); }
Matrix4 rotYZ( float ang ){ang *= PI / 180.0; return new Matrix4(1,0,0,0,0,cos( ang ),sin( ang ),0,0,-sin( ang ),cos( ang ),0,0,0,0,1); }
Matrix4 rotZX( float ang ){ang *= PI / 180.0; return new Matrix4(cos( ang ),0,-sin( ang ),0,0,1,0,0,sin( ang ),0,cos( ang ),0,0,0,0,1); }
Matrix4 rotXW( float ang ){ang *= PI / 180.0; return new Matrix4(cos( ang ),0,0,sin( ang ),0,1,0,0,0,0,1,0,-sin( ang ),0,0,cos( ang )); }
Matrix4 rotYW( float ang ){ang *= PI / 180.0; return new Matrix4(1,0,0,0,0,cos( ang ),0,-sin( ang ),0,0,1,0,0,sin( ang ),0,cos( ang )); }
Matrix4 rotZW( float ang ){ang *= PI / 180.0; return new Matrix4(1,0,0,0,0,1,0,0,0,0,cos( ang ),-sin( ang ),0,0,sin( ang ),cos( ang )); }
class Vec3 {
float x,y,z;
Vec3(){ x = y = z = 0; }
Vec3( float x, float y, float z ){ this.x = x; this.y = y; this.z = z; }
Vec3 add( Vec3 v ){ return new Vec3( x + v.x, y + v.y, z + v.z); }
Vec3 sub( Vec3 v ){ return new Vec3( x - v.x, y - v.y, z - v.z); }
Vec3 mult( float s ){ return new Vec3( x*s, y*s, z*s); }
}
class Vec4 {
float x,y,z,w;
Vec4(){ x = y = z = w = 0; }
Vec4( float x, float y, float z, float w ){ this.x = x; this.y = y; this.z = z; this.w = w; }
Vec4 add( Vec4 v ){ return new Vec4( x + v.x, y + v.y, z + v.z, w + v.w); }
Vec4 sub( Vec4 v ){ return new Vec4( x - v.x, y - v.y, z - v.z, w - v.w); }
Vec4 mult( float s ){ return new Vec4( x*s, y*s, z*s, w*s); }
}
Vec3 proj43( Vec4 p ){
float s = ((1-p.w) + 0.3);
return new Vec3( p.x*s, p.y*s, p.z*s );
}
class Box4 {
Vec4[] axies = new Vec4[3];
Vec4[] vert = new Vec4[16];
byte[][] conn = new byte[32][2];
byte[][] face = new byte[24][4];
float size;
Box4( float size ){
this.size = size;
for(int i = 0; i < 16; i++ ){
float x = ((i >> 3) & 1) - 0.5;
float y = ((i >> 2) & 1) - 0.5;
float z = ((i >> 1) & 1) - 0.5;
float w = ((i >> 0) & 1) - 0.5;
vert[i] = new Vec4(x,y,z,w);
}
int k = 0, f = 0;
for (int i = 0; i < 15; i++) {
for (int j = i+1; j < 16; j++) {
int ct = 0;
for (int dif=i^j; dif != 0; dif >>= 1)
if ((dif&1) != 0) ct++;
if (ct == 1) {
conn[k][0] = (byte)i;
conn[k][1] = (byte)j;
k++;
}else if( ct == 2 && i%2==0 && j%2==1 ){
/*face[f][0] = (byte)(i);
face[f][1] = (byte)(i+1);
face[f][2] = (byte)(j);
face[f][3] = (byte)(j-1);
f++;*/
}else if( ct == 2 && (j==i+4 || j==i+2 || j==i+10)){
face[f][0] = (byte)(i);
if( j-i==10 ){
face[f][1] = (byte)(i+2);
face[f][3] = (byte)(j-2);
}else if( j-i==4) {
face[f][1] = (byte)(i-4);
face[f][3] = (byte)(j+4);
}else{
face[f][1] = (byte)(i-2);
face[f][3] = (byte)(j+2);
}
face[f][2] = (byte)(j);
f++;
}
}
}
axies[0] = new Vec4(1,0,0,0);
axies[1] = new Vec4(0,1,0,0);
axies[2] = new Vec4(0,0,1,0);
}
void rotate( Matrix4 m ){
for( int i = 0; i < 16; i++ ){
vert[i] = m.mult( vert[i] );
}
for( int i = 0; i < 3; i++){
axies[i] = m.mult( axies[i] );
}
}
void rotateXY( float ang ){ rotate( rotXY( ang ) ); }
void rotateYZ( float ang ){ rotate( rotYZ( ang ) ); }
void rotateZX( float ang ){ rotate( rotZX( ang ) ); }
void rotateXW( float ang ){ rotate( rotXW( ang ) ); }
void rotateYW( float ang ){ rotate( rotYW( ang ) ); }
void rotateZW( float ang ){ rotate( rotZW( ang ) ); }
void draw(){
stroke( 192,192,192 );
beginShape( LINES );
for( int i = 0; i < 32; i++ ){
Vec4 p1 = vert[ conn[i][0] ];
Vec4 p2 = vert[ conn[i][1] ];
Vec3 v1 = proj43( p1 ).mult( size );
Vec3 v2 = proj43( p2 ).mult( size );
//stroke(p1.w * 6,0,p1.w * 6);
vertex( v1.x, v1.y, v1.z );
//stroke(p2.w * 6, 0, p2.w * 6);
vertex( v2.x, v2.y, v2.z );
}
endShape();
noStroke();
fill(255,155,0,255);
for( int i = 0; i < 16; i++ ){
pushMatrix();
Vec4 p = vert[ i ];
Vec3 v = proj43( p ).mult( size );
translate( v.x, v.y, v.z);
sphere(4);
popMatrix();
}
beginShape( LINES );
for( int i = 0; i < 3; i++ ){
Vec4 p2 = axies[ i ];
Vec3 v2 = proj43( p2 ).mult( size );
stroke( (((i+3)>>1)&1) * 255, (i&1) * 255 ,(((i+6)>>3)&1) *255 );
vertex( 0,0,0 );
vertex( v2.x, v2.y, v2.z );
}
endShape();
fill( 0,0,150 , 100.0 );
noStroke();
beginShape( QUADS );
for( int i = 0; i < 24; i++ ){
for( int j = 0; j < 4; j++ ){
Vec4 p = vert[ face[i][j] ];
Vec3 v = proj43(p).mult( size );
vertex( v.x, v.y, v.z );
}
}
endShape();
}
}
Box4 b = new Box4( 100 );
float[] av = new float[2];
float time;
float[] press = new float[2];
void mouseDragged(){
float dtime = millis() - time;
if( dtime == 0 ) dtime = 1;
av[0] += (mouseX-press[0]) / dtime / 150;
av[1] += (mouseY-press[1]) / dtime / 150;
press[0] = mouseX;
press[1] = mouseY;
time = millis();
}
void keyPressed() {
if( key == 'r' ){
b = new Box4( 100 );
}
}
void update(){
if( keyPressed ){
if( key == ' ' ){
b.rotateXW( -av[0] * 6 );
b.rotateYW( av[1] * 6 );
}
}else{
b.rotateZX( -av[0] * 6 );
b.rotateYZ( av[1] * 6 );
}
}
void draw(){
background( 100 );
directionalLight(190,190,190, 0, 0, -1);
for( int i = 0; i < 2; i++ ){
av[i] *= 0.95;
}
update();
pushMatrix();
translate( width/2, height/2 , 0 );
b.draw();
popMatrix();
}
72 views




