我在 OpenGL 中创建了一个 VAO,由一个 VBO(2 个三角形)、一个索引数组和一个位置数组组成。但是实例化它只显示 1 个项目。这是否意味着它在 0,0,0 处看到所有位置?
GLfloat *vertices=new GLfloat[18];
GLushort *indices=new GLushort[6];
int i=0;
vertices[i*3]=0;
vertices[i*3+1]=0;
vertices[i*3+2]=1;
vertices[i*3+3]=0.5;
vertices[i*3+4]=0;
vertices[i*3+5]=-0.5;
vertices[i*3+6]=0;
vertices[i*3+7]=0;
vertices[i*3+8]=0;
vertices[i*3+9]=0;
vertices[i*3+10]=0;
vertices[i*3+11]=1;
vertices[i*3+12]=0;
vertices[i*3+13]=0;
vertices[i*3+14]=0;
vertices[i*3+15]=-0.5;
vertices[i*3+16]=0;
vertices[i*3+17]=-0.5;
indices[0]=0;
indices[1]=1;
indices[2]=2;
indices[3]=3;
indices[4]=4;
indices[5]=5;
GLfloat translationData[] = {
2.0f, 2.0f, 2.0f,
2.0f, 2.0f,-2.0f,
2.0f,-2.0f, 2.0f,
2.0f,-2.0f,-2.0f,
-2.0f, 2.0f, 2.0f,
-2.0f, 2.0f,-2.0f,
-2.0f,-2.0f, 2.0f,
-2.0f,-2.0f,-2.0f
};
glGenVertexArrays (1, &data->vaoId);
glBindVertexArray (data->vaoId);
// Binding mesh (2 triangles in 'vertices')
glGenBuffers(1, &data->vboIdArray);
glBindBuffer(GL_ARRAY_BUFFER, data->vboIdArray); // Vertex array
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat)*18, NULL, GL_STATIC_DRAW); // Reserve space
glBufferSubData(GL_ARRAY_BUFFER,0,sizeof(GLfloat)*18,vertices); // Upload data to video card
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0,0);
// Binding indices (6 shorts in 'indices')
glGenBuffers(1, &data->vboIdIndices);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, data->vboIdIndices); // Index array
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(GLushort)*6, indices, GL_STATIC_DRAW);
// Binding positions
glGenBuffers(1, &data->vboTranslate);
glBindBuffer(GL_ARRAY_BUFFER, data->vboTranslate);
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat)*3*8, NULL, GL_STATIC_DRAW);
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(GLfloat)*3*8, translationData);
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, 0);
glVertexAttribDivisorARB(0, 0); // Mesh does not change
glVertexAttribDivisorARB(1, 1); // Position changes at each instance
glEnableVertexAttribArray (0);
glBindVertexArray(0);
为了显示实例化的 VAO,我使用:
glBindVertexArray (data->vaoId);
glDrawArraysInstancedARB(GL_TRIANGLES, 0, 6, 8); // 8 instances
glBindVertexArray(0);
我是不是忘记了什么?