Faster 3D performance?

edited December 2013 in Android Mode

Hey folks,

I'm making an android app with P3D renderer, but it's nearly uncontrollable because it's terribly slow. What I'm doing is making use of dozens of vertexes to draw some weird polysurfaces. The interaction here is just dragging vertexes to adjust its position, therefore changing the whole shape, and rotating the screen to see every face of the shape. I've tried not specifying render in size() method, but it says like the renderer does not support screenX(), screenY() and screenZ() functions. Those functions are crucial because you are to drag points in 3D space with 2D interface, so I cannot get rid of those parts. Is there any better way to deal with 3D in android mode, or are any better renderers out there?

BTW, it works just great if I run the sketch in JAVA mode. And the android device I'm testing with is NEXUS 7 with Android version 4.4.

Thank you in advance, and let me know if additional information is needed.

Tagged:

Answers

  • Perhaps you can use some of the new PShape functionality (tutorial is for 2D, but I think it works in 3D as well) that allows the shapes to be stored on the GPU for faster rendering. I have not dealt with this myself... but it's worth mentioning.

  • edited January 2014

    Hello,

    I'm also just starting to code with processing and was trying to work out to get fast 3D performance in android. Finally, using PShapes and PShape(GROUP) I got a rather smooth rendering on my smartphone. Find an example below. This is maybe not exactly what you are looking for, but maybe it helps somehow.

    float alpha;
    MLemon lemon;
    int drawMode;
    boolean mouseState;
    
    void setup()
    {
      size(displayWidth,displayHeight,P3D);
      smooth();
      lemon = new MLemon(displayHeight/3,30);
      drawMode=0;
      mouseState=false;
    }
    
    void draw()
    {
      background(0);
    
      camera(0, 0, (height/2.0) / tan(PI*30.0 / 180.0), 0, 0, 0, 0, 1, 0);
      ambientLight(50, 50, 50);
      directionalLight(200, 200, 200, 1, 1, -1);
    
      fill(255);
      text ("fps: " + frameRate, -250, -350);
    
      rotateX(alpha);
      rotateY(alpha);
    
      lemon.draw();
    
      alpha+=0.01;
    
      if (!mouseState) {
        if (mousePressed) {
          drawMode++;
          drawMode%=3;
          mouseState=true;
        }
      }
      else {
        if (!mousePressed)
          mouseState=false;
      }
    
    }
    
    class MVertex {
      PVector pos;
      PVector normal;
    
      MVertex(float x, float y, float z) {
        pos=new PVector(x,y,z);
        normal=new PVector(0,0,0);
      }
    
      void addNormal(PVector n) {
        normal.add(n);
      }
    
      void normalizeNormal() {
        normal.normalize();
      }
    }
    
    class MShape {
      PShape ps;
      MVertex[] vertex;
      PVector shapeNormal;
      PVector shapeCenter;
    
      MShape(MVertex[] points) {
        this.vertex=points;
    
        // calculate shape normal
        shapeNormal = PVector.sub(vertex[1].pos, vertex[3].pos).cross(PVector.sub(vertex[0].pos, vertex[2].pos));
        shapeNormal.normalize();
    
        // calculate shape center
        shapeCenter=PVector.add(PVector.add(vertex[0].pos,vertex[1].pos),PVector.add(vertex[2].pos,vertex[3].pos));
        shapeCenter.div(4);
      }
    
      void create() {
        ps=createShape();
        ps.beginShape(QUADS);
        ps.noStroke();
        for (int j=0;j<vertex.length;j++) {
          ps.fill(color(255,255,0));
          ps.normal(vertex[j].normal.x,vertex[j].normal.y,vertex[j].normal.z);
          ps.vertex(vertex[j].pos.x,vertex[j].pos.y,vertex[j].pos.z);
        }
        ps.endShape(CLOSE);
      }
    }
    
    // create points and faces of a "lemon" object
    class MLemon {
      MShape[] lemShapes;
      MVertex[] lemPoints;
      PShape lemGroup;
    
      MLemon(float r, int n) {
        MVertex[] a;
    
        int nrShapes=2*n*n;
        int nrPoints=2*n*(n+1);
    
        lemShapes=new MShape[nrShapes];
        lemPoints=new MVertex[nrPoints];
    
        // Points    
        float wb, wl;
        for (int b=0; b<=n; b++) {
          for (int l=0; l<2*n; l++) {
            int newPoint=l+b*2*n;
            wl=l*PI/n;
            wb=b*PI/n;
            lemPoints[newPoint]=new MVertex(0,r,0);
            vectorRotZ(wb,lemPoints[newPoint].pos);
            vectorRotY(wl,lemPoints[newPoint].pos);
            lemPoints[newPoint].pos.mult(1-0.3*sin(wb));
          }
        }
    
        // Faces
        for (int b=1; b<=n; b++) {
          for (int l=0; l<2*n; l++) {
            a = new MVertex[4];
    
            int point0=(b-1)*2*n+l;
            int point1=(b-1)*2*n+(l+1)%(2*n);
            int point2=(b)*2*n+(l+1)%(2*n);
            int point3=(b)*2*n+l;
            int newShape=(b-1)*2*n+l;
    
            a[0] = lemPoints[point0];
            a[1] = lemPoints[point1];
            a[2] = lemPoints[point2];
            a[3] = lemPoints[point3];
    
            lemShapes[newShape] = new MShape(a);
          }
        }
    
        // calculate normals
        for (int shape=0;shape<lemShapes.length;shape++) {
          for (int vert=0; vert<lemShapes[shape].vertex.length; vert++) {
            lemShapes[shape].vertex[vert].addNormal(lemShapes[shape].shapeNormal);
          }
        }
        for (int point=0;point<lemPoints.length;point++) {
          lemPoints[point].normalizeNormal();
        }
    
        // create PShapes
        for (int shape=0;shape<lemShapes.length;shape++) {
            lemShapes[shape].create();
        }
    
        // create GROUP Shape
        lemGroup=createShape(GROUP);
        for (int i=0; i<lemShapes.length; i++) {
          lemGroup.addChild(lemShapes[i].ps);
        }
      }
    
      void vectorRotY(float w, PVector v) {
        float s=sin(w);
        float c=cos(w);
        float res_x= c*v.x+s*v.z;
        float res_z=-s*v.x+c*v.z;
        v.set(res_x,v.y,res_z);    
      }
    
      void vectorRotZ(float w, PVector v) {
        float s=sin(w);
        float c=cos(w);
        float res_x=c*v.x-s*v.y;
        float res_y=s*v.x+c*v.y;
        v.set(res_x,res_y,v.z);    
      }
    
      void draw() {
        PVector m;
    
        // draw lemon
        shape(lemGroup);
    
        // draw shape normals
        if (drawMode>0) {      
          for (int shape=0; shape<lemShapes.length; shape++) {
            pushMatrix();
            stroke(255);
            translate(lemShapes[shape].shapeCenter.x,lemShapes[shape].shapeCenter.y,lemShapes[shape].shapeCenter.z);
            line(0,0,0,lemShapes[shape].shapeNormal.x*50,lemShapes[shape].shapeNormal.y*50,lemShapes[shape].shapeNormal.z*50);
            popMatrix();
          }
        }
    
        // draw vertex normals
        if (drawMode>1) {
          for (int point=0; point<lemPoints.length; point++) {
            pushMatrix();
            translate(lemPoints[point].pos.x,lemPoints[point].pos.y,lemPoints[point].pos.z);
            stroke(255,0,0);
            line(0,0,0,lemPoints[point].normal.x*50,lemPoints[point].normal.y*50,lemPoints[point].normal.z*50);
            popMatrix();
          }
        }
      }
    }
    
Sign In or Register to comment.