English 中文(简体)
GLSL gl_FragCoord.z 计算和确定冰川
原标题:GLSL gl_FragCoord.z Calculation and Setting gl_FragDepth

因此,我转播了一台硬盘(真正的地貌是一根ube,可能出现剪.,而块状的地貌是一门脑pon子),我需要计算深度。

我可以比较容易地计算在世界空间抵消的数额。 不幸的是,我花了几个小时时间,却未能赶上它的深度。

我所能取得的唯一正确结果是:

gl_FragDepth = gl_FragCoord.z

基本上,我需要知道如何计算FragCoord.z,以便我能够:

  • Take the inverse transformation from gl_FragCoord.z to eye space
  • Add the depth perturbation
  • Transform this perturbed depth back into the same space as the original gl_FragCoord.z.

如果这似乎是一个重复的问题,我表示歉意;这里还有其他一些处理类似问题的员额。 然而,在执行所有这些建议之后,没有任何工作是正确的。 此时此刻,我不是试图找人帮助,而是要求制定一部完整的法典。 这只是几个方面。

最佳回答

关键守则是:

float far=gl_DepthRange.far; float near=gl_DepthRange.near;

vec4 eye_space_pos = gl_ModelViewMatrix * /*something*/
vec4 clip_space_pos = gl_ProjectionMatrix * eye_space_pos;

float ndc_depth = clip_space_pos.z / clip_space_pos.w;

float depth = (((far-near) * ndc_depth) + near + far) / 2.0;
gl_FragDepth = depth;
问题回答

就今后的另一个参考而言,这是同文字母一样的公式。

vec4 v_clip_coord = modelview_projection * vec4(v_position, 1.0);
float f_ndc_depth = v_clip_coord.z / v_clip_coord.w;
gl_FragDepth = (1.0 - 0.0) * 0.5 * f_ndc_depth + (1.0 + 0.0) * 0.5;

此处,modelview_projection is 4x4 modelview-projection Format and v_position is Object-space status of the pixel being made (in my case based by a raymarcher).

The equation comes from the window coordinates section of this manual. Note that in my code, near is 0.0 and far is 1.0, which are the default values of gl_DepthRange. Note that gl_DepthRange is not the same thing as the near/far distance in the formula for perspective projection matrix! The only trick is using the 0.0 and 1.0 (or gl_DepthRange in case you actually need to change it), I ve been struggling for an hour with the other depth range - but that is already "baked" in my (perspective) projection matrix.

Note that this way, the equation really contains just a single multiply by a constant ((far - near) / 2) and a single addition of another constant ((far + near) / 2). Compare that to multiply, add and divide (possibly converted to a multiply by an optimizing compiler) that is required in the code of imallett.





相关问题
OpenGL 3D Selection

I am trying to create a 3D robot that should perform certain actions when certain body parts are clicked. I have successfully (sort of) implemented picking in that if you click on any x-plane part, it ...

CVDisplayLink instead of NSTimer

I have started to implement cvDisplayLink to drive the render loop instead of nstimer, as detailed in this technical note https://developer.apple.com/library/archive/qa/qa1385/_index.html Is it ...

Can the iPhone simulator handle PVR textures?

I have a really weird problem with PVR textures on the iPhone simulator- the framerate falls through the floor on the iPhone simulator, but on the iPhone itself it works just fine. Has anyone had any ...

Calculate fps (frames per second) for iphone app

I am using an opengl es iphone application. What is the most accurate way to calculate the frames per second of my application for performance tuning?

Java - Zoom / 3D Data Visualization Libraries

What are the best libraries/frameworks for doing 3D and/or Zoom interfaces in Java? I d like to be able to do some prototyping of creating new types of interfaces for navigating within data and ...

FLTK in Cygwin using Eclipse (Linking errors)

I have this assignment due that requires the usage of FLTK. The code is given to us and it should compile straight off of the bat, but I am having linking errors and do not know which other libraries ...

热门标签