Fix for rounding errors in viewport dimensions

It is currently possible for the width of a viewport to be a pixel narrower than the distance between left and right edges. The same applies for viewport height.

The error manifests itself as single-pixel-wide gaps between adjacent viewports when using multiple viewports at certain window resolutions.

The cause of the error is due to rounding and truncation; The distributive property does not hold.

I have managed to fix this by changing 2 lines in RenderManager.java:
The calculations for viewWidth and viewHeight in the function setViewPort:
(The previous calculations have just been commented out for comparison)

private void setViewPort(Camera cam) {
    // this will make sure to update viewport only if needed
    if (cam != prevCam || cam.isViewportChanged()) {
        viewX = (int) (cam.getViewPortLeft() * cam.getWidth());
        viewY = (int) (cam.getViewPortBottom() * cam.getHeight());
        //viewWidth = (int) ((cam.getViewPortRight() - cam.getViewPortLeft()) * cam.getWidth());
        //viewHeight = (int) ((cam.getViewPortTop() - cam.getViewPortBottom()) * cam.getHeight());
        viewWidth = ((int)(cam.getViewPortRight() * cam.getWidth())) - ((int)(cam.getViewPortLeft() * cam.getWidth()));
        viewHeight = ((int)(cam.getViewPortTop() * cam.getHeight())) - ((int)(cam.getViewPortBottom() * cam.getHeight()));
        uniformBindingManager.setViewPort(viewX, viewY, viewWidth, viewHeight);
        renderer.setViewPort(viewX, viewY, viewWidth, viewHeight);
        renderer.setClipRect(viewX, viewY, viewWidth, viewHeight);
        cam.clearViewportChanged();
        prevCam = cam;

//            float translateX = viewWidth == viewX ? 0 : -(viewWidth + viewX) / (viewWidth - viewX);
//            float translateY = viewHeight == viewY ? 0 : -(viewHeight + viewY) / (viewHeight - viewY);
//            float scaleX = viewWidth == viewX ? 1f : 2f / (viewWidth - viewX);
//            float scaleY = viewHeight == viewY ? 1f : 2f / (viewHeight - viewY);
//            
//            orthoMatrix.loadIdentity();
//            orthoMatrix.setTranslation(translateX, translateY, 0);
//            orthoMatrix.setScale(scaleX, scaleY, 0); 

        orthoMatrix.loadIdentity();
        orthoMatrix.setTranslation(-1f, -1f, 0f);
        orthoMatrix.setScale(2f / cam.getWidth(), 2f / cam.getHeight(), 0f);
    }
}

How do I go about submitting this as a patch?

Easiest would be to submit a pull request on github:

Or you can just post the .patch file here.

Thanks, I’ll look into this.

Ok I’ve submitted a pull-request. Thanks again :smile:

1 Like