如何将这些 OpenGL 着色器转换为适用于 android NDK 的 GLES3 的 OpenGL ES 着色器?

How to convert these OpenGL Shaders to OpenGL ES shaders for GLES3 for android NDK?

以下着色器失败,return 为 -1,当我尝试时。

col_attr = glGetAttribLocation(prog, "v_col");

我尝试了不同的设置,包括 切换 ,

gl_FragColor

outColor

#version 300 es

#version 150 core

还有很多,在我意识到我完全迷路之前,还有很多我不知道的变数。我只需要将这些简单的着色器转换为适用于 C++ 中的 Android NDK 的 GLESv3。非常感谢所有帮助。谢谢。

原始顶点着色器

#version 150 core
in vec3 v_pos;
in vec4 v_col;
out vec4 color;
uniform mat4 projection;
uniform mat4 view;
void main(){
        color = v_col;                    
        gl_Position = projection * view * vec4(v_pos, 1.0);
}

原始片段着色器

#version 150 core
in vec4 color;
void main(){
            gl_FragColor = color;
}

更新:发现只有Fragment Shader编译失败

新顶点着色器 - 编译!

return "#version 300 es                 \n"
       "in vec3 v_pos;                 \n"
       "in vec4 v_col;                 \n"
       "out vec4 color;                \n"
       "uniform mat4 projection;        \n"
       "uniform mat4 view;            \n"
       "void main()                    \n"
       "{                              \n"
       "   color = v_col;                \n"
       "   gl_Position = projection * view * vec4(v_pos, 1.0);   \n"
       "}                              \n";

新的片段着色器 - 无法编译!

return "#version 300 es                 \n"
       "in vec4 color;                  \n"
       "out vec4 outColor;              \n"
       "void main()                     \n"
       "{                               \n"
       "   outColor = color;            \n"
       "}                               \n";

新的片段着色器 - 编译!

return "#version 300 es                 \n"
        "precision mediump float;       \n"
       "in vec4 color;                  \n"
       "out vec4 outColor;              \n"
       "void main()                     \n"
       "{                               \n"
       "   outColor = color;            \n"
       "}                               \n";

您必须声明 fragment shader output 变量 out vec4 outColor
此外,您还必须添加 precision qualifier:

一个有效的 GLSL ES 3.00 片段着色器应该是:

#version 300 es

precision mediump float; 

in vec4 color;
out vec4 outColor;

void main(){
    outColor = color;
}

版本信息 (#version 300 es) 也必须在顶点着色器中更改。

参见 OpenGL ES Shading Language 3.00 Specification - 4.3.6 Output Variables 第 42 页:

Fragment outputs are declared as in the following examples:

out vec4 FragmentColor;
out uint Luminosity;

参见 OpenGL ES Shading Language 3.00 Specification - 4.5.4 Default Precision Qualifiers 第 56 页:

The fragment language has no default precision qualifier for floating point types. Hence for float, floating point vector and matrix variable declarations, either the declaration must include a precision qualifier or the default float precision must have been previously declared.