go-gl / mathgl

A pure Go 3D math library.
BSD 3-Clause "New" or "Revised" License
554 stars 65 forks source link

Incorrect coordinate mapping in ScreenToGLCoords and GLToScreenCoords. #64

Closed db47h closed 6 years ago

db47h commented 6 years ago

Unless I'm mistaken, screen X coordinates are in the range [0, screenWidth-1], not [0, screenWidth] (the same applies to the Y coordinate), and GL's coordinate system maps [screenWidth-1,0] to [1.0,1.0] and [0,screenHeight-1] to [-1.0,-1.0].

So the code for ScreenToGLCoords should be:

func ScreenToGLCoords(x, y int, screenWidth, screenHeight int) (xOut, yOut float64) {
    xOut = 2.0*float64(x)/float64(screenWidth-1) - 1.0
    yOut = -2.0*float64(y)/float64(screenHeight-1) + 1.0

    return
}
UserAB1236872 commented 6 years ago

Hey, sorry, I've been working double-long hackathon-level days at work recently. If you submit a PR I'd be glad to accept it, but it will probably have to wait a few weeks otherwise.

db47h commented 6 years ago

No worries, PR incoming.

As I wrote a test for the fix, I found out that GLToScreenCoords inverts the sign of the returned y-coordinate. So that'll get fixed as well.

Reminder to self: trivial tests for trivial functions are not that useless :wink: