Not Able to Render Multiple Shapes

OK guys, I have been using P3D and I’m trying to render multiple minecraft grass blocks using a for loop. I have a for loop in draw that runs this function, increasing x each iteration:

void makeTexturedCube(PImage pix,PImage pix2,PImage pix3,PImage pix4,PImage pix5,PImage pix6, float ix, float iy, float iz) { 
  translate(width/2.0, height/2.0, 500); 
  rotateX(rotx); 
  rotateY(roty);
  scale(90);
  
  float x = ix * 2;
  float y = iy * 2;
  float z = iz * 2;
  
  beginShape(QUADS); 
  texture(pix2); 
  // +Z "front" face 
  vertex(-1+x, -1+y,  1+z, 0, 0); 
  vertex( 1+x, -1+y,  1+z, 1, 0); 
  vertex( 1+x,  1+y,  1+z, 1, 1); 
  vertex(-1+x,  1+y,  1+z, 0, 1); 
endShape(); 
 beginShape(QUADS); 
  texture(pix4); 
  // -Z "back" face 
  vertex( 1+x, -1+y, -1+z, 0, 0); 
  vertex(-1+x, -1+y, -1+z, 1, 0); 
  vertex(-1+x,  1+y, -1+z, 1, 1); 
  vertex( 1+x,  1+y, -1+z, 0, 1); 
endShape(); 
  beginShape(QUADS); 
  texture(pix6); 
  // +Y "bottom" face 
  vertex(-1+x,  1+y,  1+z, 0, 0); 
  vertex( 1+x,  1+y,  1+z, 1, 0); 
  vertex( 1+x,  1+y, -1+z, 1, 1); 
  vertex(-1+x,  1+y, -1+z, 0, 1); 
endShape(); 
 beginShape(QUADS); 
  texture(pix); 
  // -Y "top" face 
  vertex(-1+x, -1+y, -1+z, 0, 0); 
  vertex( 1+x, -1+y, -1+z, 1, 0); 
  vertex( 1+x, -1+y,  1+z, 1, 1); 
  vertex(-1+x, -1+y,  1+z, 0, 1); 
endShape(); 
  beginShape(QUADS); 
  texture(pix3); 
  // +X "right" face 
  vertex( 1+x, -1+y,  1+z, 0, 0); 
  vertex( 1+x, -1+y, -1+z, 1, 0); 
  vertex( 1+x,  1+y, -1+z, 1, 1); 
  vertex( 1+x,  1+y,  1+z, 0, 1); 
endShape(); 
 beginShape(QUADS); 
  texture(pix5); 
  // -X "left" face 
  vertex(-1+x, -1+y, -1+z, 0, 0); 
  vertex(-1+x, -1+y,  1+z, 1, 0); 
  vertex(-1+x,  1+y,  1+z, 1, 1); 
  vertex(-1+x,  1+y, -1+z, 0, 1); 
endShape(); 
} 

I can’t seem for the life of me to figure out why it only renders one!

Thanks for any advice anyone gives

– kokomoko

1 Like

Your code looks fine as far as I can tell, but perhaps it’s drawing all the cubes in the same spot for whatever reason.

Edit: on second glance, I did notice your translate:
translate(width/2.0, height/2.0, 500);

Transformations are cumulative unless you use push() and pop(). So maybe there six cubes, half a screen away from eachother

2 Likes

No, that can’t be it. When I get rid of the translate, they are obviously no longer directly in front of me, but there still is only one. If I copy and paste the part where I make all the faces of the cube at the end of the function and just before that change the x and y, I get two cubes. Could it possibly be the fact that I’m using a for loop? I know that that wouldn’t make any sense… but it’s the only thing I can think of. It could be something with openGL?

Post the for loop then.

Here, I just set up a simple little sketch for testing (aka got rid of some stuff that I didn’t need).


float rotx, roty;

void setup() {
  size(1000, 800, P3D);
  background(0, 100, 100);
  noStroke();
  textureMode(NORMAL); 
  fill(255); 
}

void draw() {
  background(0, 100, 100);
  for(int i = 0; i<6; i++) {
    makeTexturedCube(i, 3, 3);
  }
}


void makeTexturedCube(float ix, float iy, float iz) { 
  translate(width/2.0, height/2.0, 500); 
  rotateX(rotx); 
  rotateY(roty);
  scale(90);
  
  float x = ix * 2;
  float y = iy * 2;
  float z = iz * 2;
  
  beginShape(QUADS); 
  // +Z "front" face 
  vertex(-1+x, -1+y,  1+z, 0, 0); 
  vertex( 1+x, -1+y,  1+z, 1, 0); 
  vertex( 1+x,  1+y,  1+z, 1, 1); 
  vertex(-1+x,  1+y,  1+z, 0, 1); 
endShape(); 
 beginShape(QUADS);  
  // -Z "back" face 
  vertex( 1+x, -1+y, -1+z, 0, 0); 
  vertex(-1+x, -1+y, -1+z, 1, 0); 
  vertex(-1+x,  1+y, -1+z, 1, 1); 
  vertex( 1+x,  1+y, -1+z, 0, 1); 
endShape(); 
  beginShape(QUADS);  
  // +Y "bottom" face 
  vertex(-1+x,  1+y,  1+z, 0, 0); 
  vertex( 1+x,  1+y,  1+z, 1, 0); 
  vertex( 1+x,  1+y, -1+z, 1, 1); 
  vertex(-1+x,  1+y, -1+z, 0, 1); 
endShape(); 
 beginShape(QUADS); 
  // -Y "top" face 
  vertex(-1+x, -1+y, -1+z, 0, 0); 
  vertex( 1+x, -1+y, -1+z, 1, 0); 
  vertex( 1+x, -1+y,  1+z, 1, 1); 
  vertex(-1+x, -1+y,  1+z, 0, 1); 
endShape(); 
  beginShape(QUADS); 
  // +X "right" face 
  vertex( 1+x, -1+y,  1+z, 0, 0); 
  vertex( 1+x, -1+y, -1+z, 1, 0); 
  vertex( 1+x,  1+y, -1+z, 1, 1); 
  vertex( 1+x,  1+y,  1+z, 0, 1); 
endShape(); 
 beginShape(QUADS); 
  // -X "left" face 
  vertex(-1+x, -1+y, -1+z, 0, 0); 
  vertex(-1+x, -1+y,  1+z, 1, 0); 
  vertex(-1+x,  1+y,  1+z, 1, 1); 
  vertex(-1+x,  1+y, -1+z, 0, 1); 
endShape(); 
} 

void mouseMoved() { 
  float rate = 0.005; 
  if(rotx >= -1.5 && rotx <= 1.5) {
    rotx += (pmouseY-mouseY) * rate; 
  } else {
    if(rotx <= -1.5) {
      rotx = -1.49;
    }
    if(rotx >= 1.5) {
      rotx = 1.49;
    }
  }
  roty += (mouseX-pmouseX) * rate; 
} 
1 Like

YES!!! I got it to work… I feel completely stupid now. XD You were right, I don’t know why but for some strange reason when I added pushMatrix(); and popMatrix(); the first time, it simply didn’t fix the problem. This time it did (excuse me processing?). I changed the scale to 1, so the cubes were small and I saw that they were far apart.

Thank you Tony!

:grin: I was about to ask you to try that! Glad it works!