简体   繁体   English

glVertexAttribBinding的顺序绘制命令无法按预期工作

[英]Sequential draw commands with glVertexAttribBinding not working as expected

I have a struct Vertex{glm::vec4 t,n,v;}. 我有一个结构顶点{glm :: vec4 t,n,v;}。 I have written a obj loader that takes two parameters, obj file path as string and reference to a vector of 'Vertex'es. 我已经编写了一个obj loader,它使用两个参数,obj文件路径作为字符串和对“顶点”向量的引用。 This function populates my vector and returns the number of indices(In my case indices are just sequential numbers, anyway). 这个函数填充我的向量并返回索引的数量(在我的情况下,索引只是顺序编号)。

As I have 6 objects to render, after using that function 6 times I have the following 因为我有6个对象要渲染,所以在使用该函数6次之后,我得到以下内容

vector<Vertex> objects[6];
GLint SIZES[6],OFFSETS[6],SIZES_I[6],OFFSETS_I[6];

filled. 填充。 SIZES are number of 'Vertex'es(object[i].size()) and SIZES_I are number of indices. SIZES是“顶点”(object [i] .size())的数量,SIZES_I是索引的数量。 The offsets are calculated as below 偏移量计算如下

for(int i=0;i<6;i++)
{
  if(i==0)
  {
    OFFSETS[0]=0;OFFSETS_I[0]=0;
  }
  else
  {
    OFFSETS[i]=OFFSETS[i-1]+SIZES[i-1];
    OFFSETS_I[i]=OFFSETS_I[i-1]+SIZES_S[i-1];
  }
}

I transferred vectors of Vertex into a single VBO all back to back. 我将Vertex的向量背靠背转移到单个VBO中。 Similarly for indices, transferred into buffer bound to element array buffer. 同样对于索引,转移到绑定到元素数组缓冲区的缓冲区中。 That part is shown below. 该部分如下所示。

glBufferData(GL_ARRAY_BUFFER,(OFFSETS[5]+SIZES[5])*sizeof(Vertex),data,GL_STATIC_DRAW);
for(int i=0;i<6;i++)
{
  glBindVertexBuffer(i,buffer[0],OFFSETS[i]*sizeof(Vertex),sizeof(Vertex));
}

glBufferData(GL_ELEMENT_ARRAY_BUFFER,(OFFSETS_I[5]+SIZES_I[5])*sizeof(GLuint),indices,GL_STATIC_DRAW);
glVertexAttribFormat(0,4,GL_FLOAT,GL_FALSE,offsetof(Vertex,v));

Now comes my problem. 现在是我的问题。 Of the two rendering codes shown below the first one doesn't work but the second works perfectly. 在下面显示的两个渲染代码中,第一个无效,但是第二个完美。

for(int i=0;i<6;i++)
{
  glVertexAttribBinding(0,i);
  glDrawElements(GL_TRIANGLES,SIZES_I[i],GL_UNSIGNED_INT,reinterpret_cast<void*>(OFFSETS_I[i]*sizeof(GLuint));
}

//second

glVertexAttribBinding(0,0);
for(int i=0;i<6;i++)
  glDrawElementsBaseVertex(GL_TRIANGLES,SIZES_I[i],GL_UNSIGNED_INT,reinterpret_cast<void*>(OFFSETS_I[i]*sizeof(GLuint)),OFFSETS[i]);

To summarily say what I have done so u guys can understand whats going on here, in the first case i created 6 buffer bindings on the same buffer with 6 offsets. 概括地说,我做了些什么,以便你们能理解这里发生的事情,在第一种情况下,我在具有6个偏移量的同一缓冲区上创建了6个缓冲区绑定。 In the second case there is only one binding, but I used base vertex to offset 6 times. 在第二种情况下,只有一个绑定,但是我使用基本顶点偏移了6次。 Btw both are compiling, so ignore any typos as I typed all this in my tab. 顺便说一句,两者都在编译,所以当我在选项卡中键入所有这些错误时,请忽略任何错别字。

My first attempt at debugging: Since base vertex approach is working, obj loader is fine. 我的首次调试尝试:由于基本顶点方法有效,因此obj loader很好。 Anyway to make sure of it, I just loaded a single model. 无论如何要确保它,我只是加载了一个模型。 Its working fine. 它的工作正常。

My second attempt at debugging: My suspicion naturally fell on binding calls and offsets. 我的第二次调试尝试:我的怀疑自然落在绑定调用和偏移量上。 To resolve this, I removed the for loop in the first method and initialized i with 0(Since second method ie base vertex method works, we dont bother with it). 为了解决这个问题,我在第一种方法中删除了for循环,并用0初始化了i(因为第二种方法(即基本顶点方法有效),所以我们不用理会)。 My first model appeared on screen. 我的第一个模特出现在屏幕上。 Next I initialized 'i' variable with 1. My second model was displayed on screen. 接下来,我用1初始化'i'变量。我的第二个模型显示在屏幕上。 This I repeated till i=5. 我一直重复到i = 5。 Each of my 6 models were displayed correctly. 我的6个模型中的每个模型均正确显示。 So my models were displayed individually. 所以我的模型是单独显示的。 But when I combine my calls sequentially, I get some full models, some partial models and some not at all. 但是,当我依次组合调用时,我得到了一些完整的模型,一些局部的模型以及一些根本没有的模型。

My third attempt at debugging: It seems that only last 2 models and so are displayed. 我的第三次调试尝试:似乎只显示最后2个模型,因此显示。 Remaining are not drawn or partially drawn. 其余未绘制或部分绘制。 So I reversed my for loop starting with i=5 and decrementing. 因此,我从i = 5开始递减for循环并递减。 Now first 2 models and so are displayed(Here 'first two' and 'last two' refers to the order in which models are loaded in obj reader. I did not change that). 现在显示前两个模型,以此类推(这里的“前两个”和“后两个”指的是在obj reader中加载模型的顺序。我没有改变)。 It is as if the subsequent drawing commands are somehow making the work of earlier drawing commands vanish. 好像后续的绘图命令以某种方式使早期绘图命令的工作消失了。 Sort of. 有点。

Thats it. 而已。 Here I hit a dead end. 在这里,我死胡同了。 Any ideas what might be wrong or how I might proceed with further debugging? 任何想法可能有什么问题,或者我应该如何进行进一步的调试?

Turned out that it is due to a bug in my driver. 原来,这是由于我的驱动程序中的错误所致。 The same code worked in my colleague's computer. 相同的代码在我同事的计算机上工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM