PDA

View Full Version : Texture Co-ord Generation from VP to FP



pocketmoon
09-27-2002, 01:47 PM
I've been bending Cass's ear about this but anyone else care to help an old man (me) out?

OK, I'm doing shadow mapping in Cg using Vertex and Fragment progs. I've replaced the bit TexGen does in mapping the vertex eye position back into it's light space projected position.

The following works:

In the VP I output into texcoord0 the vertex position => model matrix => Light View => Projection (NO DIVIDE BY W) :

float4 pm = mul(Model, IN.Position);
OUT.HPosition = mul(ViewProj, pm);

float4 tt = mul(LightView,pm);
OUT.TC0 = mul(Proj,tt);

In the FP I'm getting passed interpolated xyzw in TC0 so I do this:

float2 tt = IN.TC0.xy / IN.TC0.w; // Divide by W gives me a range -1 to 1
tt.xy = tt.xy * 128 + 128; // Scale for Texture rectangle
OUT.col.x = f1texRECT(tex0, tt.xy); // TEST just display projected shadow buffer

So, I'm not doing ANY scaling or divides by w in the VP. I do them in the FP. It works. It's slow.


NOW I try and move the /w out of the FP into the VP
...
tt = mul(Proj,tt);
tt.xy = tt.xy / tt.w;
OUT.TC0 = tt;

Doesn't work - shadows are way out on the ground plane.

So, Instead, try and move scaling from FP into VP - doesn't work.

So, instead try not doing the /w but calling f1texRECYproj(tex0, tt.xyz) :

float3 tt = IN.TC0.xyw ; // NO Divide by
tt.xy = tt.xy * 128 + 128; // Scale for Texture rectangle
OUT.col.x = f1texRECTproj(tex0, tt.xyz); // TEST just display projected shadow buffer

Doesnt work

I know I'm just missing something.

There must be a way to do it so that the FP only has to execute :

OUT.col.x = f1texRECTproj(tex0, tt.xyz);

because the incomming tex coords have been scaled in the VP.

Help!

Rob


[This message has been edited by pocketmoon (edited 09-27-2002).]

Nutty
09-27-2002, 02:10 PM
http://opengl.nutty.org/forum/viewtopic.php?t=47

3 way cross post. tch tch.. http://www.opengl.org/discussion_boards/ubb/wink.gif

pocketmoon
09-27-2002, 02:34 PM
I have a contest deadline to meet and I'm running my first 1/2 marathon on Sunday http://www.opengl.org/discussion_boards/ubb/smile.gif

cass
09-27-2002, 06:13 PM
In case it is useful to anyone else, I'm posting some of my exchange with Rob at this
link:
http://www.r3.nu/~cass/shadow_maps/pocketmoon.html

Thanks -
Cass

jwatte
09-28-2002, 08:29 AM
Will the interpolator also take w into account? If so, shouldn't you set the w component to 1 when you divide xy by w? If you divided xyw by w then that would happen automatically (i e you could try updating your write mask).

However, I haven't examined this part of the pipe in great detail, so I may be off base. But please let me know whether this helps any or not.

cass
09-28-2002, 09:52 AM
I commented on this in the email link from the previous post. Yes, you have to be careful what you put into the q texture coordinate -- particularly if you're going to do a pre-projection scale/bias.

Cass

pocketmoon
09-28-2002, 12:45 PM
I *think* I am doing a post projection scale/bias...

Model : Model Matrix only (no view)
ViewProj : eye view + projection
LightView : Light View Matrix (no model)
Proj : Projection Matrix:

float4 pm = mul(Model, IN.Position);
OUT.HPosition = mul(ViewProj, pm);
pm = mul(LightView,pm);
pm = mul(Proj,tt);
pm.xy = pm.xy *128.0 + 128.0;

cass
09-28-2002, 05:26 PM
Projection is actually the "projective divide". You have to do the *128+128 after the perspective divide (which happens in the fragment program.

From email I understand you've got this working now, so I'll leave it at that.

Thanks -
Cass

pocketmoon
10-01-2002, 01:26 AM
Originally posted by cass:

Projection is actually the "projective divide". You have to do the *128+128 after the perspective divide (which happens in the fragment program.

From email I understand you've got this working now, so I'll leave it at that.

Cass

Well It is working fine, but the *128+128 is happening before the fragment program and before the perspective divide - as would happen if you used glTexGen etc to create a projected texture.

I just took your advice and built a single matrix outside of the Vertex prog to do the model->view->projection->scale/bias. I then just apply this transform in the VP.

Thanks

Rob