Reputation: 89
I am trying to draw a simple cube with vbos. But when I run my program I dont get the expected result. Whereas being textured, my cube is fully opaque with one color of my texture...
You can see below my code :
loading of the texture with 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);
}
}
Generation of the vertices of the cube:
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());
}
}
Cube Drawing:
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);
}
Load of the cube into 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);
}
Upvotes: 0
Views: 75
Reputation: 211229
The data type of the vertices is float
, so the buffer offset is BUFFER_OFFSET(vertices_m.size() * sizeof(float))
, instead of sizeof(double)
.
glVertexAttribPointer(
2, 2, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(vertices_m.size() * sizeof(float)));
Are you sure that the attribute index of the texture coordinates is 2 ? Note, the vertex attribute index can be set by Layout Qualifiers (layout(location = 2) in vec2 texCoord;
) or it can be get by glGetAttribLocation
from the shader program object.
The 3rd parameter of glDrawArrays
is the number of the vertices, but not the number of elements in the array:
glDrawArrays(GL_TRIANGLES, 0, vertices_m.size() / 3);
Further I recommend to enable the Depth test, before drawing the geometry. Since the default depth function is GL_LESS
, this causes that the geometry which is closer to the view position covers the geometry behind it.
glEnable( GL_DEPTH_TEST );
Upvotes: 1