Jump to content
• Advertisement

# Simple diffuse question ... please help as I cant go to bed until this works :O)

This topic is 3573 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

Hello, I cant seem to get this working as I require. Anyone tell me why ? (Really sorry if this is mega simple, I am learning and trying to get this working). The stuff below relates to my vs and ps in an fx file. The fx file has this at the top - float3 lightpos; // light position in world coordinates The vertex shader has this in it - float4 pos=float4(IN.pos.x, IN.pos.y, IN.pos.z, 1.0); OUT.pos=mul(pos, world_matrix);// world_matrix = rot, scale, trans of object float4 lp = float4(lightpos.x, lightpos.y, lightpos.z, 1); OUT.lightpos = lp; OUT.normal=mul(IN.normal, world_matrix); And the pixel shader does this - float3 l = normalize(IN.pos-IN.lightpos.xyz); float3 normal = normalize(IN.normal); float diff=saturate(dot(l, normal.xyz)); return diff; Now I am trying to get some clubs that are in the hands of my skinned creature to diffuse correctly. Now the qeird bit - his right club works perfectly with - float3 l = normalize(IN.pos-IN.lightpos.xyz); his left club works with this - float3 l = normalize(IN.lightpos.xyz-IN.pos); What do I need to change to get this to work for both at the same time ? Please help as I am knackered. Thanks, TSEDSE

#### Share this post

##### Share on other sites
Advertisement
I would guess from your code snippets that you're transforming the normal vector incorrectly. You want only the rotation elements, can only ignore the scaling if its uniform (countered by normalize()) and you don't want any translation.

Typically you'll multiply the normal by the inverse transpose of the world matrix, but I'm pretty sure you can also do it by casting to matrix3x3 in your HLSL code. I don't have my references in front of me unfortunately...

hth
Jack

#### Share this post

##### Share on other sites
OUT.pos=mul(pos, world_matrix);// world_matrix = rot, scale, trans of object

world_matrix surely can't just be your world matrix can it? Where's the view and projection transform?

I'm guessing that since you are able to see one club correctly, world_matrix is actually your world-view-projection matrix. In that case, you must pass in the real world matrix and use that when following JollyJeffer's advice.

edit: by the way, calling "mul (float3, matrix4x4);" will automatically cast the matrix4x4 to matrix3x3.

#### Share this post

##### Share on other sites

• Advertisement

### Announcements

• Advertisement

• ### Popular Now

• 12
• 9
• 13
• 41
• 15
• Advertisement
• Advertisement
×

## Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!