• Advertisement
Sign in to follow this  

World pixel reconstruction from depth, view-space to world-space

This topic is 3141 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi all. I'm triying to recontruct pixel position from depth in my deferred rendering from view-space to world-space. I have light position in world space so I'm trying to get back world-space pixel position to compare with it. Maybe anyone know how compare in view-space. Geometry Stage: [vertex shader]
viewPosition = gl_Position;
[fragment shader]
float pixelDepth = (viewPosition.z - zNear) / (zFar - zNear);

gl_FragData[0] = gl_Color;
gl_FragData[1] = vec4(vNormal, pixelDepth);
gl_FragData[2] = vec4(worldPosition, 1.0);
gl_FragData[3] = vec4(diff_coeff, phong_coeff, two_sided, 1.0);
Light Stage: in this stage my tex coords of quad are far clip plane. I extract this from projection matrix so never change until the windows viewport change and are in world space (-500, 500, -1000) left-top. [vertex shader]
gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0;
farPlaneCoord  = gl_TextureMatrix[0] * gl_MultiTexCoord1;
I store world position only for to compare results. [fragment shader]
// Retrieves G-Buffer data
vec4 albedo   = texture2D(colorMap   , gl_TexCoord[0].st);  // Get the color
vec4 normal   = texture2D(normalMap  , gl_TexCoord[0].st);  // Get the normal
vec4 position = texture2D(positionMap, gl_TexCoord[0].st);  // Get the position
vec4 attr     = texture2D(attrMap    , gl_TexCoord[0].st);  // Get lighting attributes

// No translate
mat4 mWorld = mModelViewProj;
mWorld[3][0] = 0.0;
mWorld[3][1] = 0.0;
mWorld[3][2] = 0.0;
mWorld[3][3] = 1.0;

// Retrieves world position
vec4 farPlaneCoordWorld = mWorld * farPlaneCoord;
vec3 vWorldPos = WSPositionFromDepth(gl_TexCoord[0].st, farPlaneCoordWorld.xyz);

/*
vec3  VP = lightPos - vWorldPos;
float d  = length(VP);

// if outside of area of effect, discard pixel    
if( d > dist )                             
    discard;
gl_FragColor = albedo;
*/

// Test line!
if ( vWorldPos.x > 0.0 && vWorldPos.x < 10 )
    gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
else
    discard;

Using position texture, gl_Position = gl_ProjectionMatrix * gl_ModelViewMatrix * gl_Vertex: worldposition.th.jpg mModelView: mmodelview.th.jpg mModelViewProj, not an option move the line with the camera: mmodelviewproj.th.jpg Method from: http://mynameismjp.wordpress.com/2009/05/05/reconstructing-position-from-depth-continued/ Aditional information, How to get far clip plane from MJP post in GDNet:
//*///-----------------------------------------------------------------------------------------
void Camera::perspective( float fovx, float aspect, float znear, float zfar )
{
	// Construct a projection matrix based on the horizontal field of view
	// 'fovx' rather than the more traditional vertical field of view 'fovy'.

	float e = 1.0f / tanf( Math::degreesToRadians( fovx ) / 2.0f );
	float aspectInv = 1.0f / aspect;
	float fovy = 2.0f * atanf( aspectInv / e );
	float xScale = 1.0f / tanf( 0.5f * fovy );
	float yScale = xScale / aspectInv;

	m_projMatrix[0][0] = xScale;
	m_projMatrix[0][1] = 0.0f;
	m_projMatrix[0][2] = 0.0f;
	m_projMatrix[0][3] = 0.0f;

	m_projMatrix[1][0] = 0.0f;
	m_projMatrix[1][1] = yScale;
	m_projMatrix[1][2] = 0.0f;
	m_projMatrix[1][3] = 0.0f;

	m_projMatrix[2][0] = 0.0f;
	m_projMatrix[2][1] = 0.0f;
	m_projMatrix[2][2] = (zfar + znear) / (znear - zfar);
	m_projMatrix[2][3] = -1.0f;

	m_projMatrix[3][0] = 0.0f;
	m_projMatrix[3][1] = 0.0f;
	m_projMatrix[3][2] = (2.0f * zfar * znear) / (znear - zfar);
	m_projMatrix[3][3] = 0.0f;

	m_viewProjMatrix = m_viewMatrix * m_projMatrix;

	m_fovx = fovx;
	m_aspectRatio = aspect;
	m_znear = znear;
	m_zfar = zfar;

	// Calc near and far clip planes Also these don't change as your camera moves, 
	// so its only necessary to calculate these when you change your projection matrix. 
	// http://www.gamedev.net/community/forums/topic.asp?topic_id=474166&PageSize=25&WhichPage=2
	
	// generate coordinates for perspective projection volume
	float nearH = 2 * tanf(m_fovx / 2.0f) * m_znear;
	float nearW = nearH * aspect;
	float farH = 2 * tanf(m_fovx / 2.0f) * m_zfar;
	float farW = farH * aspect;

	float nearX = nearW / 2.0f;
	float nearY = nearH / 2.0f;
	float farX = farW / 2.0f;
	float farY = farH / 2.0f;

	//near clip plane first, top-left then clockwise
	m_projVolume[0] = Vector3(-nearX,  nearY, m_znear);
	m_projVolume[1] = Vector3( nearX,  nearY, m_znear);
	m_projVolume[2] = Vector3( nearX, -nearY, m_znear);
	m_projVolume[3] = Vector3(-nearX, -nearY, m_znear);

	//now far clip plane
	m_projVolume[4] = Vector3(-farX,  farY, m_zfar);
	m_projVolume[5] = Vector3( farX,  farY, m_zfar);
	m_projVolume[6] = Vector3( farX, -farY, m_zfar);
	m_projVolume[7] = Vector3(-farX, -farY, m_zfar);	
}
Thanks for all in advance.

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement