2013-12-18 42 views
1

我閱讀本教程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,而不是相反。順便說一下,啓用/禁用頂點屬性有什麼用處?我的意思是你(至少我)最終可能會啓用所有頂點屬性,所以爲什麼它們默認關閉?

回答

2

它在glDrawArrays調用中,當前啓用的屬性被讀取並傳遞給渲染器。如果你在這之前禁用它們,那麼它們將不會從你的緩衝區傳遞。

可以有很多潛在的屬性可用(最多glGetIntegerv(GL_MAX_VERTEX_ATTRIBS, &result);至少16),大多數應用程序不需要那麼多。

着色器中的position屬性設置爲索引0,如果未指定,則着色器將獲取具有相同位置(通常爲0,0,0,1)的所有點。而索引1是顏色數據,如果缺少,這不是什麼大問題。

+0

事實上,似乎glEnableVertexAttribArray只需要在glDrawArrays之前調用。從教程我認爲glEnableVertexAttribArray是強制性的,使glVertexAttribPointer工作,但顯然不是這樣 –

相關問題