VAO是一個對象,表示OpenGL流水線的頂點提取階段,用於向頂點着色器提供輸入。
您可以創建這樣
GLuint vao;
glCreateVertexArrays(1, &vao);
glBindVertexArray(vao);
首先,讓我們的頂點數組對象」做了一個簡單的例子。在着色器代碼
layout (location = 0) in vec4 offset; // input vertex attribute
爲了填補這個屬性考慮這樣的輸入參數,我們可以使用
glVertexAttrib4fv(0, attrib); // updates the value of input attribute 0
雖然頂點數組對象存儲這些靜態的屬性值 你,它可以做很多更多。
創建頂點數組對象後,我們可以開始填充其狀態。我們將要求OpenGL使用存儲在我們提供的緩衝區對象中的數據自動填充它。每個頂點屬性都可以從綁定到幾個頂點緩衝區綁定之一的緩衝區中獲取數據。爲此,我們使用glVertexArrayAttribBinding(GLuint vao, GLuint attribindex, GLuint bindingindex)
。我們還使用glVertexArrayVertexBuffer()
函數將緩衝區綁定到其中一個頂點緩衝區綁定。我們使用glVertexArrayAttribFormat()
函數來描述數據的佈局和格式,最後我們通過調用glEnableVertexAttribArray()
來啓用屬性的自動填充。
啓用頂點屬性,OpenGL的將反饋數據,根據您所提供 glVertexArrayVertexBuffer()
和glVertexArrayAttribFormat()
格式和位置信息的頂點着色器。當 該屬性被禁用時,頂點着色器將被提供您提供的調用glVertexAttrib*()
的靜態信息。
// First, bind a vertex buffer to the VAO
glVertexArrayVertexBuffer(vao, 0, buffer, 0, sizeof(vmath::vec4));
// Now, describe the data to OpenGL, tell it where it is, and turn on automatic
// vertex fetching for the specified attribute
glVertexArrayAttribFormat(vao, 0, 4, GL_FLOAT, GL_FALSE, 0);
glEnableVertexArrayAttrib(vao, 0);
和代碼在着色器
layout (location = 0) in vec4 position;
所有你需要打電話glDeleteVertexArrays(1, &vao)
後。
您可以閱讀OpenGL SuperBible以更好地理解它。
''頂點數組對象'由OpenGL ARB小組委員會爲您提供。是的,一個存儲**頂點** **數組**綁定的**對象**這樣一個愚蠢的名字。 – 2012-08-06 01:34:26
如果我正確地理解你,調用'glGenVertexArrays(1,&VaoId);'和'glBindVertexArray(VaoId);'創建一個VAO,並使用理智的默認值將你從'glEnableClientState'中解救出來?或者我完全誤解了你?將來我會使用很多'glEnableClientState'嗎? – Patrick 2012-08-06 02:13:49
此外,所有與「glVertexAttribPointer」相關的VAOs – Patrick 2012-08-06 02:16:19