would something like this be valid as an input to a vertex shader?

layout (location = 0) in float[6] faceTextureOffsets;

I know you can use things like vec3s but for this situation I need an array of 6 floats. I am using a texture atlas and I would like for different sides of the cubes i'm rendering to have different textures and I need a way to be able to change the position in the texture atlas.


Use a vec4 and a vec2. You can make two calls to glVertexAttribPointer to feed a float[6] from the CPU-side as a vec4 and vec2 in the shader.

void glVertexAttribPointer(vec4_index,

void glVertexAttribPointer(vec2_index,

Short for-loops in shaders are usually unrolled and functions inlined by the shader compiler so you can replace an iteration of for(int i=0; i < 6; ++i){ my_code... } with a function called 6 times as for example:

ProcessSide(0, faceTextureOffsets4.x);
ProcessSide(1, faceTextureOffsets4.y);
ProcessSide(2, faceTextureOffsets4.z);
ProcessSide(3, faceTextureOffsets4.w);
ProcessSide(4, faceTextureOffsets2.x);
ProcessSide(5, faceTextureOffsets2.y);

And there should be no performance penalty. (Depend on the GPU drivers but most do inline functions and unroll loops.)

  • \$\begingroup\$ how would I turn these vertex attributes into an array on the gpu? would I have to do this? float textureOffsets[6] = {vec4.x,vec4.y,vec4.z,vec4.w,vec2.x,vec2.y} or is there a better way \$\endgroup\$ – Ben Beazley Jan 8 '17 at 13:32
  • \$\begingroup\$ You can do that or you can avoid using it as an array completely by unrolling the loop into a series of function calls. \$\endgroup\$ – Stephane Hockenhull Jan 8 '17 at 14:32
  • \$\begingroup\$ For my situation I need to refer to these numbers by index so I need an array \$\endgroup\$ – Ben Beazley Jan 9 '17 at 16:34

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.