0
\$\begingroup\$

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.

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

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,
    4,
    GL_FLOAT,
    false,
    vertex_size,
    &(my_vertex.faceTextureOffsets[0]));

void glVertexAttribPointer(vec2_index,
    2,
    GL_FLOAT,
    false,
    vertex_size,
    &(my_vertex.faceTextureOffsets[4]));

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.)

\$\endgroup\$
3
  • \$\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\$ Commented Jan 8, 2017 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\$ Commented Jan 8, 2017 at 14:32
  • \$\begingroup\$ For my situation I need to refer to these numbers by index so I need an array \$\endgroup\$ Commented Jan 9, 2017 at 16:34

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .