Shaders [Solved] Applying shader to coloured vertex buffer

DesArts

Member
Hi there. I'm sure this one's simple to those experienced with shaders and buffers.
The goal itself IS simple to explain.

I need to apply a fade shader I have (or create a new one to perform the same function) to a plain colour/alpha un-textured vertex buffer.

I'm not that great with either and my current stuff doesn't seem to work together.

I have a vertex format:
Code:
//plain colour shape format
    vertex_format_begin();
    vertex_format_add_position();  //position of vertex
    vertex_format_add_color();
    global.VFPlainColour = vertex_format_end();
And I'm creating a buffer with that format with a bunch of triangles, and I set the colour and alpha as I go through the points, naturally. It looks like this for each triangle:

Code:
//pos 1
    vertex_position(vertex_buffer, sx1, sy1);
    vertex_colour(vertex_buffer, colour, alpha);
                 
    //pos 2
    vertex_position(vertex_buffer, sx2, sy2);
    vertex_colour(vertex_buffer, colour, alpha);
                 
    //pos 3
    vertex_position(vertex_buffer, sx3, sy3);
    vertex_colour(vertex_buffer, colour, alpha);
When I apply any shader that tries to add a colour fade, the vertex buffer seems to become invisible, but draws perfectly fine when no shader is applied. If I try to do the same thing to a primitive it doesn't become invisible but it has no effect. The most basic version of the shader (which works with sprites, I haven't changed this one for any other purpose) is as follows:
Code:
//
// Vertex: apply colour fade
//
attribute vec3 in_Position;                  // (x,y,z)
attribute vec4 in_Colour;                    // (r,g,b,a)
attribute vec2 in_TextureCoord;              // (u,v)

varying vec2 v_vTexcoord;
varying vec4 v_vColour;

void main()
{
    vec4 object_space_pos = vec4( in_Position.x, in_Position.y, in_Position.z, 1.0);
    gl_Position = gm_Matrices[MATRIX_WORLD_VIEW_PROJECTION] * object_space_pos;
 
    v_vColour = in_Colour;
    v_vTexcoord = in_TextureCoord;
}
Code:
//
// Fragment: apply colour fade
//
uniform vec4 f_colour;
varying vec2 v_vTexcoord;
varying vec4 v_vColour;

void main()
{
    vec4 col = texture2D( gm_BaseTexture, v_vTexcoord );
    col.rgb = mix(col.rgb, f_colour.rgb, f_colour.a);
    gl_FragColor = v_vColour * col;
}
I also have a shader which DOES work for textured vertex buffers (but doesn't work for the above), the details of that system are below:
Code:
//global vertex format
    vertex_format_begin();
    vertex_format_add_position();  //position of vertex
    vertex_format_add_textcoord();    //texture coordinate of vertex
    vertex_format_add_custom(vertex_type_float4, vertex_usage_textcoord);  //used to wrap texture coordinates around the region defined below
    vertex_format_add_color();
    global.VFMainColour = vertex_format_end();
And the shader that works with it looks like:
Code:
//
// Vertex: textured vertex buffer with colour fade
//
    attribute vec3 in_Position;
 
    attribute vec4 in_Colour;                    // (r,g,b,a)
    attribute vec2 in_Textcoord0;
    attribute vec4 in_Textcoord1;
 
    varying vec2 v_vTexcoord;
    varying vec4 v_vRepeat;
    varying vec4 v_vColour;
 
    void main()
    {
        vec4 object_space_pos = vec4( in_Position.x, in_Position.y, in_Position.z, 1.0);
        gl_Position = gm_Matrices[MATRIX_WORLD_VIEW_PROJECTION] * object_space_pos;
     
        v_vColour = in_Colour;
     
        v_vTexcoord = in_Textcoord0;
        v_vRepeat = in_Textcoord1;
    }
Code:
//
// Fragment: textured vertex buffer with colour fade
//
    uniform vec4 f_colour;
    varying vec2 v_vTexcoord;
    varying vec4 v_vRepeat;
    varying vec4 v_vColour;
 
    void main()
    {
        vec2 UV = v_vTexcoord - floor( v_vTexcoord / v_vRepeat.xy - v_vRepeat.zw ) * v_vRepeat.xy;
        vec4 col = texture2D( gm_BaseTexture, UV );
        col.rgb = mix(col.rgb, f_colour.rgb, f_colour.a);
        gl_FragColor = v_vColour * col;
    }

Before submitting the buffer etc I'd set the fade shaders:
Code:
shader_set(sh_fade);
        shader_set_uniform_f(ItemFadeColour, 1, 1, 1, 0.5); // 1,1,1 for white
Hopefully that's enough info, I'm sure its either an issue with the format I'm using for the plain colour buffer or the shaders aren't compatible in some way. Any help appreciated, thanks!
 

DesArts

Member
Something extra? I've messed with removing some elements, kind of at random but I have tried removing some of the texture stuff of course.

Even a new unchanged shader makes it become invisible it seems, but I'm unsure what or if anything needs to change in a plain shader. Sorry for being kinda useless!
 

Ido-f

Member
Something extra? I've messed with removing some elements, kind of at random but I have tried removing some of the texture stuff of course.

Even a new unchanged shader makes it become invisible it seems, but I'm unsure what or if anything needs to change in a plain shader. Sorry for being kinda useless!
Did you remove the texcoord attribute?
If so, did you also remove the v_vTexcoord varying, and then anything that used it in the fragment shader? (i.e -
Code:
vec4 col = texture2D( gm_BaseTexture, v_vTexcoord );
)

Another suspect I can see here is ItemFadeColour, but if you've set it to shader_get_uniform(shadername, "f_colour") then it shouldn't be the problem.
 
Last edited:

DesArts

Member
Okay that does make sense, so if I do remove this or change it:
Code:
void main()
    {
       // vec4 col = texture2D( gm_BaseTexture, UV );
        col.rgb = mix(col.rgb, f_colour.rgb, f_colour.a);
        gl_FragColor = v_vColour * col;
    }
How do I merge the original shape colour (which is no longer a texture colour) with the fade colour?
 

Ido-f

Member
Okay that does make sense, so if I do remove this or change it:
Code:
void main()
    {
       // vec4 col = texture2D( gm_BaseTexture, UV );
        col.rgb = mix(col.rgb, f_colour.rgb, f_colour.a);
        gl_FragColor = v_vColour * col;
    }
How do I merge the original shape colour (which is no longer a texture colour) with the fade colour?
Either return the texture stuff for a textured fade, but if you want the shape to gradually turn from the vertices colors to the fade color, without a texture, you can do
Code:
vec3 col = mix(v_vColour.rgb, f_colour.rgb, f_colour.a);
gl_FragColor = vec4(col, your_desired_alpha);
Or add another starting color uniform and use that in the mix function instead of v_vColour.
 

DesArts

Member
And there it is! All is working :)

Looks totally obvious now I see it laid out, definitely learned more about shaders with this. Thankyou very much, the issue is solved ~
 
Top