PDA

View Full Version : dynamically set the stride in a glVertexAttribPointer



gwartney
04-26-2016, 05:25 PM
I was wanting to know is there any way of setting the stride in a glVertexAttribPointer with out manually counting the amount of elements the array that it has to go through to get to the next set of elements. I am assuming there is a way of doing this iam just not sure how to go about doing it. Also is there any way of doing the same for an offset if needed?

GLfloat vertices[] = {
0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f,
-0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f,
0.0f, 0.5f, 0.0f, 0.0f, 0.0f, 1.0f
};
For instance i have a glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat), (GLvoid*)0);

GClements
04-26-2016, 06:41 PM
I was wanting to know is there any way of setting the stride in a glVertexAttribPointer with out manually counting the amount of elements the array that it has to go through to get to the next set of elements.

No.



I am assuming there is a way of doing this

Why? How would that even be possible?



Also is there any way of doing the same for an offset if needed?

No.



For instance i have a glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat), (GLvoid*)0);

How could glVertexAttribPointer() (or any function) figure out what the offset and stride should be? You could choose almost any values for those parameters and format the data such that they would be correct.

The only actual constraint is that the stride must be non-negative. The offset cannot be negative because it's passed as a pointer. Practically, it must be smaller than the buffer, if the data is actually being sourced from a buffer object (in the compatibility profile, it can be a pointer to client memory, so there's know way for OpenGL to know the size of the data). The stride doesn't even need to be smaller than the buffer (if it only contains a single vertex, the stride doesn't matter).

gwartney
04-26-2016, 06:57 PM
Ah ok thanks for the help

mhagain
04-27-2016, 05:57 AM
Use a struct instead of an array of floats for each vertex. Then you just do sizeof (struct) and that's your stride.

gwartney
04-27-2016, 06:46 AM
ah ok thanks