1

Excuse my noobery, but I am one. I am currently trying to figure a way to keep track of VAO and VBO id's so that I can run through them using for loops and tell OpenGL to delete them. My current method is to push them into vectors and run through them backwards at termination, which has been resulting in a segfault. Using vectors has been giving me this warning at compilation time:

lou@debian:~/Development/GamEngine$ make
src/model.cpp: In static member function ‘static void Model::_cleanup()’:
src/model.cpp:39:27: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
   39 |   glDeleteVertexArrays(1, (unsigned int*)_vaos[i]);
src/model.cpp:43:22: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
   43 |   glDeleteBuffers(1, (unsigned int*)_vbos[i]);

I'm thinking I might be able to use a destructor for the model class, and have each model delete its own VAO and VBO, because each one keeps track of these. Here is Model::_cleanup():

void Model::_cleanup()
{
    // Clean VAOs
    for(int i = _vaos.size(); i > 0; i--) {
        glDeleteVertexArrays(1, (unsigned int*)_vaos[i]);
    }
    // Clean VBOs
    for(int i = _vbos.size(); i > 0; i--) {
        glDeleteBuffers(1, (unsigned int*)_vbos[i]);
    }
}

Here is model.hpp:

#ifndef __MODEL_HPP__
#define __MODEL_HPP__

#include <glad/glad.h> 
#include <vector>

using std::vector;

class Model
{

public:
    Model(float* p_pos);
    unsigned int getVaoID() { return m_vaoID; }
    unsigned int getVboID() { return m_vboID; }
    unsigned int getVertexCount() { return m_vertexCount; }

    static void _cleanup();

private:
    unsigned int m_vaoID;
    unsigned int m_vboID;
    unsigned int m_vertexCount;

    void m_createVAO(int p_attribNum, float* p_data);
    void m_unbindVAO();

    void m_createVBO();
    void m_unbindVBO();

    static vector<unsigned int> _vaos;
    static vector<unsigned int> _vbos;

};

#endif // __MODEL_HPP__
Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
Lousifr
  • 23
  • 4
  • 1
    There is missing the address-of (`&`) operator `glDeleteVertexArrays(1, &_vaos[i]);`. Alternatively you can do `glDeleteVertexArrays(1, _vaos.data()+i);`. Anyway, what's wrong with `glDeleteVertexArrays(_vaos.size(), _vaos.data());`?. See [`std::vector`](http://www.cplusplus.com/reference/vector/vector/) – Rabbid76 Aug 30 '20 at 08:47
  • Nice hey thx, new way to do it. I thought I had to use for loops. As I said, super noob here. – Lousifr Aug 30 '20 at 09:00

1 Answers1

2

There is missing the address-of (&) operator

glDeleteVertexArrays(1, (unsigned int*)_vaos[i]);

glDeleteVertexArrays(1, &_vaos[i]);

Alternatively you can do:

glDeleteVertexArrays(1, _vaos.data()+i);

Anyway, you don't need the for loops at all (see std::vector):

void Model::_cleanup()
{
    glDeleteVertexArrays(_vaos.size(), _vaos.data());
    glDeleteBuffers(_vbos.size(), _vbos.data());
}
Rabbid76
  • 202,892
  • 27
  • 131
  • 174