首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用glVertexAttribPointer将两个单独的数组附加到着色器?

问如何使用glVertexAttribPointer将两个单独的数组附加到着色器?
EN

Stack Overflow用户
提问于 2020-03-22 15:27:48
回答 1查看 123关注 0票数 1

我有两个独立的数组。一个用来持久化纹理和弦,另一个用来持久化地形顶点。现在我正试图相互依恋。

我使用过glVertexAttribPointer,但它不起作用。

下面是我正在努力实现的一个例子:

使用这段代码,我试图生成ids。

代码语言:javascript
复制
GLuint VAO, VBO, EBO;
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
glGenBuffers(1, &EBO);

在这里,我试图将VBO和EBO绑定到VAO。

代码语言:javascript
复制
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof newVertices, &newVertices, GL_STATIC_DRAW);

/*
 * Use EBO
 */
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof new_indices, &new_indices, GL_STATIC_DRAW); 

这些约束是完美的,但我遇到的问题是:

代码语言:javascript
复制
/* This attrib pointer is used to attach vertices */
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);

/* This attrib pointer is supposed to be texture coords */
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)3);
glEnableVertexAttribArray(1);

在上面,我生成用于附加顶点的VBO和EBO,但是如何在不改变顶点数据的情况下将另一个数组附加到glVertexAttribPointer?这可能吗?

下面是两个单独的数组:

代码语言:javascript
复制
const int w = 100;
const int h = 100;

const unsigned int s = w * h * 5;
float newVertices[s];
unsigned int new_indices[s];

// This array is used for texture coords
float textureCoords[] = {
    1.0f, 1.0f,
    1.0f, 0.0f,
    0.0f, 0.0f,
    0.0f, 1.0f
};


int index = 0;
for (size_t row = 0; row < h; row++)
{
    for (size_t col = 0; col < w; col++)
    {
        float height = get_height(col, row);
        newVertices[index++] = static_cast<float>(col) / 10;
        newVertices[index++] = height*10.f;
        newVertices[index++] = static_cast<float>(row) / 10;

        // I tried to attach texture coords but this isn't work
        newVertices[index++] = textureCoords[0];
        newVertices[index++] = textureCoords[1];
    }
}

// This is used to calculate indices
int i = 0;
for (int row = 0; row < h - 1; row++) {
    for (size_t col = 0; col < w; col++)
    {
        new_indices[i++] = col + row * h;
        new_indices[i++] = col + (row + 1) * h;
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-03-22 15:52:35

我在您的代码中看到了两个问题:

  1. 你写的是(void*)3而不是(void*)(3*sizeof(float))。请记住,glVertexAttribPointer的最后一个参数是以字节为单位的偏移量,因此您需要考虑数据类型的大小。
  2. 您的所有顶点都有相同的纹理坐标(1,1),因此您将对整个网格的颜色进行采样。不清楚如何使用textureCoords数组,因为它只包含四个点。您需要为网格的每个顶点指定不同的坐标。这样做的一种方法是:

//我试图附加纹理和弦,但这不是工作,newVerticesindex++ =(浮)col/w;newVerticesindex++ =(浮)行/h;

但是,由于这是对X和Z坐标的简单转换,所以可以在顶点着色器中轻松地进行转换,而无需引入texcoord属性。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60801195

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档