-1

I want to avoid repeating the following code many times since I use plenty VBOs:

    glBindBuffer(GL_ARRAY_BUFFER, VBOs[0]);
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertices1), vertices1, GL_STATIC_DRAW);

    glBindBuffer(GL_ARRAY_BUFFER, VBOs[1]);
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertices2), vertices2, GL_STATIC_DRAW);

    glBindBuffer(GL_ARRAY_BUFFER, VBOs[2]);
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertices3), vertices3, GL_STATIC_DRAW);

    glBindBuffer(GL_ARRAY_BUFFER, VBOs[3]);
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertices4), vertices4, GL_STATIC_DRAW);

I tried to simplify this code with a cycle. Here is what I tried to do:

std::string aux = "vertices";
    std::string aux2;
    int aux3;
    std::string aux4; 
    const void* aux5;
    for (int i = 0; i <= 13; i++) {
        aux3 = i++;
        aux4 = std::to_string(aux3);
        aux2 = aux + aux4;
        aux5 = aux2.c_str();
        glBindBuffer(GL_ARRAY_BUFFER, VBOs[i]);
        glBufferData(GL_ARRAY_BUFFER, sizeof(aux2), aux5, GL_STATIC_DRAW);
    }

It didn't work out. It still shows a figure with the last color I used but doesn't paint the other figures. Is there a way to make that cycle work or should I continue repeating the code like above? Thanks for the help.

I also tried simplifying the VAOs section in the code with a cycle and it worked. Here is how I did it:

glGenVertexArrays(14, VAOs); 
    for (int i = 0; i <= 13; i++) { 
        glBindVertexArray(VAOs[i]);
        glBindBuffer(GL_ARRAY_BUFFER, VBOs[i]);
        glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
        glEnableVertexAttribArray(0);
    } 

ICS
  • 87
  • 3

1 Answers1

0

Your code with cycles doesn't work because you misunderstand how std::string works. When you call glBufferData(GL_ARRAY_BUFFER, sizeof(aux2), aux5, GL_STATIC_DRAW) it is not the same as glBufferData(GL_ARRAY_BUFFER, sizeof(vertices1), vertices1, GL_STATIC_DRAW). Lets say that vertices1 stores some memory (actual vertices like [x1_0, x1_1, x1_2, x1_3, y1_0, y1_1, y1_2, y1_3, x2_0, x2_1, ...], where x1, y1, x2, ... are your vertices). x1 = [x1_0, x1_1, x1_2, x1_3] because it is a float variable (4 bytes in memory). std::string also stores memory, so your aux2 variable will not be replaced with vertices1 string in your code, but will pass ['v', 'e', 'r', 't', 'i', 'c', 'e', 's', '1'] as a memory argument to a function (every char is 1 byte).

std::string works at runtime, whereas you want your code to be replaced in realtime. You have to look for #define if you want to archive the result. However, it is much better to have an array of vertices.

Bioliquid
  • 161
  • 7