Perspective bug in eclipse?

hello! im trying to do this en eclipse, using perspective to set the nearClip (is there any other way to set the near and far clip without the perspective() function?) in eclipse the function dont work fine, please take a look, its maybe a bug i think… o perhaps im missing something…

import processing.core.PApplet;

public class Testing extends PApplet {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		PApplet.main("Testing");
	}

	public void settings() {
		size(1280, 720, P3D);
	}

	public void setup() {

	}

	public void draw() {
		float fov = (float) (PI / 3.0);
		float cameraZ = (float) ((height / 2.0) / tan((float) (fov / 2.0)));
		float nearClippingDistance = (float) 0.01; // default is cameraZ/10.0
		perspective(fov, (width) / (height), nearClippingDistance, cameraZ * 10); // coment and discomment 
		camera(0, 0, -200, 0, 0, 100, 0, 1, 0);
		// translate(width / 2, height / 2);
		stroke(0);
		sphere(50);

	}

}
1 Like

Reading up on Processing source code, there’s a variable g that PApplet(and your sketch since it extends PApplet) has, that simply refers to the current canvas - which, when you do size(..., P3D); is set to an object of class processing.opengl.PGraphics3D, which extends PGraphicsOpenGL.

Then, function perspective(... is simply forwarded to g.perspective(..., which does the code on that object g.

That function of PGraphicsOpenGL does this:

  /**
   * Similar to gluPerspective(). Implementation based on Mesa's glu.c
   */
  @Override
  public void perspective(float fov, float aspect, float zNear, float zFar) {
    float ymax = zNear * (float) Math.tan(fov / 2);
    float ymin = -ymax;
    float xmin = ymin * aspect;
    float xmax = ymax * aspect;
    frustum(xmin, xmax, ymin, ymax, zNear, zFar);
  }

Which also calls frustum, which is this:

  /**
   * Same as glFrustum(), except that it wipes out (rather than multiplies
   * against) the current perspective matrix.
   * <P>
   * Implementation based on the explanation in the OpenGL blue book.
   */
  @Override
  public void frustum(float left, float right, float bottom, float top,
                      float znear, float zfar) {
    // Flushing geometry with a different perspective configuration.
    flush();

    cameraFOV = 2 * (float) Math.atan2(top, znear);
    cameraAspect = left / bottom;
    cameraNear = znear;
    cameraFar = zfar;

    float n2 = 2 * znear;
    float w = right - left;
    float h = top - bottom;
    float d = zfar - znear;

    projection.set(n2 / w,       0,  (right + left) / w,                0,
                        0, -n2 / h,  (top + bottom) / h,                0,
                        0,       0, -(zfar + znear) / d, -(n2 * zfar) / d,
                        0,       0,                  -1,                0);

    updateProjmodelview();
  }

Looks like it involves some scary maths, where everything is related and changing a thing could drastically screw everything up…
You could probably just mess with g.cameraNear and g.cameraFar, and then try doing g.updateProjmodelview();, but no promises.

.

Code snippets from above copied from Processing source code from here:

1 Like

Also, just figured out why the image comes out stretched - it’s because of how you are setting the second parameter of perspective.

You are setting it to width/height, which is correct, but the problem is that both width and height are integers, so Java does integer division, which results in 1.

What you need to do instead is (float)width/height so that it would do float division and actually return 1.77777 that is needed to make the aspect ratio proper.

So, replace:

	perspective(fov, (width) / (height), nearClippingDistance, cameraZ * 10); // coment and discomment 

With:

	perspective(fov, (float)width/height, nearClippingDistance, cameraZ * 10); // coment and discomment 
2 Likes

Thanks! i also notice. :slight_smile: