Ho implementato lo shader GLSL spirale descritto in un thread di stackoverflow How to implement this rotating spiral in WebGL? in HLSL ma i risultati non sono gli stessi e penso che sia dovuto alla funzione mod in GLSL che ho tradotto in fmod in HLSL. Ho provato a sostituire la chiamata a mod da una chiamata personalizzata a una funzione che ho effettuato che fa quanto descritto nella documentazione GSLS (http://www.opengl.org/sdk/docs/manglsl/xhtml/mod.xml) e funziona:Glsl mod vs Hlsl fmod
mod restituisce il valore di x modulo y . Questo è calcolato come x - y * floor (x/y).
La funzione fmod secondo MSDN documentazione http://msdn.microsoft.com/en-us/library/windows/desktop/bb509601(v=vs.85).aspx, dice il seguente:
Il virgola mobile rimanente viene calcolata tale che x = i * y + f, dove i è un numero intero, f ha lo stesso segno x, e il valore assoluto di f è inferiore al valore assoluto di y.
Ho usato il convertitore HLSL in GLSL ospitato in http://sourceforge.net/projects/hlsl2glsl e la funzione fmod è tradotta come mod, tuttavia non so se posso supporre che la mod si traduca in fmod.
La mia domanda è, quali sono le differenze tra le due funzioni e come posso tradurre la descrizione criptica di msdn di fmod in un'implementazione di pseudo-codice.
ho il sospetto che questo problema si verifica solo quando abbiamo i numeri negativi in ingresso della funzione fmod
GLSL Shader:
uniform float time;
uniform vec2 resolution;
uniform vec2 aspect;
void main(void) {
vec2 position = -aspect.xy + 2.0 * gl_FragCoord.xy/resolution.xy * aspect.xy;
float angle = 0.0 ;
float radius = length(position) ;
if (position.x != 0.0 && position.y != 0.0){
angle = degrees(atan(position.y,position.x)) ;
}
float amod = mod(angle+30.0*time-120.0*log(radius), 30.0) ;
if (amod<15.0){
gl_FragColor = vec4(0.0, 0.0, 0.0, 1.0);
} else{
gl_FragColor = vec4(1.0, 1.0, 1.0, 1.0);
}
}
HLSL Shader:
struct Psl_VertexShaderInput
{
float3 pos : POSITION;
};
struct Psl_VertexShaderOutput
{
float4 pos : POSITION;
};
struct Psl_PixelShaderOutput
{
float4 Output0 : COLOR0;
};
float3 psl_positionOffset;
float2 psl_dimension;
Psl_VertexShaderOutput Psl_VertexShaderFunction(Psl_VertexShaderInput psl_input)
{
Psl_VertexShaderOutput psl_output = (Psl_VertexShaderOutput)0;
psl_output.pos = float4(psl_input.pos + psl_positionOffset, 1);
return psl_output;
}
float time : TIME;
float2 resolution : DIMENSION;
Psl_PixelShaderOutput Psl_PixelShaderFunction(float2 pos : VPOS)
{
Psl_PixelShaderOutput psl_output = (Psl_PixelShaderOutput)0;
float2 aspect = float2(resolution.x/resolution.y, 1.0);
float2 position = -aspect.xy + 2.0 * pos.xy/resolution.xy * aspect.xy;
float angle = 0.0;
float radius = length(position);
if (position.x != 0.0 && position.y != 0.0)
{
angle = degrees(atan2(position.y, position.x));
}
float amod = fmod((angle + 30.0 * time - 120.0 * log(radius)), 30.0);
if (amod < 15.0)
{
psl_output.Output0 = float4(0.0, 0.0, 0.0, 1.0);
return psl_output;
}
else
{
psl_output.Output0 = float4(1.0, 1.0, 1.0, 1.0);
return psl_output;
}
}
technique Default
{
pass P0
{
VertexShader = compile vs_3_0 Psl_VertexShaderFunction();
PixelShader = compile ps_3_0 Psl_PixelShaderFunction();
}
}
ho nessun problema se invece di fmod chiamo la seguente funzione (per definizione del mod GLSL)
float mod(float x, float y)
{
return x - y * floor(x/y)
}
Penso che sia possibile utilizzare% per mod in HLSL. – Robinson
sì, ho provato ma% dà esattamente lo stesso problema. La spirale non è rappresentata correttamente. 1/4 di esso viene tagliato –
Sarebbe utile vedere entrambi gli shader in modo da poterli confrontare. – kvark