dqu94359 2012-10-11 19:29
浏览 88

用于OpenGL的Go矩阵函数(透视/视锥和Lookat矩阵)有什么问题?

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条回答 默认 最新

  • dongxun1244 2012-10-11 20:03
    关注

    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)
    
    评论

报告相同问题?

悬赏问题

  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码