0
votes

I am trying to draw a triangle with three vertices a,b,c. Typically, i would have these three vertex co-ordinates in an array and pass it as an attribute to my vertex shader.

But , is it possible to generate these vertex co-ordinates within the vertex shader itself rather than passing as an attribute (i.e. per vertex co-ordinate value for corresponding vertex position). If yes, any sample program to support it ?

Thanks.

1

1 Answers

0
votes

You can create variables in vertex shader and pass it to fragment shader. An example:

Vertex shader

precision highp float;

uniform float u_time;
uniform float u_textureSize;
uniform mat4  u_mvpMatrix;                   

attribute vec4 a_position;

// This will be passed into the fragment shader.
varying vec2 v_textureCoordinate0;

void main()                                 
{
    // Create texture coordinate
    v_textureCoordinate0 = a_position.xy / u_textureSize;

    gl_Position = u_mvpMatrix * a_position;
}                                             

And the fragment shader:

precision mediump float;

uniform float   u_time;
uniform float   u_pixel_amount;
uniform sampler2D u_texture0;   

// Interpolated texture coordinate per fragment.
varying vec2 v_textureCoordinate0;

void main(void)
{       
    vec2 size = vec2( 1.0 / u_pixel_amount, 1.0 / u_pixel_amount);    
    vec2 uv = v_textureCoordinate0 - mod(v_textureCoordinate0,size);    
    gl_FragColor = texture2D( u_texture0, uv );    
    gl_FragColor.a=1.0;
}

How you can see, vector 2D named v_textureCoordinate0 is created in vertex shader and its interpolated value is used in fragment shader.

I hope it help you.