mat4专案/非专案无法运作 [英] mat4 project/unproject not working

查看:121
本文介绍了mat4专案/非专案无法运作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Lua中实现GLM的项目和非项目功能,但结果令人怀疑.据我所知,我的代码(如下所示)与GLM几乎相同,但是如下面的视频所示,文本未按预期显示.当我将相机移至0,0,0时,文字形成菱形图案,这很有趣.

I am trying to implement GLM's project and unproject functions in Lua but the results are... questionable. As far as I can tell, my code (shown below) is nearly identical to GLM, yet as the video below shows, the text doesn't display as intended. When I bring the camera to 0,0,0, the text forms a diamond pattern, which is interesting.

无论摄像机在何处移动,都应在其各自的多维数据集的顶部绘制多维数据集001-009.009一词. cube.001是TL,cub.009是BR.

What should be displayed here is the words cube.001 through cube.009 should be drawn on top of their respective cubes, regardless of where the camera moves. cube.001 is TL, cube.009 is BR.

要全面了解我们的mat4库(和其他垃圾邮件),请访问 此处 .

For a full look into our mat4 library (and other junk), visit here.

-- https://github.com/g-truc/glm/blob/master/glm/gtc/matrix_transform.inl#L317
function mat4.project(obj, view, projection, viewport)
    local position = { obj.x, obj.y, obj.z, 1 }
    position = view * position
    position = projection * position

    position[1] = position[1] / position[4] * 0.5 + 0.5
    position[2] = position[2] / position[4] * 0.5 + 0.5
    position[3] = position[3] / position[4] * 0.5 + 0.5
    position[4] = position[4] / position[4] * 0.5 + 0.5

    position[1] = position[1] * viewport[3] + viewport[1]
    position[2] = position[2] * viewport[4] + viewport[2]

    return vec3(position[1], position[2], position[3])
end

-- https://github.com/g-truc/glm/blob/master/glm/gtc/matrix_transform.inl#L338
function mat4.unproject(win, view, projection, viewport)
    local inverse = (projection * view):inverse()
    local position = { win.x, win.y, win.z, 1 }
    position.x = (position.x - viewport[1]) / viewport[3]
    position.y = (position.y - viewport[2]) / viewport[4]

    position[1] = position[1] * 2 - 1
    position[2] = position[2] * 2 - 1
    position[3] = position[3] * 2 - 1
    position[4] = position[4] * 2 - 1

    position = inverse * position

    position[1] = position[1] / position[4]
    position[2] = position[2] / position[4]
    position[3] = position[3] / position[4]
    position[4] = position[4] / position[4]

    return vec3(position[1], position[2], position[3])
end

-- Get projection from cubes
local viewport = { 0, 0, 1280, 720 }
for _, cube in ipairs(self.cubes) do
    local model = cpml.mat4()
        :translate(cube.position)
        :rotate(cube.orientation.x, { 1, 0, 0 })
        :rotate(cube.orientation.y, { 0, 1, 0 })
        :rotate(cube.orientation.z, { 0, 0, 1 })
        :scale(cube.scale)

    local projection = cpml.mat4.project(
        cube.position,
        self.camera.view:transpose(),
        self.camera.projection:transpose(),
        viewport
    )
end

推荐答案

原来有两件事是错误的:

It turns out two things were wrong:

1)由于我在程序代码中的其他地方有一个错误,因此需要对矩阵进行换位.

1) the matrices need to be transposed since I have a bug... somewhere else in my code.

2)GLM在指代摄影机视图时使用模型"一词,因此我一直使用错误的矩阵.

2) GLM uses the word "model" when referring to the camera view, so I was using the wrong matrix all along.

我已经更新了代码以反映这些更正.

I've updated the code to reflect these corrections.

这篇关于mat4专案/非专案无法运作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆