opengl-esglslesopengl-es-3.1

Trouble with AMD OpenGL ES SDK 3.1 Fragment Shader


The fragment shader in the AMD OpenGL ES SDK for 3.1 (as of 5/13/2016) is this:

precision highp float;
uniform vec4 lightVec;
uniform sampler2D textureUnit0;
varying vec2 vTexCoord;
varying vec3 vNormal;
void main()
{
    vec4 diff = vec4(dot(lightVec.xyz,normalize(vNormal)).xxx, 1);
    gl_FragColor =  diff * texture(textureUnit0, vTexCoord);
}

Which is is flat out wrong. The closest I have gotten to correcting it is:

precision highp float;
uniform vec4 lightVec;
uniform sampler2D textureUnit0;
varying vec2 vTexCoord;
varying vec3 vNormal;
void main()
{
    vec4 diff = vec4(dot(lightVec.xyz,normalize(vNormal)).xxx, 1);
    gl_FragColor =  diff * texture2D(textureUnit0, vTexCoord);
}

Which is just turning the texture into texture2D on the last line.

No clue what is going on with trying to compute the diffuse shader. It's using dot to get a float, and calling a swizzle (I think that's what you call the .xxx operator) on the float to try and duplicate it?

How does someone learn more about shaders easily? It seems like a very tough area to become skilled at.


Solution

  • Are you saying that this line:

    vec4 diff = vec4(dot(lightVec.xyz,normalize(vNormal)).xxx, 1);
    

    Doesn't compile because of the swizzle on the result of the dot? Try:

    float fDot = dot(lightVec.xyz,normalize(vNormal));
    vec4 diff = vec4(fDot, fDot, fDot, 1);
    

    As for learning to write shaders better, I'd search up for tutorials on any flavour of GLSL and see if you can find one you like, the desktop versions are different but not that different, and you'll be able to find a better tutorial if you don't restrict your search to one specific revision.

    Then arm yourself with the GLSLES 3.1 spec so you can refer to anything that you don't understand/can't translate to your chosen dialect. And get the PowerVR shader tools or something similar which lets you type shader code and get instant feedback about whether it compiles and how many cycles it takes.