所以我知道如何将输入作为数组提供给着色器:
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);
我如何提供一个 float 作为输入?我到处都尝试使用谷歌搜索,但没有太多好的资源记录着色器(至少对于初学者而言)。此外,我如何使用该输入?是不是就像:
layout(location = 0) in vec3 vertPosition //for my position
layout(location = 1/*or whatever i use*/) in float rotation
//rest of the code....
最佳答案
只需将 1 设置为第二个参数:
glVertexAttribPointer(0, 1, GL_FLOAT, GL_FALSE, 0, (void*)0);
这表示 openGL 需要将多少元素传递给着色器(最多 4 个)。
要传递多个属性,您必须将第一个参数与着色器中的属性相匹配:
glVertexAttribPointer(1, 1, GL_FLOAT, GL_FALSE, 0, (void*)0);
如果你想交错数据使缓冲区看起来像:[x, y, z, rotation, x, y, z, rotation,...]
然后你需要做以下调用:
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);
wiki有很好的描述,关于vertex specification
关于c++ - OpenGL着色器输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26704622/