2012-10-02 5 views
1

텍스처를 사용하여 큐브를 그립니다.텍스처가있는 여러 객체 그리기

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 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) 
{ ... 

    // 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); 
} 

측면이 정면과 겹쳐 있습니다. 이 프로세스는 이미지에서 도시된다 enter image description here

다른 그림 (색상 깨끗한 이미지) enter image description here

오른쪽 큐브의 측면 중앙 큐브의 정면 얼굴과 중첩. 어떻게이 효과를 제거하고 그것을 사용하지 않고 여러 개의 큐브를 표시 할 수 있습니까?

답변

2

이 문제를 해결하려면 깊이 버퍼을 사용해야합니다. 이것은 서페이스가 더 가까운 서페이스의 겹쳐서 나오지 않도록하기위한 것입니다 (큐브의 정면에 큐브의면이 표시되는 것처럼). glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) :가 glClear 호출에 약간의 추가

  1. 각 프레임에 glEnable(GL_DEPTH_TEST)
  2. 지우기 깊이 버퍼 초기화에서 깊이 테스트를 사용 :

    는 다행히 그렇게 할 관련이없는 많은 작업입니다

    그 후에 더 가까운 서페이스 위에 서페이스가 튀어 나오지 않아야합니다.

+0

답장을 보내 주셔서 감사합니다. 두렵습니다. 문제가 해결되지 않습니다. glEnable (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) return 0 OperateWithMainMatrix()에 정의 된 행렬 : esMatrixLoadIdentity (& perspective); esPerspective (& perspective, 60.0f, aspect, 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