Home>

Of the presentation codevertices.push_back (vert);//////////////////////////////////////////// /////////////But when you run it
Error [The array cannot be initialized with the initializer enclosed in parentheses] is displayed. How can I push_back it?

void Text :: Rendering (float x, float y, glm :: vec4 color, const char * format, ...)
{
    float scale = 1.0f;// size
    float xx = (-SCREEN_WIDTH/2) + x;
    float yy = (SCREEN_HEIGHT/2) --y;
    x = xx;
    y = yy;
    // Generate characters
    std :: string text;// Store the generated characters
    va_list ap;
    char str [1000];
    va_start (ap, format);
    vsprintf_s (str, sizeof (str), format, ap);
    va_end (ap);
    text = std :: string (str);
    // printf ("Ah \ n");
    // shader
    ShaderProgram->Enable ();// Activate
    ShaderProgram->SetFloatUniform_3m ("uViewMatrix", getViewMatrix ());// View Matrix
    ShaderProgram->SetFloatUniform_3m ("uWorldMatrix", getWorldMatrix ());// World Matrix
    ShaderProgram->SetFloatUniform_4f ("uTextColor", color);// Set color RGB

    glActiveTexture (GL_TEXTURE0);
    glBindVertexArray (VAO);

    std :: vector<float [6] [4]>vertices;
    std :: vector<Character>ch;
    // iterate through all characters
    std :: string :: const_iterator itr;
    for (itr = text.begin ();itr! = text.end ();itr ++)
    {
        ch.push_back ((Character) Characters [* itr]);
        float xpos = x + ch.back (). Bearing.x * scale;
        float ypos = y-(ch.back (). Size.y --ch.back (). Bearing.y) * scale;
        float w = ch.back (). Size.x * scale;
        float h = ch.back (). Size.y * scale;
        // Rewrite vertex information
        float vert [6] [4] = {
            {xpos, ypos + h, 0.0f, 0.0f},
            {xpos, ypos, 0.0f, 1.0f},
            {xpos + w, ypos, 1.0f, 1.0f},
            {xpos, ypos + h, 0.0f, 0.0f},
            {xpos + w, ypos, 1.0f, 1.0f},
            {xpos + w, ypos + h, 1.0f, 0.0f}
        };
        vertices.push_back (vert);//////////////////////////////////////////// /////////////
    }

    // Set texture
    int i = 0;
    for (std :: vector<Character>:: const_iterator itr = ch.begin ();itr! = ch.end ();itr ++) {
        glBindTexture (GL_TEXTURE_2D, itr->TextureID);

        glBindBuffer (GL_ARRAY_BUFFER, VBO);
        glBufferSubData (GL_ARRAY_BUFFER, 0, std :: size (vertices.at (i)) * 6 * 4, std :: data (vertices));// be sure to use glBufferSubData and not glBufferData
      // glBufferSubData (GL_ARRAY_BUFFER, 0, sizeof (vertices), vertices);// be sure to use glBufferSubData and not glBufferData
        glBindBuffer (GL_ARRAY_BUFFER, 0);
        // render quad
        glDrawArrays (GL_TRIANGLES, 0, 6);
        // now advance cursors for next glyph (note that advance is number of 1/64 pixels)
        x + = (itr->Advance >>6) * scale;// bitshift by 6 to get value in pixels (2 ^ 6 = 64 (divide amount of 1/64th pixels by 64 to get amount of pixels))
        glBindVertexArray (0);
        glBindTexture (GL_TEXTURE_2D, 0);
    }
    // update content of VBO memory
}
  • Answer # 1

    Roughly speaking, push_back is
    (1) Make a copy of the argument
    (2) Store it as an element of vector
    That's why,
    If it is an array, you cannot simply "copy" it, so (1) is an error.

    // For example int
    int a = 5;
    int b {a};// OK
    b = a;// This is also OK
    // If it's an array ...
    float A [6] [4] = {...};
    float B [6] [4] {A};// Cannot be done with an array
    B = A;// You can't do this, right?

    As a countermeasure, for example (although it is a little troublesome), do as follows.

    // Wrap the array in a structure
    struct Vtx {float data [6] [4];};
    // Make it a structure type vector
    std :: vector<Vtx>vertices;

    Like using std :: array.

    using Row = std :: array<float, 4>;
    using Vtx = std :: array<Row, 6>;