1
\$\begingroup\$

I have made a vertex shader and fragment shader and kind of know how they work, and I know that you specify what index you want your vertices to be in inside the shader program. So if the vertex was at index 0, does each vertex take up 1 index? If each one took up an index, than how many indices would a matrix 4x4 take up? Sorry if thats confusing but I'm just starting and I don't really know how the indices within the shader works. So:

#version 140
in vec3 position; //This could be located at index 0
in mat4 randomMatrix; //If this came right after position, what index is it located at??
void main() {
    gl_Position = vec4(position.x * 0.5, position.y * 0.5, position.z * 1, 1);
}

Tell me if I have this all wrong.

\$\endgroup\$
  • \$\begingroup\$ You are using modern OpenGL so you can manually set uniform locations: \$\endgroup\$ – SurvivalMachine Mar 16 '14 at 18:52
  • \$\begingroup\$ @SurvivalMachine So are indices 'bytes'? How many indices does a matrix 4x4 take up? Thats the main thing I need to know. \$\endgroup\$ – FizzleWizzle Mar 16 '14 at 18:54
2
\$\begingroup\$

The OpenGL wiki does a pretty good job of explaining it. I'd recommend specifying the inputs (for readability) with explicit locations:

#version 140
layout (location = 0) in vec3 position;
layout (location = 1) in mat4 randomMatrix;
void main() {
    gl_Position = vec4(position.x * 0.5, position.y * 0.5, position.z * 1, 1);
}

Each index essentially takes a vec4, so if you pass a vec3 in [x, y, z], it's going to get padded internally to [x, y, z, 0]. Same with passing a float, it would be padded to [x, 0, 0, 0]. A mat4, on the other hand, takes up 4 * vec4 of space, thus it'll take up 4 indices. In your example, if you added one more variable, after the matrix, it would look like this:

#version 140
layout (location = 0) in vec3 position;
layout (location = 1) in mat4 randomMatrix;
layout (location = 5) in float foo;
void main() {
    gl_Position = vec4(position.x * 0.5, position.y * 0.5, position.z * 1, 1);
}

The matrix uses attribute indices 1, 2, 3 and 4, one for each collumn.

\$\endgroup\$
  • \$\begingroup\$ Thanks so much "Each index essentially takes a vec4" thats what im looking for! And also, opengl 3.1 doesn't support layout I'm pretty sure but I just use glBindAttribLocation which is the same. \$\endgroup\$ – FizzleWizzle Mar 16 '14 at 19:18

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.