Page 1 of 3
[SOLVED] Extracting pixel's worldspace position
Posted: Mon Jun 15, 2009 4:14 pm
by devsh
hi all...
I was wondering If i have a depth texture (with depth values of all pixels on screen), and I have TExture Coords in a shader. I can extract both to give me values xyz in range -1 to 1,l the Z would go to infinity though. How would I go on about converting these to values that I had before multiplying by ModelViewMatrix??
Do I save the ViewProj of the scene and multiply reverse???
cause then I need to remultiply by a viewproj from another camera to get the pixel position in the view of it.
Posted: Tue Jun 16, 2009 6:17 pm
by devsh
ok lets say we have this
gl_Position = gl_ModelViewMatrix*gl_vertex
how do I work out gl_vertex, when I know what gl_ModelViewMatrix is?
gl_Vertex = ?? is it gl_Position/gl_ModelViewMatrix (the shader says / is an invalid operand)
Posted: Tue Jun 16, 2009 7:10 pm
by devsh
i need to invert a matrix

tired dont get it lay it off untill tommoz
Posted: Tue Jun 16, 2009 9:12 pm
by sudi
gl_Vertex.xyzw = gl_Position*gl_ModelViewMatrixInverse
Posted: Tue Jun 16, 2009 9:15 pm
by xDan
well, Irrlicht has a method to do that, so you can pass the inverted modelview matrix in from Irrlicht.
but is this for a postprocessing effect or not? if not, and it's just a regular material, you can just pass the vertex position as a varying variable or whatever and get the interpolated position in the pixel shader
or if it was postprocessing, you could render the interpolated vertex positions into a texture, but that would only give a range of 0-255 for each component...
Posted: Tue Jun 16, 2009 9:29 pm
by devsh
I recently found out about inverse matrix
look
shadows as post process
sun view matrix
-0.927403
0.369053
0.347837
0.347832
0
1.09009
-0.610534
-0.610526
-0.453363
-0.754938
-0.711538
-0.711528
5402.62
1250.57
12168.9
12169.7
scene view matrix (inverted)
0.919097
4.25818e-05
-0.30639
1.67406e-08
0.0904479
0.66784
0.27127
-4.87888e-09
-3099.15
-2543.7
-2319.24
-0.999972
3099.48
2543.34
2320.15
0.999986
now maybe I am making fundamental mistakes do I multiply proj*view matrix or the other way round (world is unecessary), I am using another program called shader maker. So if I get a pointer and do this
Code: Select all
irr::core::matrix4 mat = VideoDriver->getTransform(irr::video::ETS_PROJECTION);
mat *= VideoDriver->getTransform(irr::video::ETS_VIEW);
irr::core::matrix4 MAT;
mat.getInverse(MAT);
for (int h =0; h<16; h++) { std::cout << MAT[h] << std::endl;}
do i print them out x0,y0,z0,w0 and then row number 1,2 and 3 or is it all Xs form all rows and then Ys and so on.
All I am tring to do I to make a black dot on the house (on sahdow map) match up with the house on RTT.
shader that I am trying to do shadows as post process with
Code: Select all
// simple fragment shader
uniform sampler2D shadowmap;
uniform sampler2D RT;
uniform sampler2D DepthRT;
uniform mat4 camview;
uniform mat4 shadowview;
void main()
{
vec2 z = texture2D(DepthRT, gl_TexCoord[0].xy).rg;
vec4 position = shadowview*(-camview*vec4(-(gl_TexCoord[0].xy-vec2(0.5,0.5))*2.0, -(z.r+z.g/256.0)*10000.0, 1.0));
z = texture2D(shadowmap, position.xy).rg;/*
if ( position.z > (z.r+z.g/256.0)*10000.0 ) gl_FragData[0] = texture2D(RT, gl_TexCoord[0].xy)-vec4(155.0,155.0,155.0,0.0);
else gl_FragData[0] = texture2D(RT, gl_TexCoord[0].xy);*/
gl_FragData[0] = vec4(z,0.0,1.0);
}
Posted: Wed Jun 17, 2009 6:01 am
by sudi
what u have to do.
rebuild from your camera rtt the world space position.
now transfrom that point into sun screenspace. use x,y as texture coords for the sun rtt. now compare the saved depth values. if your camera depth is smaler than sun depth tint it with sun color(additive). u can also color the shadowed parts a little bit. u can even have transparent materials cast half see through shadows when u render a material property into the alpha channel of the rtts. then u can decide how to color the shadow/light
Posted: Wed Jun 17, 2009 2:29 pm
by devsh
Sudi what you posted is nothing new I thought of that myself
but the thing is that I dont know how to "rebuild worldspace position"
I would greatly appreciate your help in doing that...
Posted: Wed Jun 17, 2009 5:55 pm
by devsh
ok no help I take it...
Posted: Wed Jun 17, 2009 6:13 pm
by Eigen
Geez .. 3 hours and no replies. The world is doomed! Know that not all people are online all day like you. Give some time ..
Posted: Wed Jun 17, 2009 8:00 pm
by BlindSide
What I do to rebuild worldspace position is get the camera ray in world space (There is a smgr function for that, "smgr->getSceneCollisionManager()->getRayFromScreenCoordinates()") for each corner of the screen (So for 640x480 just use (0, 0), (480, 0), etc), and then pass those to the pixel shader.
In the pixel shader to get the world space position of a pixel you just interpolate between the 4 camera rays based on the screen space texcoords and then go: Camera Position + Interpolated Screen Ray * Depth Read From Depth Map * Camera Far Value (You can get far value easily from ICameraSceneNode::getFarValue()). It's easy!

Posted: Wed Jun 17, 2009 8:52 pm
by devsh
is the ray like a normal???
Posted: Wed Jun 17, 2009 11:38 pm
by sudi
if u pass it as a varying var to the pixelshader it will be interpolated which is what u want actually.
Posted: Thu Jun 18, 2009 2:16 am
by BlindSide
devsh wrote:is the ray like a normal???
Do you not know what a ray is? Look at line3df.
Posted: Sat Jul 18, 2009 9:00 pm
by devsh
sorry I am an idiot... I took a break
I have learned that the pixel position on screen is not xy clamped to -1,1 as it is further out the corner of the screen grows (to hundred lets say). So that was the major misconception, but now I get it that the center of the screen stays 0,0 but the corners get further xy as Z increases. so... how do I calculate the screen position find out the XY in this case of a pixel knowing its Z and Field of View???