我閱讀本教程arc synthesis, vertex attributes, chapter 2 playing with colors並決定玩弄代碼。總結起來,本教程解釋瞭如何將頂點顏色和座標傳遞給頂點和片段着色器,以在三角形上放置某種顏色。OpenGL:是否啓用所有需要的頂點屬性?
這裏是顯示功能的代碼(來自具有一些改變的教程),爲預期的工作原理:
void display()
{
// cleaning screen
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT);
//using expected program and binding to expected buffer
glUseProgram(theProgram);
glBindBuffer(GL_ARRAY_BUFFER, vertexBufferObject);
glDisableVertexAttribArray(0);
glDisableVertexAttribArray(1);
//setting data to attrib 0
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 0, 0);
//glDisableVertexAttribArray(0); // if uncommenting this out, it does not work anymore
//setting data to attrib 1
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, 0, (void*)48);
//cleaning and rendering
glDrawArrays(GL_TRIANGLES, 0, 3);
glDisableVertexAttribArray(0);
glDisableVertexAttribArray(1);
glUseProgram(0);
glfwSwapBuffers();
}
現在,如果在取消對線 // glDisableVertexAttribArray(0); 將數據設置爲屬性1之前,這不再起作用。這是爲什麼 ?另外,我不明白爲什麼屬性0可以設置沒有啓用1,而不是相反。順便說一下,啓用/禁用頂點屬性有什麼用處?我的意思是你(至少我)最終可能會啓用所有頂點屬性,所以爲什麼它們默認關閉?
事實上,似乎glEnableVertexAttribArray只需要在glDrawArrays之前調用。從教程我認爲glEnableVertexAttribArray是強制性的,使glVertexAttribPointer工作,但顯然不是這樣 –