Apply PeasyCam on PGraphics3D

edited February 2018 in Library Questions

At the moment I am working on a multi-pass rendering in processing. There I have to use a new PGraphics3D canvas and I can not use the default g object. After the rendering and shading, I'm just going to "print" it on to the g object again as texture:

cam.beginHUD();
image(canvas, 0, 0);
cam.endHUD();

Now I have the problem that things like peasycam do not work anymore because they are attached to the original g and do not transform the camera matrix of my canvas.

So I tried to use the camera matrix of the original graphics and copy it to my canvas:

PGraphics3D p = (PGraphics3D)this.g;
canvas.camera = p.camera;

This did not work, maybe because peasycam is doing something special and it does not change the original camera matrix. So my next idea was to use the static apply method from peasy cam. I had to copy the function out of the original code and make it public:

import peasy.org.apache.commons.math.geometry.Rotation;
import peasy.org.apache.commons.math.geometry.Vector3D;

Vector3D positionVec = new Vector3D(cam.getPosition()[0], cam.getPosition()[1], cam.getPosition()[2]);
Vector3D rotationVec = new Vector3D(cam.getRotations()[0], cam.getRotations()[1], cam.getRotations()[2]);
apply(canvas, positionVec, new Rotation(rotationVec, 0), cam.getDistance());

Here the apply method:

void apply(final PGraphics g, final Vector3D center, final Rotation rotation, 
  final double distance) {
  final Vector3D pos = rotation.applyTo(Vector3D.plusK).scalarMultiply(distance).add(center);
  final Vector3D rup = rotation.applyTo(Vector3D.plusJ);
  g.camera((float)pos.getX(), (float)pos.getY(), (float)pos.getZ(), //
    (float)center.getX(), (float)center.getY(), (float)center.getZ(), //
    (float)rup.getX(), (float)rup.getY(), (float)rup.getZ());
}

There I always have a zero norm for rotation (Arithmetic Exception). So my final idea was to just copy all the rotations and translation to my camera matrix:

canvas.beginCamera();
canvas.camera();

canvas.rotateX(cam.getRotations()[0]);
canvas.rotateY(cam.getRotations()[1]);
canvas.rotateZ(cam.getRotations()[2]);

canvas.translate(
  cam.getPosition()[0], 
  cam.getPosition()[1], 
  cam.getPosition()[2]);

canvas.endCamera();

So now I could rotate my cube, but it was not accurate and I think there is something wrong with some axis.

I would like to ask if someone has done this before and if there is a simple method to do this?

Answers

  • T_DT_D
    Answer ✓

    cam.getState().apply(canvas); should work

  • hello! here i have the same problem but i can't resolve with cam.getState().apply()

    PShader sh;
    PShader sha;
    
    PGraphics buffer;
    PGraphics buffer2;
    
    
    
    import com.hamoid.*;
    import themidibus.*;
    import peasy.*;
    import peasy.org.apache.commons.math.*;
    import peasy.org.apache.commons.math.*;
    
    PeasyCam cam;
    float count;
    
    void settings () {
    
      fullScreen(P3D);
    }
    
    void setup(){
    
    background(0);
    
    
      cam = new PeasyCam(this, 500);
    
    
      sh = loadShader("basicShaderFrag.glsl", "basicShader.glsl");
      sha = loadShader("frag2.glsl", "basicShader.glsl");
    
     buffer = createGraphics(width, height, P3D);
     buffer2 = createGraphics(width, height, P3D);
    
    }
    
    void draw(){
    
        background(0);
         // println(frameRate);
        count +=0.005;
        sh.set("u_time", count);
    
        buffer.beginDraw();
        render(buffer);
        buffer.endDraw();
    
        buffer2.beginDraw();
        buffer2.shader(sh);
        buffer2.image(buffer,0, 0);
        buffer2.endDraw();
    
        cam.beginHUD();
        image(buffer2, 0,0);
        cam.endHUD();
    
    }
    
    
    void render(PGraphics a){
      cam.getState().apply(a);  // change here.
      a.background(0, 50);
    
      a.noStroke();
      s.run(); 
       a.sphere(200);
    }
    
  • Probably best not to bump threads over a year old with duplicate questions.

Sign In or Register to comment.