我有两个独立的数组。一个用来持久化纹理和弦,另一个用来持久化地形顶点。现在我正试图相互依恋。
我使用过glVertexAttribPointer,但它不起作用。
下面是我正在努力实现的一个例子:
使用这段代码,我试图生成ids。
GLuint VAO, VBO, EBO;
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
glGenBuffers(1, &EBO);在这里,我试图将VBO和EBO绑定到VAO。
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); 这些约束是完美的,但我遇到的问题是:
/* 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?这可能吗?
下面是两个单独的数组:
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;
}
}发布于 2020-03-22 15:52:35
我在您的代码中看到了两个问题:
(void*)3而不是(void*)(3*sizeof(float))。请记住,glVertexAttribPointer的最后一个参数是以字节为单位的偏移量,因此您需要考虑数据类型的大小。textureCoords数组,因为它只包含四个点。您需要为网格的每个顶点指定不同的坐标。这样做的一种方法是://我试图附加纹理和弦,但这不是工作,newVerticesindex++ =(浮)col/w;newVerticesindex++ =(浮)行/h;
但是,由于这是对X和Z坐标的简单转换,所以可以在顶点着色器中轻松地进行转换,而无需引入texcoord属性。
https://stackoverflow.com/questions/60801195
复制相似问题