2012-10-02 66 views
1

我想用紋理繪製立方體。用紋理繪製多個對象

void OperateWithMainMatrix(ESContext* esContext, GLfloat offsetX, GLfloat offsetY, GLfloat offsetZ) { 

UserData *userData = (UserData*) esContext->userData; 
ESMatrix modelview; 
ESMatrix perspective; 
    //Manipulation with matrix 
    ... 
    glVertexAttribPointer(userData->positionLoc, 3, GL_FLOAT, GL_FALSE, 0, cubeFaces); 
    //in cubeFaces coordinates verticles cube 
    glVertexAttribPointer(userData->normalLoc, 3, GL_FLOAT, GL_FALSE, 0, cubeFaces); 
    //for normals (use in fragment shaider for textures) 

    glEnableVertexAttribArray(userData->positionLoc); 
    glEnableVertexAttribArray(userData->normalLoc); 

    // Load the MVP matrix 
    glUniformMatrix4fv(userData->mvpLoc, 1, GL_FALSE, 
         (GLfloat*)&userData->mvpMatrix.m[0][0]); 

    //Bind base map 
    glActiveTexture(GL_TEXTURE0); 
    glBindTexture(GL_TEXTURE_CUBE_MAP, userData->baseMapTexId); 

    //Set the base map sampler to texture unit to 0 
    glUniform1i(userData->baseMapLoc, 0); 

    // Draw the cube 
    glDrawArrays(GL_TRIANGLES, 0, 36); 
    } 

(座標轉換爲OperateWithMainMatrix()) 然後畫出()函數被調用:

void Draw(ESContext *esContext) 
{ 
    UserData *userData = esContext->userData; 
    // Set the viewport 
    glViewport(0, 0, esContext->width, esContext->height); 

    // Clear the color buffer 
    glClear(GL_COLOR_BUFFER_BIT); 

    // Use the program object 
    glUseProgram(userData->programObject); 

    OperateWithMainMatrix(esContext, 0.0f, 0.0f, 0.0f); 

    eglSwapBuffers(esContext->eglDisplay, esContext->eglSurface); 
} 

這項工作很好,但如果我嘗試繪製多個多維數據集(例如下面的代碼) :

void Draw(ESContext *esContext) 
{ ... 

    // Use the program object 
    glUseProgram(userData->programObject); 

    OperateWithMainMatrix(esContext, 2.0f, 0.0f, 0.0f); 
    OperateWithMainMatrix(esContext, 1.0f, 0.0f, 0.0f); 
    OperateWithMainMatrix(esContext, 0.0f, 0.0f, 0.0f); 
    OperateWithMainMatrix(esContext, -1.0f, 0.0f, 0.0f); 
    OperateWithMainMatrix(esContext, -2.0f, 0.0f, 0.0f); 
    eglSwapBuffers(esContext->eglDisplay, esContext->eglSurface); 
} 

A側面覆蓋正面。該過程被示出在圖像: enter image description here

替代圖像(顏色和乾淨圖像): enter image description here

右立方體的側面重疊的中心立方體的前表面。 如何刪除此效果並顯示沒有它的多個立方體?

回答

2

要解決此問題,您需要利用所謂的深度緩衝區。這是確保表面不會被繪製在距離較近的表面(如立方體正面顯示的立方體的一側)上的原因。

幸運的是,這是涉及到這樣做沒有太多的工作:

  1. 啓用在初始化每一幀上glEnable(GL_DEPTH_TEST)
  2. 清除深度緩衝深度測試通過增加它的有點於glClear電話: glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)

之後,你不應該再看到你的表面在較近的表面上突然出現。

+0

感謝您的回答。 (GL_DEPTH_TEST)在我的代碼中是有效的,並且添加到函數draw()glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)沒有帶來期望的結果。 因爲這種效果是在一幀內觀察到的:當我多次調用函數OperateWithMainMatrix()。 – Simplex

+0

@Simplex - glGet(GL_DEPTH_BITS)返回什麼?你的投影矩陣是如何定義的? – Tim

+0

glGet(GL_DEPTH_BITS)返回0 在OperateWithMainMatrix()中定義的矩陣: esMatrixLoadIdentity(&perspective); (透視圖,60.0f,縱橫比,1.0f,20.0f); esMatrixLoadIdentity(&modelview); esTranslate(&modelview,offsetX,offsetY,offsetZ); esTranslate(&perspective,distanceX,0.0,distance); esRotate(&perspective,userData-> angle,0.0,1.0,0.0); esMatrixMultiply(&userData-> mvpMatrix,&modelview,&perspective); – Simplex