Skip to content

Commit

Permalink
Merge pull request #28 from diwi/bugfix_HUD
Browse files Browse the repository at this point in the history
beginHUD/endHUD projection fix
  • Loading branch information
Jonathan Feinberg authored Oct 20, 2017
2 parents 79e2bcb + a3ccfd0 commit 078290b
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 11 deletions.
1 change: 1 addition & 0 deletions .classpath
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="src" path="test"/>
<classpathentry kind="src" path="testHUD"/>
<classpathentry combineaccessrules="false" kind="src" path="/processing-core"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="output" path="bin"/>
Expand Down
50 changes: 39 additions & 11 deletions src/peasy/PeasyCam.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,17 @@
*/
package peasy;


import peasy.org.apache.commons.math.geometry.CardanEulerSingularityException;
import peasy.org.apache.commons.math.geometry.Rotation;
import peasy.org.apache.commons.math.geometry.RotationOrder;
import peasy.org.apache.commons.math.geometry.Vector3D;
import processing.core.PApplet;
import processing.core.PConstants;
import processing.core.PGraphics;
import processing.core.PMatrix3D;
import processing.event.KeyEvent;
import processing.event.MouseEvent;
import processing.opengl.PGraphicsOpenGL;

/**
*
Expand Down Expand Up @@ -99,8 +100,6 @@ public void handleWheel(final int delta) {
private final PeasyEventListener peasyEventListener = new PeasyEventListener();
private boolean isActive = false;

private final PMatrix3D originalMatrix; // for HUD restore

public final String VERSION = "202";

public PeasyCam(final PApplet parent, final double distance) {
Expand All @@ -119,11 +118,10 @@ public PeasyCam(final PApplet parent, final PGraphics pg, final double distance)
public PeasyCam(final PApplet parent, PGraphics pg, final double lookAtX, final double lookAtY,
final double lookAtZ, final double distance) {
this.p = parent;
this.g = pg;
this.g = pg;
this.startCenter = this.center = new Vector3D(lookAtX, lookAtY, lookAtZ);
this.startDistance = this.distance = Math.max(distance, SMALLEST_MINIMUM_DISTANCE);
this.rotation = new Rotation();
this.originalMatrix = parent.getMatrix((PMatrix3D)null);

feed();

Expand Down Expand Up @@ -581,21 +579,51 @@ public float[] getRotations() {
return new float[] { 0, 0, 0 };
}




private boolean pushedLights = false;

/**
* Thanks to A.W. Martin for the code to do HUD
*
* begin screen-aligned 2D-drawing.
* <pre>
* beginHUD()
* disabled depth test
* disabled lights
* ortho
* endHUD()
* </pre>
*
*/
public void beginHUD() {
g.pushMatrix();
g.hint(PConstants.DISABLE_DEPTH_TEST);
// Load the identity matrix.
g.pushMatrix();
g.resetMatrix();
// Apply the original Processing transformation matrix.
g.applyMatrix(originalMatrix);
// 3D is always GL (in processing 3), so this check is probably redundant.
if(g.isGL() && g.is3D()){
PGraphicsOpenGL pgl = (PGraphicsOpenGL)g;
pushedLights = pgl.lights;
pgl.lights = false;
pgl.pushProjection();
g.ortho(0, g.width, -g.height, 0, -Float.MAX_VALUE, +Float.MAX_VALUE);
}
}


/**
*
* end screen-aligned 2D-drawing.
*
*/
public void endHUD() {
g.hint(PConstants.ENABLE_DEPTH_TEST);
if(g.isGL() && g.is3D()){
PGraphicsOpenGL pgl = (PGraphicsOpenGL)g;
pgl.popProjection();
pgl.lights = pushedLights;
}
g.popMatrix();
g.hint(PConstants.ENABLE_DEPTH_TEST);
}

abstract public class AbstractInterp {
Expand Down
88 changes: 88 additions & 0 deletions testHUD/test/Peasycam_testHUD.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package test;

import peasy.*;
import processing.core.PApplet;


public class Peasycam_testHUD extends PApplet {


PeasyCam peasycam;

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

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

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

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

public void draw(){

// in case of surface resizing (happens asynchronous) this has no effect in setup
// perspective(90 * 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(32);

rectMode(CENTER);
noStroke();
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();

int wh = 100;
rectMode(CORNER);
noStroke();
fill(0xFFFF0000); rect( 0, 0, wh, wh);
fill(0xFF00FF00); rect(width-wh, 0, wh, wh, 30);
fill(0xFF0000FF); rect(width-wh,height-wh, wh, wh);
fill(0xFFFFFFFF); rect( 0,height-wh, wh, wh, 30);

peasycam.endHUD();


// check if everything got restored
// Note: Depth testing was disabled during the HUD-drawing
translate(0,-80,0);
noStroke();
fill(128);
box(60);



}



public static void main(String args[]) {
PApplet.main(new String[] { Peasycam_testHUD.class.getName() });
}

}

0 comments on commit 078290b

Please sign in to comment.