jPCT-AE - a 3d engine for Android => Support => Topic started by: lawless_c on July 28, 2015, 01:58:32 am

Title: Unusual reproducible bug with shaders glsl
Post by: lawless_c on July 28, 2015, 01:58:32 am
I've managed to produce a bug using glsl which will function for the Object3d it is applied to but cause errors elsewhere in the program.

For context i was using a lighting shader i found elsewhere

I modified it to work with glsl in jpct.
While using the version i created i made a call at the start of the fragment shader to

Code: [Select]
uniform [b]vec4[/b] specularColors[8];
Obviously i was going outside the Jpct spec you have here: http://www.jpct.net/jpct-ae/doc/com/threed/jpct/GLSLShader.html
Which says it should be a vec3.

If you use it the shader still works seemingly perfectly, and the object3d looks how i want it to.
However i've found it breaks another part the  program.

I was using Andre Silva's particle system https://www.youtube.com/watch?v=cn-iS4YnqnE which worked perfectly until i tried using the new seemingly unrelated shader.
It would successfully generate the first particle sprite but then crash as it attempts to generate a second particle from the same texture.

Producing this vague error  ": - ERROR: before: glError 1282"

Somehow using a vec4 instead of a vec3 causes this.

If it helps here are the shaders

The vertex shader

Code: [Select]
precision mediump float;

uniform mat4 modelViewMatrix;
uniform mat4 modelViewProjectionMatrix;
attribute vec4 position;
attribute vec3 normal;

varying float fogthickness;
varying vec3 norm;
varying vec4 eye;

void main() {

     vec3 n = normalize(modelViewMatrix * vec4(normal, 0.0)).xyz;

     norm = n;
     eye= -modelViewMatrix[3];

     fogthickness = 1.0-     dot(normalize(eye.xyz),n );

     gl_Position =  modelViewProjectionMatrix *position; //* vec4(position,1.0);

The fragment shader which causes the bug.

Code: [Select]

precision mediump float;

uniform vec3 atmosphereColour;

uniform vec4 specularColors[8];

uniform vec3 lightPositions[8];

varying vec3 norm;
varying vec4 eye;
varying float fogthickness;

void main() {

    vec4 spec = vec4(0.0);

        // normalize both input vectors
        vec3 n = normalize(norm);
        vec3 e = normalize(vec3(eye));
        vec3 ligthDirection = normalize(lightPositions[0]);

        float intensity = max(dot( n,ligthDirection), 0.0);
        float shininess= 1.0;

        // if the vertex is lit compute the specular color
        if (intensity > 0.0) {
            // compute the half vector
            vec3 h = normalize(ligthDirection + e);
            // compute the specular term into spec
            float intSpec = max(dot(h,n), 0.0);
            spec = specularColors[0] * pow(intSpec,shininess);
      vec4  colorout = max(intensity *  vec4(atmosphereColour,1) + spec, vec4(0.0));

    gl_FragColor = vec4( colorout.xyz , fogthickness);

I have a hypothesis that it's because the shader will compile perfectly but when it's run only receives vec3 specularColor from Jpct itself when it expects a vec4.
Title: Re: Unusual reproducible bug with shaders glsl
Post by: EgonOlsen on July 28, 2015, 07:01:27 pm
It might be a problem with the particle system itself. Does it fiddle around with GL commands directly? OpenGL doesn't report errors, you have to query for them. One of the parts that does this in jPCT is the shader code. So maybe your error happens elsewhere but shows when creating the shader. I suggest to put of error checking code around your particle stuff to see if that reports anything.
Title: Re: Unusual reproducible bug with shaders glsl
Post by: EgonOlsen on July 28, 2015, 07:05:26 pm
...and I don't think that the vec4 is the problem. I expect the last value to stay undefined. And why don't you use vec3 instead. You can make it an vec4 in the shader if you have to.
Title: Re: Unusual reproducible bug with shaders glsl
Post by: lawless_c on July 28, 2015, 07:17:31 pm
I'm using a vec3 now  :D , i was only using a vec4 because the example i was working from did.

It's no longer an issue for me but i thought you'd want to know about it.

I really have no idea of the cause, just that when i use vec4 it crashes and when i use vec3 it's fine.

Not sure how the particle system works.