3
votes

First off, here's the very simplistic "newbie standard" vertex shader:

in vec3 aPos;
uniform mat4 uMatModel; uniform mat4 uMatView; uniform mat4 uMatProj;

void main () {
    gl_Position = uMatProj * uMatView * uMatModel * vec4(aPos, 1.0);
}

Now what I'm rendering is a simple 6-faced cube. There is no rotation applied or inherent in the 36 vertex coordinates. Standard tutorial-style -0.5..+0.5 stuff. I'll spare you the vertex array here but rest assured, it's as simple as that.

  • uMatModel is simply the identity matrix for now, no scaling/translating/rotating as-yet
  • uMatView is a LookAt matrix (Go code below) called with pos={ 0.1, 0.1, -3.0 }, target={ 0.1, 0.1, 0.1 }, up={ 0, 1, 0 } (remember the cube vertex coords are all between -0.5 and 0.5 in all dimensions so the 0.1s should be "almost central")
  • uMatProj is a Perspective matrix (Go code below) called with fov=45 aspect=winwidth/winheight near=0.1 far=100

In theory the "camera" should be about 2-3 units "behind" the cube facing it straight. Instead, I get...

enter image description here

I wonder where the rotation is coming from... I do not even have rotations implemented yet.

So in summary I have tried to implement the required matrix functions myself in Go, working off the maths. But somewhere I must have bugged up. Can anyone spot any matrix-theoretical problems in my below code?

type Mat4x4 [4][4]float64

func (me *Mat4x4) Identity () {
    me[0][0], me[0][1], me[0][2], me[0][3] = 1, 0, 0, 0
    me[1][0], me[1][1], me[1][2], me[1][3] = 0, 1, 0, 0
    me[2][0], me[2][1], me[2][2], me[2][3] = 0, 0, 1, 0
    me[3][0], me[3][1], me[3][2], me[3][3] = 0, 0, 0, 1
}

func (me *Mat4x4) Frustum (left, right, bottom, top, near, far float64) {
    me[0][0], me[0][1], me[0][2], me[0][3] = (near * 2) / (right - left), 0, 0, 0
    me[1][0], me[1][1], me[1][2], me[1][3] = 0, (near * 2) / (top - bottom), 0, 0
    me[2][0], me[2][1], me[2][2], me[2][3] = (right + left) / (right - left), (top + bottom) / (top - bottom), -(far + near) / (far - near), -1
    me[3][0], me[3][1], me[3][2], me[3][3] = 0, 0, -(far * near * 2) / (far - near), 0
}

func (me *Mat4x4) Perspective (fovY, aspect, near, far float64) {
    var top = near * math.Tan(fovY * math.Pi / 360)
    var right = top * aspect
    me.Frustum(aspect * -top, right, -top, top, near, far)
}

func (me *Mat4x4) LookAt (eyePos, lookTarget, worldUp *Vec3) {
    var vz = eyePos.Sub(lookTarget)
    vz.Normalize()
    var vx = worldUp.Cross(&vz)
    vx.Normalize()
    var vy = vz.Cross(&vx)
    vy.Normalize()
    me[0][0], me[0][1], me[0][2], me[0][3] = vx.X, vy.X, vz.X, 0
    me[1][0], me[1][1], me[1][2], me[1][3] = vx.Y, vy.Y, vz.Y, 0
    me[2][0], me[2][1], me[2][2], me[2][3] = vx.Z, vy.Z, vz.Z, 0
    me[3][0], me[3][1], me[3][2], me[3][3] = -((vx.X * eyePos.X) + (vx.Y * eyePos.Y) + (vx.Z * eyePos.Z)), -((vy.X * eyePos.X) + (vy.Y * eyePos.Y) + (vy.Z * eyePos.Z)), -((vz.X * eyePos.X) + (vz.Y * eyePos.Y) + (vz.Z * eyePos.Z)), 1
}

Note, Vec3 here is a custom type in the same package, I have not included it here. For now I assume the Vec3 functions are correct (also a lot easier to verify) and suspect that I somehow messed up the LookAt and/or Perspective algorithms in the matrix struct.

1
Seems like they could be transposed the wrong way, but I don't know enough about Go to say for sure. If you transpose your LookAt and Perspective matrix does it look any better? Note you can transpose them with a quick boolean flip in glUniformMatrix, so you don't have to edit the actual matrix functions.Tim
Now that I found out that GLSL matrices are column-major whereas mine are row-major -- you're definitely right on the money here. Will see if this solves the issue, but it's definitely part of what's wrong here...metaleap
Changed the matrix lib completely to column-major but no change in the core problem, still a 45° tilted camera view...metaleap

1 Answers

1
votes

Is this (presumably) deg to rad conversion okay?

func (me *Mat4x4) Perspective (fovY, aspect, near, far float64) {
        var top = near * math.Tan(fovY * math.Pi / 360)
        var right = top * aspect
        me.Frustum(aspect * -top, right, -top, top, near, far)
}

Maybe there should be:

        top := near * math.Tan(fovY * 2 * math.Pi / 360)