Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/opengl/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Opengl 用VBO绘制线串_Opengl_Vector_Shader_Vbo_Polyline - Fatal编程技术网

Opengl 用VBO绘制线串

Opengl 用VBO绘制线串,opengl,vector,shader,vbo,polyline,Opengl,Vector,Shader,Vbo,Polyline,我能够使用OpenGL 4.x中的VBO和着色器渲染以下点 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); glVertexA

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

    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创建代码就快到了

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

}

glBufferData

对于glBufferData,需要数据的总大小(以字节为单位)。此外,还需要一个连续的内存段,
向量
不提供该段。从技术上讲,每个内部向量都有自己的存储数据的内存段,因此无法立即上传数据。我想到的唯一方法是首先像这样计算总尺寸:

size_t pt_count = 0;
for (auto& v : vLines)
    pt_count += v.vPointList.size();
        | x | y | z | w | x | y | z | w |
stride  |-------------->|-------------->|
分配GPU内存,但不上载任何数据

glBufferData(GL_ARRAY_BUFFER, pt_count * sizeof(Point), nullptr, GL_STATIC_DRAW);
最后一步是逐步上传所有点:

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++代码中的数据布局,我强烈建议你首先把所有的点存储在一个连续的内存段。 glvertexattributepointer

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

size_t pt_count = 0;
for (auto& v : vLines)
    pt_count += v.vPointList.size();
        | x | y | z | w | x | y | z | w |
stride  |-------------->|-------------->|
这是必需的,因为可能在两个顶点条目之间存储额外的数据。在您的情况下,偏移量必须为4*sizeof(GLdouble),或者因为数据是紧密压缩的,所以0:

指定连续常规顶点属性之间的字节偏移量。如果跨距为0,则一般顶点属性会被理解为在数组中紧密排列。初始值为0。


可以通过使用glMultiDrawArrays()解决此问题…通过正确指定索引位置找到解决方案。修复后的代码如下所示

//globals
size_t pt_count = 0;
GLint  *startIndices;
GLint *endIndices;
GLint nLineCount;


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


  startIndices= new GLint[vLines.size()];
  endIndices= new GLint[vLines.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;
    size_t firsts=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;

        startIndices[i]=firsts;
        endIndices[i]=vLines[i].vPointList.size();
        firsts+=endIndices[i];
        fprintf( stdout, "start: %d\n",startIndices[i]);
        fprintf( stdout, "size: %d\n", endIndices[i] );

    } 


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

  nLineCount=vLines.size();
  vLines.clear();
 //Draw the lines
 glMultiDrawArrays(GL_LINE_STRIP, startIndices,endIndices,nLineCount);  
//全局
大小\u t pt\u计数=0;
闪烁的星光;
闪烁指数;
闪烁线性计数;
//创建VBO
对于(int i=0;i
size\t pt\u count=0;对于(int i=0;i//globals size_t pt_count = 0; GLint *startIndices; GLint *endIndices; GLint nLineCount; //create the VBO for(int i=0;i< vLines.size();i++) pt_count += vLines[i].vPointList.size(); startIndices= new GLint[vLines.size()]; endIndices= new GLint[vLines.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; size_t firsts=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; startIndices[i]=firsts; endIndices[i]=vLines[i].vPointList.size(); firsts+=endIndices[i]; fprintf( stdout, "start: %d\n",startIndices[i]); fprintf( stdout, "size: %d\n", endIndices[i] ); } 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); nLineCount=vLines.size(); vLines.clear(); //Draw the lines glMultiDrawArrays(GL_LINE_STRIP, startIndices,endIndices,nLineCount);