VBOs with std::vector

前端 未结 2 1146
旧巷少年郎
旧巷少年郎 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:47

    If you have a std::vector v, you may obtain a T* pointing to the start of the contiguous data (which is what OpenGL is after) with the expression &v[0].


    In your case, this means passing a Vertex* to glBufferData:

    glBufferData(
       GL_ARRAY_BUFFER,
       vertices.size() * sizeof(Vertex),
       &vertices[0],
       GL_STATIC_DRAW
    );
    

    Or like this, which is the same:

    glBufferData(
       GL_ARRAY_BUFFER,
       vertices.size() * sizeof(Vertex),
       &vertices.front(),
       GL_STATIC_DRAW
    );
    

    You can rely on implicit conversion from Vertex* to void const* here; that should not pose a problem.

提交回复
热议问题