Opengl es 如何将GLSL#版本330 core转换为GLSL ES#版本100? 我试图制作一个应用程序,它在Android工作室中用NDK和JNI绘制图像,用OpenGL ES调用C++代码。我已经完成了如何在OpenGL中使用GLSL330内核的教程。但是,Android emulator不支持OpenGL ES 3.0(请注意,此链接中:)

Opengl es 如何将GLSL#版本330 core转换为GLSL ES#版本100? 我试图制作一个应用程序,它在Android工作室中用NDK和JNI绘制图像,用OpenGL ES调用C++代码。我已经完成了如何在OpenGL中使用GLSL330内核的教程。但是,Android emulator不支持OpenGL ES 3.0(请注意,此链接中:),opengl-es,glsl,glsles,vertex-array-object,Opengl Es,Glsl,Glsles,Vertex Array Object,因此,我必须使用GLSL ES#version 100,它不支持下面着色器中的“布局”、“输入”和“输出”。我应该如何编辑它们,以便它们可以在#version 100中运行?如果我编辑它们,源代码中是否有任何更改?谢谢你的关注和帮助 更新:搜索之后,我发现我可以使用glGetAttributeLocation来获取顶点着色器中变量的位置,而不是使用布局(位置=0)。然而,GLSL ES#version 100中没有VAO,因此我仍然无法弄清楚没有VAO它是如何工作的 我的顶点着色器: #vers

因此,我必须使用GLSL ES#version 100,它不支持下面着色器中的“布局”、“输入”和“输出”。我应该如何编辑它们,以便它们可以在#version 100中运行?如果我编辑它们,源代码中是否有任何更改?谢谢你的关注和帮助

更新:搜索之后,我发现我可以使用glGetAttributeLocation来获取顶点着色器中变量的位置,而不是使用布局(位置=0)。然而,GLSL ES#version 100中没有VAO,因此我仍然无法弄清楚没有VAO它是如何工作的

我的顶点着色器:

#version 330 core
layout (location = 0) in vec3 position;
layout (location = 1) in vec3 color;
layout (location = 2) in vec2 texCoord;

out vec3 ourColor;
out vec2 TexCoord;

void main()
{
    gl_Position = vec4(position, 1.0f);
    ourColor = color;
    // We swap the y-axis by substracing our coordinates from 1. This is done because most images have the top y-axis inversed with OpenGL's top y-axis.
    // TexCoord = texCoord;
    TexCoord = vec2(texCoord.x, 1.0 - texCoord.y);
}
片段着色器:

#version 330 core
in vec3 ourColor;
in vec2 TexCoord;

out vec4 color;

// Texture samplers
uniform sampler2D ourTexture1;
uniform sampler2D ourTexture2;

void main()
{
    // Linearly interpolate between both textures (second texture is only slightly combined)
    color = mix(texture(ourTexture1, TexCoord), texture(ourTexture2, TexCoord), 0.2);
}
初始化VAO,VBO,:

    // Set up vertex data (and buffer(s)) and attribute pointers
    GLfloat vertices[] = {
        // 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
    };
    GLuint VBO, VAO, ;
    glGenVertexArrays(1, &VAO);
    glGenBuffers(1, &VBO);
    glGenBuffers(1, &);

    glBindVertexArray(VAO);

    glBindBuffer(GL_ARRAY_BUFFER, VBO);
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

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

    // 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);
    // TexCoord attribute
    glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (GLvoid*)(6 * sizeof(GLfloat)));
    glEnableVertexAttribArray(2);

    glBindVertexArray(0); // Unbind VAO
绘制图像:

        // Clear the colorbuffer
        glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
        glClear(GL_COLOR_BUFFER_BIT);

        // Activate shader
        ourShader.Use();     

        // Bind Textures using texture units
        glActiveTexture(GL_TEXTURE0);
        glBindTexture(GL_TEXTURE_2D, texture1);
        glUniform1i(glGetUniformLocation(ourShader.Program, "ourTexture1"), 0);
        glActiveTexture(GL_TEXTURE1);
        glBindTexture(GL_TEXTURE_2D, texture2);
        glUniform1i(glGetUniformLocation(ourShader.Program, "ourTexture2"), 1);  

        // Draw container
        glBindVertexArray(VAO);
        glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
        glBindVertexArray(0);

最后,我设法解决了这个问题。在GLSL ES 100中,没有我上面提到的
In
out
布局(位置=0)
。因此,我必须替换它们:

中的
=>
属性

输出
=>
变化

并完全删除
布局(location=0)
,因为据我所知,在GLSL ES 100中没有这样的东西做同样的事情

由于删除了
layout(location=0)
,我们必须告诉程序顶点数据的位置,在这种情况下是位置颜色文本坐标。使用
布局(location=0)
,我们可以简单地将位置0放在

glVertexAttribPointer(0,3, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (GLvoid*)0);
glEnableVertexAttribArray(0);
如果没有它,我们首先需要使用以下方法获取顶点属性的位置:

GLint mPosition= glGetAttributeLocation(ourProgram,"position")
然后将常量0、1或2替换为我预定义的
mPosition

glVertexAttribPointer(mPosition,3, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (GLvoid*)0);
glEnableVertexAttribArray(mPosition);;
color
textCoord
上也一样

下面是我编辑的顶点着色器、片段着色器以及如何初始化VBO和传入数据:

为GLSL ES 100编辑的
顶点着色器

auto gVertexShader =
"attribute vec3 position;\n"
"attribute vec3 color;\n"
"attribute  vec2 texCoord;\n"

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

"void main()\n"
"{\n"
    "gl_Position = vec4(position,1.0); // Add the xOffset to the x position of the vertex position\n"
    "ourColor = color;\n"
    "TexCoord= vec2(texCoord.x,1.0-texCoord.y);\n"
"}\n";
static const char FRAGMENT_SHADER[] =
    "#version 100\n"
    "precision mediump float;\n"
    "varying vec4 vColor;\n"
    "void main() {\n"
    "    gl_FragColor = vColor;\n"
    "}\n";
为GLSL ES 100编辑的
片段着色器

auto gVertexShader =
"attribute vec3 position;\n"
"attribute vec3 color;\n"
"attribute  vec2 texCoord;\n"

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

"void main()\n"
"{\n"
    "gl_Position = vec4(position,1.0); // Add the xOffset to the x position of the vertex position\n"
    "ourColor = color;\n"
    "TexCoord= vec2(texCoord.x,1.0-texCoord.y);\n"
"}\n";
static const char FRAGMENT_SHADER[] =
    "#version 100\n"
    "precision mediump float;\n"
    "varying vec4 vColor;\n"
    "void main() {\n"
    "    gl_FragColor = vColor;\n"
    "}\n";
我的
InitBuffer
函数:

// Set up vertex data (and buffer(s)) and attribute pointers
    GLfloat vertices[] = {
        // 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
    };
void initBuffers()
{
    GLint mPosition,mCorlor,mTextCoord;
    GLuint VBOs[2]; // Initialize an buffer to store all the verticles and transfer them to the GPU

    glGenBuffers(2, VBOs); // Generate VBO


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

    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, VBOs[1]);//Bind the indices for information about drawing sequence
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);

    // 1. set the vertex attributes pointers
    // Position Attribute
    mPosition=glGetAttributeLocation(Program, "position");
    glVertexAttribPointer(mPosition, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (GLvoid*)0);
    glEnableVertexAttribArray(mPosition);
    // Color Attribute

    mColor=glGetAttributeLocation(Program, "color");
    glVertexAttribPointer(mColor, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (GLvoid*)(3 * sizeof(GLfloat)));
    glEnableVertexAttribArray(mColor);
    //Texture Coordinate Attribute

    mTextCoord=glGetAttributeLocation(Program, "textCoord")'
    glVertexAttribPointer(mTextCoord, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (GLvoid*)(6 * sizeof(GLfloat)));
    glEnableVertexAttribArray(mTextCoord);


}
然后我们用调用
initBuffer()
函数替换draw函数中的VAO绑定

    // Clear the colorbuffer
    glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
    glClear(GL_COLOR_BUFFER_BIT);

    // Activate shader
    ourShader.Use();     

    // Bind Textures using texture units
    glActiveTexture(GL_TEXTURE0);
    glBindTexture(GL_TEXTURE_2D, texture1);
    glUniform1i(glGetUniformLocation(ourShader.Program, "ourTexture1"), 0);
    glActiveTexture(GL_TEXTURE1);
    glBindTexture(GL_TEXTURE_2D, texture2);
    glUniform1i(glGetUniformLocation(ourShader.Program, "ourTexture2"), 1);  

    // Draw container
    initBuffers();
    glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
    glBindVertexArray(0);

希望这能帮助和我有同样问题的人。请随时问我是否有任何问题:)./P>为不同版本创建单独的着色器,并切换到C++代码中需要的着色器。或者,使用另一个仿真器,如bluestacks。谢谢你的建议,我决定在手机上运行它,它现在可以工作了。:)制服去哪儿了?