switching to ortho() from perpective() with Peasy and ControlP5

sussus
edited April 2014 in Library Questions

Hi everyone,

I am trying to switch from a perspective view mode to an ortho view mode. So far, the persp mode works well but when calling the orth mode, the cp5 group translate a bit, if the camera have been rotated, the entire stuff (object+control group) gets moved away.

Here is a simplified version of my code that reproduce the problem. Any clues on how to switch from perpective() to ortho() or about what is going on ? (I tried using resestMatrix() or begin/endCamera() without success)...

import peasy.*;
import controlP5.*;

ControlP5 cp5;
Accordion panelControl;
DropdownList ViewportList;
PeasyCam camera;

PVector Pos = new PVector(400, 300);
int viewPortValue;
int updateViewPortValue;

void setup() {
  size(600, 600, P3D);
  viewPortValue = 0;
  initialiseCamera();
  initialiseGUI();
}

void draw() {
  background(0);
  pushStyle();
  pushMatrix();
  translate(Pos.x, Pos.y);
  fill(255);
  box(50);
  popMatrix();
  popStyle();
  GUI();
}

void initialiseGUI() { //-----called in the setup
  cp5 = new ControlP5(this);

  Group InitGroup = cp5.addGroup("Initialisation") //creates a CP5 group
    .setBackgroundColor(color(20))
      .setBackgroundHeight(100);

  ViewportList = cp5.addDropdownList("viewport") //creates the viewPortList
    .setId(1)
      .setLabel("viewport")
        .setPosition(20, 20)
          .setSize(170, 200);
  ViewportList.addItem("top (persp)", 0);
  ViewportList.addItem("top (ortho)", 1);
  ViewportList.addItem("3D", 2);
  ViewportList.moveTo(InitGroup);


  panelControl = cp5.addAccordion("acc") //allows me to hide the control group
    .setPosition(5, 20)
      .setWidth(220)
        .addItem(InitGroup);

  cp5.setAutoDraw(false);
}

void controlEvent(ControlEvent theEvent) { //listen for changes in the control group
  if (theEvent.isGroup()&& theEvent.getGroup().getId()== 1) {
    updateViewPortValue = (int)theEvent.getGroup().getValue();
  }
}

void GUI() { //-----called in the draw
  //beginCamera();
  updateViewPort();
  hint(DISABLE_DEPTH_TEST);
  camera.beginHUD();
  cp5.draw();
  camera.endHUD();
  hint(ENABLE_DEPTH_TEST);
  //endCamera();**
} // the problem may be here

void initialiseCamera() { //-----called in the setup
  camera = new PeasyCam(this, (Pos.x), (Pos.y), 0, 500);
  camera.setRollRotationMode();
  camera.setRotations(-0, -0, -0);
  camera.setMinimumDistance(50);
  camera.setMaximumDistance(1000);

  camera.setLeftDragHandler(null);
  camera.setCenterDragHandler(null);
  camera.setRightDragHandler(camera.getPanDragHandler());
  camera.setWheelHandler(camera.getZoomWheelHandler());
}

void updateViewPort() { //-----called in the draw

  if (updateViewPortValue != viewPortValue) {
    viewPortValue = updateViewPortValue;

    if (viewPortValue == 0) { //top perspective

      perspective();
      camera.setLeftDragHandler(null);
      camera.setCenterDragHandler(null);
      camera.setRightDragHandler(camera.getPanDragHandler());
      camera.setWheelHandler(camera.getZoomWheelHandler());
      camera.setMinimumDistance(10);
      camera.setRotations(-0, -0, -0);
      camera.lookAt((Pos.x), (Pos.y), 0, 500);
    }

    else if (viewPortValue == 1) { //top ortho
      camera.reset(); // an attempt to avoid having the control group flushed when switching to ortho
      ortho();
      camera.setLeftDragHandler(null);
      camera.setCenterDragHandler(null);
      camera.setWheelHandler(null);
      camera.setRightDragHandler(camera.getPanDragHandler());

      camera.setRotations(-0, -0, -0);
      camera.lookAt((Pos.x), (Pos.y), 0, 500); // changing the last value translate the control group a bit more
    }
    else if (viewPortValue == 2) { //3d
      perspective();
      camera.setLeftDragHandler(null);
      camera.setCenterDragHandler(camera.getPanDragHandler());
      camera.setRightDragHandler(camera.getRotateDragHandler());
      camera.setWheelHandler(camera.getZoomWheelHandler());
      camera.setFreeRotationMode();
      camera.setRotations(-1, -0.7, 0.35);
      camera.setMinimumDistance(100);
      camera.lookAt((Pos.x), (Pos.y), 0, 500);
    }
  }
}

Answers

  • This is a tough one. It's how camera.beginHUD handles things and how cp5 does certain things. Combined it's trouble.

    This will fix it:

      //hint(DISABLE_DEPTH_TEST);
      if (viewPortValue != 1) camera.beginHUD();
      cp5.draw();
      rect(10, 10, 50, 50);
      if (viewPortValue != 1)  camera.endHUD();
     // hint(ENABLE_DEPTH_TEST);
    
  • Many thanks,

    It fixes the placement of the p5 control group only if no pan or rotation was performed in the previous viewport (and if pan is also disabled in orth view, but this is not a problem). I think reseting the camera prior to any change in viewport should fix this, I tried this unsuccessfully :

      if (updateViewPortValue != viewPortValue) camera.reset();
      updateViewPort();
    
      // hint(DISABLE_DEPTH_TEST);
      if (viewPortValue != 1) camera.beginHUD();
      cp5.draw();
      //rect(10, 10, 50, 50);
      if (viewPortValue != 1)  camera.endHUD();
      //hint(ENABLE_DEPTH_TEST);
    
Sign In or Register to comment.