[SOLVED]passing parameters to a glsl es shader?

A place for users of OGRE to discuss ideas and experiences of utilitising OGRE in their games / demos / applications.
Post Reply
pognibene
Gnoblar
Posts: 20
Joined: Thu Aug 24, 2006 1:16 pm

[SOLVED]passing parameters to a glsl es shader?

Post by pognibene »

Hi,
I'm trying to port a webgl shader picked from webgl/threejs (hence gles 2/glsl 100) in Ogre. This is a simple cubic mapping shader. My problem is that the sampling of the cubic texture in my fragment shader seems to always return a black color, and I can't figure out why. The shaders compile with no errors, and they should be ok as I just ran the shaders captured in the browser through a C++ preprocessor to remove all conditional parts. Something to do with the cubic_texture setup, but what? The textures are also loaded without errors :-(

Any help will be vastly appreciated :-)

Pascal


Sources follow:

material:

Code: Select all

vertex_program  pog/color090/vertex glsl
{
	source titi.vert
}

fragment_program pog/color090/fragment glsl
{
	source titi.frag
}

material pog/color090
{
  technique GLSL
  {
    pass
    {
        vertex_program_ref pog/color090/vertex
        {
            param_named_auto projectionMatrix projection_matrix
	    param_named_auto modelMatrix world_matrix
            param_named_auto modelViewMatrix worldview_matrix
            param_named_auto normalMatrix inverse_transpose_worldview_matrix
	    param_named_auto cameraPosition camera_position
            param_named useRefract bool false
            param_named refractionRatio float 0.5
        }

        fragment_program_ref pog/color090/fragment
        {
            param_named diffuse float3 1.0 1.0 1.0
	    param_named opacity float 1.0
	    param_named reflectivity float 1.0
	    param_named flipEnvMap float 1.0
            param_named combine int 1
            param_named envMap int 0
        }

        texture_unit
	{
		cubic_texture px.png nx.png py.png ny.png pz.png nz.png separateUV
		tex_address_mode clamp
		//tex_coord_set 0
	}
    }
  }
}
vertex shader:

Code: Select all

#version 100
precision highp float;
precision highp int;
uniform mat4 modelMatrix;
uniform mat4 modelViewMatrix;
uniform mat4 projectionMatrix;
uniform mat4 viewMatrix;
uniform mat3 normalMatrix;
uniform vec3 cameraPosition;
attribute vec3 position;
attribute vec3 normal;
attribute vec2 uv;
attribute vec2 uv2;
varying vec3 vReflect;
uniform float refractionRatio;
uniform bool useRefract;
void main()
{
    vec3 objectNormal;
    objectNormal = normal;
    vec3 transformedNormal = normalMatrix * objectNormal;
    vec4 mvPosition;
    mvPosition = modelViewMatrix * vec4 ( position, 1.0 );
    gl_Position = projectionMatrix * mvPosition;
    vec4 worldPosition = modelMatrix * vec4 ( position, 1.0 );
    vec3 worldNormal = mat3 ( modelMatrix[ 0 ].xyz, modelMatrix[ 1 ].xyz, modelMatrix[ 2 ].xyz ) * objectNormal;
    worldNormal = normalize ( worldNormal );
    vec3 cameraToVertex = normalize ( worldPosition.xyz - cameraPosition );
    if ( useRefract )
    {
        vReflect = refract ( cameraToVertex, worldNormal, refractionRatio );
    }
    else
    {
        vReflect = reflect ( cameraToVertex, worldNormal );
    }
}
fragment shader:

Code: Select all

#version 100
precision highp float;
precision highp int;
uniform mat4 viewMatrix;
uniform vec3 cameraPosition;
uniform vec3 diffuse;
uniform float opacity;
uniform float reflectivity;
uniform samplerCube envMap;
uniform float flipEnvMap;
uniform int combine;

varying vec3 vReflect;
void main()
{
    gl_FragColor = vec4 ( diffuse, opacity );
    float specularStrength;
    specularStrength = 1.0;
    vec3 reflectVec;
    reflectVec = vReflect;
    vec4 cubeColor = textureCube ( envMap, vec3 ( flipEnvMap * reflectVec.x, reflectVec.yz ) );
    if ( combine == 1 )
    {
        gl_FragColor.xyz = mix ( gl_FragColor.xyz, cubeColor.xyz, specularStrength * reflectivity );
    }
    else if ( combine == 2 )
    {
        gl_FragColor.xyz += cubeColor.xyz * specularStrength * reflectivity;
    }
    else
    {
        gl_FragColor.xyz = mix ( gl_FragColor.xyz, gl_FragColor.xyz * cubeColor.xyz, specularStrength * reflectivity );
    }
}
Last edited by pognibene on Wed Feb 19, 2014 4:36 pm, edited 1 time in total.
User avatar
c6burns
Beholder
Posts: 1512
Joined: Fri Feb 22, 2013 4:44 am
Location: Deep behind enemy lines
x 138

Re: passing parameters to a glsl es shader?

Post by c6burns »

Try using combinedUVW instead of separateUV
pognibene
Gnoblar
Posts: 20
Joined: Thu Aug 24, 2006 1:16 pm

Re: passing parameters to a glsl es shader?

Post by pognibene »

c6burns wrote:Try using combinedUVW instead of separateUV
Same result - color returned from the cubic sampler is always full black :-(
User avatar
c6burns
Beholder
Posts: 1512
Joined: Fri Feb 22, 2013 4:44 am
Location: Deep behind enemy lines
x 138

Re: passing parameters to a glsl es shader?

Post by c6burns »

Sorry, I'm avoiding actually trying to set up that shader. I've done enough shading work of my own in the last little while. Your shader looks fairly simple, so perhaps start from the environment map reflection example in the Ogre samples (Sample_CubeMapping). It has GLES2 versions of the shaders, and uses RTT to create a dynamic cube map, which is then rendered as a reflection off a shiny ogrehead :)
User avatar
Jabberwocky
OGRE Moderator
OGRE Moderator
Posts: 2819
Joined: Mon Mar 05, 2007 11:17 pm
Location: Canada
x 218
Contact:

Re: passing parameters to a glsl es shader?

Post by Jabberwocky »

Just in case it helps, there are some examples of using a cubic_texture with a glsl shader in the Ocean.material file, plus associated vert and frag files.
Image
pognibene
Gnoblar
Posts: 20
Joined: Thu Aug 24, 2006 1:16 pm

[FIXED] Re: passing parameters to a glsl es shader?

Post by pognibene »

Thank you all for your help. This pointed me in the right direction :-)
I replaced my cubic textures by the one from the Ogre gles example, and sure enough, it works a treat.
My black texture seemed to be a combination of 2 problems:
-combinedUVW works only with the first syntax for cubic texture, where only a single image name is given and Ogre will deduce the names for front, back, left, right, up and down, as per section 17 of the manual. I wanted to avoid the naming convention on the textures to use exactly the same material definition in Ogre and Threejs. Does not seem to be possible so I'll have to rename them.
-Threejs wants the textures in a different order than Ogre. I had to reorder them to get the expected result.

Best

Pascal
Post Reply