[solved] simple vertex shader with RTSShader System

Problems building or running the engine, queries about how to use features etc.
Post Reply
paul424
Goblin
Posts: 235
Joined: Thu May 24, 2012 7:16 pm
x 5

[solved] simple vertex shader with RTSShader System

Post by paul424 »

Ogre Version: 1.12.9 :?:
Operating System: Linux OpenSuse :?:
Render System: OpenGL :?:

Hi, I try to write a simple distortion vetex shader for Opendungeons game https://github.com/tomluchowski/OpenDun ... ntal-merge , I have simple snippet to start with :

SimpleShader.glsl :

Code: Select all

in vec3 position;
void main() {
gl_Position = projection * view * model * vec4(position, 1.0);
}
How do I pinpoint it to say Gold.mesh, what I assume I should somehow change the Gold.material file

Code: Select all

// Gold genrated by blender2ogre 0.6.0
import RTSS/NormalMapping_MultiPass from "RTShaderSystem.material"
material Gold : RTSS/NormalMapping_MultiPass
{
    receive_shadows on

    technique
    {
        pass lighting
        {
            // Override the normal map.
            rtshader_system
            {
                lighting_stage normal_map GoldNormal.png tangent_space 0 bilinear 1 -1.0
            }
        }

        pass decal
        {
            ambient 0.8 0.8 0.8 1.0
            diffuse 0.8 0.8 0.8 1.0
            specular 0.2 0.2 0.2 0.0 2.0
            emissive 0.1 0.1 0.1 0.0

            texture_unit decalmap
            {
                texture Gold.png
                tex_address_mode wrap
                scale 1.0 1.0
                colour_op modulate
            }
        }
    }
}
Please please help me :)
EDIT: Here I found how to manually load vertex and fragment shaders : but that 's not what I do need, right ?
Last edited by paul424 on Thu Jan 14, 2021 9:06 pm, edited 5 times in total.

paroj
OGRE Team Member
OGRE Team Member
Posts: 1298
Joined: Sun Mar 30, 2014 2:51 pm
x 556
Contact:

Re: simple vertex shader with RTSShader System

Post by paroj »

paul424 wrote:
Wed Dec 30, 2020 10:56 am
EDIT: Here I found how to manually load vertex and fragment shaders , but that 's not what I do need, right ?
depending on what you want to do, this would be the way to go.

The RTSS is not meant to handle "simple" shaders, but rather to inject shader code into the default rendering pipeline. E.g. changing how fog is applied to the surface as defined by .material files.

See:
- https://www.ogre3d.org/2020/02/07/rtss- ... e-ogre-way
- https://ogrecave.github.io/ogre/api/1.1 ... s_overview

paul424
Goblin
Posts: 235
Joined: Thu May 24, 2012 7:16 pm
x 5

Re: simple vertex shader with RTSShader System

Post by paul424 »

Hmm the following shader

Code: Select all

#version 330 core
layout (location = 0) in vec4 aPos;

layout (std140) uniform Matrices
{
    mat4 projection;
    mat4 view;
    mat4 model;
};
float freq = 3.14;
void main()
{
    vec4 worldPos = model * aPos;
    worldPos.z += sin((worldPos.x + worldPos.y ) * freq);
    gl_Position = projection * view * vec4(worldPos.xyz, 1.0);
}  
triggered with such code :

Code: Select all

void RenderManager::triggerDistortion()
{

    Ogre::HighLevelGpuProgramPtr vertex = Ogre::HighLevelGpuProgramManager::getSingletonPtr()->createProgram( "basicVS", Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME, "glsl", Ogre::GpuProgramType::GPT_VERTEX_PROGRAM);
    vertex->setSourceFile( "distortion.vert" );

    //Creating new material
    Ogre::MaterialPtr matPtr = Ogre::MaterialManager::getSingletonPtr()->getByName("Gold");

    Ogre::Pass* pass = matPtr->getTechnique(0)->getPass(0);

    // pass settings
    // pass->setLightingEnabled( false );
             
    pass->setVertexProgram( vertex->getName() );

}
DOESN"T AFFECT ANY GOLD TILE !

What is wrong with that code ?

paroj
OGRE Team Member
OGRE Team Member
Posts: 1298
Joined: Sun Mar 30, 2014 2:51 pm
x 556
Contact:

Re: simple vertex shader with RTSShader System

Post by paroj »

probably you dont assign it anywhere. Also look out for log messages. The shader you specified requires GL3+.
See, this for how to get parameters in your shader:
https://ogrecave.github.io/ogre/api/lat ... Parameters

paul424
Goblin
Posts: 235
Joined: Thu May 24, 2012 7:16 pm
x 5

Re: simple vertex shader with RTSShader System

Post by paul424 »

First thing first, " The shader you specified requires GL3+."

well I cannot start Opendungeons with GL3Plus, all I get is :
RT Shader System not available but trying to render using OpenGL 3.X+ Core.When GLSL shaders are necessary, the RT Shader System component of Ogre is required for rendering CEGUI.
I've compiled CEGUI and ogre3d with all bells and whistles. Including for CEGUI : GL3Plus support , and ogre3d GL3Plus support plus support for RT Shader System , but it won't work .

How do I modify my program so it won't need GL3Plus, that is the first question.

paroj
OGRE Team Member
OGRE Team Member
Posts: 1298
Joined: Sun Mar 30, 2014 2:51 pm
x 556
Contact:

Re: simple vertex shader with RTSShader System

Post by paroj »

replace "#version 330 core" by "#version 120"

paul424
Goblin
Posts: 235
Joined: Thu May 24, 2012 7:16 pm
x 5

Re: simple vertex shader with RTSShader System

Post by paul424 »

Finally I plunged the vertex shader to work ; however with the simplest computing vertexes from model to projection space I get strange artifacts :

Image

Code: Select all

#version 120 

 in vec4 aPos;

uniform    mat4 worldMatrix;
uniform    mat4 projectionMatrix;
uniform    mat4 viewMatrix;


float freq = 3.14;
void main()
{
    vec4 worldPos = worldMatrix * aPos;
//    worldPos.z += sin((worldPos.x + worldPos.y ) * freq);
    gl_Position = projectionMatrix * viewMatrix * vec4(worldPos.xyz, 1.0);
}  
EDIT: and here's the Gold.material as well :

Code: Select all

// Gold genrated by blender2ogre 0.6.0
import RTSS/NormalMapping_MultiPass from "RTShaderSystem.material"

vertex_program myVertexShader glsl
{
  source distortion.glsl
    default_params
    {
        param_named_auto projectionMatrix projection_matrix
        param_named_auto viewMatrix view_matrix
        param_named_auto worldMatrix world_matrix
    }
  
}


material Gold : RTSS/NormalMapping_MultiPass
{
    receive_shadows on

    technique
    {

        pass lighting
        {
            // Override the normal map.
            rtshader_system
            {
                lighting_stage normal_map GoldNormal.png tangent_space 0 bilinear 1 -1.0
            }
        }

        pass decal
        {
            ambient 0.8 0.8 0.8 1.0
            diffuse 0.8 0.8 0.8 1.0
            specular 0.2 0.2 0.2 0.0 2.0
            emissive 0.1 0.1 0.1 0.0

            texture_unit decalmap
            {
                texture Gold.png
                tex_address_mode wrap
                scale 1.0 1.0
                colour_op modulate
            }
        }
        pass zero
        {
            vertex_program_ref myVertexShader
            {
                
                
                
            }
        }
    }
}

paul424
Goblin
Posts: 235
Joined: Thu May 24, 2012 7:16 pm
x 5

Re: simple vertex shader with RTSShader System

Post by paul424 »

It looks like two surfaces fighting, when I use vertex shader do I need necesserily a custom fragment shader ?

paroj
OGRE Team Member
OGRE Team Member
Posts: 1298
Joined: Sun Mar 30, 2014 2:51 pm
x 556
Contact:

Re: simple vertex shader with RTSShader System

Post by paroj »

- for GL3+ you must define a fragment shader as well.
- each "pass" sections defines a surface
- see https://ogrecave.github.io/ogre/api/lat ... attributes

paul424
Goblin
Posts: 235
Joined: Thu May 24, 2012 7:16 pm
x 5

Re: simple vertex shader with RTSShader System

Post by paul424 »

Putting aside the vertex shader quest, what do I need to start OpenDungeons with Gl3Plus renderer ?

paul424
Goblin
Posts: 235
Joined: Thu May 24, 2012 7:16 pm
x 5

Re: simple vertex shader with RTSShader System

Post by paul424 »

Also , I finally plunged the vertex and fragment shader to work :
ANd I get such artifacts --- it's because the mesh isn't too dense, in fact the square one is only two triangles....
Any ideas how do I make the mesh denser without starting the blender modeler application ?

Image

paroj
OGRE Team Member
OGRE Team Member
Posts: 1298
Joined: Sun Mar 30, 2014 2:51 pm
x 556
Contact:

Re: simple vertex shader with RTSShader System

Post by paroj »

paul424 wrote:
Sat Jan 02, 2021 6:02 pm
Putting aside the vertex shader quest, what do I need to start OpenDungeons with Gl3Plus renderer ?
Image

when using the snapcraft branch of https://github.com/paroj/OpenDungeons

paroj
OGRE Team Member
OGRE Team Member
Posts: 1298
Joined: Sun Mar 30, 2014 2:51 pm
x 556
Contact:

Re: simple vertex shader with RTSShader System

Post by paroj »

paul424 wrote:
Sat Jan 02, 2021 7:48 pm
ANd I get such artifacts --- it's because the mesh isn't too dense, in fact the square one is only two triangles....
Any ideas how do I make the mesh denser without starting the blender modeler application ?
you could make sure that adjacent edges get the same sine parameters, so there are no seams in the first place.

paul424
Goblin
Posts: 235
Joined: Thu May 24, 2012 7:16 pm
x 5

Re: simple vertex shader with RTSShader System

Post by paul424 »

Thanks for all the answers, however the seams bug is caused but not enough dense mesh, I just tested ... Now I need only to attach some lighting pass to the gold model.

paul424
Goblin
Posts: 235
Joined: Thu May 24, 2012 7:16 pm
x 5

Re: simple vertex shader with RTSShader System

Post by paul424 »

Here is example ambient lighting from https://learnopengl.com/Lighting/Basic-Lighting , but the real question is how do I forward the ambientStrength value from the game code to the fragment shader ..... I suppose I need to do it in the material file ... but how ?

Code: Select all

void main()
{
//   float ambientStrength = 0.1;
    vec3 ambient = ambientStrength * lightColor;

    vec3 result = ambient * objectColor;
    FragColor = vec4(result, 1.0);
}  

paul424
Goblin
Posts: 235
Joined: Thu May 24, 2012 7:16 pm
x 5

Re: simple vertex shader with RTSShader System

Post by paul424 »

Do I need per-pixel lighting or vertex-based lighting ?
Should I break into ambient pass and diffuse + specular pass in my material ?
What about parallax rendering ( bumpmaping ) ?
I found this old snippets, written in Cg language which is now passe .... What elements of it are still valid ?

PerPixel Lighting : A simple, everytime accurate per-pixel cg shader for Ogre, right out of box http://wiki.ogre3d.org/PerPixel+Lighting
PerPixel Lighting II : Multipass and One-pass per-pixel lighting shaders PerPixel Lighting II http://wiki.ogre3d.org/PerPixel+Lighting+II
PerPixel Lighting With Offset(Parallax) Mapping : Shader in the Per-Pixel Lighting series http://wiki.ogre3d.org/PerPixel+Lightin ... 29+Mapping

paul424
Goblin
Posts: 235
Joined: Thu May 24, 2012 7:16 pm
x 5

Re: simple vertex shader with RTSShader System

Post by paul424 »

Oki , I ended up having this , learning from https://learnopengl.com/Advanced-Lighti ... al-Mapping

Code: Select all

// Gold genrated by blender2ogre 0.6.0
// import RTSS/NormalMapping_MultiPass from "RTShaderSystem.material"

vertex_program myVertexShader glsl
{
  source distortion.vert
    default_params
    {
        param_named_auto projectionMatrix projection_matrix
        param_named_auto viewMatrix view_matrix
        param_named_auto worldMatrix world_matrix
    }
  
}

fragment_program myFragmentShader glsl
{
  source distortion.frag
    default_params
    {
//          param_named_auto surfaceAmbient surfaceAmbient  1.0 1.0 1.0 1.0
         param_named_auto lightColour light_diffuse_colour 0 0 0  
         param_named_auto lightPos light_position 0 0 0 
    }
}



material Gold //: RTSS/NormalMapping_MultiPass
{
//     receive_shadows on

    technique
    {
        pass zero
        {
            
            vertex_program_ref myVertexShader
            {
                
            }
            fragment_program_ref myFragmentShader
            {
                param_named decalmap int 0
                param_named normalmap int 1
            }
 
            texture_unit 
            {
                texture Gold.png

            }
            texture_unit 
            {
                texture GoldNormal.png

            }     
        }
    }
}

Code: Select all

#version 330 core
uniform    mat4 projectionMatrix;
uniform    mat4 viewMatrix;
uniform    mat4 worldMatrix;
layout (location = 0) in vec4 aPos;
layout (location = 2) in vec3 aNormal;
layout (location = 14) in vec3 aTangent;

layout (location = 8) in vec2 uv_0;
layout (location = 9) in vec2 uv_1;
out vec2 out_UV0;
out vec2 out_UV1; 
out vec3 FragPos;

out mat3 TBN;


float freq = 3.14;
void main()
{
    vec4 worldPos = worldMatrix * aPos;
    worldPos.z *= (1 + sin((worldPos.x + 2*worldPos.y ) * freq)/8.0);
    vec3 T = normalize(vec3( worldMatrix * vec4(aTangent,   0.0)));

    vec3 N = normalize(vec3( worldMatrix * vec4(aNormal,    0.0)));
    vec3 B = cross(T,N);
    gl_Position = projectionMatrix * viewMatrix * vec4(worldPos.xyz, 1.0);
    FragPos = worldPos.xyz;
    TBN = mat3(T, B, N);
    out_UV0 = uv_0;
    out_UV1 = uv_1;
}  

Code: Select all

#version 330 core  
#extension GL_ARB_separate_shader_objects: enable
#extension GL_ARB_texture_rectangle: enable

uniform sampler2D myTexture;
uniform sampler2D m_NormalMap;
uniform vec4 surfaceAmbient;
uniform vec3 lightColour; 
uniform vec3 lightPos;
in vec2 out_UV0;
in vec2 out_UV1;
in vec3 FragPos;



in VS_OUT {
    mat3 TBN;
} fs_in;  
out vec4 color;

void main (void)  
{  
    vec3 texelColor = texture(myTexture, out_UV0).rgb;
    vec3 Normal = texture(m_NormalMap, out_UV1).rgb;

    Normal.xy = 2 * Normal.xy - (1.0,1.0);
    Normal = normalize(fs_in.TBN * Normal); 
    
    vec3 lightDir = normalize(lightPos - FragPos); 
    float diff = max(dot(Normal, lightDir), 0.0);
    vec3 diffuse = diff * lightColour; 
    vec3 result = (diffuse  /*+ surfaceAmbient.rgb*/  ) * texelColor;
    color = vec4(result.xyz, 1.0);
       
}    
1) it works only with Ogre Renderer GLSL, and not GLSL3+ ( with the second it crashes with frame (22:01:09) (StackTraceUnix) [CRITICAL] (StackTraceUnix.cpp:207) [bt]: (1) /usr/local/lib/OGRE/RenderSystem_GL3Plus.so.1.12.9 : Ogre::GLSLMonolithicProgram::buildGLUniformReferences()+0x3a [0x7fcd4c4b7b0a]
yuck... )


What does it make conformat to GLSL and not GLSL3+ ?

paul424
Goblin
Posts: 235
Joined: Thu May 24, 2012 7:16 pm
x 5

Re: [solved] simple vertex shader with RTSShader System

Post by paul424 »

I needed enable 'separate shader objects' and GLSL3+ works .....

Post Reply