Tuesday, May 19, 2009

Disco Socks



Tried a few more variations. This one runs in either OPENGL or JAVA2D mode. The drawing is much nicer in JAVA2D mode but it crashes at random times, hence the OPENGL mode. Not sure why it crashes, I think it has something to do with the ellipse() function not liking negative dimensions... I know it's my own fault!

ß e n


import processing.opengl.*;

/**
* Disco Socks v1
*/

//
// Set to false for JAVA2D mode.
//
boolean kOpenGL = true ;

void setup()
{

size( 800, 600, kOpenGL ? OPENGL : JAVA2D ) ;
colorMode( RGB, 1.0 ) ;

smooth() ;
}


void draw()
{
//
// Animate on the current time.
//
float t = millis() * 0.0005 ;

//
// Clear the background..
//
if ( kOpenGL )
{
//
// If OPENGL mode then subdivide the background to
// get some colour variation in.
//
noStroke() ;

subDivideRectangle( 6, 0, 0, width, height, noise( t ) ) ;
}
else
//
// Use a flat background for JAVA2D mode.
//
background( smoothColour( 1.3, 1.7, 1.5, noise(t) ) ) ;

//
// Use different strokes for OPENGL and JAVA2D mode.
//
stroke( 0, 0, 0, 0.4 ) ;

if ( kOpenGL )
strokeWeight( 0.1 ) ;
else
strokeWeight( 1.0 ) ;


//
// Control the width of the ellipses.
//
float range = width / 2.0 ;

for ( int i = 0 ; i < 100 ; ++ i )
{
pushMatrix() ;

//
// Split the sock half way along!
//
if ( i == 50 )
t += noise(t) * noise(t) ;

//
// Random colour for this segment.
//
color colour = smoothColour( i/0.3, i/0.7, i/0.9, noise( t ) ) ;
fill( colour ) ;

//
// Randomise the locaiton of this segment.
//
translate( width/2, height/2 ) ;
translate( random_unit( t, range, 0, i ), random_unit( t, range, 1, i ) ) ;

//
// Roate the segment by a random angle.
//
rotate( random_unit( t, PI, 0, i ) ) ;

//
// Draw a the segment as an ellipse with random width and height.
//
// rect( 0, 0, 3.0 + random_unit( t, range, 2, i ), 3.0 + random_unit( t, range, 3, i ) ) ;
ellipse( 0, 0, 10.0 + random_unit( t, range, 2, i ), 10.0 + random_unit( t, range, 3, i ) ) ;

popMatrix() ;
}
}

//
// Draw a subdivide rectangle
//
// Coordinates of vertices used to randomise the colours.
//
void subDivideRectangle( int depth, float x, float y, float width, float height, float alpha )
{

if ( depth == 1 )
{
beginShape() ;

fill( smoothColour( millis() * 0.001, x * 0.001, y * 0.001, alpha ) );
vertex( x, y ) ;

fill( smoothColour( millis() * 0.001, x * 0.001, (y+height) * 0.001, alpha ) );
vertex( x, y+height ) ;

fill( smoothColour( millis() * 0.001, (x+width) * 0.001, (y+height) * 0.001, alpha ) );
vertex( x+width, y+height ) ;

fill( smoothColour( millis() * 0.001, (x+width) * 0.001, y * 0.001, alpha ) );
vertex( x+width, y ) ;

endShape() ;
}
else
{
depth -- ;

subDivideRectangle( depth, x, y, width/2, height/2, alpha ) ;
subDivideRectangle( depth, x, y+height/2, width/2, height/2, alpha ) ;
subDivideRectangle( depth, x+width/2, y, width/2, height/2, alpha ) ;
subDivideRectangle( depth, x+width/2, y+height/2, width/2, height/2, alpha ) ;
}
}

float random_unit( float t, float scale, int index1, float index2 )
{
return 2.0 * scale * ( 0.5f - noise( t, index1, index2/100.0 ) ) ;
}

/**
* Generate a vector whose components change smoothly over time in the range [ 0, 1 ].
* Each component uses a sin() function to map the current time in milliseconds somewhere
* in the range [ 0, 1 ].A 'speed' factor is specified for each component.
*/
PVector smoothVector( float s1, float s2, float s3 )
{
float mills = 0.00003f * millis() ;

float x = 0.5f * sin( mills * s1 ) + 0.5f ;
float y = 0.5f * sin( mills * s2 ) + 0.5f ;
float z = 0.5f * sin( mills * s3 ) + 0.5f ;

return new PVector( x, y, z ) ;
}

/**
* Generate a colour which smoothly changes over time.
* The speed of each component is contolled by the parameters s1, s2 and s3.
*/
color smoothColour( float s1, float s2, float s3, float alpha )
{
PVector v = smoothVector( s1, s2, s3 ) ;

return color( v.x, v.y, v.z, alpha ) ;
}

5 comments:

monkstone said...

Completely paradoxical you might think this applet more likely to crash the ide, not a bit of it runs very nicely, some sickly green and pink hues though.

monkstone said...

Just for completeness tested it JAVA2D mode, wan background replaces garish colors, no crashing ide for me, strange...

lazydog said...

Sounds like you've got OpenGL problems!

ß e n

monkstone said...

Its not the OpenGL thats giving me the problems, in fact that applet runs just fine, it's just I don't care much for the colors...
It's just random other things I've been working on recently in the processing ide.
It's weird because the same code that gives me problems runs fine and dandy in Netbeans which must have bigger memory footprint.

lazydog said...

Oh okay, I misread your post!

ß e n