Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vertex attributes not being passed to shader

I've just switched my code over to using a separate shader instead of passing a boolean uniform to decide which algorithm to use. Unfortunately, after vigorous testing, I've discovered that one of the attributes (halo) is not being passed through the the new shader. The other attribute it uses (position) is passed through, though.

Abdridged code follows:

Java code:
    // Attributes
    protected static int position = 0;
    protected static int colour = 1;
    protected static int texture = 2;
    protected static int halo = 3;
    protected static int normal = 4;

    protected static int program1;
    protected static int program2;

    ...

    // Linking shader1
    GLES20.glBindAttribLocation(program1, position, "position");
    GLES20.glBindAttribLocation(program1, colour, "colour");
    GLES20.glBindAttribLocation(program1, texture, "texCoord");
    GLES20.glBindAttribLocation(program1, normal, "normal");

    GLES20.glLinkProgram(program1);         

    ...

    // Linking shader2
    GLES20.glBindAttribLocation(program2, position, "position");
    GLES20.glBindAttribLocation(program2, halo, "halo");

    GLES20.glLinkProgram(program2);         

    ...


    GLES20.glUseProgram(program1);

    GLES20.glVertexAttribPointer(
        position,
        3,
        GLES20.GL_FLOAT,
        false,
        0,
        buffer);

    ...

    //Render with program1

    ...

    GLES20.glUseProgram(program2);

    GLES20.glVertexAttribPointer(
        halo,
        1,
        GLES20.GL_FLOAT,
        false,
        0,
        doHaloBuffer);

    GLES20.glEnable(GLES20.GL_BLEND);
    GLES20.glDisable(GLES20.GL_DEPTH_TEST);

    ...

    // Using lines for testing purposes
    GLES20.glDrawElements(GLES20.GL_LINE_LOOP, haloIndexCount, GLES20.GL_UNSIGNED_SHORT, haloIndexBuffer);

    ...

Fragment shaders are just simple "Render the texture and colour you get" shaders

shader1.vsh:
    attribute vec3 position;
    attribute vec4 colour;
    attribute vec2 texCoord;
    attribute vec3 normal;

    ...

    varying vec2 fragTexCoord;
    varying vec4 fragColour;

    ...

    // All attributes used at some point


shader2.vsh:
    attribute vec3 position;
    attribute float halo;

    varying vec4 fragColour;

        ...
        vec4 colour = vec4(1.0, 1.0, 0.0, 1.0);

        if(halo > 0.5){
            colour.g = 0.0;

        ...

        }

        fragColour = colour;

        ...

If i change halo > 0.5 to halo == 0.0 or swap the green values in the above statements, red is rendered otherwise yellow is rendered. I tried altering the input buffer to be all 1.0 for testing but it made no difference. It seems that halo is not being passed through.

Previously, I had the two shaders merged and had a boolean uniform to decide which code to run and it worked fine. Nothing else has changed; the input buffers are the same, the counts are the same it's just that I'm using separate shaders now that is different. Any thoughts?

like image 597
Ben Jaguar Marshall Avatar asked Jan 19 '26 09:01

Ben Jaguar Marshall


1 Answers

check if the halo attribute is enabled just before rendering with glDrawElements

like image 144
Majid Max Avatar answered Jan 21 '26 00:01

Majid Max



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!