[Solved] Help converting a GLSL shader Topic is solved

Problems building or running the engine, queries about how to use features etc.
tritonas00
Halfling
Posts: 89
Joined: Sun Apr 08, 2018 2:21 pm
x 42

[Solved] Help converting a GLSL shader

Post by tritonas00 »

Ogre Version: 13.5.3
Operating System: Arch Linux
Render System: OpenGL

Hi! I'm trying to port this nice water shader https://blendswap.com/blend/9572 from blender to Ogre.

I'm a bit confused about a few things.

Original vertex shader

Code: Select all

attribute vec4 Tangent;
varying vec4 fragPos;
varying vec3 T, B, N; //tangent binormal normal
varying vec3 viewPos, worldPos;
varying float timer;
uniform mat4 ModelMatrix;

mat3 m3( mat4 m )
{
	mat3 result;
	
	result[0][0] = m[0][0]; 
	result[0][1] = m[0][1]; 
	result[0][2] = m[0][2]; 

	result[1][0] = m[1][0]; 
	result[1][1] = m[1][1]; 
	result[1][2] = m[1][2]; 
	
	result[2][0] = m[2][0]; 
	result[2][1] = m[2][1]; 
	result[2][2] = m[2][2]; 
	
	return result;
}

void main() 
{
    gl_TexCoord[0] = gl_MultiTexCoord0;
	vec3 pos = vec3(gl_Vertex);
	
	T   = m3(ModelMatrix)*Tangent.xyz;
	B   = m3(ModelMatrix)*cross(gl_Normal, Tangent.xyz);
	N   = m3(ModelMatrix)*gl_Normal; 

    worldPos = vec3(ModelMatrix*gl_Vertex);
    fragPos = ftransform();
    viewPos = pos - m3(ModelMatrix)*gl_ModelViewMatrixInverse[3].xyz;
    gl_Position = ftransform();
    //timer = gl_Color.r*2.0;
}

For a start:

  1. Tangent i suppose would be Ogre::VES_TANGENT (tangent) ?
  2. ftransform(), what would be Ogre's equivalent? worldviewproj_matrix * vertex?
  3. ModelMatrix and gl_ModelViewMatrixInverse, what would be Ogre's equivalents?

https://ogrecave.github.io/ogre/api/13/ ... attributes
https://ogrecave.github.io/ogre/api/13/ ... 24f0d43ce3

Thanks!

UPDATE

Most of the porting is done, thanks all who helped! I will push a PR to add it as Ogre sample when done.

Image

Image

Image

Last edited by tritonas00 on Mon Nov 13, 2023 12:43 pm, edited 6 times in total.
tritonas00
Halfling
Posts: 89
Joined: Sun Apr 08, 2018 2:21 pm
x 42

Re: [13.5.3] Help converting a GLSL shader

Post by tritonas00 »

So managed to run the shader without errors, only assigned normalSampler for now which is a texture.

Original shader code

My attempts:

vertex

Code: Select all

#version 400

attribute vec2 uv0;
attribute vec3 normal;
attribute vec4 vertex;
attribute vec4 tangent;
varying vec4 fragPos;
varying vec3 T, B, N; //tangent binormal normal
varying vec3 viewPos, worldPos;
//varying float timer;
uniform mat4 worldViewProj;
uniform mat4 ModelMatrix;
uniform mat4 ViewMatrix;

out vec2 f_texcoord;

mat3 m3( mat4 m )
{
	mat3 result;
	
	result[0][0] = m[0][0]; 
	result[0][1] = m[0][1]; 
	result[0][2] = m[0][2]; 

	result[1][0] = m[1][0]; 
	result[1][1] = m[1][1]; 
	result[1][2] = m[1][2]; 
	
	result[2][0] = m[2][0]; 
	result[2][1] = m[2][1]; 
	result[2][2] = m[2][2]; 
	
	return result;
}

void main() 
{
        //  gl_TexCoord[0] = gl_MultiTexCoord0;
	//  vec3 pos = vec3(vertex);

        f_texcoord = uv0;
	
	T   = m3(ModelMatrix)*tangent.xyz;
	B   = m3(ModelMatrix)*cross(normal, tangent.xyz);
	N   = m3(ModelMatrix)*normal; 

    worldPos = vec3(ModelMatrix*vertex);
    fragPos = worldViewProj * vertex;
    viewPos = ViewMatrix[3].xyz;
    gl_Position = worldViewProj * vertex;
    //timer = gl_Color.r*2.0;
}

fragment

Code: Select all

#version 400

in vec2 f_texcoord;

varying vec4 fragPos; //fragment coordinates
varying vec3 T, B, N; //tangent binormal normal
varying vec3 viewPos;
varying vec3 worldPos;
uniform float timer;
uniform sampler2D reflectionSampler,refractionSampler, depthSampler, normalSampler;
uniform vec3 cameraPos;

//----------------
//tweakables

vec2 windDir = vec2(0.5, -0.8); //wind direction XY
float windSpeed = 0.2; //wind speed

float visibility = 28.0;

float scale = 1.0; //overall wave scale

vec2 bigWaves = vec2(0.3, 0.3); //strength of big waves
vec2 midWaves = vec2(0.3, 0.15); //strength of middle sized waves
vec2 smallWaves = vec2(0.15, 0.1); //strength of small waves

vec3 waterColor = vec3(0.2,0.4,0.5); //color of the water
float waterDensity = 0.0; //water density (0.0-1.0)
    
float choppy = 0.15; //wave choppyness
float aberration = 0.002; //chromatic aberration amount
float bump = 2.6; //overall water surface bumpyness
float reflBump = 0.04; //reflection distortion amount
float refrBump = 0.03; //refraction distortion amount

//vec3 sunPos = vec3(gl_ModelViewMatrixInverse*gl_LightSource[0].position);
vec3 sunPos = vec3(1.0,1.0,1.0);
float sunSpec = 1000.0; //Sun specular hardness

float scatterAmount = 3.5; //amount of sunlight scattering of waves
vec3 scatterColor = vec3(0.0,1.0,0.95);// color of the sunlight scattering
//----------------

vec3 tangentSpace(vec3 v)
{
	vec3 vec;
	vec.xy=v.xy;
	vec.z=sqrt(1.0-dot(vec.xy,vec.xy));
	vec.xyz= normalize(vec.x*T+vec.y*B+vec.z*N);
	return vec;
}

float fresnel_dielectric(vec3 Incoming, vec3 Normal, float eta)
{
    /* compute fresnel reflectance without explicitly computing
       the refracted direction */
    float c = abs(dot(Incoming, Normal));
    float g = eta * eta - 1.0 + c * c;
    float result;

    if(g > 0.0) {
        g = sqrt(g);
        float A =(g - c)/(g + c);
        float B =(c *(g + c)- 1.0)/(c *(g - c)+ 1.0);
        result = 0.5 * A * A *(1.0 + B * B);
    }
    else
        result = 1.0;  /* TIR (no refracted component) */

    return result;
}


void main() {
    //vec2 fragCoord = (fragPos.xy/fragPos.w)*0.5+0.5;
    vec2 fragCoord = f_texcoord;
    fragCoord = clamp(fragCoord,0.002,0.998);

	//normal map
	vec2 nCoord = vec2(0.0); //normal coords
    vec2 coo = ((f_texcoord.st)/1106.208)-0.5;
    float depth = texture2D(depthSampler,coo).r;
    float coast = smoothstep(0.3,0.7,depth);
    float coast1 = smoothstep(0.49,0.5,depth);
     
    choppy = choppy * (coast)+0.05;
    bump = -bump*clamp(1.0-coast+0.0,0.0,1.0);
    bump = bump*clamp(1.0-coast1+0.0,0.0,1.0);
    
    //scale = scale - (coast*0.1);
    //scale = scale * (coast*0.1+0.9);
    float time = timer - (coast)*80.0; //hmmm
    //timer = timer * (coast*0.5+0.5)*1.5;
    //smallWaves.x = smallWaves.x + (0.1*coast);
    //smallWaves.y = smallWaves.y + (0.1*coast);
    //bigWaves.x = bigWaves.x - (0.1*(1.0-coast));
    //bigWaves.y = bigWaves.y + (0.1*(1.0-coast));
    
    vec3 mudext = vec3(1.0, 0.7, 0.5);//mud extinction
    float atmosphere = length(cameraPos-worldPos)/200.0;
    
    vec3 atmopherefog = clamp(1.0-exp(-atmosphere/mudext),0.0,1.0);
    
  	nCoord = worldPos.xy * (scale * 0.04) + windDir * time * (windSpeed*0.04);
	vec3 normal0 = 2.0 * texture2D(normalSampler, nCoord + vec2(-time*0.015,-time*0.005)).rgb - 1.0;
	nCoord = worldPos.xy * (scale * 0.1) + windDir * time * (windSpeed*0.08)-(normal0.xy/normal0.zz)*choppy;
	vec3 normal1 = 2.0 * texture2D(normalSampler, nCoord + vec2(+time*0.020,+time*0.015)).rgb - 1.0;
 
 	nCoord = worldPos.xy * (scale * 0.25) + windDir * time * (windSpeed*0.07)-(normal1.xy/normal1.zz)*choppy;
	vec3 normal2 = 2.0 * texture2D(normalSampler, nCoord + vec2(-time*0.04,-time*0.03)).rgb - 1.0;
	nCoord = worldPos.xy * (scale * 0.5) + windDir * time * (windSpeed*0.09)-(normal2.xy/normal2.z)*choppy;
	vec3 normal3 = 2.0 * texture2D(normalSampler, nCoord + vec2(+time*0.03,+time*0.04)).rgb - 1.0;
  
  	nCoord = worldPos.xy * (scale* 1.0) + windDir * time * (windSpeed*0.4)-(normal3.xy/normal3.zz)*choppy;
	vec3 normal4 = 2.0 * texture2D(normalSampler, nCoord + vec2(-time*0.02,+time*0.1)).rgb - 1.0;  
    nCoord = worldPos.xy * (scale * 2.0) + windDir * time * (windSpeed*0.7)-(normal4.xy/normal4.zz)*choppy;
    vec3 normal5 = 2.0 * texture2D(normalSampler, nCoord + vec2(+time*0.1,-time*0.06)).rgb - 1.0;

	
	
	vec3 normal = normalize(normal0 * bigWaves.x + normal1 * bigWaves.y +
                            normal2 * midWaves.x + normal3 * midWaves.y +
						    normal4 * smallWaves.x + normal5 * smallWaves.y);

    //normal.x = -normal.x; //in case you need to invert Red channel
    //normal.y = -normal.y; //in case you need to invert Green channel
   
    vec3 nVec = tangentSpace(normal*bump); //converting normals to tangent space    
    vec3 vVec = normalize(viewPos);
    vec3 lVec = normalize(sunPos);
    
    //normal for light scattering
	vec3 lNormal = normalize(normal0 * bigWaves.x*0.5 + normal1 * bigWaves.y*0.5 +
                            normal2 * midWaves.x*0.1 + normal3 * midWaves.y*0.1 +
						    normal4 * smallWaves.x*0.1 + normal5 * smallWaves.y*0.1);
    lNormal = tangentSpace(lNormal*bump);
    vec3 pNormal = tangentSpace(vec3(0.0));
    
	vec3 lR = reflect(lVec, lNormal);
    vec3 llR = reflect(lVec, pNormal);
    
    float sunFade = clamp((sunPos.z+10.0)/20.0,0.0,1.0);
    float scatterFade = clamp((sunPos.z+50.0)/200.0,0.0,1.0);
    vec3 sunext = vec3(0.45, 0.55, 0.68);//sunlight extinction
    
	float s = clamp((dot(lR, vVec)*2.0-1.2), 0.0,1.0);
    float lightScatter = clamp((clamp(dot(-lVec,lNormal)*0.7+0.3,0.0,1.0)*s)*scatterAmount,0.0,1.0)*sunFade *clamp(1.0-exp(-(sunPos.z/500.0)),0.0,1.0);
    scatterColor = mix(vec3(scatterColor)*vec3(1.0,0.4,0.0), scatterColor, clamp(1.0-exp(-(sunPos.z/500.0)*sunext),0.0,1.0));

    //fresnel term
    float ior = 1.33;
    ior = (cameraPos.z>0.0)?(1.333/1.0):(1.0/1.333); //air to water; water to air
    float eta = max(ior, 0.00001);
    float fresnel = fresnel_dielectric(-vVec,nVec,eta);
    
    //texture edge bleed removal
    float fade = 12.0;
    vec2 distortFade = vec2(0.0);
    distortFade.s = clamp(fragCoord.s*fade,0.0,1.0);
    distortFade.s -= clamp(1.0-(1.0-fragCoord.s)*fade,0.0,1.0);
    distortFade.t = clamp(fragCoord.t*fade,0.0,1.0);
    distortFade.t -= clamp(1.0-(1.0-fragCoord.t)*fade,0.0,1.0); 
    
    vec3 reflection = texture2D(reflectionSampler, fragCoord+(nVec.st*vec2(reflBump,reflBump*(6.0))*distortFade)).rgb;
    
    vec3 luminosity = vec3(0.30, 0.59, 0.11);
	float reflectivity = pow(dot(luminosity, reflection.rgb*2.0),3.0);

	float reflectivity1 = pow(dot(luminosity, reflection.rgb),3.0);
        
    vec3 R = reflect(vVec, nVec);

    float specular = clamp(pow(atan(max(dot(R, lVec),0.0)*1.55),1000.0)*reflectivity*8.0,0.0,1.0);
    
    vec3 specColor = mix(vec3(1.0,0.5,0.2), vec3(1.0,1.0,1.0), clamp(1.0-exp(-(sunPos.z/500.0)*sunext),0.0,1.0));

    vec2 rcoord = reflect(vVec,nVec).st;
    vec3 refraction = vec3(0.0);
    
    refraction.r = texture2D(refractionSampler, (fragCoord-(nVec.st*refrBump*distortFade))*1.0).r;
    refraction.g = texture2D(refractionSampler, (fragCoord-(nVec.st*refrBump*distortFade))*1.0-(rcoord*aberration)).g;
    refraction.b = texture2D(refractionSampler, (fragCoord-(nVec.st*refrBump*distortFade))*1.0-(rcoord*aberration*2.0)).b;
    
      
    float waterSunGradient = dot(normalize(cameraPos-worldPos), -normalize(sunPos));
    waterSunGradient = clamp(pow(waterSunGradient*0.7+0.3,2.0),0.0,1.0);  
    vec3 waterSunColor = vec3(0.0,1.0,0.85)*waterSunGradient;
    waterSunColor = (cameraPos.z<0.0)?waterSunColor*0.5:waterSunColor*0.25;//below or above water?
   
    float waterGradient = dot(normalize(cameraPos-worldPos), vec3(0.0,0.0,-1.0));
    waterGradient = clamp((waterGradient*0.5+0.5),0.2,1.0);
    vec3 watercolor = (vec3(0.0078, 0.5176, 0.700)+waterSunColor)*waterGradient*1.5;
    vec3 waterext = vec3(0.6, 0.8, 1.0);//water extinction
    
    watercolor = mix(watercolor*0.3*sunFade, watercolor, clamp(1.0-exp(-(sunPos.z/500.0)*sunext),0.0,1.0));
    
    float fog = length(cameraPos-worldPos)/visibility; 
    fog = (cameraPos.z<0.0)?fog:1.0;
    fog = clamp(pow(fog,1.0),0.0,1.0);
    
    float darkness = visibility*2.0;
    darkness = clamp((cameraPos.z+darkness)/darkness,0.0,1.0);
    
    fresnel = clamp(fresnel,0.0,1.0);
        
    vec3 color = mix(mix(refraction,scatterColor,lightScatter),reflection,fresnel*0.6);
    color = (cameraPos.z<0.0)?mix(clamp(refraction*1.2,0.0,1.0),reflection,fresnel):color;   
    color = (cameraPos.z<0.0)?mix(color, watercolor*darkness*scatterFade, clamp(fog/ waterext,0.0,1.0)):color;
    
    //color = mix(color,atmopherefog*0.8,atmopherefog.b);
    
    //gl_FragColor = vec4(vec3(color*0.001+(specColor*specular)),1.0);
    gl_FragColor = vec4(vec3(color+(specColor*specular)),1.0);
}

material

Code: Select all

vertex_program myVertexShader1 glsl
{
    source example1.vert

    default_params
    {
        param_named_auto worldViewProj worldviewproj_matrix
        param_named_auto ModelMatrix world_matrix
        param_named_auto ViewMatrix view_matrix
    }
}

fragment_program myFragmentShader1 glsl
{
    source example1.frag
}

material tracks/basicwater
{
    technique
    {
        pass
        {
            scene_blend add

            vertex_program_ref myVertexShader1
            {
            }
            fragment_program_ref myFragmentShader1
            {
                param_named_auto timer time
                param_named_auto cameraPos camera_position
                param_named normalSampler int 0
            }

            texture_unit
            {
                texture water-normal.png
             //   tex_coord_set 0
             //   filtering linear linear linear
            }
        }
    }
}

I see only color, no animations though i assigned timer to time

Image

Why the animations don't work? :?
I suppose the color looks like this because i didn't assign reflectionSampler refractionSampler and depthSampler yet? It is supposed to look like:

Image

paroj
OGRE Team Member
OGRE Team Member
Posts: 2251
Joined: Sun Mar 30, 2014 2:51 pm
x 1227

Re: [13.5.3] Help converting a GLSL shader

Post by paroj »

tritonas00 wrote: Wed Dec 28, 2022 12:34 pm

I suppose the color looks like this because i didn't assign reflectionSampler refractionSampler and depthSampler yet?

yes. take a look at the Fresnel sample in the samplebrowser

tritonas00
Halfling
Posts: 89
Joined: Sun Apr 08, 2018 2:21 pm
x 42

Re: [13.5.3] Help converting a GLSL shader

Post by tritonas00 »

paroj wrote: Wed Dec 28, 2022 2:07 pm
tritonas00 wrote: Wed Dec 28, 2022 12:34 pm

I suppose the color looks like this because i didn't assign reflectionSampler refractionSampler and depthSampler yet?

yes. take a look at the Fresnel sample in the samplebrowser

Got it https://github.com/OGRECave/ogre/blob/m ... erial#L486, thanks!

tritonas00
Halfling
Posts: 89
Joined: Sun Apr 08, 2018 2:21 pm
x 42

Re: [13.5.3] Help converting a GLSL shader

Post by tritonas00 »

So, added the compositor like in the sample (https://github.com/OGRECave/ogre/blob/9 ... .h#L87-L91):

Code: Select all

auto compositor = CompositorManager::getSingleton().addCompositor(RoR::App::GetAppContext()->GetViewport(), "Fresnel");
CompositorManager::getSingleton().setCompositorEnabled(RoR::App::GetAppContext()->GetViewport(), "Fresnel", true);
compositor->getRenderTarget("reflection")->addListener(this);

and edited the material:

Code: Select all

            vertex_program_ref myVertexShader1
            {
            }
            fragment_program_ref myFragmentShader1
            {
                param_named_auto timer time
                param_named_auto cameraPos camera_position
                param_named normalSampler int 0
                param_named reflectionSampler int 1
            }

            texture_unit
            {
                texture water-normal.png
             //   tex_coord_set 0
             //   filtering linear linear linear
            }

	    texture_unit
	    {
	        content_type compositor Fresnel reflection
	        tex_address_mode clamp
	    }
        }
    }

i get:
InvalidParametersException: Compositor 'Fresnel' not found in addCompositor at /home/babis/Downloads/ror-dependencies/Source/ogre/OgreMain/src/OgreCompositorManager.cpp (line 164)

Not sure what i do wrong

paroj
OGRE Team Member
OGRE Team Member
Posts: 2251
Joined: Sun Mar 30, 2014 2:51 pm
x 1227

Re: [13.5.3] Help converting a GLSL shader

Post by paroj »

you also need to add the Fresnel compositor script

tritonas00
Halfling
Posts: 89
Joined: Sun Apr 08, 2018 2:21 pm
x 42

Re: [13.5.3] Help converting a GLSL shader

Post by tritonas00 »

paroj wrote: Wed Dec 28, 2022 9:15 pm

you also need to add the Fresnel compositor script

Yep that did it but it seems i can't enable the compositor properly. Once i add

Code: Select all

texture_unit
{
    content_type compositor Fresnel reflection
    tex_address_mode clamp
}

in my material i get:

RuntimeAssertionException: currentChain failed. A pass that wishes to reference a compositor texture attempted to render in a pipeline without a compositor in _setPass at /home/babis/Downloads/ror-dependencies/Source/ogre/OgreMain/src/OgreSceneManager.cpp (line 1036) :x

tritonas00
Halfling
Posts: 89
Joined: Sun Apr 08, 2018 2:21 pm
x 42

Re: [13.5.3] Help converting a GLSL shader

Post by tritonas00 »

Im really not sure about this

Code: Select all

own['shader'].setUniformDef('ModelMatrix', g.MODELMATRIX)

Is this Ogre's equivalent?

Code: Select all

param_named_auto ModelMatrix world_matrix

If i remove that in the Blender sample i get a flat color plane there also.

tritonas00
Halfling
Posts: 89
Joined: Sun Apr 08, 2018 2:21 pm
x 42

Re: [13.5.3] Help converting a GLSL shader

Post by tritonas00 »

I removed the #version 400 from my shaders and now i can use them as is, copy-pasted from blender. I still get a flat color plane, no animations

Image

Also tried to assign the shaders to the Fresnel ogre sample and used reflectionSampler and refractionSampler

Image

:?

tritonas00
Halfling
Posts: 89
Joined: Sun Apr 08, 2018 2:21 pm
x 42

Re: [13.5.3] Help converting a GLSL shader

Post by tritonas00 »

paroj wrote: Wed Dec 28, 2022 9:15 pm

you also need to add the Fresnel compositor script

Ok this is getting hard to figure out. Tried to strip it down to minimal into blender, it seems the main element is the refraction. By removing that you get just a color plane, no animations. In Ogre i use the refraction and reflection from the Fresnel sample. The only thing i manage to get is a flat plane with reflection/refraction in one corner of the plane, and changing colors depending the camera position.

Before continue trying, Is this something that Ogre actually can render or it fully relies on blender game engine magic?

Last edited by tritonas00 on Sun Jan 22, 2023 10:08 pm, edited 1 time in total.
rpgplayerrobin
Orc Shaman
Posts: 792
Joined: Wed Mar 18, 2009 3:03 am
x 452

Re: [13.5.3] Help converting a GLSL shader

Post by rpgplayerrobin »

Shaders take a lot of time to get into and understand completely. There is nothing that is not possible to do, and Blender do not have any specific features that you yourself cannot make in Ogre.

When I have problems with shaders I first check Ogre.log to see if there are any errors or warnings at all.
If there are no errors or warnings, I try to strip down the shader to as basic as possible and try adding a bit of it at a time.

If it is too hard to debug what is wrong with the shader that way, I recreate the shader in Direct311 and run the graphics debug in Visual Studio, which makes it possible to actually debug the shader line-by-line by selecting a pixel on the screen.
But since you are using Linux and GLSL shaders instead of HLSL, I don't know what kind of a graphics debugger you can use.

If I were you, I would alter the Blender shader in Blender to be as simple as possible, with just a color or something.
Then I would do the same for your GLSL shader you use in Ogre and see if it looks exactly the same as with the Blender shader in Blender.
From there, I would add small pieces of code to both at once and see if they look exactly the same.
There you will find your issue for sure.

tritonas00
Halfling
Posts: 89
Joined: Sun Apr 08, 2018 2:21 pm
x 42

Re: [13.5.3] Help converting a GLSL shader

Post by tritonas00 »

rpgplayerrobin wrote: Thu Jan 05, 2023 10:01 pm

If I were you, I would alter the Blender shader in Blender to be as simple as possible, with just a color or something.
Then I would do the same for your GLSL shader you use in Ogre and see if it looks exactly the same as with the Blender shader in Blender.
From there, I would add small pieces of code to both at once and see if they look exactly the same.
There you will find your issue for sure.

Yeah, considering to do that.

Also im not sure about what to use for depthSampler

rpgplayerrobin
Orc Shaman
Posts: 792
Joined: Wed Mar 18, 2009 3:03 am
x 452

Re: [13.5.3] Help converting a GLSL shader

Post by rpgplayerrobin »

I would skip that completely in both Blender and Ogre for now, just comment out that code.
Later you can add it back again when everything else works, but the depth texture might be different in Blender compared to Ogre, so I would do that as a last step.

More in detail on how to create the depth texture here:
viewtopic.php?p=552615#p552615
viewtopic.php?p=552905
viewtopic.php?p=552632

tritonas00
Halfling
Posts: 89
Joined: Sun Apr 08, 2018 2:21 pm
x 42

Re: [13.5.3] Help converting a GLSL shader

Post by tritonas00 »

Sounds good. i removed all texture samplers in the blender shader (just color):

Image

Why it still reflects though? :!:

Then added only the normal sampler (water-normal.png*):

Image

I will strip the fragment shader in blender to use only those, then will try to re-create that in Ogre, step by step.

*I take water-normal.png from the unity port repo though (https://github.com/muckSponge/Optically ... r/Textures) because i don't know how to export it from blender.

Edit

Ok it should be that

Image

tritonas00
Halfling
Posts: 89
Joined: Sun Apr 08, 2018 2:21 pm
x 42

Re: [13.5.3] Help converting a GLSL shader

Post by tritonas00 »

So i stripped down the fragment shader into blender to use only normalSampler calculations. No reflection, no refraction, no depth sampler, no cameraPos.

Image

With this, this is what i get into game:

Image

This is my material:

Code: Select all

vertex_program myVertexShader1 glsl
{
    source example1.vert

    default_params
    {
        param_named_auto ModelMatrix world_matrix
    }
}

fragment_program myFragmentShader1 glsl
{
    source example1.frag
}

material tracks/basicwater
{
    technique
    {
        pass
        {
            vertex_program_ref myVertexShader1
            {
            }
            fragment_program_ref myFragmentShader1
            {
                param_named_auto timer time
                param_named normalSampler int 0
            }

            texture_unit
            {
                texture water-normal.png
             //   tex_coord_set 0
             //   filtering linear linear linear
            }
        }
    }
}

I'm fairly sure i do something really stupid wrong here, because i can't even produce the color.
I will go with one single color, i feel like coords are just wrong.

Question
Should i use the blender shader into Ogre as is since its GLSL already?

tritonas00
Halfling
Posts: 89
Joined: Sun Apr 08, 2018 2:21 pm
x 42

Re: [13.5.3] Help converting a GLSL shader

Post by tritonas00 »

Finally some results! :D

By stripping to minimal found out that param_named_auto ModelMatrix world_matrix was wrong. It should be param_named_auto ModelMatrix worldviewproj_matrix. Normal sampler and animations do work now.

Same results:

Blender

Image

RoR:

Image

Next step: bring the original colors in, then refraction and reflection.

tritonas00
Halfling
Posts: 89
Joined: Sun Apr 08, 2018 2:21 pm
x 42

Re: [13.5.3] Help converting a GLSL shader

Post by tritonas00 »

It started to blend, still need to figure out some things.

tritonas00
Halfling
Posts: 89
Joined: Sun Apr 08, 2018 2:21 pm
x 42

Re: [13.5.3] Help converting a GLSL shader

Post by tritonas00 »

Brought reflection and refraction samplers in, i think their coords are wrong, but getting there :D

Wish i had more help from someone which is familiar with this stuff, instead of going blindly here :?

Reflection and refraction textures move with the camera. fragCoord is wrong?

Code: Select all

vec3 reflection = texture2D(reflectionSampler, fragCoord+(nVec.st*vec2(reflBump,reflBump*(6.0))*distortFade)).rgb;

fragCoord:

Code: Select all

vec2 fragCoord = (fragPos.xy/fragPos.w)*0.5+0.5;

fragPos:

Code: Select all

fragPos = ftransform();

nVec:

Code: Select all

vec3 nVec = tangentSpace(normal*bump);

tangentSpace

Code: Select all

vec3 tangentSpace(vec3 v)
{
	vec3 vec;
	vec.xy=v.xy;
	vec.z=sqrt(1.0-dot(vec.xy,vec.xy));
	vec.xyz= normalize(vec.x*T+vec.y*B+vec.z*N);
	return vec;
}

T B N

Code: Select all

mat3 m3( mat4 m )
{
	mat3 result;
	
	result[0][0] = m[0][0]; 
	result[0][1] = m[0][1]; 
	result[0][2] = m[0][2]; 

	result[1][0] = m[1][0]; 
	result[1][1] = m[1][1]; 
	result[1][2] = m[1][2]; 
	
	result[2][0] = m[2][0]; 
	result[2][1] = m[2][1]; 
	result[2][2] = m[2][2]; 
	
	return result;
}

T   = m3(ModelMatrix)*Tangent.xyz;
B   = m3(ModelMatrix)*cross(gl_Normal, Tangent.xyz);
N   = m3(ModelMatrix)*gl_Normal; 

and ModelMatrix is worldviewproj_matrix

rpgplayerrobin
Orc Shaman
Posts: 792
Joined: Wed Mar 18, 2009 3:03 am
x 452

Re: [13.5.3] Help converting a GLSL shader

Post by rpgplayerrobin »

Impossible to tell if that specific variable is wrong or not from here.
Debug that variable by outputting it as a color. Then do the same in the Blender shader and see if it has the same results or not.
Each variable can be tested this way in both Ogre and Blender, to be able to find the differences and why they happen.

tritonas00
Halfling
Posts: 89
Joined: Sun Apr 08, 2018 2:21 pm
x 42

Re: [13.5.3] Help converting a GLSL shader

Post by tritonas00 »

rpgplayerrobin wrote: Fri Jan 13, 2023 12:32 am

Impossible to tell if that specific variable is wrong or not from here.
Debug that variable by outputting it as a color. Then do the same in the Blender shader and see if it has the same results or not.
Each variable can be tested this way in both Ogre and Blender, to be able to find the differences and why they happen.

Welp, worldPos and viewPos in the vertex shader produce different color order in RoR vs blender, though changing

Code: Select all

worldPos = vec3(ModelMatrix*gl_Vertex);

to

Code: Select all

worldPos = vec3(gl_MultiTexCoord0);

seems to works fine in RoR (without reflaction/ refraction) and it is the same the unity port did https://github.com/muckSponge/Optically ... shader#L35

fragPos seem to produce the same results in both RoR and blender.

this is how RoR does it https://github.com/RigsOfRods/rigs-of-r ... g#L99-L102 and that works as expected but im not sure if i need to do it like that or fix/modify the GLSL shader to use proper coords, which i'm also not sure how to do.

Code: Select all

    //texture edge bleed removal
    float fade = 12.0;
    vec2 distortFade = vec2(0.0);
    distortFade.s = clamp(fragCoord.s*fade,0.0,1.0);
    distortFade.s -= clamp(1.0-(1.0-fragCoord.s)*fade,0.0,1.0);
    distortFade.t = clamp(fragCoord.t*fade,0.0,1.0);
    distortFade.t -= clamp(1.0-(1.0-fragCoord.t)*fade,0.0,1.0); 
    

    vec3 reflection = texture2D(reflectionSampler, fragCoord+(nVec.st*vec2(reflBump,reflBump*(6.0))*distortFade)).rgb;

and debugging only that:

Code: Select all

gl_FragColor = vec4(vec3(reflection),1.0);

the problem is it moves with the camera:

which probably means fragCoord is wrong

Code: Select all

vec2 fragCoord = (fragPos.xy/fragPos.w)*0.5+0.5;

which probably means fragPos

Code: Select all

fragPos = ftransform();

is wrong

rpgplayerrobin
Orc Shaman
Posts: 792
Joined: Wed Mar 18, 2009 3:03 am
x 452

Re: [13.5.3] Help converting a GLSL shader

Post by rpgplayerrobin »

As I am not that good with GLSL (I only do HLSL shaders for my game), I am not sure what can be wrong.

But with a quick google search, the function ftransform seems to be something ancient and uses some kind of built-in matrix.
Maybe you should not use that function anymore and instead use the matrix supplied from Ogre.

tritonas00
Halfling
Posts: 89
Joined: Sun Apr 08, 2018 2:21 pm
x 42

Re: [13.5.3] Help converting a GLSL shader

Post by tritonas00 »

rpgplayerrobin wrote: Fri Jan 13, 2023 4:32 pm

As I am not that good with GLSL (I only do HLSL shaders for my game), I am not sure what can be wrong.

But with a quick google search, the function ftransform seems to be something ancient and uses some kind of built-in matrix.
Maybe you should not use that function anymore and instead use the matrix supplied from Ogre.

Based on viewtopic.php?t=66656 tried

Code: Select all

worldviewproj_matrix * vertex

same result. This is getting harder... :?

If i manage at least to get proper reflection/refraction coords (meaning not moving with the camera) then probably this thing will stabilize and then configure to the desired result.

I can get stable result without reflection/refraction though. Fairly sure the vertex shader is wrong.

Last edited by tritonas00 on Sun Jan 22, 2023 10:10 pm, edited 1 time in total.
rpgplayerrobin
Orc Shaman
Posts: 792
Joined: Wed Mar 18, 2009 3:03 am
x 452

Re: [13.5.3] Help converting a GLSL shader

Post by rpgplayerrobin »

So you get different results in Blender compared to Ogre even though all variables seems to have the same data when outputting them as colors?

I don't know the full GLSL code so I don't really know (and it might still be hard since I am only experienced in HLSL) but I guess the reflection map uses an UV that is a projection matrix.

That UV in the fragment shader might need to be flipped, I use this code in HLSL to flip it:

Code: Select all

float2 projectionUV = projectionCoord.xy / projectionCoord.w;
projectionUV.y = (1.0 - saturate(renderTargetFlipping)) + renderTargetFlipping * projectionUV.y;

"projectionCoord" is the float4 coordinates from the vertex shader.
"renderTargetFlipping" is defined as "float renderTargetFlipping;" in the fragment shader, which comes from the material with "param_named_auto renderTargetFlipping render_target_flipping".

You could also take code from the Ogre sample to get the reflection working.

tritonas00
Halfling
Posts: 89
Joined: Sun Apr 08, 2018 2:21 pm
x 42

Re: [13.5.3] Help converting a GLSL shader

Post by tritonas00 »

rpgplayerrobin wrote: Fri Jan 13, 2023 10:14 pm

So you get different results in Blender compared to Ogre even though all variables seems to have the same data when outputting them as colors?

Almost all of them produce different color sequence (black, yellow, green, red) in blender and ror, only fragPos produce the same colors, makes sense because its ftransform(). The rest, worldPos and viewPos heavily rely on ogre input because of ModelMatrix which i assigned worldviewproj_matrix to it, through the material. I managed to fix worldPos by setting it to gl_MultiTexCoord0 and that helped to bring the normal sampler to life.

rpgplayerrobin wrote: Fri Jan 13, 2023 10:14 pm

I don't know the full GLSL code so I don't really know (and it might still be hard since I am only experienced in HLSL) but I guess the reflection map uses an UV that is a projection matrix.

That UV in the fragment shader might need to be flipped, I use this code in HLSL to flip it:

Code: Select all

float2 projectionUV = projectionCoord.xy / projectionCoord.w;
projectionUV.y = (1.0 - saturate(renderTargetFlipping)) + renderTargetFlipping * projectionUV.y;

"projectionCoord" is the float4 coordinates from the vertex shader.
"renderTargetFlipping" is defined as "float renderTargetFlipping;" in the fragment shader, which comes from the material with "param_named_auto renderTargetFlipping render_target_flipping".

You could also take code from the Ogre sample to get the reflection working.

I posted the original GLSL code in the second post, here it is https://pastebin.com/raw/6LsewwQY. But yes i probably need to do reflection/refraction like RoR fresnel sample does it, which is cg though and i have some difficulty to convert it to GLSL.

I tried in the vertex shader

Code: Select all

varying vec4 projectionCoord;
in main():
projectionCoord = gl_MultiTexCoord1;

and then in the fragment shader:

Code: Select all

varying vec4 projectionCoord;
in main():
vec2 final = projectionCoord.xy / projectionCoord.w;

and applied the reflection to that instead of fragPos, didn't work out :/

tritonas00
Halfling
Posts: 89
Joined: Sun Apr 08, 2018 2:21 pm
x 42

Re: [13.5.3] Help converting a GLSL shader

Post by tritonas00 »

Did another try in the Fresnel sample, using it's reflection/refraction maps. Not the expected results again. :(

Considering to give up at this point.

rpgplayerrobin here is the unity variant also (i think it is HLSL) if you want to give it a try. https://github.com/muckSponge/Optically ... shader#L40

Last edited by tritonas00 on Sun Jan 22, 2023 10:11 pm, edited 1 time in total.