Drawable.cpp:
(…)
void Drawable::initGLContent(std::vector<float>& vertices, std::vector<unsigned int>& indices) {
glGenBuffers(1, &_VBO);
glGenVertexArrays(1, &_VAO);
glGenBuffers(1, &_EBO);
if (_EBO == 0) {
std::cerr << "glGenBuffers failed!" << std::endl;
}
glBindVertexArray(_VAO);
glBindBuffer(GL_ARRAY_BUFFER, _VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices[0]) * vertices.size(), &vertices[0], GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _EBO);
GLint currentEBO;
glGetIntegerv(GL_ELEMENT_ARRAY_BUFFER_BINDING, ¤tEBO);
std::cout << "After binding, EBO is: " << currentEBO << std::endl;
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices[0]) * indices.size(), &indices[0], GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)(3 * sizeof(float)));
glEnableVertexAttribArray(1);
glBindVertexArray(0);
}
collidable.cpp:
#include "collidable.hpp"
namespace eng
{
Texture* Collidable::_defaultTexture = nullptr;
std::vector<unsigned int> Collidable::_indices {
0, 1,
1, 2,
2, 3,
3, 0, // square
4, 5,
5, 6,
6, 7,
7, 4, // second square
0, 4,
1, 5,
2, 6,
3, 7
};
Collidable::Collidable(Shader& shader) : Drawable(shader) {
if(!_defaultTexture) {
_defaultTexture = new Texture("assets/default.png");
}
populateVertices();
initGLContent(_vertices, _indices);
_position = glm::vec3(0.0f, 0.0f, 0.0f);
_size = glm::vec3(0.0f, 0.0f, 1.0f);
}
Collidable::~Collidable() {
}
void Collidable::draw(GLFWwindow* window) const{
static bool first = true;
auto texStart = std::array<float, 2>{1.0f, 1.0f};
auto texRatio = std::array<float, 2>{1.0f, 1.0f};
_shader.setVec2f("texStart", texStart);
_shader.setVec2f("texRatio", texRatio);
_shader.setBool("flipX", false);
glBindTexture(GL_TEXTURE_2D, _defaultTexture->getGlTextureName());
glBindVertexArray(_VAO);
GLint currentEBO = 0;
glGetIntegerv(GL_ELEMENT_ARRAY_BUFFER_BINDING, ¤tEBO);
if(first) {
first = false;
std::cout << "[Collidable::draw] bound EBO: " << currentEBO << std::endl;
}
glPointSize(5.0f);
glDrawElements(GL_LINES, _indices.size(), GL_UNSIGNED_INT, 0);
glBindVertexArray(0);
}
}
sprite.cpp
std::vector<unsigned int> Sprite::_indices{0,1,2,0,2,3};
Sprite::Sprite(const Texture& texture, Shader& shader) : _texture(texture), Drawable(shader),
_vertices{0.0f, 0.0f, 0.0f, 0.0f, 0.0f,
1.0f, 0.0f, 0.0f, 1.0f, 0.0f,
1.0f, 1.0f, 0.0f, 1.0f, 1.0f,
0.0f, 1.0f, 0.0f, 0.0f, 1.0f} {
initGLContent(_vertices, _indices);
_position = glm::vec3(0.0f, 0.0f, 0.0f);
_size = glm::vec3(0.0f, 0.0f, 0.0f);
}
void Sprite::draw(GLFWwindow* window) const {
static int first = 0;
auto texMin = std::array<float, 2>{1.0f, 1.0f};
auto texRatio = std::array<float, 2>{1.0f, 1.0f};
_shader.setVec2f("texStart", texMin);
_shader.setVec2f("texRatio", texRatio);
_shader.setBool("flipX", false);
glBindTexture(GL_TEXTURE_2D, _texture.getGlTextureName());
glBindVertexArray(_VAO);
GLint currentEBO = 0;
glGetIntegerv(GL_ELEMENT_ARRAY_BUFFER_BINDING, ¤tEBO);
if(first < 46) {
first++;
std::cout << "[Sprite::draw] bound EBO: " << currentEBO << std::endl;
}
glDrawElements(GL_TRIANGLES, _indices.size(), GL_UNSIGNED_INT, 0);
glBindVertexArray(0);
}
when running i get logs:
After binding, EBO is: 2
After binding, EBO is: 4
After binding, EBO is: 6
After binding, EBO is: 8
…
After binding, EBO is: 92
After binding, EBO is: 94
After binding, EBO is: 96
And
[Sprite::draw] EBO bound with VAO: 2
[Sprite::draw] EBO bound with VAO: 4
[Sprite::draw] EBO bound with VAO: 6
…
[Sprite::draw] EBO bound with VAO: 88
[Sprite::draw] EBO bound with VAO: 90
[Sprite::draw] EBO bound with VAO: 94
[Collidable::draw] EBO bound with VAO: 0
If I understand vao’s and ebo’s ways of working correctly, i bind ebo to vao once and then everytime i want to draw something i bind only vao. I checked if maybe my indices vectors are the same or something strange with memory happens, but all pointers were valid during testing and all the data stays valid as well. I’m out of ideas, i’m sure it’s just my lack of understanding, I’m completely lost.