Problems with Heads Up display (HUD) and perspective

edited September 2015 in Questions about Code

I am able to create a Heads Up Display (2D) overlay on my 3D image but I find my code fails when I set the perspective() to be different than the default perspective.

Below is the code.

If I comment out the perspective statement it works as expected drawing the overlay 2D rectangle in the upper left corner. If I leave in the perspective statement the rectangle is drawn inset from the corner by quite a bit. I have tried putting the originalMatrix assignment either before or after the perspective statement to no effect.

// comment
float fov =  (float) PI/2.5f; // lens focal length or something like that

PMatrix originalMatrix; 

public void setup() {
    size(900,600, P3D);
    originalMatrix = (PMatrix) getMatrix();
    float cameraZ= (height/2.0f) / tan(fov/2.0f); // sets clipping plane
    perspective(fov, width*1.0f/height, 
    cameraZ/1000.0f, cameraZ*10.0f);  // near clipping plane smaller than normal

    //originalMatrix = (PMatrix) getMatrix();  // optional 
}

public void display() {

camera(   // set some camera angle-- doesn't seem to matter to the issue
    0f, -800f, 0f,
    2 + 0.05f * ts.x, ts.y, 2 + 0.05f * ts.z,// 2f,-100f,2f,
    0f, 1f, 0f);

    //  ****  (omitted)  DRAW SOME 3D stuff here ******

    // here I add the HUD which in this case is just a square drawn in the upper left corner.
    hint(DISABLE_DEPTH_TEST);
    pushMatrix();
    resetMatrix();
    applyMatrix(originalMatrix);

    fill(100,0,0,100);
    rect(0,0,100,100);
    popMatrix();
    hint(ENABLE_DEPTH_TEST);
}
Sign In or Register to comment.