在OpenGL中使用顶点缓冲区

时间:2015-07-27 17:58:38

标签: opengl

我使用了很多多边形(在RAM中),这非常慢。请说:如何在OpenGL中使用顶点缓冲区? (功能等) (编程语言 - C ++)

1 个答案:

答案 0 :(得分:0)

你应该真正阅读这方面的教程,如下所示: http://www.opengl-tutorial.org/beginners-tutorials/tutorial-2-the-first-triangle/#The_VAO

我可以给你这段代码:

float vertices[] = {
 1, 0, 0, 1, 
 0, 1, 0, 1,
 0, 0, 1, 1,
 };

GLuint vertexnumber = 3; //Amount of vertices in your array

int VertexStrideSize = 4*sizeof(float); //How much values you give for one vertex

// Create the vertex buffer object
GLuint buf;
glGenBuffers( 1, &buf );    //Create the buffer
glBindBuffer( GL_ARRAY_BUFFER, buf ); //Binding the buffer
glBufferData( GL_ARRAY_BUFFER, VertexStrideSize*vertexnumber, vertices,  GL_STATIC_DRAW ); //Fill the buffer

// For your vertex shader
GLuint posLoc = glGetAttribLocation(shadername, "aPosition"); //In your shader you can use the variable aPosition now as a input with "in vec4 aPosition"
glVertexAttribPointer(posLoc, 4, GL_FLOAT, GL_FALSE, VertexStrideSize,  0);
glEnableVertexAttribArray(posLoc);