2012-07-02 12 views
5

Sto imparando OpenGL 4.0 e voglio disegnare un triangolo semplice usando OpenGL 4.0 e GLSL. Sto usando VAO con gli array interfogliati a farlo, ma il display è risultato non è quello che voglio:Usa array interfogliati in VAO

enter image description here

Ora vi posto una parte del mio codice:

void SceneBasic::setupVAOInterleavedArrays() 
{ 
    //三角形的顶点和颜色信息数组:混合数组 
    float positionAndColorData[] = { 
     -0.8f, -0.8f, 0.0f,1.0f, 0.0f, 0.0f, 
     0.8f, -0.8f, 0.0f,0.0f, 1.0f, 0.0f, 
     0.0f, 0.8f, 0.0f,0.0f, 0.0f, 1.0f }; 

    //glInterleavedArrays(GL_C3F_V3F,0,positionAndColorData) 

    GLuint vboHandle;//VBO 
    glGenBuffers(1,&vboHandle); 

    glBindBuffer(GL_ARRAY_BUFFER,vboHandle); 
    glBufferData(GL_ARRAY_BUFFER,18 * sizeof(float), 
     positionAndColorData,GL_STATIC_DRAW); 

    //VAO 
    glGenVertexArrays(1,&vaoHandle); 
    glBindVertexArray(vaoHandle); 

    //enable the generic vertex attribute indexes 
    //indicates that the values for the attributes will be accessed 
    //and used for rendering 
    glEnableVertexAttribArray(0);//position 
    glEnableVertexAttribArray(1);//color 

    //make the connection between the buffer objects and the generic 
    //vertex attributes indexes 
    glBindBuffer(GL_ARRAY_BUFFER,vboHandle); 
    glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,3 * sizeof(float),BUFFER_OFFSET(0)); 
    glBindBuffer(GL_ARRAY_BUFFER,vboHandle); 
    glVertexAttribPointer(1,3,GL_FLOAT,GL_FALSE,3 * sizeof(float),BUFFER_OFFSET(3 * sizeof(float))); 
} 

void SceneBasic::initScene() 
{ 
    compileAndLinkShader(); 

    //setupVAO(); 
    setupVAOInterleavedArrays(); 
} 

void SceneBasic::render() 
{ 
    glClear(GL_COLOR_BUFFER_BIT); 

    glBindVertexArray(vaoHandle); 
    glDrawArrays(GL_TRIANGLES,0,3); 
    glBindVertexArray(0); 
} 

Ma se io non utilizzare matrice interlacciata, il risultato è giusto: enter image description here

questa è la parte del mio codice, quando io non uso le matrici interlacciati:

void SceneBasic::setupVAO() 
{ 
    //三角形的顶点和颜色信息数组 
    float positionData[] = { 
     -0.8f, -0.8f, 0.0f, 
     0.8f, -0.8f, 0.0f, 
     0.0f, 0.8f, 0.0f }; 
    float colorData[] = { 
     1.0f, 0.0f, 0.0f, 
     0.0f, 1.0f, 0.0f, 
     0.0f, 0.0f, 1.0f }; 

    glGenBuffers(2,vboHandles); 
    GLuint positionBufferHandle = vboHandles[0]; 
    GLuint colorBufferHandle = vboHandles[1]; 

    glBindBuffer(GL_ARRAY_BUFFER,positionBufferHandle); 
    glBufferData(GL_ARRAY_BUFFER,9 * sizeof(float), 
     positionData,GL_STATIC_DRAW); 

    glBindBuffer(GL_ARRAY_BUFFER,colorBufferHandle); 
    glBufferData(GL_ARRAY_BUFFER,9 * sizeof(float), 
     colorData,GL_STATIC_DRAW); 

    //VAO 
    glGenVertexArrays(1,&vaoHandle); 
    glBindVertexArray(vaoHandle); 

    //enable the generic vertex attribute indexes 
    //indicates that the values for the attributes will be acessed 
    //and used for rendering 
    glEnableVertexAttribArray(0); 
    glEnableVertexAttribArray(1); 

    //make the connection between the buffer objects abd the generic 
    //vertex attributes indexes 
    glBindBuffer(GL_ARRAY_BUFFER,positionBufferHandle); 
    glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,0,BUFFER_OFFSET(0)); 

    glBindBuffer(GL_ARRAY_BUFFER,colorBufferHandle); 
    glVertexAttribPointer(1,3,GL_FLOAT,GL_FALSE,0,BUFFER_OFFSET(0)); 
} 

Sono così curioso, perché il mio codice non produce il risultato previsto quando utilizzo gli array interleaved?

risposta

8

La falcata è errata, poiché hai 6 elementi per vertice, devi passare il passo 6 * sizeof (float) come passo.