Skip to content

Commit

Permalink
Merge pull request #2 from bbredesen/master
Browse files Browse the repository at this point in the history
Master to main
  • Loading branch information
bbredesen committed Mar 5, 2023
2 parents ae5679f + 09a93c3 commit 9f013d7
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 66 deletions.
7 changes: 3 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,7 @@ floats on the GPU. As a consequence, and rather than constantly forcing float32
You can generate a simple tranformation with the `NewMat__` variants:

```go
import (
"github.com/bbredesen/vkm"
"github.com/chewxy/math32"
)
import "github.com/bbredesen/vkm"
// ...
transVec := vkm.NewVec(-1, -2, -3)
transMat := vkm.NewMatTranslate(transVec)
Expand All @@ -43,6 +40,8 @@ Rotations are measured in radians by default, but each rotation function has a
normalize the vector first:

```go
import "github.com/chewxy/math32"
// ...
axisVec := vkm.NewVec(1, 1, 1).Normalize()
aRotationMat := vkm.NewMatRotate(axisVec, math32.Pi)
```
Expand Down
29 changes: 0 additions & 29 deletions matrix.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,33 +154,4 @@ func (m Mat) Inverse() Mat {
(m[0][0]*m[1][1]*m[2][2] + m[1][0]*m[2][1]*m[0][2] + m[2][0]*m[0][1]*m[1][2] - m[2][0]*m[1][1]*m[0][2] - m[1][0]*m[0][1]*m[2][2] - m[0][0]*m[2][1]*m[1][2]) / d,
},
}
// return Mat{
// {
// (m[1][1]*m[2][2]*m[3][3] + m[1][2]*m[3][2]*m[1][3] + m[3][1]*m[1][2]*m[2][3] - m[3][1]*m[2][2]*m[1][3] - m[2][1]*m[1][2]*m[3][3] - m[1][1]*m[3][2]*m[2][3]) / d,
// -(m[1][0]*m[2][2]*m[3][3] + m[2][0]*m[3][2]*m[1][3] + m[3][0]*m[1][2]*m[2][3] - m[3][0]*m[2][2]*m[1][3] - m[2][0]*m[1][2]*m[3][3] - m[1][0]*m[3][2]*m[2][3]) / d,
// (m[1][0]*m[2][1]*m[3][3] + m[2][0]*m[3][1]*m[1][3] + m[3][0]*m[1][1]*m[2][3] - m[3][0]*m[2][1]*m[1][3] - m[2][0]*m[1][1]*m[3][3] - m[1][0]*m[3][1]*m[2][3]) / d,
// -(m[1][0]*m[2][1]*m[3][2] + m[2][0]*m[3][1]*m[1][2] + m[3][0]*m[1][1]*m[2][2] - m[3][0]*m[2][1]*m[1][2] - m[2][0]*m[1][1]*m[3][2] - m[1][0]*m[3][1]*m[2][2]) / d,
// },

// {
// -(m[0][1]*m[2][2]*m[3][3] + m[2][1]*m[3][2]*m[0][3] + m[3][1]*m[0][2]*m[2][3] - m[3][1]*m[2][2]*m[0][3] - m[2][1]*m[0][2]*m[3][3] - m[0][1]*m[3][2]*m[2][3]) / d,
// (m[0][0]*m[2][2]*m[3][3] + m[2][0]*m[3][2]*m[0][3] + m[3][0]*m[0][2]*m[2][3] - m[3][0]*m[2][2]*m[0][3] - m[2][0]*m[0][2]*m[3][3] - m[0][0]*m[3][2]*m[2][3]) / d,
// -(m[0][0]*m[2][1]*m[3][3] + m[2][0]*m[3][1]*m[0][3] + m[3][0]*m[0][1]*m[2][3] - m[3][0]*m[2][1]*m[0][3] - m[2][0]*m[0][1]*m[3][3] - m[0][0]*m[3][1]*m[2][3]) / d,
// (m[0][0]*m[2][1]*m[3][2] + m[2][0]*m[3][1]*m[0][2] + m[3][0]*m[0][1]*m[2][2] - m[3][0]*m[2][1]*m[0][2] - m[2][0]*m[0][1]*m[3][2] - m[0][0]*m[3][1]*m[2][2]) / d,
// },

// {
// (m[0][1]*m[1][2]*m[3][3] + m[1][1]*m[3][2]*m[0][3] + m[3][1]*m[0][2]*m[1][3] - m[3][1]*m[1][2]*m[0][3] - m[1][1]*m[0][2]*m[3][3] - m[0][1]*m[3][2]*m[1][3]) / d,
// -(m[0][0]*m[1][2]*m[3][3] + m[1][0]*m[3][2]*m[0][3] + m[3][0]*m[0][2]*m[1][3] - m[3][0]*m[1][2]*m[0][3] - m[1][0]*m[0][2]*m[3][3] - m[0][0]*m[3][2]*m[1][3]) / d,
// (m[0][0]*m[1][1]*m[3][3] + m[1][0]*m[3][1]*m[0][3] + m[3][0]*m[0][1]*m[1][3] - m[3][0]*m[1][1]*m[0][3] - m[1][0]*m[0][1]*m[3][3] - m[0][0]*m[3][1]*m[1][3]) / d,
// -(m[0][0]*m[1][1]*m[3][2] + m[1][0]*m[3][1]*m[0][2] + m[3][0]*m[0][1]*m[1][2] - m[3][0]*m[1][1]*m[0][2] - m[1][0]*m[0][1]*m[3][2] - m[0][0]*m[3][1]*m[1][2]) / d,
// },

// {
// -(m[0][1]*m[1][2]*m[2][3] + m[1][1]*m[2][2]*m[0][3] + m[2][1]*m[0][2]*m[1][3] - m[2][1]*m[1][2]*m[0][3] - m[1][1]*m[0][2]*m[2][3] - m[0][1]*m[2][2]*m[1][3]) / d,
// (m[0][0]*m[1][2]*m[2][3] + m[1][0]*m[2][2]*m[0][3] + m[2][0]*m[0][2]*m[1][3] - m[2][0]*m[1][2]*m[0][3] - m[1][0]*m[0][2]*m[2][3] - m[0][0]*m[2][2]*m[1][3]) / d,
// -(m[0][0]*m[1][1]*m[2][3] + m[1][0]*m[2][1]*m[0][3] + m[2][0]*m[0][1]*m[1][3] - m[2][0]*m[1][1]*m[0][3] - m[1][0]*m[0][1]*m[2][3] - m[0][0]*m[2][1]*m[1][3]) / d,
// (m[0][0]*m[1][1]*m[2][2] + m[1][0]*m[2][1]*m[0][2] + m[2][0]*m[0][1]*m[1][2] - m[2][0]*m[1][1]*m[0][2] - m[1][0]*m[0][1]*m[2][2] - m[0][0]*m[2][1]*m[1][2]) / d,
// },
// }
}
33 changes: 4 additions & 29 deletions projection.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,21 @@ import "github.com/chewxy/math32"
// Perspective generates a perspective projection matrix for Vulkan. Note that in Vulkan, the standard z clipping space is in the range
// [0..1], and x/y in the range [-1..1] (or rather -w to +w).
//
// aspect is the ratio of height divided by width
// aspect is the ratio of width divided by height
//
// fov is the vertical field of view measured in radians
//
// near and far are the near and far bounds of the frustum
func Perspective(fov, aspect, near, far float32) Mat {
f := 1 / math32.Tan(fov/2)
return Mat{
{-f / aspect, 0, 0, 0},
{f / aspect, 0, 0, 0},
{0, f, 0, 0},
{0, 0, far / (near - far), -1},
{0, 0, far * near / (near - far), 0},
}
}

// GlTFPerspective is the perspective matrix mandated by the glTF specification.
func GlTFPerspective(fov, aspect, near, far float32) Mat {
f := 1 / math32.Tan(fov/2)
return Mat{
{f / aspect, 0, 0, 0},
{0, f, 0, 0},
{0, 0, (near + far) / (near - far), -1},
{0, 0, (2 * far * near) / (near - far), 0},
}
}

// InvertedDepthPerspective generates a Vulkan perspective matrix with depth "reversed". This means that you must set
// DepthCompareOp to vk.COMPARE_OP_GREATER, and clear the depth buffer to zero, instead of one. The reason you might use
// this perspective over the "standard" perspecvtive matrix is for greater depth precision. Floating point numbers are
Expand All @@ -41,7 +30,7 @@ func GlTFPerspective(fov, aspect, near, far float32) Mat {
func InvertedDepthPerspective(fov, aspect, near, far float32) Mat {
f := 1 / math32.Tan(fov/2)
return Mat{ // Clipping Z reversed!
{-f / aspect, 0, 0, 0},
{f / aspect, 0, 0, 0},
{0, f, 0, 0},
{0, 0, near / (far - near), -1},
{0, 0, far * near / (far - near), 0},
Expand All @@ -64,18 +53,8 @@ func OrthoProjection(width, height, near, far float32) Mat {
}
}

// GlTFOrthoProjection is the orthographic projection matrix mandated by the glTF specification.
func GlTFOrthoProjection(xmag, ymag, znear, zfar float32) Mat {
return Mat{
{2 / xmag, 0, 0, 0},
{0, 2 / ymag, 0, 0},
{0, 0, 2 / (znear - zfar), (znear + zfar) / (znear - zfar)},
{0, 0, 0, 1},
}
}

// LookAt creates a view matrix from the provided eye and focus points, and an
// up vector. Note that this function does NOT generate a persepctive matrix. Note that LookAt (and all projection
// up vector. This function does NOT generate a persepctive matrix. Note that LookAt (and all projection
// helpers in this library) are based on Vulkan's clip space, which differs from OpenGL.
//
// LookAt only orients the view from an arbitrary orientation down the positive Z axis. The up vector does not need to be orthagonal to
Expand All @@ -101,10 +80,6 @@ func Camera(eye Pt, look Vec, up Vec) Mat {

m := Mat{u, v, w, {0, 0, 0, 1}} //.Transpose() //.Translate()
r := t.MultM(m)
// m = Identity()

// m[1][1], m[2][2] = -1, -1
// m[3][3] = -1
// return x.MultM(m)
return r.Inverse()
}
13 changes: 9 additions & 4 deletions projection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,17 @@ import (
func TestPerspective(t *testing.T) {
pMat := PerspectiveDeg(90.0, 1.0, 2.0, 10.0)

p0 := Origin() // Should be clipped out, projected to negative Inf
p1 := NewPt(0, 0, 1) // Should be clipped on Z
p2 := NewPt(0, 0, -2) // Should be projected to (0, 0, 0.0)
p3 := NewPt(5, 5, -5) // Should be projected to (1, 1, <1.0)
p0 := Origin() // Should be clipped out, projected to negative Inf
p1 := NewPt(0, 0, 1) // Should be clipped on Z
p2 := NewPt(0, 0, -2) // Should be projected to (0, 0, 0.0)
p3 := NewPt(5, 5, -5) // Should be projected to (1, 1, <1.0)
p4 := NewPt(-10, 10, -10) // Should be projected to (-1, 1, 1.0)

r0 := pMat.MultP(p0)
r1 := pMat.MultP(p1)
r2 := pMat.MultP(p2)
r3 := pMat.MultP(p3)
r4 := pMat.MultP(p4)

if !testClipped(r0) {
t.Errorf("Origin was not clipped! Result: %+v", r0)
Expand All @@ -29,6 +31,9 @@ func TestPerspective(t *testing.T) {
if testClipped(r3) {
t.Errorf("Point on back plane was clipped! Result: %+v", r3)
}
if testClipped(r4) {
t.Errorf("Point on corner boundary was clipped! Result %+v", r4)
}
}

func testClipped(pt Pt) bool {
Expand Down

0 comments on commit 9f013d7

Please sign in to comment.