【发布时间】:2019-08-04 04:23:34
【问题描述】:
我需要能够使用 Vulkan 将屏幕像素取消投影到对象空间中,但是我的数学在某个地方出错了。
这是今天的着色器供参考:
void main()
{
//the depth of this pixel is between 0 and 1
vec4 obj_space = vec4( float(gl_FragCoord.x)/ubo.screen_width, float(gl_FragCoord.y)/ubo.screen_height, gl_FragCoord.z, 1.0f);
//this puts us in normalized device coordinates [-1,1 ] range
obj_space.xy = ( obj_space.xy * 2.0f ) -1.0f;
//this two lines will put is in object space coordinates
//mvp_inverse is derived from this in the c++ side:
//glm::inverse(app.three_d_camera->get_projection_matrix() * app.three_d_camera->view_matrix * model);
obj_space = ubo.mvp_inverse * obj_space;
obj_space.xyz /= obj_space.w;
//the resulting position here is wrong
out_color = obj_space;
}
当我用颜色输出位置时,颜色是关闭的。我知道我可以简单地将对象空间位置从顶点着色器传递到片段着色器,但我想了解为什么我的数学不起作用,这将帮助我理解 Vulkan,也许自己也学一点数学。
谢谢!
【问题讨论】:
-
box_space_pos应该是obj_space变量,这是一个错字。对不起,伙计们,问题还是一样。 -
您可以编辑问题。
-
数学基本上与 API 无关。您是否在普通的 C 代码中尝试过(更容易调试)?还是(上帝保佑)在纸上?您是否还测试过 MVP 矩阵是否合理?
标签: projection vulkan