typedef struct _vertex{
GLfloat position[2];
GLfloat colors[4];
} Vertex; //定义顶点
GLfloat positions[NumVertices][2] = {
{ -0.90, -0.90 }, // Triangle 1
{ 0.85, -0.90 },
{ -0.90, 0.85 },
{ 0.90, -0.85 }, // Triangle 2
{ 0.90, 0.90 },
{ -0.85, 0.90 }
};
GLfloat colors[NumVertices][4] = {
{ 1.0, 0.0, 0.0, 1.0}, // Triangle 1
{ 1.0, 0.0, 0.0, 1.0},
{ 1.0, 0.0, 0.0, 1.0},
{ 1.0, 0.0, 0.0, 1.0}, // Triangle 2
{ 0.0, 1.0, 0.0, 1.0},
{ 0.0, 0.0, 1.0, 1.0}
};
Vertex vertexs[NumVertices];
for(int i=0;i<NumVertices;i++){
vertexs[i].position[0] = positions[i][0];
vertexs[i].position[1] = positions[i][1];
vertexs[i].colors[0] = colors[i][0];
vertexs[i].colors[1] = colors[i][1];
vertexs[i].colors[2] = colors[i][2];
vertexs[i].colors[3] = colors[i][3];
}
glGenBuffers(NumBuffers, Buffers);
glBindBuffer(GL_ARRAY_BUFFER, Buffers[ArrayBuffer]);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertexs),vertexs, GL_STATIC_DRAW);
GLuint prog = Program::Load( vert, frag );
glUseProgram( prog );
glEnableVertexAttribArray( 0 );
glVertexAttribPointer( 0, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*) offsetof(Vertex, position));
// colors
glEnableVertexAttribArray( 1 );
glVertexAttribPointer( 1, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*) offsetof(Vertex, colors));
这样,在vertex shader中就可以通过
attribute vec4 a_position;
attribute vec4 a_color;
来接收属性了。