简体   繁体   English

Pointcloud渲染,交错VBO

[英]Pointcloud rendering, interleaved VBO

I am trying to render an vbo that has 7 components (interlaving right?). 我正在尝试渲染具有7个组件的vbo(互穿对吗?)。 The first 4 are x,y,z,w (w is a scaling factor) and the remaining 3 are to be used for brigthness adjustment in the fragment shader. 前4个是x,y,z,w(w是缩放因子),其余3个将用于片段着色器中的亮度调整。

This is how i create the vbo: 这就是我创建VBO的方式:

std::vector<float> pointCloudData;
while(endOfFile != true){

    ... // a lot of textfile handling/reading before, total of 100361 coordinates read. 

    for (int i = 0; i < 4; i++){
        pointCloudData.push_back(xyzFromFile[i]); //store the read x y z w coordinate
    }
    // store extra data
    // right now im setting everything to 1.f just to see results in fragment shader!
    pointCloudData.push_back(1.f);
    pointCloudData.push_back(1.f);
    pointCloudData.push_back(1.f);
}

v_size = pointCloudData.size();

// create vbo 
glGenVertexArrays(1, &_vaoID);
    glGenBuffers(1, &_vboID);
    glBindVertexArray(_vaoID);
    glBindBuffer(GL_ARRAY_BUFFER, _vboID);
    glBufferData(GL_ARRAY_BUFFER, v_size*sizeof(GLfloat), &pointCloudData[0], GL_DYNAMIC_DRAW);
glBindVertexArray(0);

In rendering method: 在渲染方法中:

    glBindVertexArray(_vaoID);
    glBindBuffer(GL_ARRAY_BUFFER, _vboID);
    glEnableVertexAttribArray(positionAttrib);

    GLsizei stride = sizeof(GLfloat) * 7;
    glVertexAttribPointer(positionAttrib, 4, GL_FLOAT, GL_FALSE, stride, (void*)0); // x y z
    glVertexAttribPointer(brightnessDataAttrib, 3, GL_FLOAT, GL_FALSE, stride, (void*)(4 * sizeof(GLfloat))); // remaining data

    glDrawArrays(GL_POINTS, 0, vertsToDraw); 
    glDisableVertexAttribArray(positionAttrib);
    glBindBuffer(GL_ARRAY_BUFFER, 0);
    glBindVertexArray(0);

Vertex shader: 顶点着色器:

layout(location = 0) in vec4 in_position;
layout(location = 2) in vec3 in_brightness;

out vec3 vs_brightness;
void main()
{ 
    vs_brightness = in_brightness;
    gl_Position =  ViewProjection * vs_position;
}

Fragment shader: 片段着色器:

in vec3 vs_brightness;
void main(void)
{
    diffuse = vec4(vs_brightness[1],0,0,1); // at this point i just want SOMETHING to show up
}

If this is a bad way, how can I do it differently? 如果这是一个不好的方法,我该怎么做呢? How can I send the remaining 3 scalars to the fragment shader? 如何将剩余的3个标量发送到片段着色器? (its a total of 3 * 100361 scalars). (总计3 * 100361标量)。

This mostly looks ok. 这基本上看起来还可以。 You're never enabling the second vertex attribute, though. 但是,您永远不会启用第二个顶点属性。 You'll need this somewhere in your setup code: 您将在安装代码中的某处需要它:

glEnableVertexAttribArray(brightnessDataAttrib);

This assumes that positionAttrib is 0 and brightnessDataAttrib is 2, matching the locations in the vertex shader. 假设positionAttrib为0, brightnessDataAttrib为2,与顶点着色器中的位置匹配。

While not a correctness issue, you're not taking good advantage of the VAO. 虽然不是正确性问题,但您没有充分利用VAO。 During setup, you create and bind a VAO, but then don't set up any of the state that is tracked in a VAO. 在设置过程中,您将创建并绑定VAO,但不要设置VAO中跟踪的任何状态。 Instead, you have all of that in the draw call. 取而代之的是,所有这些都在平局中。 This defeats the purpose of using a VAO. 这违反了使用VAO的目的。 The idea is that it holds on to all the vertex setup state, and then you can get all the necessary state for drawing by simply binding the VAO again. 想法是,它保留所有顶点设置状态,然后您可以通过再次绑定VAO来获得绘制所需的所有状态。

So in the setup code, a better structure looks like this: 因此,在设置代码中,一个更好的结构如下所示:

glBindBuffer(GL_ARRAY_BUFFER, vboID);
glBufferData(GL_ARRAY_BUFFER, ...);

glBindVertexArray(vaoID);
glEnableVertexAttribArray(...);
glVertexAttribPointer(...);
glBindVertexArray(0);

glBindBuffer(GL_ARRAY_BUFFER, 0);

Then in the draw code, you only need this: 然后在绘制代码中,您只需要这样做:

glBindVertexArray(vaoID);
glDrawArrays(...);
glBindVertexArray(0);

The glBindVertexArray(0) calls aren't really necessary if you're using VAOs in all of your code, since everybody will be binding the necessary VAO before drawing. 如果您在所有代码中都使用VAO,则实际上不需要glBindVertexArray(0)调用,因为每个人在绘制之前都会绑定必要的VAO。 Some people like to have the explicit unbind just in case other parts of the code want to render without a VAO. 有些人喜欢显式取消绑定,以防代码的其他部分想要在不使用VAO的情况下进行渲染。 But it's a waste otherwise. 但这是浪费。

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

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