2012-04-30 72 views
5

我的程序的目的是顯示2個不同紋理的四邊形。問題是加載的最古老的紋理設置在兩個四邊形(僅texQuadB)上。我嘗試用texQuadA和texQuadB替換buf [3]的兩個調用,但它不起作用。我找不到用特定的VBO綁定紋理的正確方法。與VBO紋理綁定無法正常工作

#define OFFSET_BUFFER(bytes) ((GLfloat *)NULL + bytes) 

GLfloat verticesQuadA[12] = 
{ 
    -1.0f, 0.0f, 1.0f, 
    -1.0f, 1.0f, 1.0f, 
    0.0f, 1.0f, 1.0f, 
    0.0f, 0.0f, 1.0f, 
}; 

GLfloat verticesQuadB[12] = 
{ 
    0.0f, 0.0f, 1.0f, 
    0.0f, 1.0f, 1.0f, 
    1.0f, 1.0f, 1.0f, 
    1.0f, 0.0f, 1.0f, 
}; 

GLfloat colors[12] = 
{ 
    1.0f, 0.0f, 0.0f, 
    0.0f, 1.0f, 0.0f, 
    0.0f, 0.0f, 1.0f, 
    1.0f, 0.0f, 1.0f, 
}; 

GLfloat texture[8] = 
{ 
    0.0f, 0.0f, 
    1.0f, 0.0f, 
    1.0f, 1.0f, 
    0.0f, 1.0f 
}; 

int main(int argc, char *argv[]) 
{ 
    SDL_Init(SDL_INIT_VIDEO); 
    SDL_WM_SetCaption("Texture Mapping",NULL); 
    SDL_SetVideoMode(500, 500, 32, SDL_OPENGL); 

    bool continuer = true; 
    SDL_Event event; 
    GLuint buf[4]; 
    GLuint texQuadA, texQuadB; 

    glClearDepth(1.0f); 
    glClearColor(0.1f, 0.1f, 0.1f, 0.1f); 
    glEnable(GL_DEPTH_TEST); 
    glMatrixMode(GL_PROJECTION); 
    glLoadIdentity(); 
    gluPerspective(70.0f, (float)500.0f/(float)500.0f, 1.0f, 3000.0f); 
    glewInit(); 

    texQuadA = loadTexture("caisse.jpg"); 
    texQuadB = loadTexture("metal.jpg"); 

    //glBindTexture(GL_TEXTURE_2D, texQuadA); 
    //glBindTexture(GL_TEXTURE_2D, texQuadB); 

    glGenBuffers(4, buf); 

    glBindBuffer(GL_ARRAY_BUFFER, buf[0]); 
    glBufferData(GL_ARRAY_BUFFER, sizeof(verticesQuadA), verticesQuadA, GL_STATIC_DRAW); 

    glBindBuffer(GL_ARRAY_BUFFER, buf[1]); 
    glBufferData(GL_ARRAY_BUFFER, sizeof(verticesQuadB), verticesQuadB, GL_STATIC_DRAW); 

    glBindBuffer(GL_ARRAY_BUFFER, buf[2]); 
    glBufferData(GL_ARRAY_BUFFER, sizeof(colors), colors, GL_STATIC_DRAW); 

    glBindBuffer(GL_ARRAY_BUFFER, buf[3]); 
    glBufferData(GL_ARRAY_BUFFER, sizeof(texture), texture, GL_STATIC_DRAW); 

    while (continuer) 
    { 
     SDL_WaitEvent(&event); 
     switch(event.type) 
     { 
      case SDL_QUIT: 
       continuer = false; 
     } 

     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 

     glEnable(GL_TEXTURE_2D); 

     glMatrixMode(GL_MODELVIEW); 
     glLoadIdentity(); 

     gluLookAt(0.0f, 0.0f, 3.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f); 

     //Draw Quad A --------------------------------------- 

     glBindBuffer(GL_ARRAY_BUFFER, buf[0]); 
     glVertexPointer(3, GL_FLOAT, 0, OFFSET_BUFFER(0)); 

     glBindBuffer(GL_ARRAY_BUFFER, buf[2]); 
     glColorPointer(3, GL_FLOAT, 0, OFFSET_BUFFER(0)); 

     glBindBuffer(GL_ARRAY_BUFFER, buf[3]); 
     glTexCoordPointer(2, GL_FLOAT, 0, OFFSET_BUFFER(0)); 

     glEnableClientState(GL_VERTEX_ARRAY); 
     glEnableClientState(GL_COLOR_ARRAY); 
     glEnableClientState(GL_TEXTURE_COORD_ARRAY); 

     glDrawArrays(GL_QUADS, 0, 4); 

     glDisableClientState(GL_TEXTURE_COORD_ARRAY); 
     glDisableClientState(GL_COLOR_ARRAY); 
     glDisableClientState(GL_VERTEX_ARRAY); 

     //Draw Quad B --------------------------------------- 

     glBindBuffer(GL_ARRAY_BUFFER, buf[1]); 
     glVertexPointer(3, GL_FLOAT, 0, OFFSET_BUFFER(0)); 

     glBindBuffer(GL_ARRAY_BUFFER, buf[2]); 
     glColorPointer(3, GL_FLOAT, 0, OFFSET_BUFFER(0)); 

     glBindBuffer(GL_ARRAY_BUFFER, buf[3]); 
     glTexCoordPointer(2, GL_FLOAT, 0, OFFSET_BUFFER(0)); 

     glEnableClientState(GL_VERTEX_ARRAY); 
     glEnableClientState(GL_COLOR_ARRAY); 
     glEnableClientState(GL_TEXTURE_COORD_ARRAY); 

     glDrawArrays(GL_QUADS, 0, 4); 

     glDisableClientState(GL_TEXTURE_COORD_ARRAY); 
     glDisableClientState(GL_COLOR_ARRAY); 
     glDisableClientState(GL_VERTEX_ARRAY); 

     //---------------------------------------------------- 

     glFlush(); 
     SDL_GL_SwapBuffers(); 
    } 
    glDeleteBuffers(3, buf); 
    glDisable(GL_TEXTURE_2D); 
    SDL_Quit(); 

    return 0; 
} 

回答

7

OpenGL是一個狀態機。綁定紋理是設置該狀態的一部分。啓用紋理時,OpenGL將在繪製幾何圖形時使用當前的紋理狀態(您綁定的最後一個紋理)。在繪製Quad A之前,綁定texQuadA。在繪製Quad B之前,綁定texQuadB:

glBindTexture(GL_TEXTURE_2D, texQuadA); 
// Draw Quad A 

glBindTexture(GL_TEXTURE_2D, texQuadB); 
// Draw Quad B 

在這種情況下,VBOs不相關。

+0

當然,這是一個愚蠢的職位大聲笑。非常感謝您快速回答。再見。 – user1364743

1

VBOs與紋理對象沒有交互。綁定VBO不會將其與紋理關聯。

要選擇要使用哪種紋理進行繪製,請在使用繪圖命令之前調用glBindTexture。