[SOLVED] Extracting pixel's worldspace position
[SOLVED] Extracting pixel's worldspace position
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.
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.
Last edited by devsh on Fri Jul 24, 2009 11:44 am, edited 4 times in total.
gl_Vertex.xyzw = gl_Position*gl_ModelViewMatrixInverse
We're programmers. Programmers are, in their hearts, architects, and the first thing they want to do when they get to a site is to bulldoze the place flat and build something grand. We're not excited by renovation:tinkering,improving,planting flower beds.
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...
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...
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
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
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;}
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);
}
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
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
We're programmers. Programmers are, in their hearts, architects, and the first thing they want to do when they get to a site is to bulldoze the place flat and build something grand. We're not excited by renovation:tinkering,improving,planting flower beds.
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!
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!

ShadowMapping for Irrlicht!: Get it here
Need help? Come on the IRC!: #irrlicht on irc://irc.freenode.net
Need help? Come on the IRC!: #irrlicht on irc://irc.freenode.net
if u pass it as a varying var to the pixelshader it will be interpolated which is what u want actually.
We're programmers. Programmers are, in their hearts, architects, and the first thing they want to do when they get to a site is to bulldoze the place flat and build something grand. We're not excited by renovation:tinkering,improving,planting flower beds.
Do you not know what a ray is? Look at line3df.devsh wrote:is the ray like a normal???
ShadowMapping for Irrlicht!: Get it here
Need help? Come on the IRC!: #irrlicht on irc://irc.freenode.net
Need help? Come on the IRC!: #irrlicht on irc://irc.freenode.net
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???
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???