static const float p_vertex[12] = {-0.5,0.5,0.0, -0.5,-0.5,0.0, 0.5,-0.5,0.0, 0.5,0.5,0.0}; static const short p_index[6] = {0,1,2, 0,2,3}; static ssShader* g_shader = 0; static GLuint g_vao = 9999; static GLuint g_vbo[3]; {//glDrawArrays //vao glGenVertexArrays(1, &g_vao); glBindVertexArray(g_vao); //vbo glGenBuffers(1, &g_vbo[0]); glBindBuffer(GL_ARRAY_BUFFER, g_vbo[0]); glBufferData(GL_ARRAY_BUFFER, sizeof(float)*12, p_vertex, GL_STATIC_DRAW); //綁定vbo到vao glEnableVertexAttribArray(0); //glBindBuffer(GL_ARRAY_BUFFER, g_vbo[0]); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0); glBindVertexArray(0); glBindBuffer(GL_ARRAY_BUFFER, 0); glBindVertexArray(g_vao); glDrawArrays(GL_TRIANGLES, 0, 3); glBindVertexArray(0); } {//glDrawElements //vao glGenVertexArrays(1, &g_vao); glBindVertexArray(g_vao); //vbo glGenBuffers(1, &g_vbo[0]); glBindBuffer(GL_ARRAY_BUFFER, g_vbo[0]); glBufferData(GL_ARRAY_BUFFER, sizeof(float)*12, p_vertex, GL_STATIC_DRAW); glGenBuffers(1, &g_vbo[1]); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, g_vbo[1]); glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(short)*6, p_index, GL_STATIC_DRAW); //綁定vbo到vao glEnableVertexAttribArray(0); glBindBuffer(GL_ARRAY_BUFFER, g_vbo[0]); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0); glBindVertexArray(0); glBindBuffer(GL_ARRAY_BUFFER, 0); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); glBindVertexArray(g_vao); //glDrawArrays(GL_TRIANGLES, 0, 3); glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, 0); glBindVertexArray(0); } 必須先調用glBindVertexArray(0)再調用glBindBuffer(GL_ARRAY_BUFFER, 0)