1

I'm trying to render a white square in OpenGL and I have this function to do so:

void main_loop(window_data *window)
{
    printf("%s\n", glGetString(GL_VERSION));

    GLuint vertex_array_objects, vertex_buffer_objects;
    glGenVertexArrays(1, &vertex_array_objects);
    glBindVertexArray(vertex_array_objects);
    glGenBuffers(1, &vertex_buffer_objects);
    glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer_objects);

    GLfloat vertices[6][2] = {
        {-0.90, -0.90},
        {0.85, -0.90},
        {-0.90, 0.85},
        {0.90, -0.85},
        {0.90, 0.90},
        {-0.85, 0.90}};

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

    glVertexAttribPointer(0,
            2,
            GL_FLOAT,
            GL_FALSE,
            0,
            (void*)0);

    glEnableVertexAttribArray(0);

    while (!glfwWindowShouldClose(window->glfw_window))
    {
        glClear(GL_COLOR_BUFFER_BIT);
        glBindVertexArray(vertex_array_objects);
        glDrawArrays(GL_TRIANGLES, 0, 6);
        glfwSwapBuffers(window->glfw_window);
        glfwPollEvents();
    }
    return;
}

Which works perfectly fine, but then I tried to split this up into two functions like so:

void initialize_image(GLuint *vArray, GLuint *vBuffer, GLfloat vertices[][2])
{
    glGenVertexArrays(1, vArray);
    glBindVertexArray(*vArray);
    glGenBuffers(1, vBuffer);
    glBindBuffer(GL_ARRAY_BUFFER, *vBuffer);
    glBufferData(
            GL_ARRAY_BUFFER,
            sizeof(vertices),
            vertices,
            GL_STATIC_DRAW);
    glVertexAttribPointer(
            0,
            2,
            GL_FLOAT,
            GL_FALSE,
            0,
            (void*)0);
    glEnableVertexAttribArray(0);
    return;
}

And then I call it in the main_loop function (just before the while loop):

    initialize_image(&vArray, &vBuffer, vertices);

But that keeps giving me a black screen of nothingness. What could be causing this?

Red
  • 435
  • 1
  • 5
  • 13
  • 1
    possible duplicate of [Weird OpenGL issue when factoring out code](http://stackoverflow.com/questions/26793266/weird-opengl-issue-when-factoring-out-code) – Reto Koradi Nov 20 '14 at 03:58
  • You're reffering to exactly same type of bug ! WOW - that was a nice catch. – Anonymous Nov 20 '14 at 20:52

1 Answers1

2

When you pass pointer to vertices array its sizeof() is the pointer size and not your data size!

Pass additional vertices size argument to your function and use it instea d of sizeof for glBufferData call.

Anonymous
  • 2,122
  • 19
  • 26