使用VBO绘制线串 [英] Using VBO to draw line string

查看:106
本文介绍了使用VBO绘制线串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我能够使用OpenGL 4.x中的VBO和着色器来渲染以下点.

I am able to render the below points using the VBO and shaders in OpenGL 4.x.

    typedef struct Point
    {
        double x,y,z,w;
    }Point;

    std::vector<Point>vPoints;

    glBufferData(GL_ARRAY_BUFFER,  vPoints.size()* sizeof(Point),  &vPoints[0], GL_STATIC_DRAW);
    glVertexAttribPointer(0, 4, GL_DOUBLE, GL_FALSE, vPoints.size()*sizeof(GLdouble), (GLvoid*)0)

我们如何指定VBO使用以下变量绘制线串

How do we specify VBO to draw a line string using below variables

    typedef struct LineString
    {
     vector<Point> vPointList;

    }LineString;
    vector<LineString> vLines;
    glBufferData(GL_ARRAY_BUFFER,  ????,  ????, GL_STATIC_DRAW);
    glVertexAttribPointer(0, 4, GL_DOUBLE, GL_FALSE, ????, (GLvoid*)0)

我尝试了以下设置,但似乎不起作用

I tried below setting and it does not seem to work

   glBufferData(GL_ARRAY_BUFFER,  vLines.size()* sizeof(LineString)*sizeof(Point),  &vLines[0].vPointList[0], GL_STATIC_DRAW);
   glVertexAttribPointer(0, 4, GL_DOUBLE, GL_FALSE, vLines.size()*sizeof(GLdouble), (GLvoid*)0);   

我想我几乎可以用下面的代码来创建VBO

I thought I almost got there with below code for VBO creation

         for(int i=0;i< vLines.size();i++)
           pt_count += vLines[i].vPointList.size();

  fprintf( stdout, "Total point Count: %d\n", pt_count);    
  glBufferData(GL_ARRAY_BUFFER, pt_count * sizeof(Point), nullptr, GL_STATIC_DRAW);

size_t start = 0;
for(int i=0;i< vLines.size();i++)
{
    size_t v_size =vLines[i].vPointList.size() * sizeof(Point);
    glBufferSubData(GL_ARRAY_BUFFER, start, v_size, &vLines[i].vPointList[0]);
    start += v_size;
}

if(start == pt_count * sizeof(Point) )
{
    fprintf( stdout, "INFO: %s\n", "Same count");   

}

glVertexAttribPointer(0,4,GL_DOUBLE,GL_FALSE,4 * sizeof(GLdouble),(GLvoid *)0);

glVertexAttribPointer(0, 4, GL_DOUBLE, GL_FALSE, 4 * sizeof(GLdouble), (GLvoid*)0);

和绘图调用.我注意到第一条线的最后一点正连接到该线的第一点.

and drawing call.I noticed that the last point of first line is getting connected to first point of the line.

    glDrawArrays(GL_LINE_STRIP, 0,pt_count);

推荐答案

glBufferData

对于glBufferData,需要以字节为单位的数据总大小.此外,需要连续的内存段,而vector<vector<T>>不提供该连续的内存段.从技术上讲,每个内部向量都有其自己的存储数据的存储段,这使得无法一次上传数据.我想到的唯一方法是,首先像这样计算总大小:

For glBufferData, the total size of the data in bytes is required. In addition a consecutive memory segment is required, which a vector<vector<T>> does not provide. Technically, each inner vector has it's own memory segment where the data is stored, which makes it impossible to upload the data at once. The only way that comes to my mind is to first calculate the total size like this:

size_t pt_count = 0;
for (auto& v : vLines)
    pt_count += v.vPointList.size();

分配GPU内存,但不上传任何数据

The allocate GPU memory, but without uploading any data

glBufferData(GL_ARRAY_BUFFER, pt_count * sizeof(Point), nullptr, GL_STATIC_DRAW);

然后最后一步是逐步上传所有点:

The final step is then to upload all the points step by step:

size_t start = 0;
for (auto& v : vLines)
{
    size_t v_size = v.vPointList.size() * sizeof(Point);
    glBufferSubData(GL_ARRAY_BUFFER, start, v_size, &v.vPointList[0]);
    start += v_size;
}

但是,如果您可以更改C ++代码中的数据布局,我强烈建议您首先将所有点存储在连续的内存段中.

But if you can change the data layout in the C++ code, I would strongly advice you to store all the points in a consecutive memory segment in the first place.

glVertexAttribPointer

glVertexAttribPointer的第五个参数告诉OpenGL缓冲区中连续顶点之间的偏移量.因此,请考虑这样的缓冲区:

The fifth parameter of glVertexAttribPointer tells OpenGL how much offset is between consecutive vertices in the buffer. So think of a buffer like this:

        | x | y | z | w | x | y | z | w |
stride  |-------------->|-------------->|

这是必需的,因为有可能在两个顶点条目之间存储附加数据.在您的情况下,偏移量必须为4 * sizeof(GLdouble),或者由于数据被紧密打包,所以0:

this is needed, since is is possible, that between two vertex entries additional data is stored. In your case, the offset has to be 4 * sizeof(GLdouble), or since the data is tightly packed, 0:

指定连续的通用顶点属性之间的字节偏移.如果stride为0,则将通用顶点属性理解为紧密包装在数组中.初始值为0. 参考

Specifies the byte offset between consecutive generic vertex attributes. If stride is 0, the generic vertex attributes are understood to be tightly packed in the array. The initial value is 0. reference

这篇关于使用VBO绘制线串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆