GCC, stringification, and inline GLSL?

前端 未结 5 976
孤城傲影
孤城傲影 2020-11-29 08:37

I\'d like to declare GLSL shader strings inline using macro stringification:

#define STRINGIFY(A)  #A
const GLchar* vert = STRINGIFY(
#version 120\\n
attribu         


        
5条回答
  •  暗喜
    暗喜 (楼主)
    2020-11-29 08:57

    An alternative approach: include a header file, but with a .glsl extension.

    For example, I have a file called bar.h.glsl. It exposes my vertex and fragment shader code as raw literals:

    #pragma once
    
    namespace bar {
    
    static const char vertex[] = R"(#version 410 core
    
    
    // ----- vertex shader start -----
    layout( location = 0 ) in vec4 vPosition;
    
    uniform float Time;
    uniform float Width;
    
    void main()
    {
        float x = vPosition.x;
        float y = vPosition.y * Width;
        float yOffset = mix(sin(Time), sin(Time * 0.75), x * 0.5 + 0.5);
        gl_Position = vec4(x, y + yOffset, vPosition.z, vPosition.w);
    }
    // ------ vertex shader end ------
    
    
    )";
    static const char fragment[] = R"(#version 410 core
    
    
    // ----- fragment shader start ----
    out vec4 fragColor;
    
    void main()
    {
        fragColor = vec4(1.0, 1.0, 1.0, 1.0);
    }
    // ------ fragment shader end -----
    
    
    )";
    
    }
    

    Then in my source, I simply include the file:

    #include "bar.h.glsl"
    

    and access the glsl strings like so:

    bool success = barShader.Compile( bar::vertex, bar::fragment );
    

    This way, although I need to overlook a bit of C code in my glsl files, I get the best of glsl syntax highlighting without having to dynamically load the file.

提交回复
热议问题