【发布时间】:2020-01-08 11:05:15
【问题描述】:
我正在尝试将纹理图像映射到球体。
我在不同的向量中有顶点和纹理坐标,这就是我使用glBufferSubData的原因。
std::vector<glm::vec3> sphere_vertices;
std::vector<int> sphere_indices;
std::vector<glm::vec2> sphere_texcoords;
我不使用任何颜色,只使用顶点、索引、纹理。
我正在使用:
// upload geometry to GPU
glBindVertexArray(sphere_VAO);
glGenBuffers(1, &sphere_vertices_VBO);
glBindBuffer(GL_ARRAY_BUFFER, sphere_vertices_VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(float) * sphere_vertices.size() + sizeof(float) * sphere_texcoords.size(),
0, GL_STATIC_DRAW);
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(float) * sphere_vertices.size(), sphere_vertices.data());
glBufferSubData(GL_ARRAY_BUFFER, sizeof(float) * sphere_vertices.size(), sizeof(float) * sphere_texcoords.size(), sphere_texcoords.data());
glGenBuffers(1, &sphere_indices_VBO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, sphere_indices_VBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(int) * sphere_indices.size(), sphere_indices.data(), GL_STATIC_DRAW);
// setup vertex attributes
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
// texture coords attrib
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(float) , (void*)(sizeof(float) * sphere_vertices.size()));
并且图像没有正确映射到球体上。
我收到的图片:
-- 更新 ---
我将sphere_texcoords.push_back((glm::vec2((x + 1) / 2.0, (y + 1) / 2.0))); 用于 texcoords,现在可以使用了!
【问题讨论】: