2011-07-08 285 views
1

我正在使用VBO在OpenGL中使用GLSL着色器繪製一系列多維數據集,該GLSL着色器執行多紋理(包含5個紋理)。如果我不傳遞紋理,它會正常工作。但是如果我嘗試使用紋理,整個屏幕只會被繪製爲白色(可能是最後一個清晰的顏色)。如果我使用立即模式(沒有VBO)傳遞紋理,那麼在那裏也很好。我無法分辨爲什麼會有問題。OpenGL VBO繪圖多紋理問題

代碼:

// create vertex/normal/color/texcoord VBO 
glGenBuffersARB(1, &vboId); 
glBindBufferARB(GL_ARRAY_BUFFER_ARB, vboId); 
glBufferDataARB(GL_ARRAY_BUFFER_ARB, sizeof(vert_buf)+sizeof(norm_buf)+sizeof(col_buf)+sizeof(tex_buf), 0, GL_STREAM_DRAW); 
glBufferSubDataARB(GL_ARRAY_BUFFER_ARB, 0, sizeof(vert_buf), vert_buf); 
glBufferSubDataARB(GL_ARRAY_BUFFER_ARB, sizeof(vert_buf), sizeof(norm_buf), norm_buf); 
glBufferSubDataARB(GL_ARRAY_BUFFER_ARB, sizeof(vert_buf)+sizeof(norm_buf), sizeof(col_buf), col_buf); 
glBufferSubDataARB(GL_ARRAY_BUFFER_ARB, sizeof(vert_buf)+sizeof(norm_buf)+sizeof(col_buf), sizeof(tex_buf), tex_buf); 
glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0); 

// create geometry indices VBO 
glGenBuffersARB(1, &vboId2); 
glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, vboId2); 
glBufferDataARB(GL_ELEMENT_ARRAY_BUFFER_ARB, sizeof(index_buf), index_buf, GL_STATIC_DRAW_ARB); 
glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, 0); 

// bind vertex/normal/color/texcoord VBO 
glBindBufferARB(GL_ARRAY_BUFFER_ARB, vboId); 

// enable vertex arrays 
glEnableClientState(GL_NORMAL_ARRAY); 
glEnableClientState(GL_COLOR_ARRAY); 
glEnableClientState(GL_VERTEX_ARRAY); 

// specify vertex and index arrays with their offsets 
glVertexPointer(3, GL_FLOAT, 0, 0); 
glNormalPointer(GL_FLOAT, 0, (void*)sizeof(vert_buf)); 
glColorPointer(3, GL_FLOAT, 0, (void*)(sizeof(vert_buf)+sizeof(norm_buf))); 

// bind geometry indices VBO 
glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, vboId2); 
glIndexPointer(GL_UNSIGNED_INT, 0, 0); 

// set the texture units 
GLvoid* start = (void*)(sizeof(vert_buf)+sizeof(norm_buf)+sizeof(col_buf)); 
glClientActiveTexture(GL_TEXTURE0 + (GLuint)vid_regions[0].tex_id); // same as GL_TEXTURE1 
glTexCoordPointer(2, GL_FLOAT, 0, start); 
glEnableClientState(GL_TEXTURE_COORD_ARRAY); 
glClientActiveTexture(GL_TEXTURE0 + (GLuint)vid_regions[1].tex_id); // same as GL_TEXTURE2 
glTexCoordPointer(2, GL_FLOAT, 0, start); 
glEnableClientState(GL_TEXTURE_COORD_ARRAY); 
glClientActiveTexture(GL_TEXTURE0 + (GLuint)vid_regions[2].tex_id); // same as GL_TEXTURE3 
glTexCoordPointer(2, GL_FLOAT, 0, start); 
glEnableClientState(GL_TEXTURE_COORD_ARRAY); 
glClientActiveTexture(GL_TEXTURE0 + noiseTexID); // same as GL_TEXTURE4 
glTexCoordPointer(2, GL_FLOAT, 0, start); 
glEnableClientState(GL_TEXTURE_COORD_ARRAY); 
glClientActiveTexture(GL_TEXTURE0 + lutTexID); // same as GL_TEXTURE5 
glTexCoordPointer(2, GL_FLOAT, 0, start); 
glEnableClientState(GL_TEXTURE_COORD_ARRAY); 

// draw VBOs 
glDrawElements(GL_TRIANGLES, 36*ROWS*COLS, GL_UNSIGNED_INT, 0); 

// disable arrays 
glDisableClientState(GL_VERTEX_ARRAY); 
glDisableClientState(GL_COLOR_ARRAY); 
glDisableClientState(GL_NORMAL_ARRAY); 

// disable texture arrays 
glClientActiveTexture(GL_TEXTURE0 + (GLuint)vid_regions[0].tex_id); 
glDisableClientState(GL_TEXTURE_COORD_ARRAY); 
glClientActiveTexture(GL_TEXTURE0 + (GLuint)vid_regions[1].tex_id); 
glDisableClientState(GL_TEXTURE_COORD_ARRAY); 
glClientActiveTexture(GL_TEXTURE0 + (GLuint)vid_regions[2].tex_id); 
glDisableClientState(GL_TEXTURE_COORD_ARRAY); 
glClientActiveTexture(GL_TEXTURE0 + noiseTexID); 
glDisableClientState(GL_TEXTURE_COORD_ARRAY); 
glClientActiveTexture(GL_TEXTURE0 + lutTexID); 
glDisableClientState(GL_TEXTURE_COORD_ARRAY); 

// unbind VBOs 
glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0); 
glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, 0); 

注:維也納各組織的創建過程中的初始設置階段被調用。繪圖發生在繪製GLUT例程中。

+0

也許你應該發佈着色器代碼。而你的「設置紋理單元」似乎是非常錯誤的。如果所有紋理單元的座標相同,則不必多次傳遞它們。 – Jonathan

回答

0

嘗試重置您的紋理水平回到0:

glClientActiveTexture(GL_TEXTURE0); 

它的情況並不少見的怪事發生時有效紋理水平不復位(比方說,如果你使用宗教組織等)。

將黑色/白色/透明以外的其他顏色設置爲清晰顏色也可能會有所幫助,以查看屏幕是否用清晰的顏色繪製,或者如果您以某種方式看到了紋理元素拉伸或其他內容。

+0

啊太棒了!就是這樣。謝謝! –