0

I have created a VAO in OpenGL, comprised of a VBO (2 triangles), an array of indices and an array of positions. However instancing it does only show 1 item. Does it mean it sees all positions at 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);

And to display the instanced VAOs, I use:

glBindVertexArray (data->vaoId);
glDrawArraysInstancedARB(GL_TRIANGLES, 0, 6, 8); // 8 instances
glBindVertexArray(0);

Did I forget something?

Peter O.
  • 32,158
  • 14
  • 82
  • 96
Laurent Crivello
  • 3,809
  • 6
  • 45
  • 89
  • You had showed your `translationData` vector, but not the `vertices` vector. Second, remember that openGL only show to you what is inside an unitary cube, your translationData is outside this unitary cube – Amadeus May 02 '16 at 14:43
  • Question editted with other arrays. Re. your unitary cube remark, this is strange because I can properly display meshes which are beyond the 1.0 coordinates. Or do I get your remark wrongly ? – Laurent Crivello May 02 '16 at 14:59
  • I don't know without seeing how you are doing your transformation, but, anyway, you can test it by changing how your translationData is defined – Amadeus May 02 '16 at 15:01
  • What do you mean by "doing your transformation" ? I make the assumption that simply passing the translation array to the GL_ARRAY_BUFFER should be enough. Isn't it ? – Laurent Crivello May 02 '16 at 15:17

0 Answers0