在内存中采用交叉模式存储,向gpu传入顶点数据的方法
GPU:
#version 100
attribute highp vec2 aPosition;
attribute highp vec2 aTexcoord;
CPU:
init()
//将顶点数组元素都存入一个缓存对象中
static const float position[] = {
-1.0f, -1.0f,0.0f,1.0f,//2 顶点位置 2 纹理坐标
1.0f, -1.0f,1.0f,0.0f,
-1.0f, 1.0f,0.0f,1.0f,
1.0f, 1.0f,1.0f,1.0f
};
//-----------基于索引绘制
static const GLushort plane_indices[] =
{
0, 1, 2, 3
};
glGenBuffers(1, &_index_buffer);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _index_buffer);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(plane_indices), plane_indices, GL_STATIC_DRAW);
//--------------
glGenBuffers(1, &_quad_vbo);
glBindBuffer(GL_ARRAY_BUFFER, _quad_vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(position), NULL, GL_STATIC_DRAW);
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(position), position);
glGenVertexArrays(1, &_vao);
glBindVertexArray(_vao);
GLint naPosition = mProgram->getAttribLocation("aPosition");
glEnableVertexAttribArray(0);
//注意,这里的offset 是内存中大小的偏移,必须准确计算出内存大小偏移量 sizeof
glVertexAttribPointer(naPosition, 2, GL_FLOAT, GL_FALSE, 4*sizeof(float), (GLvoid*)0);
GLint naTexcoord = mProgram->getAttribLocation("aTexcoord");
glEnableVertexAttribArray(1);
glVertexAttribPointer(naTexcoord, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), (const GLvoid*)(2*sizeof(float)));
glBindVertexArray(0);
glDisableVertexAttribArray(0);
glDisableVertexAttribArray(1);