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.
IndexProgramming Questions & HelpSyntax Questions › superquadric made of images
Page Index Toggle Pages: 1
superquadric made of images (Read 643 times)
superquadric made of images
Jul 18th, 2009, 5:13am
 
How can i make this skech faster ? i think i have to reduce the draw() part but i am not able to do that. I don't manage to do it.
How can i do ? It is composed of a superquadric form which can be changed with controls. i think it is a bit slow. Here the code :

int samples = 20;
PImage b;
float a1 = 10., a2 = 10., a3 = 10.;
float u1 = 0., u2 = 20., v1 = 0., v2 = 20.;
float dU = (u2 - u1) / samples;
float dV = (v2 - v1) / samples;
float n = 1., e = 1.;

void setup(){
 background(0);
 size(screen.width,screen.height, P3D);
 //size(500,500, P3D); //setup the screen size(screen.width,screen.height, P3D);
 //ambientLight(51, 102, 126);
directionalLight(126, 126, 126, 0, 0, -1);
ambientLight(102, 102, 102);
camera(-15,15,-20,0,0,0,0,0,1); //get a viewpoint
b = loadImage("wormhole.png");
}


void draw(){
background(0);
   
    rotateY(frameCount * 0.01);
 
  float u = u1;
  for(int i=0; i<samples; i++){
   float v = v1;
   for(int j=0; j<samples; j++){
     float x = a1 * sqCos (u, n) * sqCos (v, e);
     float y = a2 * sqCos (u, n) * sqSin (v, e);
     float z = a3 * sqSin (u, n);
   pushMatrix();
   translate(x, y, z);
  image(b, 0, 0, 1, 1);
  popMatrix();
     v += dV;
     }
   u += dU;

}}

float sign ( float x ) {
 if ( x < 0 )return -1;
 if ( x > 0 )return 1;
 return 0;
}
float sqSin( float v, float n ) {
 return sign(sin(v)) * pow(abs(sin(v)),n);
}
float sqCos( float v, float n ) {
 return sign(cos(v)) * pow(abs(cos(v)),n);
}
void keyPressed() {
 if (key == CODED) {
   if (keyCode == UP) {
     n = 3.;
     e = 3.;
   }
if (keyCode == LEFT) {
     n = 1.;
     e = 1.;
   }
if (keyCode == RIGHT) {
     n = -0.2;
     e = 1.;
   }
if (keyCode == DOWN) {
     n = 1;
     e = 0.3;
   }
}
}
Re: superquadric made of images
Reply #1 - Jul 18th, 2009, 12:17pm
 
hey Mohammed.
this looks really nice. its not very complex, although i dont see where u1,u2,v1,v2  play a role. but i think sqSin and sqCos cost the most time to calculate. am i right- after pressing a key you just need to make the calculation only once, the y rotation is everything that animates.
So why dont you just calculate x,y,z only once and safe them into an 2dim array of PVectors for your sample dimensions i and j.
i think it will make it way faster, if not, get a new graphic card  Smiley
Re: superquadric made of images
Reply #2 - Jul 20th, 2009, 5:54am
 
Hello ramin,

great it is faster ! but i have got a new problem, now my control
doesn t work at all. Maybe my  calculation is closed because it is in my setup part. So we can t modify them. How can i modify the calcul in my setup part (bold in the follow sketch) in order to make my control work ? Here the code :

int samples = 20;
PImage b;
PVector [][]pos = new PVector[samples][samples];
float a1 = 10., a2 = 10., a3 = 10.;
float u1 = 0., u2 = 20., v1 = 0., v2 = 20.;
float dU = (u2 - u1) / samples;
float dV = (v2 - v1) / samples;
float n = 1., e = 1.;
//float x, y, z;
void setup(){
 background(0);
 //size(screen.width,screen.height, P3D);
 size(500,500, P3D);
 //ambientLight(51, 102, 126);
directionalLight(126, 126, 126, 0, 0, -1);
camera(-15,15,-20,0,0,0,0,0,1); //get a viewpoint
// Images must be in the "data" directory to load correctly
//b = loadImage("berlin-1.jpg");
//b = loadImage("wormhole.png");

float u = u1;
  for(int i=0; i<samples; i++){
   float v = v1;
   for(int j=0; j<samples; j++){
    float x = a1 * sqCos (u, n) * sqCos (v, e);
     float y = a2 * sqCos (u, n) * sqSin (v, e);
     float z = a3 * sqSin (u, n);
     pos[i][j] = new PVector(x, y, z);
   
   v += dV;
     }
   u += dU;

}
}

void draw(){
background(0);
   rotateY(frameCount * 0.01);
   for(int i=0; i<samples; i++){
    for(int j=0; j<samples; j++){
      pos[i][j].display();
}}}

float sign ( float x ) {
 if ( x < 0 )return -1;
 if ( x > 0 )return 1;
 return 0;
}
float sqSin( float v, float n ) {
 return sign(sin(v)) * pow(abs(sin(v)),n);
}
float sqCos( float v, float n ) {
 return sign(cos(v)) * pow(abs(cos(v)),n);
}

void keyPressed() {
 if (key == CODED) {
   if (keyCode == UP) {
     n = 3.;
     e = 3.;
   }
if (keyCode == LEFT) {
     n = 1.;
     e = 1.;
   }
if (keyCode == RIGHT) {
     n = -0.2;
     e = 1.;
   }
if (keyCode == DOWN) {
     n = 1;
     e = 0.3;
   }
}
}
Re: superquadric made of images
Reply #3 - Jul 24th, 2009, 2:28am
 
hey Mohammed.
sorry for beeing away the days.
the applet now doesn't run here because of  
 pos[i][j].display();
in the draw, but about your problem, just create a method modify() or something with all that stuff:
Code:

float u = u1;
 for(int i=0; i<samples; i++){
  float v = v1;
  for(int j=0; j<samples; j++){
   float x = a1 * sqCos (u, n) * sqCos (v, e);
    float y = a2 * sqCos (u, n) * sqSin (v, e);
    float z = a3 * sqSin (u, n); pos[i][j] = new PVector(x, y, z);
 
  v += dV;
    }
  u += dU;

}

or what you need there. so you call that method in the end of your setup and in the end of keyPressed
Page Index Toggle Pages: 1