**0**

###
#2
Members - Reputation: **1040**

Posted 26 May 2012 - 04:47 AM

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, 26 May 2012 - 04:48 AM.**

###
#4
Crossbones+ - Reputation: **9043**

Posted 26 May 2012 - 06:19 AM

The MSDN has more information.

The slowsort algorithm is a perfect illustration of the multiply and surrender paradigm, which is perhaps the single most important paradigm in the development of reluctant algorithms. The basic multiply and surrender strategy consists in replacing the problem at hand by two or more subproblems, each slightly simpler than the original, and continue multiplying subproblems and subsubproblems recursively in this fashion as long as possible. At some point the subproblems will all become so simple that their solution can no longer be postponed, and we will have to surrender. Experience shows that, in most cases, by the time this point is reached the total work will be substantially higher than what could have been wasted by a more direct approach.

- *Pessimal Algorithms and Simplexity Analysis*

###
#5
Members - Reputation: **146**

Posted 26 May 2012 - 07:15 AM

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

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

float3 udir = float3 (1, 0, 1); udir = normalize(udir);

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

###
#6
Crossbones+ - Reputation: **9043**

Posted 26 May 2012 - 07:25 AM

The slowsort algorithm is a perfect illustration of the multiply and surrender paradigm, which is perhaps the single most important paradigm in the development of reluctant algorithms. The basic multiply and surrender strategy consists in replacing the problem at hand by two or more subproblems, each slightly simpler than the original, and continue multiplying subproblems and subsubproblems recursively in this fashion as long as possible. At some point the subproblems will all become so simple that their solution can no longer be postponed, and we will have to surrender. Experience shows that, in most cases, by the time this point is reached the total work will be substantially higher than what could have been wasted by a more direct approach.

- *Pessimal Algorithms and Simplexity Analysis*

###
#7
Members - Reputation: **146**

Posted 26 May 2012 - 08:07 AM

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.

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; } }

**Edited by Hseptic, 26 May 2012 - 08:09 AM.**

###
#10
Crossbones+ - Reputation: **9043**

Posted 26 May 2012 - 07:06 PM

The slowsort algorithm is a perfect illustration of the multiply and surrender paradigm, which is perhaps the single most important paradigm in the development of reluctant algorithms. The basic multiply and surrender strategy consists in replacing the problem at hand by two or more subproblems, each slightly simpler than the original, and continue multiplying subproblems and subsubproblems recursively in this fashion as long as possible. At some point the subproblems will all become so simple that their solution can no longer be postponed, and we will have to surrender. Experience shows that, in most cases, by the time this point is reached the total work will be substantially higher than what could have been wasted by a more direct approach.

- *Pessimal Algorithms and Simplexity Analysis*