• <legend id='kjXQn'><style id='kjXQn'><dir id='kjXQn'><q id='kjXQn'></q></dir></style></legend>

    <i id='kjXQn'><tr id='kjXQn'><dt id='kjXQn'><q id='kjXQn'><span id='kjXQn'><b id='kjXQn'><form id='kjXQn'><ins id='kjXQn'></ins><ul id='kjXQn'></ul><sub id='kjXQn'></sub></form><legend id='kjXQn'></legend><bdo id='kjXQn'><pre id='kjXQn'><center id='kjXQn'></center></pre></bdo></b><th id='kjXQn'></th></span></q></dt></tr></i><div id='kjXQn'><tfoot id='kjXQn'></tfoot><dl id='kjXQn'><fieldset id='kjXQn'></fieldset></dl></div>
      <bdo id='kjXQn'></bdo><ul id='kjXQn'></ul>

    <tfoot id='kjXQn'></tfoot>

        <small id='kjXQn'></small><noframes id='kjXQn'>

      1. 带有 std::vector 的 VBO

        时间:2023-09-18
          <tbody id='E0lcc'></tbody>
          • <bdo id='E0lcc'></bdo><ul id='E0lcc'></ul>

            <small id='E0lcc'></small><noframes id='E0lcc'>

            <legend id='E0lcc'><style id='E0lcc'><dir id='E0lcc'><q id='E0lcc'></q></dir></style></legend>
            <i id='E0lcc'><tr id='E0lcc'><dt id='E0lcc'><q id='E0lcc'><span id='E0lcc'><b id='E0lcc'><form id='E0lcc'><ins id='E0lcc'></ins><ul id='E0lcc'></ul><sub id='E0lcc'></sub></form><legend id='E0lcc'></legend><bdo id='E0lcc'><pre id='E0lcc'><center id='E0lcc'></center></pre></bdo></b><th id='E0lcc'></th></span></q></dt></tr></i><div id='E0lcc'><tfoot id='E0lcc'></tfoot><dl id='E0lcc'><fieldset id='E0lcc'></fieldset></dl></div>

                <tfoot id='E0lcc'></tfoot>

                  本文介绍了带有 std::vector 的 VBO的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我用 C++ 和 OpenGL 编写了一个模型加载器.我已经使用 std::vectors 来存储我的顶点数据,但现在我想将它传递给 glBufferData(),但是数据类型却大不相同.我想知道是否有办法在 std::vectorglBufferData() 的文档const GLvoid * 之间进行转换.

                  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(), however the data types are wildly different. I want to know if there's a way to convert between std::vector to the documented const GLvoid * for glBufferData().

                  typedef struct
                  {
                      float x, y, z;
                      float nx, ny, nz;
                      float u, v;
                  }
                  Vertex;
                  
                  vector<Vertex> vertices;
                  

                  glBufferData() 调用

                  glBufferData(GL_ARRAY_BUFFER, vertices.size() * 3 * sizeof(float), vertices, GL_STATIC_DRAW);
                  

                  我收到以下(预期的)错误:

                  I get the following (expected) error:

                  error: cannot convert ‘std::vector<Vertex>’ to ‘const GLvoid*’ in argument passing
                  

                  如何将向量转换为与 glBufferData() 兼容的类型?

                  How can I convert the vector to a type compatible with glBufferData()?

                  注意.我现在不在乎正确的内存分配;vertices.size() * 3 * sizeof(float) 很可能会出现段错误,但我想先解决类型错误.

                  NB. I don't care about correct memory allocation at the moment; vertices.size() * 3 * sizeof(float) will most likely segfault, but I want to solve the type error first.

                  推荐答案

                  如果你有一个 std::vectorv,你可以获得一个 T* 指向连续数据的开始(这是 OpenGL 所追求的),表达式 &v[0].

                  If you have a std::vector<T> 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].

                  就您而言,这意味着将 Vertex* 传递给 glBufferData:

                  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
                  );
                  

                  <小时>

                  您可以在此处依赖从 Vertex*void const* 的隐式转换;这应该不会造成问题.


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

                  这篇关于带有 std::vector 的 VBO的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:如何仅使用 OpenGL 方法绘制文本? 下一篇:OpenGL GL_POLYGON 凹多边形不着色

                  相关文章

                  最新文章

                      <bdo id='AH8WS'></bdo><ul id='AH8WS'></ul>

                      <small id='AH8WS'></small><noframes id='AH8WS'>

                      <i id='AH8WS'><tr id='AH8WS'><dt id='AH8WS'><q id='AH8WS'><span id='AH8WS'><b id='AH8WS'><form id='AH8WS'><ins id='AH8WS'></ins><ul id='AH8WS'></ul><sub id='AH8WS'></sub></form><legend id='AH8WS'></legend><bdo id='AH8WS'><pre id='AH8WS'><center id='AH8WS'></center></pre></bdo></b><th id='AH8WS'></th></span></q></dt></tr></i><div id='AH8WS'><tfoot id='AH8WS'></tfoot><dl id='AH8WS'><fieldset id='AH8WS'></fieldset></dl></div>
                    1. <legend id='AH8WS'><style id='AH8WS'><dir id='AH8WS'><q id='AH8WS'></q></dir></style></legend>
                      <tfoot id='AH8WS'></tfoot>