# Perspective bug in eclipse?

**URL:** <https://discourse.processing.org/t/perspective-bug-in-eclipse/7220>\
**Category:** Coding Questions\
**Created:** [January 5, 2019, 4:10pm UTC](https://discourse.processing.org/t/perspective-bug-in-eclipse/7220 "2019-01-05T16:10:40Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![vjjv](https://avatars.discourse-cdn.com/v4/letter/v/df705f/32.png) [@vjjv](https://discourse.processing.org/u/vjjv)\
**Post date:** [January 5, 2019, 4:10pm UTC](https://discourse.processing.org/t/perspective-bug-in-eclipse/7220/1 "2019-01-05T16:10:40Z")

</div>

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…

```auto
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);

	}

}

```

---

<div class="post-metadata">

**Author:** ![Architector\_4](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/architector_4/32/813_2.png) [@Architector\_4](https://discourse.processing.org/u/Architector_4)\
**Post date:** [January 5, 2019, 5:41pm UTC](https://discourse.processing.org/t/perspective-bug-in-eclipse/7220/2 "2019-01-05T17:41:26Z")

</div>

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:

```auto
  /**
   * 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:

```auto
  /**
   * 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:

> <https://github.com/processing/processing/blob/master/core/src/processing/opengl/PGraphicsOpenGL.java>

---

<div class="post-metadata">

**Author:** ![Architector\_4](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/architector_4/32/813_2.png) [@Architector\_4](https://discourse.processing.org/u/Architector_4)\
**Post date:** [January 5, 2019, 5:47pm UTC](https://discourse.processing.org/t/perspective-bug-in-eclipse/7220/3 "2019-01-05T17:47:11Z")

</div>

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:

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

```

With:

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

```

---

<div class="post-metadata">

**Author:** ![vjjv](https://avatars.discourse-cdn.com/v4/letter/v/df705f/32.png) [@vjjv](https://discourse.processing.org/u/vjjv)\
**Post date:** [January 5, 2019, 9:26pm UTC](https://discourse.processing.org/t/perspective-bug-in-eclipse/7220/4 "2019-01-05T21:26:34Z")

</div>

> [@Architector\_4](#):
>
> in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNES

Thanks! i also notice. 🙂
