mat4 project/unproject not working

穿精又带淫゛_ 提交于 2019-12-13 07:12:56

问题


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.

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.

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

回答1:


It turns out two things were wrong:

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

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.



来源:https://stackoverflow.com/questions/26620942/mat4-project-unproject-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!