Why does my 3D sketch run slow?

edited April 2014 in Questions about Code
//begin setup
void setup(){
  size(600,400,P3D);
}//end setup

//camera positions
float px = 0;
float py = -100;
float pz = 0;

//camera velocity
float pxv = 0;
float pyv = 0;
float pzv = 0;


//for if I add robot mouse

//boolean entered = false;
//void mouseReleased(){entered=true;}


//key press storage
boolean[] keys = new boolean[355];
void keyPressed(){keys[keyCode]=true;}
void keyReleased(){keys[keyCode]=false;}


//begin draw
void draw(){

  //environment
  background(125,250,250);
  box(10);

  //movement
  if(keys[UP]){
    px+=sin(mouseX*0.02);
    py+=cos(mouseX*0.02);
  }
  if(keys[DOWN]){
    px-=sin(mouseX*0.02);
    py-=cos(mouseX*0.02);
  }
  if(keys[LEFT]){
    px+=sin((mouseX-75)*0.02);
    py+=cos((mouseX-75)*0.02);
  }
  if(keys[RIGHT]){
    px-=sin((mouseX-75)*0.02);
    py-=cos((mouseX-75)*0.02);
  }
  if(keys[SHIFT]){
    pz+=1;
  }
  if(keys[ENTER]){
    pz-=1;
  }

  //camera
  camera(px,py,pz,px+sin(mouseX*0.02),py+cos(mouseX*0.02),pz,0,0,1);


}//end draw

Here is my code ^^

It runs fine at the moment, but when I add a 3D array and render cubes with it, it slows the code WAY down. Why does it do that? I see all of these 3D programs that can load a gigantic amount of cubes, and they run fast.

I suspect that it has something to do with Processing's "cube();", but I'm not sure.

Tagged:

Answers

  • "a 3d array"? what? why don't you post the code that is running slow instead of posting some preliminary code. that way it's hard to help you.

    what means slow? how many fps on what kind of a machine is it producing?

  • edited April 2014
    //the 3D array
    int[][][] world = new int[30][30][30];
    
    //world updater
    void worldReader(){
      for(int i = 0; i<world.length; i++){
        for(int j = 0; j<world[i].length; j++){
          for(int k = 0; k<world[i][j].length; k++){
            pushMatrix();
    
            translate(i*50,j*50,k*50);
            fill(50,150,0);
            box(50);
    
            popMatrix();
          }
        }
      }
    }
    
    
    //begin setup
    void setup(){
      size(600,400,P3D);
    }//end setup
    
    
    
    //camera positions
    float px = 150;
    float py = 150;
    float pz = -150;
    
    //camera velocity
    float pxv = 0;
    float pyv = 0;
    float pzv = 0;
    
    
    //for if I add robot mouse
    
    //boolean entered = false;
    //void mouseReleased(){entered=true;}
    
    void mouseReleased(){println(frameRate);}
    
    //key press storage
    boolean[] keys = new boolean[355];
    void keyPressed(){keys[keyCode]=true;}
    void keyReleased(){keys[keyCode]=false;}
    
    
    //begin draw
    void draw(){
    
      //environment
      background(125,250,250);
    
    
      //movement
      if(keys[UP]){
        px+=sin(mouseX*0.02)*5;
        py+=cos(mouseX*0.02)*5;
      }
      if(keys[DOWN]){
        px-=sin(mouseX*0.02)*5;
        py-=cos(mouseX*0.02)*5;
      }
      if(keys[LEFT]){
        px+=sin((mouseX-75)*0.02*5);
        py+=cos((mouseX-75)*0.02*5);
      }
      if(keys[RIGHT]){
        px-=sin((mouseX-75)*0.02*5);
        py-=cos((mouseX-75)*0.02*5);
      }
      if(keys[SHIFT]){
        pz+=3;
      }
      if(keys[ENTER]){
        pz-=3;
      }
    
      //update world
      worldReader();
    
      //camera
      camera(px,py,pz,px+sin(mouseX*0.02),py+cos(mouseX*0.02),pz,0,0,1);
    
    }//end draw
    

    okay, here it is with the slow code ^^

    It was running at 3 fps :( My OS is Windows 7

  • edited April 2014 Answer ✓

    you are trying to draw 30 * 30 * 30=27000 cubes. that's a lot of cubes. so it's no wonder that it is running slow.

    you could use a pshape. that should give you better performance. with pshape the cubes are pushed over to the graphicscard only at statup. with your current solution the cubes are pushed over in every frame.

    check the cubicGridRetained example. it's filed under demos\performance.

  • How exactly would I use PShape in P3D?

  • did you check the example? it's all in there.

  • Okay :)

    btw, the example wouldn't run, even with expanded memory...

  • What error did it gave? And to how much memory did you set it?

  • edited April 2014

    here's what it gave me with 686 megabytes of expanded memory:

    Framebuffer error (framebuffer unsupported), rendering will probably not work as expected Read http://wiki.processing.org/w/OpenGL_Issues for help.
    OpenGL error 1280 at bot beginDraw(): invalid enumerant
    OpenGL error 1286 at bot endDraw(): invalid framebuffer operation
    Framebuffer error (framebuffer unsupported), rendering will probably not work as expected Read http://wiki.processing.org/w/OpenGL_Issues for help.
    #
    # A fatal error has been detected by the Java Runtime Environment:
    #
    #  EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x0000000180058713, pid=3992, tid=4912
    #
    # JRE version: Java(TM) SE Runtime Environment (7.0_40-b43) (build 1.7.0_40-b43)
    # Java VM: Java HotSpot(TM) 64-Bit Server VM (24.0-b56 mixed mode windows-amd64 compressed oops)
    # Problematic frame:
    # C  [ig4icd64.dll+0x58713]
    #
    # Failed to write core dump. Minidumps are not enabled by default on client versions of Windows
    #
    # An error report file with more information is saved as:
    # C:\Users\Owner\Desktop\Processing\processing-2.1.1\hs_err_pid3992.log
    #
    # If you would like to submit a bug report, please visit:
    #   http://bugreport.sun.com/bugreport/crash.jsp
    # The crash happened outside the Java Virtual Machine in native code.
    # See problematic frame for where to report the bug.
    #
    Could not run the sketch (Target VM failed to initialize).
    Make sure that you haven't set the maximum available memory too high.
    For more information, read revisions.txt and Help ? Troubleshooting.
    
  • I suspect that either your graphics card doesn't support all the required features or you need to update the drivers for it to do so. What hardware are you running this on?

  • I'm not too savvy with finding hardware stats, so how would I find out the stuff your asking for? :(

Sign In or Register to comment.