我阅读了很多关于使用片段着色器获取深度的信息.
如
http://www.opengl.org/discussion_boards/ubbthreads.php?ubb=showflat&Number=234519
但我还是不知道这是否gl_FragCoord.z是线性的.
GLSL规范称屏幕上的范围为[0,1]而未提及线性与否.
我认为线性是至关重要的,因为我将使用渲染模型来匹配Kinect的深度图.
那么如果它不是线性的,那么如何在世界空间中线性化呢?
我正在写一个延迟着色器,我正试图更紧密地收拾我的gbuffer.但是,我似乎无法正确计算视图空间深度的视图位置
// depth -> (gl_ModelViewMatrix * vec4(pos.xyz, 1)).z; where pos is the model space position
// fov -> field of view in radians (0.62831855, 0.47123888)
// p -> ndc position, x, y [-1, 1]
vec3 getPosition(float depth, vec2 fov, vec2 p)
{
vec3 pos;
pos.x = -depth * tan( HALF_PI - fov.x/2.0 ) * (p.x);
pos.y = -depth * tan( HALF_PI - fov.y/2.0 ) * (p.y);
pos.z = depth;
return pos;
}
Run Code Online (Sandbox Code Playgroud)
计算出的位置是错误的.我知道这是因为我仍然在gbuffer中存储正确的位置并使用它进行测试.
我制作了一个小 Three.js 应用程序,它将一堆圆圈从画布底部移动到顶部:
let renderer, scene, light, circles, camera;
initialize();
animate();
function initialize() {
renderer = new THREE.WebGLRenderer({ alpha: true, antialias: true });
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
scene = new THREE.Scene();
light = new THREE.AmbientLight();
scene.add(light);
circles = new THREE.Group();
scene.add(circles);
camera = new THREE.PerspectiveCamera(45, renderer.domElement.clientWidth / renderer.domElement.clientHeight, 1);
camera.position.z = circles.position.z + 500;
}
function animate() {
// Update each circle.
Array.from(circles.children).forEach(circle => {
if (circle.position.y < visibleBox(circle.position.z).max.y) {
circle.position.y += 4;
} else {
circles.remove(circle);
}
});
// Create …Run Code Online (Sandbox Code Playgroud)我有一个具有一定投影矩阵的透视相机,我只想从中提取流、近平面和远平面。我知道 Three.js 中有一个函数:
.updateProjectionMatrix()
Run Code Online (Sandbox Code Playgroud)
它根据上面列出的参数创建一个投影矩阵,基本上我想要相反的过程。
使用 glm 设置 ViewMatrix 很容易:
glm::lookAt(Position, Direction, UpVector);
Run Code Online (Sandbox Code Playgroud)
但是如果我尝试将 funktion 与 modelMatrix 一起使用,我会得到令人困惑的值(模型的位置不正确,而且旋转看起来也不正确)。我只想以与设置相机相同的方式设置对象。我可以使用 lookAt 功能并在之后进行一些更改吗?还是我必须为此编写自己的功能?如果是这样,如何?
我用这个固定了位置:
m_Orientation = glm::lookAtLH(Position, Direction, UpVector);
m_Orientation[3][0] = -m_Orientation[3][0];
m_Orientation[3][1] = -m_Orientation[3][1];
m_Orientation[3][2] = -m_Orientation[3][2];
Run Code Online (Sandbox Code Playgroud)
也在 vertexshader 里面我用这个:
gl_Position = CameraMatrix * ModelMatrix * Pos;
Run Code Online (Sandbox Code Playgroud)
其中 CameraMatrix 是一个 viewProjectionMatrix,ModelMatrix(我的问题)和 Pos 是我的顶点在模型空间中的位置
c++ ×2
glsl ×2
javascript ×2
opengl ×2
three.js ×2
depth ×1
depth-buffer ×1
frustum ×1
glm-math ×1
shader ×1