Jump to content
  • Advertisement
Sign in to follow this  
emdin

To tangent space or not

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

I'm giving Shaders and HLSL a first real shot, but I'm confused about two things. There are a couple of effects I want to combine, the important ones for our ship in the game are: * Phong, diffuse and specular * Reflection, using a cube-map for now * Bumpmapping, using a Dot3-normal map To get the bumpmapping running, I've transformed the View- and Light-vectors to "Tangent Space" in the Vertex Shader (using the normal and the tangent). Once in the Pixel Shader it's quite easy to sample the normal from the normal-map and calculate the Phong-shading. But how can I get the Reflection on the ship to work? I can't work from "Tangent Space" to sample from the Cube-map, I need to get to preferably "World Space". One solution would be to pass View- and Light-vectors from the Vertex Shader in "World Space", but then I can't use the normal-map (since it's in "Tangent Space"). The second thing I'm wondering about is where you can get the Tangent data from? In most of the examples I've found on the internet it's included in the Vertex stream, so I'm assuming it can be exported from the 3D-application. Or is there a way to calculate it?

Share this post


Link to post
Share on other sites
Advertisement
To get the reflection to work, just compute the reflection vector in the vertex shader and pass that through as texture coordinates. I don't think the reflection vector requires per-pixel calculation because it varies linearly between the vertices.

Yes, normally you would pass in the TBN data through the vertex stream - but how do you get this in the first place? Well, you can calculate it as long as you have the xyz and uv coordinates of your model.

Here is some code by Eric Lengyel which does just that. I'm sure you'll be able to disect it and fit it to your needs.

-Mezz

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
> I don't think the reflection vector requires per-pixel calculation because it varies linearly between the vertices.

But then the Reflection vector wouldn't take the "bumps" into consideration and the reflection would appear as if no normal-map was used...

Share this post


Link to post
Share on other sites
Good point. Forgot about that.

Edit:
So, to actually fix this problem pass through the vectors in tangent space for your bump mapping. Also pass through the vectors in world space for your reflection, if you're using, shall I say, ps_2_0 capable cards this is easy because you have plenty of registers, can do all kinds of DTR operations and have lots of instructions to muck around with.

With lesser shader models you will find it difficult to do some of this stuff, unless maybe you use the sort of hardcoded dependent texture read stuff, even then you probably won't get specular that's too good. With the early shader models I just do reflections as I suggested previously, even though it doesn't look quite as good.

-Mezz

Share this post


Link to post
Share on other sites
You just need to calculate a matrix to transform from tangent space to cube map space in the vertex shader which you pass into the pixel shader where you use it to transform the normal from the bumpmap into cube-map space and then do your reflection. This is posible on PS_1_X hardware. The user manual that comes with the NVidia Cg Toolkit has an example you should be able to find other examples using google, just search for 'bump-reflection mapping' or something similar.

Share this post


Link to post
Share on other sites
Thanks for the helpful links, the example in "NVidia Cg Toolkit" proved to be exactly what I was looking for.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

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

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!