Red
Red

Reputation: 475

OpenGL blank screen

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?

Upvotes: 1

Views: 195

Answers (1)

Anonymous
Anonymous

Reputation: 2172

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.

Upvotes: 2

Related Questions