Converting refraction HLSL shader to GLSL

Post your questions, suggestions and experiences regarding to Image manipulation, 3d modeling and level editing for the Irrlicht engine here.
Post Reply
gragor
Posts: 6
Joined: Fri Apr 19, 2024 5:29 pm

Converting refraction HLSL shader to GLSL

Post by gragor »

I have perfectly working refraction shader in HLSL:
float4x4 WVPMatrix;
float refractionDisplacement;
float colorStrength;

struct VS_OUTPUT
{
float4 Pos : POSITION;
float2 texCoord : TEXCOORD0;
float3 eyeLinear : TEXCOORD1;
};


VS_OUTPUT vertexMain(float4 Pos : POSITION,
float2 texCoord : TEXCOORD0)
{
VS_OUTPUT Out;

float4 sPos = mul(float4(Pos.xzy, 1), WVPMatrix);
Out.Pos = sPos;

Out.eyeLinear.x = 0.5 * (sPos.z + sPos.x);
Out.eyeLinear.y = 0.5 * (sPos.z - sPos.y);
Out.eyeLinear.z = sPos.z;
Out.texCoord = texCoord;

return Out;
}


sampler renderTexture;
sampler bump;
sampler diffuse;

float4 pixelMain(float3 texCoord: TEXCOORD0,
float3 eyeLinear: TEXCOORD1) : COLOR
{
float4 normal = 2.0 * tex2D(bump, texCoord) - 1.0;
float2 coord = eyeLinear.xy / eyeLinear.z;
float4 refr = tex2D(renderTexture, 0.1+0.8*(coord + refractionDisplacement * normal.xy));
float4 diff = tex2D(diffuse, texCoord) * colorStrength;

return (refr * diff) + refr;

}

Can someone please convert it to GLSL?
Thank you
CuteAlien
Admin
Posts: 9733
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: Converting refraction HLSL shader to GLSL

Post by CuteAlien »

No guarantee, I just typed this down quickly without any testing, but it should be something like this:

Code: Select all

// Vector shader
uniform mat4 WVPMatrix;

out vec2 texCoord;
out vec3 eyeLinear;

void main()
{
	vec4 sPos = WVPMatrix * gl_Vertex;
	gl_Position = sPos;
	
	eyeLinear.x = 0.5 * (sPos.z + sPos.x);
	eyeLinear.y = 0.5 * (sPos.z - sPos.y);
	eyeLinear.z = sPos.z;	
	
	texCoord = gl_MultiTexCoord0.xy;
}


// Fragment shader (new file)

uniform float refractionDisplacement;
uniform float colorStrength;

layout(binding = 0) uniform sampler2D renderTexture;	// assuming texture 0, use other binding otherwise
layout(binding = 1) uniform sampler2D bump;	// assuming texture 1
layout(binding = 2) uniform sampler2D diffuse;	// assuming texture 2

in vec2 texCoord;
in vec3 eyeLinear;

void main()
{
	vec4 normal = 2.0 * texture(bump, texCoord) - 1.0;
	vec2 coord = eyeLinear.xy / eyeLinear.z;
	vec4 refr = texture(renderTexture, 0.1+0.8*(coord + refractionDisplacement * normal.xy));
	vec4 diff = texture(diffuse, texCoord) * colorStrength;

	gl_FragColor = (refr * diff) + refr;
} 
I might have used some functions which are not availabe in all GL version, then you have to add a first line (really first line, no comments before it) to increase version number, like:

Code: Select all

#version 430 compatibility
Also this is for OpenGL+glsl as used in Irrlicht. For ES shaders there are slight differences (probably needs some variable instead of gl_MultiTexCoord0 and gl_Vertex, if you need that ask again and I can look it up... or check the shaders in the ogl-es branch of Irrlicht).

Also note that your texCoord switched from float2 to float3 for some reason. I didn't do that.
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
gragor
Posts: 6
Joined: Fri Apr 19, 2024 5:29 pm

Re: Converting refraction HLSL shader to GLSL

Post by gragor »

This is what I get, when I render just refraction, with no diffuse texture:

https://i.ibb.co/kXTYgWv/refraction-rendering.png
gragor
Posts: 6
Joined: Fri Apr 19, 2024 5:29 pm

Re: Converting refraction HLSL shader to GLSL

Post by gragor »

My vertex shader is slightly different:
uniform mat4 WVPMatrix;
uniform float refractionDisplacement;
uniform float colorStrength;


varying vec2 texCoord;
varying vec3 eyeLinear;

void main(void)
{
vec4 sPos = WVPMatrix * gl_Vertex;
gl_Position = sPos;

eyeLinear.x = 0.5 * (sPos.z + sPos.x);
eyeLinear.y = 0.5 * (sPos.z - sPos.y);
eyeLinear.z = sPos.z;

texCoord = gl_MultiTexCoord0.xy; // this is fix
}
CuteAlien
Admin
Posts: 9733
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: Converting refraction HLSL shader to GLSL

Post by CuteAlien »

Ah OK, I've edited it above in my code as well. And looks interesting. Did mean texCoord certainly.
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
gragor
Posts: 6
Joined: Fri Apr 19, 2024 5:29 pm

Re: Converting refraction HLSL shader to GLSL

Post by gragor »

I got it working.
void main(void)
{
vec4 sPos = WVPMatrix * gl_Vertex;
gl_Position = sPos;

eyeLinear.x = 0.5 * (sPos.w + sPos.x);
eyeLinear.y = 0.5 * (sPos.w + sPos.y);
eyeLinear.z = sPos.w;

texCoord = gl_MultiTexCoord0.xy;
}
Post Reply