OpenGL:第二个VBO破坏了第一个VBO

时间:2017-08-29 19:26:48

标签: c++ opengl

我有一个代码,最初有一个着色器(ourShader),一个VAO和一个VBO。它将使用模板测试渲染具有纹理立方体的场景及其在地面上的反射。现在,我想添加一个帧缓冲区进行后期处理,所以我需要第二个着色器。我添加了第二个着色器(screenShader)和第二个VBO。我还没有在我的绘图中使用它们,但是我添加它们的简单事实使我的代码呈现黑色,红色三角形而不是通常的场景。

我的代码如下所示:

    Shader ourShader(string("core.vs"), string("core.frag")); // shader class creating a shader program from vertex shader and fragment shader source files.
    Shader screenShader(string("core2.vs"), string("core2.frag"));

    // Setting up attributes, VAO, VBO
    GLuint VAO;
    array<GLuint, 2> VBO;
    glGenVertexArrays(1, &VAO);
    glGenBuffers(2, &VBO[0]);

    glBindVertexArray(VAO);

    // Data for first shader and first VBO
    glBindBuffer(GL_ARRAY_BUFFER, VBO[0]);
    glBufferData(GL_ARRAY_BUFFER, sizeof(verticesCube), verticesCube, GL_STATIC_DRAW); 

    GLint posAttribLoc = glGetAttribLocation(ourShader.Program, "position");
    glVertexAttribPointer(posAttribLoc, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (GLvoid*)0); // specify (to the active VAO) how to retrieve the values for the attribute "position" from the data stored ("vertices" here) on the active VBO (GPU)
    glEnableVertexAttribArray(posAttribLoc); // enable attribute for rendering

    GLint colAttribLoc = glGetAttribLocation(ourShader.Program, "color");
    glVertexAttribPointer(colAttribLoc, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (GLvoid*)(3 * sizeof(GLfloat)));
    glEnableVertexAttribArray(colAttribLoc);

    GLint texAttribLoc = glGetAttribLocation(ourShader.Program, "texCoord");
    glVertexAttribPointer(texAttribLoc, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (GLvoid*)(6 * sizeof(GLfloat)));
    glEnableVertexAttribArray(texAttribLoc);

    // ##### PART GIVING A WEIRD RESULT #####
    // Data for second shader and second VBO
    glBindBuffer(GL_ARRAY_BUFFER, VBO[1]);
    glBufferData(GL_ARRAY_BUFFER, sizeof(verticesRectangle), verticesRectangle, GL_STATIC_DRAW);

    GLint posAttribLoc2 = glGetAttribLocation(screenShader.Program, "position");
    glVertexAttribPointer(posAttribLoc2, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(GLfloat), (GLvoid*)0); // specify (to the active VAO) how to retrieve the values for the attribute "position" from the data stored ("vertices" here) on the active VBO (GPU)
    glEnableVertexAttribArray(posAttribLoc2); // enable attribute for rendering

    GLint texAttribLoc2 = glGetAttribLocation(screenShader.Program, "texCoord");
    glVertexAttribPointer(texAttribLoc2, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(GLfloat), (GLvoid*)(2 * sizeof(GLfloat)));
    glEnableVertexAttribArray(texAttribLoc2);
   // ##### END #####

    // Setting up texture that will be used for the first shader
    GLuint texture;
    int width, height;
    glGenTextures(1, &texture);
    glActiveTexture(GL_TEXTURE0);
    glBindTexture(GL_TEXTURE_2D, texture); // makes "texture" the current texture and attaches it to texture unit 0

    // Set the wrapping 
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); 
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

    // Set the filtering 
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

    unsigned char* image = SOIL_load_image("res/images/image1.jpg", &width, &height, 0, SOIL_LOAD_RGBA);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, image);
    glGenerateMipmap(GL_TEXTURE_2D);
    SOIL_free_image_data(image);

    // Unbind VBO, texture before main loop
    glBindBuffer(GL_ARRAY_BUFFER, 0);
    glBindTexture(GL_TEXTURE_2D, 0); 
    glfwSwapInterval(1);
    glfwSetKeyCallback(window, Input::keyCallback); // Input is a singleton class handling inputs. It works well.
    glfwSetCursorPosCallback(window, Input::mouseCallback);
    glfwSetScrollCallback(window, Input::scrollCallback);

    while (glfwWindowShouldClose(window) == GLFW_FALSE) {
        // MAIN LOOP
        // ...
    }
    glDeleteVertexArrays(1, &VAO);
    glDeleteBuffers(2, &VBO[0]);

我认为主循环并不重要。我想说的是,如果我删除处理第二个VBO的几行,那么场景渲染得很好。否则,我得到一个奇怪的三角形。

我的两个着色器将这些源文件用于各自的顶点着色器和片段着色器。顺便说一下,我的着色器没有编译错误。

core.vs:

#version 330 core
in vec3 position;
in vec2 texCoord;
in vec3 color;

out vec2 TexCoord;
out vec3 Color;

uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;

void main()
{
    gl_Position = projection*view*model*vec4(position, 1.0);
    TexCoord = vec2(texCoord.x, 1.0 - texCoord.y);
    Color = color;
}

core.frag:

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

out vec4 outColor;
uniform sampler2D ourTexture0;

void main()
{
    outColor = vec4(Color, 1)*texture(ourTexture0, TexCoord);
}

core2.vs:

#version 330 core
in vec2 position;
in vec2 texCoord;
out vec2 TexCoord;
void main()
{
    gl_Position = vec4(position, 0.0, 1.0);
    TexCoord = texCoord;
}

core2.frag:

#version 330 core
in vec2 TexCoord;
out vec4 outColor;
uniform sampler2D texFramebuffer;
void main()
{
    outColor = texture(texFramebuffer, TexCoord);
}

顶点看起来像这样(但只有读取它们的方式很重要):

    GLfloat verticesRectangle[] = {
        // position     // texture coordinates
        -0.5f, -0.5f,   0.0f, 0.0f,     
        0.5f, -0.5f,    1.0f, 0.0f,     
        -0.5f, 0.5f,    0.0f, 1.0f,     
        0.5f, 0.5f,     1.0f, 1.0f      
    };

    GLfloat verticesCube[] = {
        // position          // color          // texture coordinates
        -0.5f, -0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f,
        0.5f, -0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f,
        0.5f,  0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f,
        0.5f,  0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f,
        -0.5f,  0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f,
        -0.5f, -0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f,

        -0.5f, -0.5f,  0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f,
        0.5f, -0.5f,  0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f,
        0.5f,  0.5f,  0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f,
        0.5f,  0.5f,  0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f,
        -0.5f,  0.5f,  0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f,
        -0.5f, -0.5f, 0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f,

        -0.5f, 0.5f, 0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f,
        -0.5f, 0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f,
        -0.5f, -0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f,
        -0.5f, -0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f,
        -0.5f, -0.5f, 0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f,
        -0.5f, 0.5f, 0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f,

        0.5f, 0.5f, 0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f,
        0.5f, 0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f,
        0.5f, -0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f,
        0.5f, -0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f,
        0.5f, -0.5f, 0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f,
        0.5f, 0.5f, 0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f,

        -0.5f, -0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f,
        0.5f, -0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f,
        0.5f, -0.5f, 0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f,
        0.5f, -0.5f, 0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f,
        -0.5f, -0.5f, 0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f,
        -0.5f, -0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f,

        -0.5f, 0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f,
        0.5f, 0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f,
        0.5f, 0.5f, 0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f,
        0.5f, 0.5f, 0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f,
        -0.5f, 0.5f, 0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f,
        -0.5f, 0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f,

        -1.0f, -0.5f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, // reflection surface // not a part of the cube itself
        -1.0f, -0.5f, -1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f,
        1.0f, -0.5f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f,
        1.0f, -0.5f, -1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f,
        -1.0f, -0.5f, -1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f,
        1.0f, -0.5f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f
    };

我已经查看了Binding a second vertex buffer seems to spoil my first vertex buffer, OpenGL OES ios 5.1但该人遇到了这个问题,因为他没有正确使用他的VAO。

1 个答案:

答案 0 :(得分:2)

很可能两个属性位置具有相同的值。由于您只使用一个VAO,因此您可以覆盖某些绑定。使用多个独立几何的正确方法是在每个几何体的VAO上使用。

正确的代码必须看起来像这样:

glBindVertexArray(vao1);
glBindBuffer(VBO[0])
glVertexAttribPointer...
glEnableVertexAttribArray...
//Setup all attributes for first VBO

glBindVertexArray(vao2);
glBindBuffer(VBO[1])
glVertexAttribPointer...
glEnableVertexAttribArray...
//Setup all attributes for second VBO

渲染时执行以下操作:

glBindVertexArray(vao1);
glDraw*... //Draw VAO1

glBindVertexArray(vao2);
glDraw*.... //Draw VAO2
相关问题