VBOs with std::vector

前端 未结 2 1153
旧巷少年郎
旧巷少年郎 2020-11-28 23:23

I\'ve written a model loader in C++ an OpenGL. I\'ve used std::vectors to store my vertex data, but now I want to pass it to glBufferData(), howeve

2条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-28 23:55

    This should do the trick:

    &vertices[0]
    

    Some prefer &vertices.front(), but that's more typing and I'm bone lazy.

    To be even lazier, you could overload glBufferData thus:

    template 
    inline void glBufferData(GLenum target, const vector& v, GLenum usage) {
        glBufferData(target, v.size() * sizeof(T), &v[0], usage);
    }
    

    Then you can write:

    glBufferData(GL_ARRAY_BUFFER, vertices, GL_STATIC_DRAW);
    

    and also avoid bugs (your struct is bigger than 3 * sizeof(float)).

提交回复
热议问题