Ivo*_*tão 13 opengl math directx glsl hlsl
我在HLSL中实现了这个问题中描述的螺旋GLSL着色器,但结果并不相同.我认为这是因为mod
我已经fmod
在HLSL中翻译了GLSL 中的函数.我怀疑只有在fmod
函数输入中有负数时才会出现此问题.
我已经尝试mod
通过调用我已经制作的函数替换调用来执行GLSL文档中描述的函数并且它可以工作:
mod
返回x
modulo 的值y
.计算方法如下x - y * floor(x/y)
.
我使用的工作代码fmod
是:
float mod(float x, float y)
{
return x - y * floor(x/y)
}
Run Code Online (Sandbox Code Playgroud)
与GLSL相比mod
,MSDN表示 HLSL fmod
功能可以做到这一点:
计算浮点余数,使得
x = i * y + f
其中i
,整数f
具有与其相同的符号x
,并且绝对值f
小于绝对值y
.
我使用了HLSL到GLSL转换器,并将该fmod
函数翻译为mod
.但是,我不知道我是否可以假设mod
转换为fmod
.
mod
和HLSL有fmod
什么区别? fmod
转换为伪代码实现?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 );
}
}
Run Code Online (Sandbox Code Playgroud)
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();
}
}
Run Code Online (Sandbox Code Playgroud)
Chr*_*odd 17
正如你所说,它们是不同的.GLSL mod
将始终具有相同的符号y
而不是x
.否则它是相同的 - 一个值f
,x = i*y + f
其中where i
是一个整数和|f| < |y|
.如果你想尝试某种重复模式,那么GLSL mod
通常就是你想要的.
相比之下,HLSL fmod
相当于x - y * trunc(x/y)
.它们x/y
是正面的,不同的x/y
是负面的.