无法在OpenGL中的多个位置实例化VAO

时间:2016-05-02 14:31:29

标签: c++ opengl vbo vao geometry-instancing

我在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);

我忘记了什么吗?

0 个答案:

没有答案