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 › Need Help mapping an image on cubes
Page Index Toggle Pages: 1
Need Help mapping an image on cubes (Read 353 times)
Need Help mapping an image on cubes
May 9th, 2008, 8:16pm
 
I am taking the example "cubeswithincubes" and instead of having random colors I wanted to put images on the cubes. Does anyone have any suggestions on to do this. Im new to java and processing. Thanks in advance!

Heres my code


PImage img; // image

Cube stage; // external large cube
int cubies = 20;
Cube[]c = new Cube[cubies]; // internal little cubes
color[][]quadBG = new color[cubies][6];

// controls cubie's movement
float[]x = new float[cubies];
float[]y = new float[cubies];
float[]z = new float[cubies];
float[]xSpeed = new float[cubies];
float[]ySpeed = new float[cubies];
float[]zSpeed = new float[cubies];

// controls cubie's rotation
float[]xRot = new float[cubies];
float[]yRot = new float[cubies];
float[]zRot = new float[cubies];

// size of external cube
float bounds = 900;

void setup(){
 size(1200, 1200, P3D);
 img = loadImage("me.jpg");
 frameRate(30);
 
 
 for (int i=0; i<cubies; i++){
   // each cube face has a random color component
   
 
   /*
   float colorShift = random(-75, 75);
   quadBG[i][0] = color(175+colorShift, 30, 30);
   quadBG[i][1] = color(30, 175+colorShift, 30);
   quadBG[i][2] = color(30, 30, 175+colorShift);
   quadBG[i][3] = color(175+colorShift, 175+colorShift, 30);
   quadBG[i][4] = color(175+colorShift, 30, 175+colorShift);
   quadBG[i][5] = color(175+colorShift, 87+colorShift, 30);
   */
   
   // cubies are randomly sized
   float cubieSize = random(30, 90);
   c[i] =  new Cube(cubieSize, cubieSize, cubieSize);

   //initialize cubie's position, speed and rotation
   x[i] = 0;
   y[i] = 0;
   z[i] = 0;

   xSpeed[i] = random(-2, 2);
   ySpeed[i] = random(-2, 2);
   zSpeed[i] = random(-2, 2);

   xRot[i] = random(40, 100);
   yRot[i] = random(40, 100);
   zRot[i] = random(40, 100);
 }
 // instantiate external large cube
 stage =  new Cube(bounds, bounds, bounds);
}

void draw(){
 background(50);
 // center in display window
 translate(width/2, height/2, -130);
 // outer transparent cube
 noFill();
 // rotate everything, including external large cube
 rotateX(frameCount*PI/225);
 rotateY(frameCount*PI/250);
 rotateZ(frameCount*PI/275);
 stroke(255);
 // draw external large cube
 stage.create();

 //move/rotate cubies
 for (int i=0; i<cubies; i++){
   pushMatrix();
   translate(x[i], y[i], z[i]);
   rotateX(frameCount*PI/xRot[i]);
   rotateY(frameCount*PI/yRot[i]);
   rotateX(frameCount*PI/zRot[i]);
   noStroke();
   c[i].create(quadBG[i]);
   x[i]+=xSpeed[i];
   y[i]+=ySpeed[i];
   z[i]+=zSpeed[i];
   popMatrix();

   // draw lines connecting cubbies
   stroke(10);
   if (i< cubies-1){
     line(x[i], y[i], z[i], x[i+1], y[i+1], z[i+1]);
   }

   // check wall collisions
   if (x[i]>bounds/2 || x[i]<-bounds/2){
     xSpeed[i]*=-1;
   }
   if (y[i]>bounds/2 || y[i]<-bounds/2){
     ySpeed[i]*=-1;
   }
   if (z[i]>bounds/2 || z[i]<-bounds/2){
     zSpeed[i]*=-1;
   }
 }
}


// Custom Cube class

class Cube{
 Point3D[] vertices = new Point3D[24];
 float w, h, d;

 // Constructors
 // default constructor
 Cube(){
 }

 // constructor 2
 Cube(float w, float h, float d){
   this.w = w;
   this.h = h;
   this.d = d;

   // cube composed of 6 quads
   //front
   vertices[0] = new Point3D(-w/2,-h/2,d/2);
   vertices[1] = new Point3D(w/2,-h/2,d/2);
   vertices[2] = new Point3D(w/2,h/2,d/2);
   vertices[3] = new Point3D(-w/2,h/2,d/2);
   //left
   vertices[4] = new Point3D(-w/2,-h/2,d/2);
   vertices[5] = new Point3D(-w/2,-h/2,-d/2);
   vertices[6] = new Point3D(-w/2,h/2,-d/2);
   vertices[7] = new Point3D(-w/2,h/2,d/2);
   //right
   vertices[8] = new Point3D(w/2,-h/2,d/2);
   vertices[9] = new Point3D(w/2,-h/2,-d/2);
   vertices[10] = new Point3D(w/2,h/2,-d/2);
   vertices[11] = new Point3D(w/2,h/2,d/2);
   //back
   vertices[12] = new Point3D(-w/2,-h/2,-d/2);
   vertices[13] = new Point3D(w/2,-h/2,-d/2);
   vertices[14] = new Point3D(w/2,h/2,-d/2);
   vertices[15] = new Point3D(-w/2,h/2,-d/2);
   //top
   vertices[16] = new Point3D(-w/2,-h/2,d/2);
   vertices[17] = new Point3D(-w/2,-h/2,-d/2);
   vertices[18] = new Point3D(w/2,-h/2,-d/2);
   vertices[19] = new Point3D(w/2,-h/2,d/2);
   //bottom
   vertices[20] = new Point3D(-w/2,h/2,d/2);
   vertices[21] = new Point3D(-w/2,h/2,-d/2);
   vertices[22] = new Point3D(w/2,h/2,-d/2);
   vertices[23] = new Point3D(w/2,h/2,d/2);
 }
 void create(){
   // draw cube
   for (int i=0; i<6; i++){
     beginShape(QUADS);
     for (int j=0; j<4; j++){
       vertex(vertices[j+4*i].x, vertices[j+4*i].y, vertices[j+4*i].z);
     }
     endShape();
   }
 }
  void create(color[]quadBG){
   // draw cube
   for (int i=0; i<6; i++){
     fill(quadBG[i]);
     beginShape(QUADS);
     for (int j=0; j<4; j++){
       vertex(vertices[j+4*i].x, vertices[j+4*i].y, vertices[j+4*i].z);
     }
     endShape();
   }
 }
}


//  Extremely simple  class to hold each 3D vertex

class Point3D {
 float x, y, z;

 // constructors
 Point3D(){
 }

 Point3D(float x, float y, float z){
   this.x = x;
   this.y = y;
   this.z = z;
 }
}
Re: Need Help mapping an image on cubes
Reply #1 - May 9th, 2008, 11:07pm
 
just look at some of the examples in the learning section.  here's a link to the first one, should be fairly self explanatory:

http://processing.org/learning/3d/texture1.html
Re: Need Help mapping an image on cubes
Reply #2 - May 12th, 2008, 6:26am
 
Yeah i studied this example earlier, but still can't make it work.
It's a little more tricky since the quads are random sizes and can change as they move in space.

I tried using the texture( ) function Heres a code i used:

I wasn't sure if i should put it in the void create or the void create that uses the quadBG

       text  void create(){
   // draw cube
   for (int i=0; i<6; i++){
     beginShape(QUADS);
     for (int j=0; j<4; j++){
       texture(img);
       vertex(vertices[j+4*i].x, vertices[j+4*i].y, vertices[j+4*i].z);
     }
     endShape();
   }
 }
 void create(color[]quadBG){
   // draw cube
   for (int i=0; i<6; i++){
     fill(quadBG[i]);
     beginShape(QUADS);
     for (int j=0; j<4; j++){
       texture(img);
       vertex(vertices[j+4*i].x, vertices[j+4*i].y, vertices[j+4*i].z);
     }
     endShape();
   }
 }
}


as you can tell I'm not too familiar with the java syntax, but manipulating the code and seeing what happens has always been my success to learning
if anyone had any other direction for me that would be greatly appreciated.
Page Index Toggle Pages: 1