Skip to content

ortho broken (again) - suggested simple fix #6175

@usuallyannoyed

Description

@usuallyannoyed

Description

ortho is broken and doesn't work as expected. I found other bug reports on this but they were >7 years old so I'm reposting, a lot has changed since then.

Expected Behavior

ortho(0, width, 0, height), with a clear modelview matrix (resetMatrix), should enable the user to draw within the bounds specified.

Current Behavior

It doesn't work. Because of a bug in the ortho function (see below), this results in the user having to draw within (-height, 0) (all negative) to see. This is wrong and counterintuitive.

Online comments about people having to compensate because of some nonsense about left handed or right handed system, or processing's negative y, are blaming the user and proposing workarounds for a clear bug.

Steps to Reproduce

void setup() {
  size(500, 500, P3D);
  setupPOGL(); 
}

void draw() {
  ortho(0, width, 0, height);
  resetMatrix(); // clear modelview matrix
  fill(255,255,255);
  //translate(0, -height); // fixes it
  beginShape(TRIANGLES);
  vertex(640, 640);
  vertex(0, 640);
  vertex(0, 0);
  endShape();
}

Your Environment

Windows 10. Processing 3.5.4

Possible Causes / Solutions

https://github.com/processing/processing/blob/master/core/src/processing/opengl/PGraphicsOpenGL.java
Lines 4493-4522 highlight the problem.
Note line 4515 says: // The minus sign is needed to invert the Y axis.
And then on line 4517 it's -y. What this does is to flip the y axis on zero. Not invert the y axis. This is why when the ortho isn't centered, it puts /all/ the y into the negative space.

To fix it is quite simple. To invert the y axis, you just invert the Y in NDC, /after/ the projection into NDC. This can be done by pre-multiplying the projection matrix with a -1 y scale. If you precalculate it, you can see that this can be fixed by simply putting a - also in front of the ty on line 4517

// This has a pre-multiplied -1 y scale to flip the y axis in NDC.
    projection.set(x,  0, 0, tx,
                   0, -y, 0, -ty,
                   0,  0, z, tz,
                   0,  0, 0,  1);

This is rooted in Processing's unfortunate decision to have the Y axis going down to follow traditional computer coordinate systems. While that decision is defensible, the way it's implemented ad-hoc throughout is really frustrating for any serious graphics programmer. It really should have been added at the bottom level (in NDC or on the viewport), and exposed as a flag that someone could disable.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions