# How to pass vector<struct> to GLSL shader?

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;
sdf_vertex[whichFont].push_back(sdf_vertexTemp);
}


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) );
//
// Specify the texture info
glVertexAttribPointer ( shaderProgram[SHADER_TTF_FONT].inTextureCoordsID, 2, GL_FLOAT, GL_FALSE, sizeof(_sdf_Vertex), (GLvoid*)offsetof(_sdf_Vertex, textureCoord) );
//
// Specify the color array
glVertexAttribPointer ( shaderProgram[SHADER_TTF_FONT].inColorID, 4, GL_FLOAT, GL_FALSE, sizeof(_sdf_Vertex), (GLvoid*)offsetof(_sdf_Vertex, color) );

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?

Thanks.

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

The problem I can see, these two statements:

&sdf_vertex[whichFont]


and

&sdf_indexes[whichFont]


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

sdf_vertex[whichFont].data()


and

sdf_indexes[whichFont].data()


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.

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