glTF2/PBR with Displacement map, possible?

Problems building or running the engine, queries about how to use features etc.
User avatar
Arthurfernandes
Halfling
Posts: 48
Joined: Mon Oct 28, 2024 10:50 pm
x 7

glTF2/PBR with Displacement map, possible?

Post by Arthurfernandes »

Ogre Version: 14.3.4
Operating System: Windows 10
Render System: DirectX 11

Hey everyone,

I'm currently exploring Ogre a bit deeper, especially around materials. I found the PBR example in the SampleBrowser and started applying it to all my entities. For testing purposes, I'm using every map available in the material, and so far everything is working great.

However, the last thing I'm trying to implement is a Displacement Map, and that's where things are not going so well. I only understand the basics of shaders, so I've been using ChatGPT to help me through the process.

Here are the changes it suggested:

Changes to .material file:

  • At the beginning:

Code: Select all

set $Displacement white.png
set $DisplacementScale 0.05
  • In the vertex_program_ref glTF2/PBR_vs block:

Code: Select all

param_named      u_DisplacementScale float $DisplacementScale
param_named      u_DisplacementSampler int 8
preprocessor_defines HAS_NORMALS;HAS_UV;HAS_TANGENTS

Changes to pbr-vert.glsl:

  • In OGRE_UNIFORMS:

Code: Select all

uniform sampler2D u_DisplacementSampler;
uniform float u_DisplacementScale;
  • In the vertex shader's main function (MAIN_DECLARATION):

Code: Select all

MAIN_DECLARATION
{
  vec4 pos = mul(u_ModelMatrix, position);
  v_Position = vec3(pos.xyz) / pos.w;

  #ifdef HAS_NORMALS
  #ifdef HAS_TANGENTS
  vec3 normalW = normalize(mul(u_ModelMatrix, vec4(normal.xyz, 0.0)).xyz);
  vec3 tangentW = normalize(mul(u_ModelMatrix, vec4(tangent.xyz, 0.0)).xyz);
  vec3 bitangentW = cross(normalW, tangentW) * tangent.w;
  v_TBN = mtxFromCols(tangentW, bitangentW, normalW);
  #else // HAS_TANGENTS != 1
  v_Normal = normalize(vec3(mul(u_ModelMatrix, vec4(normal.xyz, 0.0))));
  v_Normal = normalW;
  #endif
  #endif

  #ifdef HAS_UV
  v_UV = uv0;
  #else
  v_UV = vec2(0.,0.);
  #endif
  
vec4 worldPos = mul(u_ModelMatrix, position); #ifdef HAS_DISPLACEMENTMAP float displacement = texture(u_DisplacementSampler, v_UV).r; worldPos.xyz += normalW * (displacement * u_DisplacementScale); #endif gl_Position = mul(u_MVPMatrix, worldPos); }

After all these changes, when I try to run the application I get this error:

Code: Select all

Pass 0: vertex program glTF2/PBR_vs cannot be used - compile error

Does anyone know what might be causing this and how to fix it?

Notes:
I verified that HAS_DISPLACEMENTMAP is defined in the .program file.

The texture unit "Displacement" is added to the material.

Shader code seems to compile fine in other contexts.

Thanks in advance for any help

rpgplayerrobin
Orc Shaman
Posts: 734
Joined: Wed Mar 18, 2009 3:03 am
x 409

Re: glTF2/PBR with Displacement map, possible?

Post by rpgplayerrobin »

Where that error appears in the code (or in Ogre.log), there must be more information about why it does not compile.

As I do not use RTSS I am not sure exactly what is wrong here, but the only thing I can think of is that you are using normalW in the shader even though it is not certain that the shader has a normalW variable.
Try this instead:

Code: Select all

  #ifdef HAS_NORMALS
  #ifdef HAS_TANGENTS
  #ifdef HAS_DISPLACEMENTMAP
  float displacement = texture(u_DisplacementSampler, v_UV).r;
  worldPos.xyz += normalW * (displacement * u_DisplacementScale);
  #endif
  #endif
  #endif
paroj
OGRE Team Member
OGRE Team Member
Posts: 2156
Joined: Sun Mar 30, 2014 2:51 pm
x 1157

Re: glTF2/PBR with Displacement map, possible?

Post by paroj »

the RTSS supports all variants of that, including POM since 14.0. Take a look:
https://www.ogre3d.org/2023/07/08/ogre- ... on-mapping

User avatar
Arthurfernandes
Halfling
Posts: 48
Joined: Mon Oct 28, 2024 10:50 pm
x 7

Re: glTF2/PBR with Displacement map, possible?

Post by Arthurfernandes »

paroj wrote: Sun May 11, 2025 10:59 pm

the RTSS supports all variants of that, including POM since 14.0. Take a look:
https://www.ogre3d.org/2023/07/08/ogre- ... on-mapping

Is there any example showing how to use it?

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

Re: glTF2/PBR with Displacement map, possible?

Post by paroj »

the screenshots show the bumpmapping sample ;)

rpgplayerrobin
Orc Shaman
Posts: 734
Joined: Wed Mar 18, 2009 3:03 am
x 409

Re: glTF2/PBR with Displacement map, possible?

Post by rpgplayerrobin »

It seems it is integrated into RTSS, here is an example material for it:

Code: Select all

// Steep parallax occlusion mapping
material RTSS/ParallaxOcclusionMapping
{
   technique
   {
       pass
       {
           specular 1 1 1 32

       // Base diffuse texture map
       texture_unit
       {
          texture Bricks076C_diffspec.dds
       }

       texture_unit
       {
           texture Bricks076C_normheight.dds
           rtshader_system
           {
              normal_map parallax_occlusion
           }
       }
   }
   }
}

But your scenario has nothing to do with this of course, since parallax mapping is done in the fragment shader, while yours try to adjust the height of a vertex in the vertex shader (essentially from a heightmap texture).

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

Re: glTF2/PBR with Displacement map, possible?

Post by paroj »

POM takes the heightmap into account in the fragment shader. As the geometry is not altered (as this would require a high tesselation) it is technically not the same, but looks very similar.

User avatar
Arthurfernandes
Halfling
Posts: 48
Joined: Mon Oct 28, 2024 10:50 pm
x 7

Re: glTF2/PBR with Displacement map, possible?

Post by Arthurfernandes »

paroj wrote: Tue May 13, 2025 11:37 pm

POM takes the heightmap into account in the fragment shader. As the geometry is not altered (as this would require a high tesselation) it is technically not the same, but looks very similar.

I saw this, but is there any way to perform real displacement in Ogre — as in actually altering geometry based on a displacement map?

rpgplayerrobin
Orc Shaman
Posts: 734
Joined: Wed Mar 18, 2009 3:03 am
x 409

Re: glTF2/PBR with Displacement map, possible?

Post by rpgplayerrobin »

I would not rely on Ogre being able to do everything, your first post in this thread was logical and I would continue with that if I were you.

You must check why the compile error happens, since that error message that you wrote does not help much.
Every time a compile error happens for me I get the full detail in the ogre log and/or as an error message that pops up when it fails to compile a shader.
If that does not happen for you for some reason, you must go into the source file and put a breakpoint at where the compilation fails (simply search for the error message shown).

Also, make sure to not use a shader cache while you are doing your tests.
I delete my shader cache file each time I change a shader in any way, otherwise it can sometimes create bugs.

Did you try changing the shader to what I wrote in my first reply and see if the compile error still happens?