GLSL ES 3.0,无法编译顶点着色器:'position':不是合法的布局限定符 id

GLSL ES 3.0, Failed to compile vertex Shader :'position' : not a legal layout qualifier id

我正在尝试制作一个使用 OpenGL ES 3.0 绘制图像的应用程序。为此,我将 Android Studio 与 NDK、JNI 结合使用。当我 运行 在 Logcat 中收到此错误时,我做错了什么?非常感谢您的关注和帮助。

错误:

Could not compile shader 35633:
                 Vertex shader compilation failed.
                 ERROR: 0:2: 'position' : not a legal layout qualifier id 
                 ERROR: 0:2: '' : the location is not within attribute range [0, MAX_ATTRIBUTES-1] 
                 ERROR: 0:3: 'position' : not a legal layout qualifier id 
                 ERROR: 0:3: '' : the location is not within attribute range [0, MAX_ATTRIBUTES-1] 
                 ERROR: 0:4: 'position' : not a legal layout qualifier id 
                 ERROR: 0:4: '' : the location is not within attribute range [0, MAX_ATTRIBUTES-1] 
                 ERROR: 6 compilation errors.  No code generated.

我的顶点着色器:

"#version 300 es\n"
"layout (position=0) in vec3 position;\n"
"layout (position=1) in vec3 color;\n"
"layout (position=2) in vec2 texCoord;\n"

"out vec3 ourColor;\n"
"out vec2 TexCoord;\n"

"void main()\n"
"{\n"
    "gl_Position = vec4(position,1.0f); // Add the xOffset to the x position of the vertex position\n"
    "ourColor = color;\n"
    "TexCoord= vec2(texCoord.x,1.0f-texCoord.y);\n"
"}";

我的 initBuffer() 函数设置所有缓冲区:

GLfloat recVertices[] = {
        // Positions          // Colors          // Texture Coords
        0.5f,  0.5f, 0.0f,   1.0f, 0.0f, 0.0f,   1.0f, 1.0f,   // Top Right
        0.5f, -0.5f, 0.0f,   0.0f, 1.0f, 0.0f,   1.0f, 0.0f,   // Bottom Right
        -0.5f, -0.5f, 0.0f,   0.0f, 0.0f, 1.0f,   0.0f, 0.0f,   // Bottom Left
        -0.5f,  0.5f, 0.0f,   1.0f, 1.0f, 0.0f,   0.0f, 1.0f    // Top Left
};

GLuint indices[] = {  // Note that we start from 0!
        0, 1, 3, // First Triangle
        1, 2, 3  // Second Triangle
};
GLunint VAO;
    void initBuffers()
{

    GLuint VBOs[2], EBO; // Initialize an buffer to store all the verticles and transfer them to the GPU
    glGenVertexArrays (1,&VAO);

    glGenBuffers(1, VBOs);


    glGenBuffers(1, &EBO);
    glBindVertexArray (VAO);

    // Bind the Vertex Array
    glBindBuffer(GL_ARRAY_BUFFER, VBOs[0]);//0. Copy verticles array for OpenGL to use
    glBufferData(GL_ARRAY_BUFFER, sizeof(recVertices), recVertices, GL_STATIC_DRAW);

    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);

    // 1. set the vertex attributes pointers
    // Position Attribute
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (GLvoid*)0);
    glEnableVertexAttribArray(0);
    // Color Attribute
    glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (GLvoid*)(3 * sizeof(GLfloat)));
    glEnableVertexAttribArray(1);
    //Texture Coordinate Attribute
    glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (GLvoid*)(6 * sizeof(GLfloat)));
    glEnableVertexAttribArray(2);

    glBindVertexArray(0);
}

我的 generateTexture() 函数加载图像:

    void generateTexture()
{

    glGenTextures(1 , &mTexture);
    glBindTexture(GL_TEXTURE_2D, mTexture);// Bind our 2D texture so that following set up will be applied

    //Set texture wrapping parameter
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_MIRRORED_REPEAT);
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_MIRRORED_REPEAT);

    //Set texture Filtering parameter
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);

    //Load the image
    int picWidth,picHeight,n;
    unsigned char* image = stbi_load("D:\Lighthouse.jpg",&picWidth,&picHeight,0,0);

    //Generate the image
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB , picWidth , picHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, image);
    glGenerateMipmap(GL_TEXTURE_2D);

    stbi_image_free(image);// Free the reference to the image
    glBindTexture(GL_TEXTURE_2D,0); //Unbind 2D textures

}

最后是我的渲染函数:

void renderFrame() {
    static float grey;
    grey += 0.01f;
    if (grey > 1.0f) {
        grey = 0.0f;
    }

    generateTexture();
    glClearColor(grey+0.05f, grey-0.03f, grey+0.02f, grey-0.04f);
    checkGlError("glClearColor");
    glClear( GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
    checkGlError("glClear");

    glUseProgram(gProgram);
    checkGlError("glUseProgram");

    glActiveTexture(GL_TEXTURE0);
    checkGlError("glActiveTexture");
    glBindTexture(GL_TEXTURE_2D,mTexture);
    checkGlError("glBindTexture");
    GLint mlocation = glGetUniformLocation(gProgram,"ourTexture");
    checkGlError("glGetUniformLocation");
    glUniform1i(mlocation,0);
    checkGlError("glUniform1i");
    initBuffers();
    glBindVertexArray(VAO);
    checkGlError("glBindVertexArray");
    glDrawElements(GL_TRIANGLES,6,GL_UNSIGNED_INT,0);

}

不确定 GLSL ES 3,但在 "standard" GLSL 中,这应该是 layout (location = n),而不是 layout (position = n)

您可以查看 at the wiki 以获得可用的不同布局限定符。