Problems with translate on 3D PShapes(GROUP)

I am having an issue with using TRANSLATE on a 3D PShape(GROUP). The code below works great until I try to add a second brain area by removing the comment tag on the line near the end of the code.

//brain.translate(td,0,0); brain.addChild(area);    // This messes up the drawing, so dunno what to do

I want to build the brain by adding the other 26 brain areas around the core cerebellum. I am running out of memory if I use too many cells in the cerebellum area, so I was hoping to multiply the number of cells by dividing into area and hoping that the memory garbage collection would work better the more hierarchical I go.....

ANY and ALL suggestion on any part of the code, especially the highlighted area above would be much appreciated. Thank you to all those that have helped me with my project so far. Listed below is the smallest possible listing of the code without all of the extras added on.....

//----------------------------------------------------------

// sketch:  Brain Test   
//----------------------------------------------------------
/**
This sketch creates an interactive AI Brain to view.
Base off from RGB_Cube.pde by toni holzer on OpenProcessing.org     
*/

import peasy.*;
PeasyCam cam;

int scrWidth  = displayWidth;
int scrHeight = displayHeight;     
int winSize = 800;  //size of display window in both directions

color fillColor = color(222);  // light gray -background
color lineColor = color(88);   // dark gray - lines & letters

float boundries = winSize *.8;                    // overall brain size in comparison to window
int   cells_per_side = 5;                         // number of cells per side - no greater than 100
float d2 = cells_per_side * 0.5 - 0.5;            //used in creating matrix
float init_rescale = 0.5, rescale=init_rescale;   // 0.1 .. 1.0  percentage cell takes of allocated space
float gridSize = boundries / cells_per_side;
float cellSize = gridSize * init_rescale;
float td = cells_per_side/2*gridSize;            // amount an AREA needs tanslated off center area
int trans = 88;                                  // transparency setting  solid=255 transparent=88
boolean freemode = false;      // true: grab the brain
int    inputKeyCode = 0;
color[] quadBG = { #FF0000, #00FF00, #0000FF, #FFFF00, #00FFFF, #FF00FF  };
CubeCelledBrain Brain;
//--------------------------------------------------------------------------------------------------------------------------------------------
void setup() {
  size(winSize,winSize,P3D);
  cursor(HAND);
  stroke(0,0,0);
  cam = new PeasyCam(this, winSize*1.25);
  Brain = new CubeCelledBrain(cells_per_side, cellSize, gridSize);
  }

void draw() { 
  background(122);
  if (freemode) cam.rotateY(radians(1));
  pushMatrix();  Brain.draw();  popMatrix();}
//--------------------------------------------------------------------------------------------------------------------------------------------
void keyPressed(){ 
  inputKeyCode = keyCode;
  if      (key == 'f') freemode = !freemode;      // keyCode 70
  else if ((keyCode ==  37) |(keyCode ==  226)|(keyCode ==  100))    cam.rotateY(radians(3));   // cursor left - nonum 4 - num4
  else if ((keyCode ==  39) |(keyCode ==  227)|(keyCode ==  102))    cam.rotateY(radians(-3));  // cursor right - nonum 6 - num 6
  else if((keyCode ==  38)  |(keyCode == 224) |(keyCode == 104))     cam.rotateX(radians(-3));  // cursor up or num 8
  else if((keyCode ==  40)  |(keyCode == 225) |(keyCode ==  98))     cam.rotateX(radians(3));}   // cursor down or num 2
//--------------------------------------------------------------------------------------------------------------------------------------------
// Custom Cube Celled Brain Class
class CubeCelledBrain {

  float cellsize, cs, gridsize;  // width, height, depth, size of cell
  int cells_per_side;
  PVector[] vertices = new PVector[24];
  PShape  brain, area, cell;
  PShape[] face = new PShape[6];
  PShape  tempcell, tempface;
  PVector tempvec;
  int     count=0;


//*****************************************************************************************************
  CubeCelledBrain(int tempN, float tempS, float tempG) {  // CONSTRUCTOR
    cells_per_side = tempN;    cs = tempS;     gridsize = tempG;

    // cube composed of 8 verticies
    vertices[0] = new PVector(-cs/2, -cs/2, cs/2);    vertices[1] = new PVector(cs/2, -cs/2, cs/2);
    vertices[2] = new PVector(cs/2, cs/2, cs/2);      vertices[3] = new PVector(-cs/2, cs/2, cs/2);
    vertices[4] = new PVector(cs/2, -cs/2, -cs/2);    vertices[5] = new PVector(-cs/2, -cs/2, -cs/2);
    vertices[6] = new PVector(-cs/2, cs/2, -cs/2);    vertices[7] = new PVector(cs/2, cs/2, -cs/2);  
    // fv=faceverticies     mapping of the verticies to faces  
    int[][] fv ={{0,1,2,3},{0,5,6,3},{1,4,7,2},{5,4,7,6},{0,5,4,1},{3,6,7,2}};

// Create the shape group
    brain = createShape(GROUP);
    area = createShape(GROUP);
    for (int i = -cells_per_side/2; i < cells_per_side/2+1; i++) {
      for (int j = -cells_per_side/2; j < cells_per_side/2+1; j++) {
          for (int k = -cells_per_side/2; k < cells_per_side/2+1; k++) {         
            cell = createShape(GROUP);
               for (int fn = 0; fn < 6; fn++) {
               face[fn]=createShape();
               face[fn].beginShape();
                  face[fn].fill(quadBG[fn]);
                  face[fn].vertex(vertices[fv[fn][0]].x, vertices[fv[fn][0]].y, vertices[fv[fn][0]].z);
                  face[fn].vertex(vertices[fv[fn][1]].x, vertices[fv[fn][1]].y, vertices[fv[fn][1]].z);
                  face[fn].vertex(vertices[fv[fn][2]].x, vertices[fv[fn][2]].y, vertices[fv[fn][2]].z);
                  face[fn].vertex(vertices[fv[fn][3]].x, vertices[fv[fn][3]].y, vertices[fv[fn][3]].z);
                  face[fn].setName("Face " + fn);
                  face[fn].endShape();
                  cell.addChild(face[fn]);     }
            cell.translate(i*gridsize, j*gridsize, k*gridsize);
            cell.setName("Cell " + count++);
            area.addChild(cell);          }}}
          brain.addChild(area);    
          //brain.translate(td,0,0); brain.addChild(area);    // This messes up the drawing, so dunno what to do  
     }

void draw() { shape(brain); }        // Draw the brain
}  // end CLASS
Tagged:

Answers

  • Answer ✓

    Better use a for loop for this:

    // Create the shape group
    brain = createShape(GROUP);
    for (int m=0; m<10; m++) {
      area = createShape(GROUP);
      for (int i = -cells_per_side/2; i < cells_per_side/2+1; i++) {
        for (int j = -cells_per_side/2; j < cells_per_side/2+1; j++) {
          for (int k = -cells_per_side/2; k < cells_per_side/2+1; k++) {        
            cell = createShape(GROUP);
            for (int fn = 0; fn < 6; fn++) {
              face[fn]=createShape();
              face[fn].beginShape();
              face[fn].fill(quadBG[fn]);
              face[fn].vertex(vertices[fv[fn][0]].x, 
                vertices[fv[fn][0]].y, 
                vertices[fv[fn][0]].z);
              face[fn].vertex(vertices[fv[fn][1]].x,
                vertices[fv[fn][1]].y,
                vertices[fv[fn][1]].z);
              face[fn].vertex(vertices[fv[fn][2]].x,
                vertices[fv[fn][2]].y,
                vertices[fv[fn][2]].z);
              face[fn].vertex(vertices[fv[fn][3]].x,
                vertices[fv[fn][3]].y,
                vertices[fv[fn][3]].z);
              face[fn].setName("Face " + fn);
              face[fn].endShape();
              cell.addChild(face[fn]);
            }
            cell.translate(i*gridsize, j*gridsize, k*gridsize);
            cell.setName("Cell " + count++);
            area.addChild(cell);
          }
        }
      }
      area.translate(m*td, 0, 0); 
      brain.addChild(area);
    }
    

    Also your td value should be:

    float td = cells_per_side*gridSize;
    

    And put this as the first line in draw() so your shapes don't get cut from the view:

    perspective(PI/3, float(width)/height, 1, 1000000);
    
  • Thank you very very much. It is amazing how the placement of a few lines can change everything. I am back to working and hopefully can make it to the next brick wall where I need help again. Thanks a lot.

Sign In or Register to comment.