GLSL to C++ help

I’m trying to convert some GLSL code to C++ and I’m not familiar with GLSL syntax, anyone lend a hand?

It’s Inigo Quilez’s noise derivatives from Shader - Shadertoy BETA



vec3 noised( in vec2 x )
{
    vec2 f = fract(x);
    vec2 u = f*f*(3.0-2.0*f);

#if 1
    // texel fetch version
    ivec2 p = ivec2(floor(x));
    float a = texelFetch( iChannel0, (p+ivec2(0,0))&255, 0 ).x;
	float b = texelFetch( iChannel0, (p+ivec2(1,0))&255, 0 ).x;
	float c = texelFetch( iChannel0, (p+ivec2(0,1))&255, 0 ).x;
	float d = texelFetch( iChannel0, (p+ivec2(1,1))&255, 0 ).x;
#else    
    // texture version    
    vec2 p = floor(x);
	float a = textureLod( iChannel0, (p+vec2(0.5,0.5))/256.0, 0.0 ).x;
	float b = textureLod( iChannel0, (p+vec2(1.5,0.5))/256.0, 0.0 ).x;
	float c = textureLod( iChannel0, (p+vec2(0.5,1.5))/256.0, 0.0 ).x;
	float d = textureLod( iChannel0, (p+vec2(1.5,1.5))/256.0, 0.0 ).x;
#endif
    
	return vec3(a+(b-a)*u.x+(c-a)*u.y+(a-b-c+d)*u.x*u.y,
				6.0*f*(1.0-f)*(vec2(b-a,c-a)+(a-b-c+d)*u.yx));
}


As far as I’ve got…



FVector UFastNoise::GetNoise2DDeriv(float x, float y)
{
	FVector2D p = FVector2D(x, y);
	p.X = FMath::FloorToFloat(p.X);
	p.Y = FMath::FloorToFloat(p.Y);
	FVector2D f = FVector2D(x, y) - p;

	FVector2D u = f*f*(3.0 - 2.0*f);

	float a = GetNoise2D(p.X, p.Y);
	float b = GetNoise2D(p.X + 1.f, p.Y);
	float c = GetNoise2D(p.X, p.Y + 1.f);
	float d = GetNoise2D(p.X + 1.0f, p.Y + 1.0f);

	FVector result;

	result = FVector(a + (b - a)*u.X + (c - a)*u.Y + (a - b - c + d)*u.X*u.Y,
		6.0*f*(1.0 - f)*(FVector2D(b - a, c - a) + (a - b - c + d)*(FVector2D(u.Y, u.X))));

	return result;

}


Bit lazy I know but someone familiar with GLSL can probably save me the headache!

In the original code it samples a texture to generate the noise so I guess the question is what is the GetNoise2D function in your code? :stuck_out_tongue:

I’m going to assume that it accesses an array with noise, inputs being the x row and y column in the array (integers).

In that case change your code like so:


	float a = GetNoise2D( ((int)p.X+0)&255, ((int)p.Y+0)&255 );
	float b = GetNoise2D( ((int)p.X+1)&255, ((int)p.Y+0)&255 );
	float c = GetNoise2D( ((int)p.X+0)&255, ((int)p.Y+1)&255 );
	float d = GetNoise2D( ((int)p.X+1)&255, ((int)p.Y+1)&255 );

The &255 part insures that the value will be wrapped from 0 to 255 (assuming the noise array is 256x256).