GLSL reusable/shared functions, shared constants (OpenGL ES 2.0)?

拥有回忆 提交于 2019-12-03 12:03:52

You can do that similarly as in C - you declare functions in headers and define it in common C file.

In GLSL you'll need to do following:

  1. in some shader (string) you define function (lets call it COMMON):

    float getCommonValue() { return 42; }
    
  2. in all shaders you want to use this function you only declare it and use it (lets call it SHADER1):

    float getCommonValue();
    void main() { gl_Color = vec4(getCommonValue(), 0, 0, 0); }
    
  3. when compiling shaders with glCompileShader you compile COMMON shader only once and store shader GLuint somewhere

  4. when you link program with glLinkProgram for SHADER1 you attach to program with glAttachShader both shaders - COMMON and SHADER1. Thus you'll be able to call getCommonValue function from one shader to other.

  5. you can reuse COMMON shader GLuint value multiple times for different sahder programs (SHADER1, SHADER2, ...).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!