简体   繁体   中英

Opengl, VAO creation within a class

Initializing VAOs within the main.cpp file and rendering them works as expected. However when I move the exact same initialization code to a separate class, causes my uniforms to not be found (optimized out?)

When initializing array objects within the main.cpp file (works fine):

GLuint createCube(){
    GLuint VBO;
    GLuint VAO;
    GLuint EAO;
    GLfloat vertexData[] = {
        0.5f, -0.5f, -0.5f,
        0.5f, -0.5f, 0.5f,
        -0.5f, -0.5f, 0.5f,
        -0.5f, -0.5f, -0.5f,
        0.5f, 0.5f, -0.5f,
        0.5f, 0.5f, 0.5f,
        -0.5f, 0.5f, 0.5f,
        -0.5f, 0.5f, -0.5f,

    };
    GLuint elementData[] = {
        2-1, 3-1, 4-1,
        8-1, 7-1, 6-1,
        5-1, 6-1, 2-1,
        6-1, 7-1, 3-1,
        3-1, 7-1, 8-1,
        1-1, 4-1, 8-1,
        1-1, 2-1, 4-1,
        5-1, 8-1, 6-1,
        1-1, 5-1, 2-1,
        2-1, 6-1, 3-1,
        4-1, 3-1, 8-1,
        5-1, 1-1, 8-1,

    };

    // make and bind the VAO
    glGenVertexArrays(1, &VAO);
    glBindVertexArray(VAO);

    // make and bind the VBO
    glGenBuffers(1, &VBO);
    glBindBuffer(GL_ARRAY_BUFFER, VBO);

    // make and bind the eao
    glGenBuffers(1, &EAO);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EAO);

    //put data into VBO and EAO
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertexData), vertexData, GL_STATIC_DRAW);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(elementData), elementData, GL_STATIC_DRAW);


    // connect the xyz to the "vert" attribute of the vertex shader
    glEnableVertexAttribArray(render.get_shader()->attrib("vert"));
    glVertexAttribPointer(render.get_shader()->attrib("vert"), 3, GL_FLOAT, GL_FALSE, 0, NULL);

    // unbind the VAO, EAO, VBO

    glBindVertexArray(0);
    glBindBuffer(GL_ARRAY_BUFFER, 0);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);

    return VAO;


}

int main()
{
    GLuint VAO = createCube();
    glm::mat4 MVP = createMVP();

    bool running = true;
    while(running){
        running = eventHandler();

        render.draw(VAO,36,MVP);

        ui.swapBuffer();
    }

    return 0;
}

When initializing array objects in a class (MVP uniform in my shader cannot be found, maybe optimized out?)

class cube{
public:
    cube(Render rendrer)
    {
        GLuint VBO;
        GLuint EAO;
        GLfloat vertexData[] = {
            0.5f, -0.5f, -0.5f,
            0.5f, -0.5f, 0.5f,
            -0.5f, -0.5f, 0.5f,
            -0.5f, -0.5f, -0.5f,
            0.5f, 0.5f, -0.5f,
            0.5f, 0.5f, 0.5f,
            -0.5f, 0.5f, 0.5f,
            -0.5f, 0.5f, -0.5f,

        };
        GLuint elementData[] = {
            2-1, 3-1, 4-1,
            8-1, 7-1, 6-1,
            5-1, 6-1, 2-1,
            6-1, 7-1, 3-1,
            3-1, 7-1, 8-1,
            1-1, 4-1, 8-1,
            1-1, 2-1, 4-1,
            5-1, 8-1, 6-1,
            1-1, 5-1, 2-1,
            2-1, 6-1, 3-1,
            4-1, 3-1, 8-1,
            5-1, 1-1, 8-1,

        };

        // make and bind the VAO
        glGenVertexArrays(1, &VAO);
        glBindVertexArray(VAO);

        // make and bind the VBO
        glGenBuffers(1, &VBO);
        glBindBuffer(GL_ARRAY_BUFFER, VBO);

        // make and bind the eao
        glGenBuffers(1, &EAO);
        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EAO);

        //put data into VBO and EAO
        glBufferData(GL_ARRAY_BUFFER, sizeof(vertexData), vertexData, GL_STATIC_DRAW);
        glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(elementData), elementData, GL_STATIC_DRAW);


        // connect the xyz to the "vert" attribute of the vertex shader
        glEnableVertexAttribArray(rendrer.get_shader()->attrib("vert"));
        glVertexAttribPointer(rendrer.get_shader()->attrib("vert"), 3, GL_FLOAT, GL_FALSE, 0, NULL);

        // unbind the VAO, EAO, VBO

        glBindVertexArray(0);
        glBindBuffer(GL_ARRAY_BUFFER, 0);
        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
    }
    GLuint get_VAO() const
    {
        return VAO;
    }
private:
    GLuint VAO;
};

int main()
{

    cube cube_object(render);
    GLuint VAO = cube_object.get_VAO();
    glm::mat4 MVP = createMVP();

    bool running = true;
    while(running){
        running = eventHandler();

        render.draw(VAO,36,MVP);

        ui.swapBuffer();
    }

    return 0;
}

Here is my vertex shader:

#version 430

in vec3 vert;
uniform mat4 MVP;

out vec3 vertout;

void main(void)
{
    vertout = vert;
    gl_Position = MVP*vec4(vert, 1.0);
}

Here is my fragment shader:

#version 430

in vec3 vertout;
out vec4 outcolor;

void main(void)
{

    outcolor = mix(vec4(1,0,0,1),vec4(0,1,0,1),dot(vertout, vertout));
}

Does creating a VAO in a class cause the MVP uniform to be removed, or is it something else?

EDIT:

I should clarify: The object still renders, but it renders white and with the MVP uniform missing.

For OpenGL operations to work, there must be a OpenGL context around and made current on the thread. If your "other" classes are instanciated without a OpenGL context being available it's as if nothing was created at all. To make matters worse, glGetError may return GL_NO_ERROR if there's no context around, so just checking for errors using that may not show that problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM