Sign in to follow this  
zurekx

Simple HLSL questions

Recommended Posts

I have some questions about HLSL. First of all, when I pass things from the program to the vertex shader, or between the VS and the PS, do I have to give them a certain "type", like NORMAL0 or POSITION0? Cant I just simply pass a float between the PS and the VS without any information about what it is? I've seen people doing it by passing that kinda stuff as texture coordinates, but is it neccesary? In GLSL for instance, I can just pass a varying variable. Another thing is, when I pass the normal to the VS from my code, I use the following D3DVERTEXELEMENT9 (for the normals) { 0, 20, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_NORMAL, 0 }, The problem is that the normal appears as a float4 with w = 1.0. Is there any way I can fix so the w coordinate is 0 by default for normals? It's easy to take care of in the VS, but I just find it weird that it seems to be 1 by default.

Share this post


Link to post
Share on other sites
Quote:
Original post by zurekx
I have some questions about HLSL. First of all, when I pass things from the program to the vertex shader, or between the VS and the PS, do I have to give them a certain "type", like NORMAL0 or POSITION0? Cant I just simply pass a float between the PS and the VS without any information about what it is? I've seen people doing it by passing that kinda stuff as texture coordinates, but is it neccesary? In GLSL for instance, I can just pass a varying variable.


You can basically pass any type between the VS to the PS, like float, but they need to have a semantic so they can be distinguished by the compiler/runtime. The normal approach is to use one of the available TEXCOORDn semantics, so you can pass your float like:
float someValue : TEXCOORD0/1/2/3. NORMAL is usually reserved for normals in the VS and POSITION as input and output from the VS.

Quote:
Another thing is, when I pass the normal to the VS from my code, I use the following D3DVERTEXELEMENT9 (for the normals)

The problem is that the normal appears as a float4 with w = 1.0. Is there any way I can fix so the w coordinate is 0 by default for normals? It's easy to take care of in the VS, but I just find it weird that it seems to be 1 by default.


Why do you need .w to be 0? float3 are usually expanded to a float4 because the hardware calculates with four components, thus resulting in .w being 1.

Share this post


Link to post
Share on other sites
Yeah, but I want my normals to have w = 0 (so it wont be affected by translations when I multiply it). Just wondered if it could be set to 0 automatically.

Share this post


Link to post
Share on other sites
well, you can do something like

output.Normal = mul((float3x3)WorldIT, Normal)

or

output.Normal = mul(WorldIT, float4(Normal, 0));

I'm not sure which one is correct here.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this