jpm
jpm

Reputation: 1083

How to add directives in OpenGL/GLES shaders?

Basically I want to create one file which contains my vertex shader as well as fragment shader.

like this.

    #ifdef VERTEX
    attribute vec4  a_pos; 
    attribute vec2   a_texCoords; 
    uniform mat4 combined;  
    varying vec2 v_texCoords; 
         void main(){ 
            v_texCoords =   a_texCoords; 
            gl_Position =  combined *   a_pos; 
        }
    #endif

    #ifdef FRAGMENT
     varying vec2 v_texCoords; 
     uniform sampler2D u_texture;
    void main() {
        gl_FragColor = texture2D(u_texture, v_texCoords);
        //gl_FragColor = vec4(1,0,0,1);
    }
    #endif

but How to pass directives while compiling the shader like make_file in c/c++?

Upvotes: 2

Views: 655

Answers (1)

jpm
jpm

Reputation: 1083

I just prepended #define VERTEX\n to the start of the code string when using it as vertex shader source code, and #define FRAGMENT\n when using it as fragment shader source code

like this:-

void compileShader(int TYPE, String source){
     switch(TYPE){
     case GL20.GL_VERTEX_SHADER:
            source = "#define VERTEX \n" + source;
     case GL20.GL_FRAGMENT_SHADER:
            source = "#define FRAGMENT \n" + source;
     }
     //then compile source
}

Upvotes: 2

Related Questions