2015-01-22 8 views
1

올바른 역 투영 행렬이 있습니다. 그러나 아무 것도 옳은 것처럼 보이지 않습니다! 재구성 된 위치가 완전히 변형되지 않았으며 z 값이 작아지지 않았습니까? 누구 제안?LWGL // 프래그먼트 쉐이더에서 위치 재구성

vec3 calculatePosition(vec2 coord, float depth) 
{ 
vec4 clipSpaceLocation; 
clipSpaceLocation.x = coord.x * 2.0 - 1.0; 
clipSpaceLocation.y = coord.y * 2.0 - 1.0; 
clipSpaceLocation.z = depth * 2.0 - 1.0; 
clipSpaceLocation.w = 1.0; 
vec4 homogenousPosition = uProjectionInverse * clipSpaceLocation; 

return homogenousPosition.xyz/homogenousPosition.w; 
} 

z 값은 대략 -0,001입니다. 왜?

COORD 싶게 깊이는 다음과 같습니다

vec2 coord = vec2(gl_FragCoord.x/width, gl_FragCoord.y/height); 
float currentDepth = texture(depthBuffer, coord).r; 

나는 대학에 대한 SSAO를 구현해야하고 내가 LWJGL - 플러그인 이클립스와 자바를 사용합니다. 제발 도와주세요. 나는 일주일도 안 남았어.

편집 : 지금이 시도 ...하지만 여전히 lenearized되지 않은 :

float camera_space_z_from_depth(sampler2D depthbuffer, vec2 uv) { 
    float depth = texture(depthbuffer, uv).x; 
    return (2 * uNearPlane)/(uFarPlane + uNearPlane - depth * (uFarPlane -  uNearPlane)); 
} 

vec3 calculatePosition(vec2 coord, float depth) 
{ 
    vec4 clipSpaceLocation; 
    clipSpaceLocation.x = coord.x * 2.0 - 1.0; 
    clipSpaceLocation.y = coord.y * 2.0 - 1.0; 
    clipSpaceLocation.z = depth * 2.0 - 1.0; 
    clipSpaceLocation.w = 1.0; 
    vec4 homogenousPosition = uProjectionInverse * clipSpaceLocation; 

    return homogenousPosition.xyz/homogenousPosition.w; 
} 
vec3 getPosition(vec2 coord){ 
    float currentDepth = camera_space_z_from_depth(depthBuffer,coord); 
    vec3 position = calculatePosition(coord, currentDepth); 
    return position; 
} 
+0

원래 코드 (안 편집) 지금까지 확인을 보인다. 편집에서의 선형화는 깊이 렌더링 패스가 일반 z 좌표를 사용했다는 가정하에 실제로는 이해가되지 않습니다. 나에게 이것은 uProjectionInverse 또는 - 그리고 이것은 내 첫 번째 추측 일 것이다 - 깊이 텍스처 (또는 depth rende rpass 그 자체)에 문제가있는 것처럼 보인다. '투영 값 0.001'에 대한 가까운 값이 있을까요? 그렇다면, 당신이 개발하려고하는 깊이 값이 모든 점에서 단지 0 인 것처럼 보입니다. – derhass

답변

0

내가 여기 말했듯이 당신이 당신의 깊이 값을 선형화 할 필요가 가정 :

https://www.opengl.org/wiki/Compute_eye_space_from_window_space

그냥 사용해보세요.

float camera_space_z_from_depth(sampler2D depthbuffer, vec2 uv) { 
    const float depth = texture(depthbuffer, uv).x; 
    return (camera_near/(camera_far - depth * (camera_far - camera_near))) * camera_far; 
} 

는 편집 :

은보십시오. 변수 의미는 자명 (선형화 필요 없음) :

vec3 GetPosition(vec2 fragmentCoordinates, float depth) 
{ 
    vec3 normalizedDeviceCoordinatesPosition; 
    normalizedDeviceCoordinatesPosition.xy = (2.0 * fragmentCoordinates)/uScreenSize - 1; 
    normalizedDeviceCoordinatesPosition.z = 2.0 * depth - 1.0; 

    vec4 clipSpacePosition; 
    clipSpacePosition.w = uProjection[3][2]/(normalizedDeviceCoordinatesPosition.z - (uProjection[2][2]/uProjection[2][3])); 
    clipSpacePosition.xyz = normalizedDeviceCoordinatesPosition * clipSpacePosition.w; 

    vec4 eyePosition = uInverseProjection * clipSpacePosition; 

    return eyePosition.xyz/eyePosition.w; 
} 

주의해야한다 : 지금까지 나는 당신의 깊이 텍스처가 GL_DEPTH_COMPONENT이며 GL_DEPTH_ATTACHMENT로 프레임 버퍼에 연결되어 있으리라 믿고있어.

은 즉 :

glBindTexture(GL_TEXTURE_2D, mDepthTextureId); 
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, mrScreenWidth, mrScreenHeight, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE, 0); 
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); 
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); 
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_NONE); 
glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, mDepthTextureId, 0); 
+0

OK thanks to try =) – Kriss

+0

이 코드를 테스트하기 위해 코드 조각에 사용 된 camera_near 및 camera_far 변수를 하드 코딩 할 수 있습니다. –

+0

죄송하지만 작동하지 않습니다 ... – Kriss