OpenGL 2.1 を使用しているため、 を使用して属性インデックスをプルする必要がありますglGetAttribLocation()
。0
GLSL コンパイラが頂点と texcoords をオンにすることを期待することはできません2
。
void render(Shader* shader_program = NULL)
{
if (shader_program)
shader_program->bind();
GLint position_loc = glGetAttribLocation( shader_program->id(), "position" );
GLint tex_loc = glGetAttribLocation( shader_program->id(), "tex" );
GLint normal_loc = glGetAttribLocation( shader_program->id(), "normal" );
glEnableVertexAttribArray( position_loc );
glEnableVertexAttribArray( normal_loc );
glEnableVertexAttribArray( tex_loc );
for (size_t i = 0; i < entries.size(); ++i)
{
glBindBuffer(GL_ARRAY_BUFFER, entries[i].vb);
// I think this tells it where to look for the vertex information we've loaded.
glVertexAttribPointer(position_loc, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), 0);
glVertexAttribPointer(normal_loc, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (const GLvoid*)12); // starts at 12 because 3 floats for position before 2 floats for normal
glVertexAttribPointer(tex_loc, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (const GLvoid*)20); // makes room for 5 floats
...
シェーダーをリンクする前に OpenGL にそれらを配置する場所 ( ) を指定することもできますglBindAttribLocation()
が、設定方法によっては面倒です。
OpenGL 3.1+ では、layout
修飾子を介して特定のインデックスに属性を配置するようにコンパイラに強制できます。