首先,这是非常简单的“新手标准”顶点着色器:
in vec3 aPos;
uniform mat4 uMatModel; uniform mat4 uMatView; uniform mat4 uMatProj;
void main () {
gl_Position = uMatProj * uMatView * uMatModel * vec4(aPos, 1.0);
}
现在我正在渲染的是一个简单的6面立方体。 36个顶点坐标中没有应用或固有的旋转。标准教程风格-0.5 .. + 0.5的东西。我会在这里省略顶点数组,但请放心,就像那样简单。
理论上,“相机”应该是面向它的立方体“后面”约2-3个单位。相反,我得到......
我想知道轮换的来源......我甚至没有实现轮换。
总而言之,我试图在Go中自己实现所需的矩阵函数,从而解决数学问题。但在某个地方,我一定是在窃听。 我可以在下面的代码中发现任何矩阵理论问题吗?
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
}
注意, Vec3 这是同一个包中的自定义类型,我没有在此处包含它。现在我假设Vec3函数是正确的(也更容易验证)并且怀疑我在某种程度上弄乱了矩阵结构中的 LookAt 和/或 Perspective 算法。
答案 0 :(得分:1)
这个(推测)deg对rad转换好吗?
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)
}
也许应该有:
top := near * math.Tan(fovY * 2 * math.Pi / 360)