290 likes | 598 Views
Vertex Array Objects & Buffer Objects. 靜宜大學資工系 蔡奇偉副教授. 大綱. Vertex Attributes Vertex Array Object (端點陣列物件 ) Buffer Objects (暫存區物件). Vertex Attributes (端點屬性). 每一個端點可以具下列的屬性: 位置( position ) 顏色 ( color ) 法 向量 ( normal ) 紋理貼圖座 檔 ( texture coordinates ) 雲霧特效 座標 ( fog coordinates )
E N D
Vertex Array Objects & Buffer Objects 靜宜大學資工系 蔡奇偉副教授
大綱 • Vertex Attributes • Vertex Array Object(端點陣列物件) • Buffer Objects(暫存區物件)
Vertex Attributes(端點屬性) • 每一個端點可以具下列的屬性: • 位置(position) • 顏色(color) • 法向量(normal) • 紋理貼圖座檔(texture coordinates) • 雲霧特效座標(fog coordinates) • 應用程式資料(application data)
Vertex Array Objects(端點陣列物件) • Vertex Array Objects (VAO) are OpenGL Objects that store the set of bindings between Vertex Attributes and the user's source vertex data. • glGenVertexArrays– 建立VAO • glDeleteVertexArrays– 刪除VAO • glIsVertexArray– 檢查是否為VAO • glBindVertexArray – 連結 VAO
glGenVertexArrays–建立VAO 範例 // 建立一個 VAO GLuintvao; glGenVertexArrays(1, &vao); // 建立三個 VAO GLuintvao[3]; glGenVertexArrays(3, vao);
glDeleteVertexArrays – 刪除VAO 範例 // 一個 VAO GLuintvao; glGenVertexArrays(1, &vao); glDeleteVertexArrays(1, &vao); // 三個 VAO GLuintvao[3]; glGenVertexArrays(3, vao); glDeleteVertexArrays(3, vao);
glIsVertexArray– 檢查是否為VAO 範例 GLuintvao, k = 100; GlbooleanisVAO; glGenVertexArrays(1, &vao); isVAO= glIsVertexArray(vao); // return true isVAO = glIsVertexArray(k); // return false
範例 GLuintvao; glGenVertexArrays(1, &vao); glBindVertexArray(vao); GLuintvao[3]; glGenVertexArrays(3, vao); glBindVertexArray(vao[1]);
VAO 的內部 參見:Vertex Array Object (OpenGL Wiki)
Buffer Objects(暫存區物件) • Buffer Objects is the general term for unformatted linear memory allocated by the OpenGL context. These can be used to store vertex data, pixel data retrieved from images or the framebuffer, and a variety of other things. • glGenBuffers– 建立BO • glDeleteBuffers – 刪除BO • glIsBuffer – 檢查是否為BO • glBindBuffer – 連結 BO 至特定目標 • glBufferData – 配置 BO 的資料
glGenBuffers – 建立BO 範例 GLuint buffer; glGenBuffers(1, &buffer );
glDeleteBuffers – 刪除BO 範例 GLuint buffer; glGenBuffers(1, &buffer); glDeleteBuffers(1, &buffer);
glIsBuffer – 檢查是否為BO 範例 GLuintbuffer, k = 100; GlbooleanisBO; glGenBuffers(1, &buffer ); isBO= glIsBuffer(buffer); // return true isBO= glIsBuffer(k); // return false
glBindBuffer – 連結 BO 至特定目標 範例 GLuint buffer; glGenBuffers(1, &buffer); glBindBuffer(GL_ARRAY_BUFFER, buffer);
Values for usage Parameter of glBufferData() Operations Update Frequency
範例 // Create and initialize a buffer object GLuint buffer; glGenBuffers(1, &buffer); glBindBuffer(GL_ARRAY_BUFFER, buffer); glBufferData(GL_ARRAY_BUFFER, sizeof(points), points, GL_STATIC_DRAW);
glGetAttribLocation – 取得屬性變數的位址 範例 GLuint loc = glGetAttribLocation(program, "position"); #version 330 in vec4 position; void main() { gl_Position = position; }
glVertexAttribPointer When rendering, OpenGL pulls vertex data from arrays stored in buffer objects. What we need to tell OpenGL is what format our vertex array data in the buffer object is stored in. That is, we need to tell OpenGL how to interpret the array of data stored in the buffer. The glVertexAttribPointer() function tells OpenGL all of this. glVertexAttribPointer() always refers to whatever buffer is bound to GL_ARRAY_BUFFER at the time that this function is called.
範例 #define BUFFER_OFFSET( offset ) ((GLvoid*) (offset)) GLuintloc = glGetAttribLocation( program, "position" ); glEnableVertexAttribArray( loc ); glVertexAttribPointer( loc, 2, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0) ); #version 330 in vec4 position; void main() { gl_Position = position; }
參考資料 Vertex Array Object (OpenGL Wiki)