0

I have a vertex struct that stores the location of a vertex in a 2d plane and can transform its position and also used to generate vertices to make a geometric shape. i need the parameter of one of the function calls to determine how many of this struct i will need i tried using an array but that didn't work since array's only accept const variables and i tried vectors but when i generate the buffers for the vertices i can't use a vector as "There is no conversion from vector to const void" here's the function.

void Sprite::init(Type mode, int verticesNum, float scale) {
    //generate buffer if it hasn't been generated
    if (_vboID == 0) {
        glGenBuffers(1, &_vboID);
    }
    //make vector of structs using verticesNum as how many vertices are needed
    std::vector<Vertex> vertexData(verticesNum);

    if (mode == Type::CIRCLE) {
        for (int i = verticesNum; i < verticesNum; i++) {
            float angle = (M_PI / 180) * i;
            vertexData[i].setPosition(cos(angle)*scale, sin(angle)*scale);
        }
    }

    //Tell opengl to bind our vertex buffer object
    glBindBuffer(GL_ARRAY_BUFFER, _vboID);
    //Upload the data to the GPU
    //this line is where i get the error as it can't use vertex data
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertexData), /*right here*/vertexData/*to be more specific*/, GL_STATIC_DRAW);

    //Unbind the buffer
    glBindBuffer(GL_ARRAY_BUFFER, 0);
}
zee
  • 2,933
  • 2
  • 16
  • 28
  • you could use malloc to allocate enough space to fit all the vertex objects you need, something like "void* vertexData = malloc(sizeof(Vertex)*verticesNum);" – iedoc May 10 '16 at 01:46
  • sorry, instead of void* you probably want Vertex*, "Vertex* vertexData = (Vertex*)malloc(sizeof(Vertex)*verticesNum);" – iedoc May 10 '16 at 01:47
  • @iedoc this is SPARTA! errrrr... C++. Anyway, we use `new Vertex[verticesNum]` here. That or `std::vector vertexData(verticesNum);`as OP did. OP just tried to use the `vector` wrong. `malloc` has its place, but in C++ it is way out on the periphery of the bizarre edge cases. Go for statically allocated array, `std::array`, `std::vector`, `new` and finally `malloc` if somehow you find a case not covered by the preceding four. – user4581301 May 10 '16 at 02:08
  • @user4581301 haha, your totally right! i'm sorry, i've been working a lot with c recently – iedoc May 10 '16 at 13:41

1 Answers1

2

According to its manual page, glBufferData()'s third parameter is a const GLvoid *.

So, what are you trying to do here?

std::vector<Vertex> vertexData(verticesNum);

// ...

glBufferData(GL_ARRAY_BUFFER, sizeof(vertexData), vertexData, GL_STATIC_DRAW);

Well, there's your problem. vertextData is a std::vector. A std::vector just can't magically convert itself into a native pointer.

You are probably trying to pass a pointer to the first element of the vector, here. If so, then this should be:

glBufferData(GL_ARRAY_BUFFER, sizeof(vertexData), &vertexData[0], GL_STATIC_DRAW);

Additionally, I noticed the following, out of a corner of my eye:

for (int i = verticesNum; i < verticesNum; i++) {

This can't be right, either. The body of this for loop will never execute.

Sam Varshavchik
  • 114,536
  • 5
  • 94
  • 148
  • Oh sorry about the for loop that was a type I made while copying the code `i` is actually set to 0 and sorry I'm very new to c++ but you're suggestion works – zee May 10 '16 at 22:18