我试图用vbos绘制一个简单的立方体。但是,当我运行程序时,没有得到预期的结果。尽管具有纹理,但我的立方体是完全不透明的,只有一种颜色...
您可以在我的代码下面看到:
使用SFML加载纹理:
void Grass::load() {
sf::Image texture;
if (!texture.loadFromFile(path_m))
std::cerr << "Error while loading texture : " << path_m << std::endl;
else {
glGenTextures(1, &id_m);
glBindTexture(GL_TEXTURE_2D, id_m);
{
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, texture.getSize().x,
texture.getSize().y, 0, GL_RGBA, GL_UNSIGNED_BYTE,
texture.getPixelsPtr());
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
}
glBindTexture(GL_TEXTURE_2D, 0);
}
}
生成立方体的顶点:
void Chunck::generate() {
{
std::vector vec = { -1.0, -1.0, -1.0, 1.0, -1.0, -1.0, 1.0, 1.0, -1.0, // Face 1
-1.0, -1.0, -1.0, -1.0, 1.0, -1.0, 1.0, 1.0, -1.0, // Face 1
1.0, -1.0, 1.0, 1.0, -1.0, -1.0, 1.0, 1.0, -1.0, // Face 2
1.0, -1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, -1.0, // Face 2
-1.0, -1.0, 1.0, 1.0, -1.0, 1.0, 1.0, -1.0, -1.0, // Face 3
-1.0, -1.0, 1.0, -1.0, -1.0, -1.0, 1.0, -1.0, -1.0, // Face 3
-1.0, -1.0, 1.0, 1.0, -1.0, 1.0, 1.0, 1.0, 1.0, // Face 4
-1.0, -1.0, 1.0, -1.0, 1.0, 1.0, 1.0, 1.0, 1.0, // Face 4
-1.0, -1.0, -1.0, -1.0, -1.0, 1.0, -1.0, 1.0, 1.0, // Face 5
-1.0, -1.0, -1.0, -1.0, 1.0, -1.0, -1.0, 1.0, 1.0, // Face 5
-1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, -1.0, // Face 6
-1.0, 1.0, 1.0, -1.0, 1.0, -1.0, 1.0, 1.0, -1.0 }; // Face 6
vertices_m.insert(vertices_m.begin(), vec.begin(), vec.end());
}
{
std::vector vec = {0, 0, 1, 0, 1, 1, // Face 1
0, 0, 0, 1, 1, 1, // Face 1
0, 0, 1, 0, 1, 1, // Face 2
0, 0, 0, 1, 1, 1, // Face 2
0, 0, 1, 0, 1, 1, // Face 3
0, 0, 0, 1, 1, 1, // Face 3
0, 0, 1, 0, 1, 1, // Face 4
0, 0, 0, 1, 1, 1, // Face 4
0, 0, 1, 0, 1, 1, // Face 5
0, 0, 0, 1, 1, 1, // Face 5
0, 0, 1, 0, 1, 1, // Face 6
0, 0, 0, 1, 1, 1}; // Face 6
texture_m.insert(texture_m.begin(), vec.begin(), vec.end());
}
}
立方体图:
void Chunck::draw() const {
glBindBuffer(GL_ARRAY_BUFFER, vboID_m);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0));
glEnableVertexAttribArray(0);
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(vertices_m.size() * sizeof(double)));
glEnableVertexAttribArray(2);
glBindTexture(GL_TEXTURE_2D, grassTexture_m.getID());
glDrawArrays(GL_TRIANGLES, 0, vertices_m.size());
glBindTexture(GL_TEXTURE_2D, 0);
glDisableVertexAttribArray(2);
glDisableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
将多维数据集加载到VBO:
void Chunck::load() {
grassTexture_m.load();
if (glIsBuffer(vboID_m) == GL_TRUE)
glDeleteBuffers(1, &vboID_m);
glGenBuffers(1, &vboID_m);
glBindBuffer(GL_ARRAY_BUFFER, vboID_m);
{
glBufferData(GL_ARRAY_BUFFER, (vertices_m.size() + texture_m.size()) * sizeof(float), 0,
GL_STATIC_DRAW);
glBufferSubData(GL_ARRAY_BUFFER, 0, vertices_m.size() * sizeof(float), vertices_m.data());
glBufferSubData(GL_ARRAY_BUFFER, vertices_m.size() * sizeof(float),
texture_m.size() * sizeof(float), texture_m.data());
}
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
答案 0 :(得分:1)
顶点的数据类型为float
,因此缓冲区偏移为BUFFER_OFFSET(vertices_m.size() * sizeof(float))
,而不是sizeof(double)
。
glVertexAttribPointer(
2, 2, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(vertices_m.size() * sizeof(float)));
您确定纹理坐标的属性索引为 2 吗?注意,顶点属性索引可以由Layout Qualifiers(layout(location = 2) in vec2 texCoord;
)设置,也可以由glGetAttribLocation
获取
从着色器程序对象开始。
glDrawArrays
的第3个参数是顶点的数目,而不是数组中元素的数目:
glDrawArrays(GL_TRIANGLES, 0, vertices_m.size() / 3);
此外,我建议在绘制几何图形之前启用Depth test。由于默认的深度函数为GL_LESS
,因此会导致更靠近视图位置的几何图形覆盖了它后面的几何图形。
glEnable( GL_DEPTH_TEST );