Sign in to follow this  
Hseptic

Unit vector in an effect file

Recommended Posts

you can use the "normalize()" function in hlsl to make a vector unit length, or you can create the vector in your app and send it to the shaders using a constant buffer.

in hlsl here are a couple unit vectors for you

float3 unitVector1 = float3(1,0,0); // unit vector

float3 unitVector2 = float3(10, 2, 7); // Not unit vector yet

unitVector = normalize(unitVector); // Now it's a unit length vector Edited by iedoc

Share this post


Link to post
Share on other sites
[quote name='iedoc' timestamp='1338029274' post='4943442']
you can use the "normalize()" function in hlsl to make a vector unit length, or you can create the vector in your app and send it to the shaders using a constant buffer.

in hlsl here are a couple unit vectors for you

float3 unitVector1 = float3(1,0,0); // unit vector

float3 unitVector2 = float3(10, 2, 7); // Not unit vector yet

unitVector = normalize(unitVector); // Now it's a unit length vector
[/quote]

Unfortunately, when I try to normalize my vector in the effect file like so...

[CODE]
float3 udir = float3 (1, 0, 1);
udir = normalize(udir);
[/CODE]

... it says "unrecognized identifier udir". Why won't it recognize my vector?

Share this post


Link to post
Share on other sites
Can you show your entire shader? As far as I can tell these two lines are valid (out of context, anyway). There must be something else in the shader code that's preventing this to work.

Share this post


Link to post
Share on other sites
[quote name='Bacterius' timestamp='1338038745' post='4943468']
Can you show your entire shader? As far as I can tell these two lines are valid (out of context, anyway). There must be something else in the shader code that's preventing this to work.
[/quote]

[CODE]
uniform extern float4x4 gWVP;
uniform extern float gTime;
struct OutputVS
{
float4 posH : POSITION0;
float4 color : COLOR0;
};
float3 udir = float3 (1, 0, 1);
udir = normalize(udir);
static float a[2] = {0.8f, 0.2f};
static float k[2] = {1.0, 8.0f};

static float w[2] = {1.0f, 8.0f};

static float p[2] = {0.0f, 1.0f};
float SumOfRadialSineWaves(float x, float z)
{
float3 p = float3(x, 0, z);
float my_x = dot(udir, p);

float sum = 0.0f;
for(int i = 0; i < 2; ++i)
sum += a[i]*sin(k[i]*my_x - gTime*w[i] + p[i]);

return sum;
}
float4 GetColorFromHeight(float y)
{
if( abs(y) <= 0.2f ) // black
return float4(0.0f, 0.0f, 0.0f, 1.0f);
else if(abs(y) <= 0.5f ) // blue
return float4(0.0f, 0.0f, 1.0f, 1.0f);
else if(abs(y) <= 0.8f ) // green
return float4(0.0f, 1.0f, 0.0f, 1.0f);
else if(abs(y) <= 1.0f ) // red
return float4(1.0f, 0.0f, 0.0f, 1.0f);
else // yellow
return float4(1.0f, 1.0f, 0.0f, 1.0f);
}
OutputVS ColorVS(float3 posL : POSITION0)
{
OutputVS outVS = (OutputVS)0;
posL.y = SumOfRadialSineWaves(posL.x, posL.z);

outVS.color = GetColorFromHeight(posL.y);

outVS.posH = mul(float4(posL, 1.0f), gWVP);

return outVS;
}
float4 ColorPS(float4 c : COLOR0) : COLOR
{
return c;
}
technique HeightColorTech
{
pass P0
{
vertexShader = compile vs_2_0 ColorVS();
pixelShader = compile ps_2_0 ColorPS();
FillMode = WireFrame;

}
}
[/CODE] Edited by Hseptic

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