am I setting glVertexAttribPointer correctly?

I’m trying to change my particle effects to use instancing, but it isn’t working, the result is hard to describe, switching the usage of position and size in the vertex shaders produces no difference in outcome: some of the particles are correct, and some have the position and size reversed.


template<typename T>
struct Point2
{
	T x,y;
};

struct RenderData
{
	Point2<GLfloat> position;
	Point2<GLfloat> size;
};

GLuint vbo;
std::vector<RenderData> renderData;


glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(RenderData) * renderData.size(), renderData.data(), GL_STREAM_DRAW);

#define bind_float_array(name)\
	glVertexAttribPointer(gl_##name, sizeof(renderData[0].name)/sizeof(GLfloat), GL_FLOAT, GL_FALSE,\
		sizeof(renderData[0]) - sizeof(renderData[0].name),\
		(void*) offsetof(RenderData, name));\
	glEnableVertexAttribArray(gl_##name);\
	glVertexAttribDivisor(gl_##name, 1);

	bind_float_array(position);
	bind_float_array(size);

glBindBuffer(GL_ARRAY_BUFFER, 0);

Am I doing anything wrong there?

And this is the vertex shader I’m using:


#version 400

uniform mat4 projection_matrix;
uniform mat4 modelview_matrix;

in vec3 a_Vertex;
in vec2 a_TexCoord0;

in vec2 a_Position;
in vec2 a_Size;
in vec4 a_Color;

out vec2 texCoord0;

void main() 
{
	texCoord0 = a_TexCoord0;
	vec3 vert = a_Vertex * vec3(a_Size, 1);
	vert = vert + vec3(a_Position, 0);
	vec4 pos = modelview_matrix * vec4(vert, 1.0);
	gl_Position = projection_matrix * pos;
}

Yes. Apart from over-using the preprocessor (in modern C++ code, almost anything beyond #include qualifies as over-use), the stride parameter is the offset in bytes between the start of one item and the start of the next, not the size of the “gap” between items. So it should just be sizeof(renderData[0]).

Thank you!

Yes, plus the fact that, even in C, macros are often hard to debug.