我已经在HLSL中实现了这个问题中描述的螺旋GLSL着色器,但结果并不相同。我认为这是因为我在HLSL中翻译了GLSL中的
我已经尝试用我自己制作的函数替换
与GLSL的
浮点余数计算如下:
我使用了一个HLSL到GLSL转换器,将
mod
函数成为fmod
函数所导致的。我怀疑这个问题只会在fmod
函数的输入中有负数时才会出现。我已经尝试用我自己制作的函数替换
mod
函数的调用,它实现了GLSL文档中描述的功能,并且能够正常工作:
我使用的代替
mod
返回x
模除y
的值。这可以计算为x - y * floor(x/y)
。
fmod
的可工作代码如下:float mod(float x, float y)
{
return x - y * floor(x/y)
}
与GLSL的
mod
函数相比,MSDN说HLSL的fmod
函数会这样做:浮点余数计算如下:
x = i * y + f
,其中i
是整数,f
与x
具有相同的符号,并且f
的绝对值小于y
的绝对值。我使用了一个HLSL到GLSL转换器,将
fmod
函数翻译为mod
。但是,我不知道是否可以假设mod
翻译成fmod
。
问题
- GLSL
mod
和HLSLfmod
之间有什么区别? - 如何将MSDN的晦涩描述转换为伪代码实现?
GLSL着色器
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着色器
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();
}
}