[SOLVED] Extracting pixel's worldspace position

You are an experienced programmer and have a problem with the engine, shaders, or advanced effects? Here you'll get answers.
No questions about C++ programming or topics which are answered in the tutorials!
devsh
Competition winner
Posts: 2049
Joined: Tue Dec 09, 2008 6:00 pm
Location: UK
Contact:

[SOLVED] Extracting pixel's worldspace position

Post 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.
Last edited by devsh on Fri Jul 24, 2009 11:44 am, edited 4 times in total.
devsh
Competition winner
Posts: 2049
Joined: Tue Dec 09, 2008 6:00 pm
Location: UK
Contact:

Post 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)
devsh
Competition winner
Posts: 2049
Joined: Tue Dec 09, 2008 6:00 pm
Location: UK
Contact:

Post by devsh »

i need to invert a matrix :( tired dont get it lay it off untill tommoz
sudi
Posts: 1686
Joined: Fri Aug 26, 2005 8:38 pm

Post by sudi »

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.
xDan
Competition winner
Posts: 673
Joined: Thu Mar 30, 2006 1:23 pm
Location: UK
Contact:

Post 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...
devsh
Competition winner
Posts: 2049
Joined: Tue Dec 09, 2008 6:00 pm
Location: UK
Contact:

Post 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.

Image
Image
Image

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);
}
sudi
Posts: 1686
Joined: Fri Aug 26, 2005 8:38 pm

Post 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
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.
devsh
Competition winner
Posts: 2049
Joined: Tue Dec 09, 2008 6:00 pm
Location: UK
Contact:

Post 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...
devsh
Competition winner
Posts: 2049
Joined: Tue Dec 09, 2008 6:00 pm
Location: UK
Contact:

Post by devsh »

ok no help I take it...
Eigen
Competition winner
Posts: 375
Joined: Fri Jan 27, 2006 2:01 pm
Location: Estonia
Contact:

Post 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 ..
BlindSide
Admin
Posts: 2821
Joined: Thu Dec 08, 2005 9:09 am
Location: NZ!

Post 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! :D
ShadowMapping for Irrlicht!: Get it here
Need help? Come on the IRC!: #irrlicht on irc://irc.freenode.net
devsh
Competition winner
Posts: 2049
Joined: Tue Dec 09, 2008 6:00 pm
Location: UK
Contact:

Post by devsh »

is the ray like a normal???
sudi
Posts: 1686
Joined: Fri Aug 26, 2005 8:38 pm

Post by sudi »

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.
BlindSide
Admin
Posts: 2821
Joined: Thu Dec 08, 2005 9:09 am
Location: NZ!

Post by BlindSide »

devsh wrote:is the ray like a normal???
Do you not know what a ray is? Look at line3df.
ShadowMapping for Irrlicht!: Get it here
Need help? Come on the IRC!: #irrlicht on irc://irc.freenode.net
devsh
Competition winner
Posts: 2049
Joined: Tue Dec 09, 2008 6:00 pm
Location: UK
Contact:

Post 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???
Post Reply