2010-08-24 69 views
2

以下代碼是XCode iPhone方形示例的修改版本,用於繪製立方體。但是,當我使用剔除時,多維數據集渲染時會丟失面部。誰能告訴我我做錯了什麼?爲什麼剔除會移除未被遮擋的立方體的面部?

#import "ES1Renderer.h" 

@implementation ES1Renderer 

// Create an OpenGL ES 1.1 context 
- (id)init 
{ 
    if ((self = [super init])) 
    { 
     context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES1]; 

     if (!context || ![EAGLContext setCurrentContext:context]) 
     { 
      [self release]; 
      return nil; 
     } 

     // Create default framebuffer object. The backing will be allocated for the current layer in -resizeFromLayer 
     glGenFramebuffersOES(1, &defaultFramebuffer); 
     glGenRenderbuffersOES(1, &colorRenderbuffer); 
     glBindFramebufferOES(GL_FRAMEBUFFER_OES, defaultFramebuffer); 
     glBindRenderbufferOES(GL_RENDERBUFFER_OES, colorRenderbuffer); 
     glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, colorRenderbuffer); 
     glEnable(GL_CULL_FACE); 
     /*glEnable(GL_LIGHTING); 
     glEnable(GL_LIGHT0); 
     const GLfloat light0Ambient[] = {0.05, 0.05, 0.05, 1.0}; 
     glLightfv(GL_LIGHT0, GL_AMBIENT, light0Ambient); 
     const GLfloat light0Diffuse[] = {0.5, 0.5, 0.5, 1.0}; 
     glLightfv(GL_LIGHT0, GL_DIFFUSE, light0Diffuse); 
     const GLfloat light0Specular[] = {0.7, 0.7, 0.7, 1.0}; 
     glLightfv(GL_LIGHT0, GL_SPECULAR, light0Specular); 
     const GLfloat light0Position[] = {0.0, 0.0, 1.0, 0.0}; 
     glLightfv(GL_LIGHT0, GL_POSITION, light0Position); 
     const GLfloat light0Direction[] = {0.0, 0.0, -1.0}; 
     glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, light0Direction); 
     glLightf(GL_LIGHT0, GL_SPOT_CUTOFF, 100.0); */ 
     glMatrixMode(GL_PROJECTION); 
     glLoadIdentity(); 
     glOrthof(-1.0f, 1.0f, -1.5f, 1.5f, -1.0f, 1.0f); 
     glScalef(0.5f, 0.5f, 0.5f); 
     glMatrixMode(GL_MODELVIEW); 
     glLoadIdentity(); 

    } 

    return self; 
} 

- (void)render 
{ 
    // Replace the implementation of this method to do your own custom drawing 

    static const GLfloat squareVertices[] = { 
     -0.5f, -0.5f, -0.5f, // 0 
     0.5f, -0.5f, -0.5f, // 1 

     -0.5f, 0.5f, -0.5f, // 2 
     0.5f, 0.5f, -0.5f, // 3 

     -0.5f, -0.5f, 0.5f, // 4 
     0.5f, -0.5f, 0.5f, // 5 

     -0.5f, 0.5f, 0.5f, // 6 
     0.5f, 0.5f, 0.5f, // 7 
    }; 

    static const GLubyte squareColors[] = { 
     255, 0, 0, 255, 
     0, 255, 0, 255, 
     0, 0, 255, 255, 
     255, 255, 255, 255, 

     255, 0, 0, 255, 
     0, 255, 0, 255, 
     0, 0, 255, 255, 
     255, 255, 255, 255, 
    }; 

    static const GLubyte triangles [] = { 
     0, 1, 2, // front 
     1, 3, 2, 
     2, 3, 6, // top 
     3, 7, 6, 
     6, 4, 5, // back 
     5, 7, 6, 
     4, 0, 1, // bottom 
     1, 5, 4, 
     4, 0, 2, // left 
     2, 6, 4, 
     1, 3, 5, // right 
     3, 7, 5, 
    }; 

    // static float transY = 0.0f; 

    // This application only creates a single context which is already set current at this point. 
    // This call is redundant, but needed if dealing with multiple contexts. 
    [EAGLContext setCurrentContext:context]; 

    // This application only creates a single default framebuffer which is already bound at this point. 
    // This call is redundant, but needed if dealing with multiple framebuffers. 
    glBindFramebufferOES(GL_FRAMEBUFFER_OES, defaultFramebuffer); 
    glViewport(0, 0, backingWidth, backingHeight); 


    glRotatef((GLfloat)1.0f, 0.0f, 0.0f, 1.0f); 
    // transY++; 

    glClearColor(0.0f, 0.0f, 0.0f, 1.0f); 
    glClear(GL_COLOR_BUFFER_BIT); 

    glVertexPointer(3, GL_FLOAT, 0, squareVertices); 
    glEnableClientState(GL_VERTEX_ARRAY); 
    glColorPointer(4, GL_UNSIGNED_BYTE, 0, squareColors); 
    glEnableClientState(GL_COLOR_ARRAY); 

    //int n = ((int)[[NSDate date] timeIntervalSince1970])/2; 
    glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_BYTE, triangles); 


    // This application only creates a single color renderbuffer which is already bound at this point. 
    // This call is redundant, but needed if dealing with multiple renderbuffers. 
    glBindRenderbufferOES(GL_RENDERBUFFER_OES, colorRenderbuffer); 
    [context presentRenderbuffer:GL_RENDERBUFFER_OES]; 
} 

- (BOOL)resizeFromLayer:(CAEAGLLayer *)layer 
{ 
    // Allocate color buffer backing based on the current layer size 
    glBindRenderbufferOES(GL_RENDERBUFFER_OES, colorRenderbuffer); 
    [context renderbufferStorage:GL_RENDERBUFFER_OES fromDrawable:layer]; 
    glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_WIDTH_OES, &backingWidth); 
    glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_HEIGHT_OES, &backingHeight); 

    if (glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES) != GL_FRAMEBUFFER_COMPLETE_OES) 
    { 
     NSLog(@"Failed to make complete framebuffer object %x", glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES)); 
     return NO; 
    } 

    return YES; 
} 

- (void)dealloc 
{ 
    // Tear down GL 
    if (defaultFramebuffer) 
    { 
     glDeleteFramebuffersOES(1, &defaultFramebuffer); 
     defaultFramebuffer = 0; 
    } 

    if (colorRenderbuffer) 
    { 
     glDeleteRenderbuffersOES(1, &colorRenderbuffer); 
     colorRenderbuffer = 0; 
    } 

    // Tear down context 
    if ([EAGLContext currentContext] == context) 
     [EAGLContext setCurrentContext:nil]; 

    [context release]; 
    context = nil; 

    [super dealloc]; 
} 

@end 

回答

2

看看第一個繪製的三角形:它的指數是0,1,2。嘗試想象在3D中相應的三角形(或繪製它)。從世界的中心,頂點逆時針;從臉的另一面看,它們是順時針的。

默認情況下,臉部剔除會選擇順時針方向的臉部。

所以,你有兩個選擇:

  • 變化指數的三角形[]的順序:2,1,0,...
  • 更改默認行爲:glCullFace(GL_FRONT);