PDA

View Full Version : Headache with glVertexAttribPointer



CyberMew
11-06-2012, 10:58 AM
I need this in my shader

attribute vec4 weight;
attribute vec4 index;

through this,

glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ARRAY_BUFFER, bufferId);
glVertexAttribPointer(weightLoc, 4, GL_FLOAT, GL_FALSE, 8, reinterpret_cast<const GLvoid*>( offsetof(structName, weight)));
glEnableVertexAttribArray(weightLoc);
glVertexAttribPointer(indexLoc, 4, GL_FLOAT, GL_FALSE, 8, reinterpret_cast<const GLvoid*>( offsetof(structName, index)));
glEnableVertexAttribArray(indexLoc);


struct structName
{
float weight;
int index;
}

How can I get it to send 4 floats as 1 vec4, when my data are apart? They are structured such that there are an array of 4 of structName, if you get what I mean. Setting the stride by 8 or 32 doesn't seem to work.. I already have them buffered to the GPU with rows of 4 structName.

Any help is appreciated!

aqnuep
11-06-2012, 12:03 PM
This is simply not possible. Either restructure your client data to a SOA (struct-of-array) layout or have 8 separate attributes in your shader (weight0, index0, weight1, index1, ...).

CyberMew
11-06-2012, 08:24 PM
Thanks, I will try to have rows of weight[4] and index[4] then, as per your former suggestion. If that case, just to confirm with the following code, I should have a vec4 weight and vec4 index instead of a float weight[4] and int index[4] right?


struct structName
{
float weight[4];
int index[4];
}

glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ARRAY_BUFFER, bufferId);
glVertexAttribPointer(weightLoc, 4, GL_FLOAT, GL_FALSE, 32, reinterpret_cast<const GLvoid*>( offsetof(structName, weight)));
glEnableVertexAttribArray(weightLoc);
glVertexAttribPointer(indexLoc, 4, GL_INT, GL_FALSE, 32, reinterpret_cast<const GLvoid*>( offsetof(structName, index)));
glEnableVertexAttribArray(indexLoc);

Thanks again for the help!

aqnuep
11-07-2012, 05:46 AM
This should work.