We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexDiscussionExhibition › Glass tube of electricity
Page Index Toggle Pages: 1
Glass tube of electricity (Read 972 times)
Glass tube of electricity
Jan 9th, 2010, 7:40pm
 
Ok.. I know the code is sloppy.. just playing around and thought it was cool.  It's based on some sine wave code I found in the hacks section.  Figured I would share it.
Code:
import processing.opengl.*;
import javax.media.opengl.GL;
int xspacing = 30;   // How far apart should each horizontal location be spaced
int w;  // Width of entire wave
int maxwaves = 1;   // total # of waves to add together
int wavecycles =4;  //times to run it

float theta = 0.0;
float[] amplitude = new float[maxwaves];   // Height of wave
float[] dx = new float[maxwaves];    // Value for incrementing X, to be calculated as a function of period and xspacing
float[] yvalues;   // Using an array to store height values for the wave (not entirely necessary)
float[] zvalues;
PGraphicsOpenGL pgl;
GL gl;

void setup() {
 size(350, 130,OPENGL);
   hint(DISABLE_OPENGL_2X_SMOOTH);
   hint(ENABLE_OPENGL_2X_SMOOTH); //after disabling the 2x this works fine
 
   pgl = (PGraphicsOpenGL) g;
   gl = pgl.gl;
   gl.glDepthMask(false);
   gl.glEnable( gl.GL_BLEND );
   gl.glHint (gl.GL_LINE_SMOOTH_HINT, gl.GL_NICEST);
   gl.glEnable (gl.GL_LINE_SMOOTH);
   frameRate(30);
}

void draw() {
 gl.glEnable( gl.GL_BLEND );
 gl.glBlendFunc(GL.GL_SRC_ALPHA,GL.GL_ONE);
 background(0);
 for (int i=0;i < wavecycles; i++) {
   calcWave(-100,50,0,300,100,100);
   renderWave(-100,50,0,300,100,100);
 }

 strokeWeight(2);
 
 stroke(255,255,255,180);
 rect(-3,37,width,23);
 noStroke();

 fill(255,255,255,90);
 rect(-3,39,width,10);
 
 gl.glDisable( gl.GL_BLEND );
 fill(255);
 stroke(160,160,160,140);

 rect(-3,35,45,28);
 noStroke();
 
 fill(200,200,200,255);
 rect(-4,49,45,13);
 
 fill(255);
 stroke(160,160,160,140);

 rect(width-42,35,45,28);
 noStroke();
 
 fill(200,200,200,255);
 rect(width-41,49,45,13);
 strokeWeight(2);
 stroke(210,210,210);
 line(40,37,40,61);
 line(width-40,37,width-40,61);
}

void calcWave(int x1, int y1, int z1, int x2, int y2, int z2) {
   w = (int)dist(x1, y1, z1, x2, y2, z2);

 for (int i = 0; i < maxwaves; i++) {
   amplitude[i] = random(6,10);
   float period = random(100,300); // How many pixels before the wave repeats
   dx[i] = (TWO_PI / period) * xspacing;
 }

 yvalues = new float[w/xspacing];
 zvalues = new float[w/xspacing];
 
 // Increment theta (try different values for 'angular velocity' here
 theta += 1.42;

 // Set all height values to zero
 for (int i = 0; i < yvalues.length; i++) {
   yvalues[i] = y1;
   zvalues[i] = z1;
 }

 // Accumulate wave height values
 for (int j = 0; j < maxwaves; j++) {
   float x = theta;
   for (int i = 0; i < yvalues.length; i++) {
// Every other wave is cosine instead of sine
if (j % 2 == 0)  yvalues[i] += sin(x)*amplitude[j];
else yvalues[i] += cos(x)*amplitude[j];

if (j % 2 == 0)  zvalues[i] += sin(yvalues[i])*amplitude[j];
else zvalues[i] += cos(yvalues[i])*amplitude[j];

x+=dx[j];
   }
 }
}

void renderWave(int x1, int y1, int z1, int x2, int y2, int z2) {

 noFill();
 int ranx = int(random(-4,0));
 int rany = int(random(-4,0));
 int ranz = int(random(-4,0));
 beginShape();
 strokeWeight(3);
 stroke(0,0,255,160);

 for (int x = 0; x < yvalues.length; x++) {
curveVertex(int(x*xspacing)+ranx,int(yvalues[x])+rany,int(zvalues[x])+ranz);
 }
 endShape();
 beginShape();
 strokeWeight(1);
 stroke(255,255,255,120);
 
 for (int x = 0; x < yvalues.length; x++) {
    curveVertex(int(x*xspacing)+ranx,int(yvalues[x])+rany,int(zvalues[x])+ranz);
 }
 endShape();

 strokeWeight(6);
 stroke(255,255,255,50);
 int yval = yvalues.length-2;

line(int(1*xspacing),int(yvalues[1]),int(zvalues[1]),int(yval*xspacing),int(yvalues[yval]),int(zvalues[yval]));
}

Re: Glass tube of electricity
Reply #1 - Jan 10th, 2010, 8:37am
 
Nice 3D glassy feel to it - I really liked it a lot.
Cheesy
Page Index Toggle Pages: 1