jdf / peasycam

Dead-simple mouse-driven camera for Processing
http://MrFeinberg.com/peasycam/
Apache License 2.0
116 stars 35 forks source link

beginHUD/endHUD projection fix #28

Closed diwi closed 7 years ago

diwi commented 7 years ago

beginHUD/endHUD seems to fail for custom set camera projections, e.g. a FoV different than 60 degrees. I just found this by accident and am a bit puzzled that I never encountered it before.

perspective(80 * DEG_TO_RAD, width/(float)height, 1, 5000);

Before: The rectangular border should align with the screen-border. Note, lightning also effects the shading of the HUD-scope. bug_hud

After: fixed_hud

Example (also in the PR):


import peasy.*;

// camera control
PeasyCam peasycam;

PGraphics2D pg_screen;

public void settings() {
  size(800, 600, P3D);
  smooth(8);
}

public void setup() {
//    surface.setResizable(true);

  // default FoV is 60
  perspective(80 * DEG_TO_RAD, width/(float)height, 1, 5000);

  // camera
  peasycam = new PeasyCam(this, 300);

  // just some mask for HUD display test
  pg_screen = (PGraphics2D)createGraphics(width, height, P2D);
  pg_screen.smooth(0);
  pg_screen.beginDraw();
  {
      pg_screen.clear();
      pg_screen.beginShape();
      pg_screen.fill(0xFF000000); pg_screen.vertex(0, 0);
      pg_screen.fill(0xFFFF0000); pg_screen.vertex(width, 0);
      pg_screen.fill(0xFFFFFFFF); pg_screen.vertex(width, height);
      pg_screen.fill(0xFF00FF00); pg_screen.vertex(0, height);
      pg_screen.endShape();
      pg_screen.blendMode(REPLACE);
      pg_screen.noStroke();
      pg_screen.fill(0,0);
      pg_screen.rect(20, 20, width-40, height-40);
  }
  pg_screen.endDraw();
}

public void draw(){

  // in case of surface resizing (happens asynchronous) this has no effect in setup
  // perspective(120 * DEG_TO_RAD, width/(float)height, 1, 5000);
  // peasycam.feed();

  // 3D scene
  ambientLight(128, 128, 128);
  pointLight(255, 128, 64, -200, -200, 10);
  pointLight(64, 128, 255, +200, +200, 10);

  background(16);

  rectMode(CENTER);
  strokeWeight(1);
  stroke(0);
  fill(128);
  rect(0, 0, 400, 400);

  strokeWeight(2);
  stroke(255, 64,  0); line(0,0,0,100,0,0);
  stroke( 32,255, 32); line(0,0,0,0,100,0);
  stroke(  0, 64,255); line(0,0,0,0,0,100);

  translate(80,80,80);
  noStroke();
  fill(128);
  box(50);

  // screen-aligned 2D HUD
  peasycam.beginHUD();
  image(pg_screen, 0, 0);
  peasycam.endHUD();

}
jdf commented 7 years ago

Hi. I'm interested in this work, but right now there's a lot of commented lines, making it hard to understand the changes. It looks like you're still in the middle of working on it. Let me know when you're ready for me to review it.

diwi commented 7 years ago

The comments are the original code. Didn't want to get rid of it just yet.

diwi commented 7 years ago

But basically i think thats it.

I'm not sure about g.noLights(); though. It is useful, but it is not mandatory for the beginHUD/endHUD scope to work.

jdf commented 7 years ago

When you're 100% ready for review (meaning, it 100% works, and there's no old code lying around), let me know.

diwi commented 7 years ago

Hi, cleaned up and ready to review.

diwi commented 7 years ago

"All checks have failed" ... not sure why this keeps popping up?

checks_failed

jdf commented 7 years ago

Ignore it.

diwi commented 7 years ago

I think it's all covered now. Are there further things to look for?

jdf commented 7 years ago

What is the setting that corresponds to undoing the ortho call?

jdf commented 7 years ago

Thank you.

diwi commented 7 years ago

This update also handles resizeable windows

surface.setResizable(true);

... which is what brought me to this issue in the first place.

jdf commented 7 years ago

woohoo! I'll build and deploy.