OpenGL data gone between init & render

I’m using OpenGl with glew & glm inside an audio plugin. in the newOpenGLContextCreated function I’m doing the following:

    // ...
    m_shader.reset(new tobanteAudio::Shader());
    m_shader->loadFromFile(R"(shader\cube.vert)", R"(shader\cube.frag)");

    // ...

and in renderOpenGL:

    // ...
    m_cube.reset(new tobanteAudio::Cube());

    // Shader

    // Projection
    glm::mat4 model = glm::mat4(1.0f);
    glm::mat4 mvp   = m_camera->getProjectionMatrix() * m_camera->getViewMatrix() * model;
    m_shader->setMatrix4("u_mvp", mvp);

    // Render

    // ...

I’m uploading the vertices in the constructor of m_cube. I would like to do that in the newOpenGLContextCreated so that the data is only uploaded once, but if I move the m_cube.reset() line, the cube only appears for a split second and then is gone.

I had the same results with a simpler triangle, which was not abstracted away and called glGenVertexArrays directly in the main component. The same also happens if I use an OpenGLAppComponent.

If I can’t send the data to the GPU in the newOpenGLContextCreated function, where would be a suitable place?

You can. What is happening in m_cube->render()? The cube’s buffer(s) should be bound and the layout (attributes) set-up before every render call.

Cube constructor:

        GLuint VBO;
        glGenVertexArrays(1, &m_vao);
        glGenBuffers(1, &VBO);
        // bind the Vertex Array Object first, then bind and set vertex
        // buffer(s), and then configure vertex attributes(s).

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

        glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)nullptr);

        glBindBuffer(GL_ARRAY_BUFFER, 0);

Cube render:

void render()
    // 12*3 indices starting at 0 ->12 triangles -> 6 squares
    glDrawArrays(GL_TRIANGLES, 0, 12 * 3);

Vertex shader:

layout (location = 0) in vec3 vertexPosition_modelspace;

// Values that stay constant for the whole mesh.
uniform mat4 u_mvp;

void main()
    // Output position of the vertex, in clip space : u_mvp * position
    gl_Position =  u_mvp * vec4(vertexPosition_modelspace,1);

The exact same class works fine in a GLFW application, where the data is uploaded before the main loop and in the loop the cube + an imgui overlay is rendered.

Here’s an example of setting vertex data once during newOpenGLContextCreated in plain JUCE, no glew. Not sure if that could be causing problems?
LoadDataInGLSetup.h (4.8 KB)

Thanks. I’ll try it later today.

I have tried your example and if works perfectly fine. Even after replacing the context.extensions calls with glew. So glew is not the problem, which is create news for me. Thanks for the help, I’m sure I’ll find the problem from here.

1 Like