1

So I know how to provide input to a shader as an array:

GLuint vertexbuffer;
glGenBuffers(1, &vertexbuffer);
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(g_vertex_buffer_data), g_vertex_buffer_data, GL_STATIC_DRAW);
//inside loop, provide the shader a layout location
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);

How would I provide a float as an input? I've tried Googling all over the place but there aren't very many good resources documenting shaders (for beginners at least). Furthermore, how do I use that input? Is it just like:

layout(location = 0) in vec3 vertPosition //for my position
layout(location = 1/*or whatever i use*/) in float rotation  
//rest of the code....

1 Answer 1

3

just set 1 as the second parameter:

glVertexAttribPointer(0, 1, GL_FLOAT, GL_FALSE, 0, (void*)0);

That signifies how many elements openGL needs to pass to the shader (up to 4).

To pass multiple attributes you can have to match the first parameter to the attribute in the shader:

glVertexAttribPointer(1, 1, GL_FLOAT, GL_FALSE, 0, (void*)0);

if you want to interleave the data so the buffer looks like: [x, y, z, rotation, x, y, z, rotation,...] then you will need to make the following calls:

glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(float)*4, (void*)0);
glVertexAttribPointer(1, 1, GL_FLOAT, GL_FALSE, sizeof(float)*4, (void*)sizeof(float)*3);

The wiki has a good description, about vertex specification

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you for your answer. How would I use multiple inputs (from multiple places) in my code? (second part of the question, sorry if it wasn't obvious :)
that is the first parameter, there are various ways to pass it to the shader, one popular way is to interleave the data, I'll edit the answer.
how would i use the rotation variable in my glsl? would it be layout(location = 0) in vec3 position layout(location = 1) in vec3 rotation
it'd be layout(location = 0) in vec3 position; layout(location = 1) in float rotation

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.