I am using core OpenGL to draw fonts. This works when I do just one font set at a time. Now I am trying to use three different sets in an array.

I have this structure:

struct _sdf_Vertex
    glm::vec2       position;
    glm::vec2       textureCoord;
    glm::vec4       color;

and I use three fonts - so this:

vector<_sdf_Vertex>     sdf_vertex[3];
vector<unsigned int>        sdf_indexes[3];

I then populate each font type like this ( this works fine - I can see all the data in each element):

void sdf_addVertexInfo(uint whichFont, glm::vec2 position, glm::vec2 textureCoord, glm::vec4 color)
_sdf_Vertex     sdf_vertexTemp;             // Temp to hold vertex info before adding to vector

sdf_vertexTemp.position = position;
sdf_vertexTemp.textureCoord = textureCoord;
sdf_vertexTemp.color = color;

and then I want to pass one set of font data to the shader:

    glBufferData ( GL_ARRAY_BUFFER, sizeof(_sdf_Vertex) * sdf_vertex[whichFont].size(), &sdf_vertex[whichFont], GL_DYNAMIC_DRAW );

    glVertexAttribPointer ( shaderProgram[SHADER_TTF_FONT].inVertsID, 2, GL_FLOAT, GL_FALSE, sizeof(_sdf_Vertex), (GLvoid*)offsetof(_sdf_Vertex, position) );
    glEnableVertexAttribArray ( shaderProgram[SHADER_TTF_FONT].inVertsID );
    // Specify the texture info
    glVertexAttribPointer ( shaderProgram[SHADER_TTF_FONT].inTextureCoordsID, 2, GL_FLOAT, GL_FALSE, sizeof(_sdf_Vertex), (GLvoid*)offsetof(_sdf_Vertex, textureCoord) );
    glEnableVertexAttribArray ( shaderProgram[SHADER_TTF_FONT].inTextureCoordsID );
    // Specify the color array
    glVertexAttribPointer ( shaderProgram[SHADER_TTF_FONT].inColorID, 4, GL_FLOAT, GL_FALSE, sizeof(_sdf_Vertex), (GLvoid*)offsetof(_sdf_Vertex, color) );
    glEnableVertexAttribArray ( shaderProgram[SHADER_TTF_FONT].inColorID );

glBufferData ( GL_ELEMENT_ARRAY_BUFFER, sdf_indexes[whichFont].size() * sizeof(unsigned int), &sdf_indexes[whichFont], GL_DYNAMIC_DRAW ) );
glDrawElements ( GL_TRIANGLES, sdf_indexes[whichFont].size(), GL_UNSIGNED_INT, 0 ) );

Checking the vertex shader with renderDoc - I'm not seeing any of the sdf_vertex data coming through, or sometimes it's all scrambled. I think it's got to do with the how I'm trying to point glBufferData to the vector of structs inside an array.

How do I pass the location of the vector inside an array?



So you've got to arrays of vectors, sdf_vertex and sdf_indexes.

The problem I can see, these two statements:




are accessing the address of each of the vecotors, rather than the data in those vectors.

You should be able to update those in place to




Note .data() is C++11, if you're unable to use that there's the older .front().

** I'm assuming C++ here, though that's not tagged or explictly mentioned.

  • \$\begingroup\$ @Droid you should accept this answer so that others can know what worked (and to give some rep!) \$\endgroup\$ – Maximus Minimus May 7 '18 at 17:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.