2011-05-20 39 views
1

我想要得到的是一個翻轉動畫,其中正面是一個專輯縮略圖,它旋轉並縮放到顯示專輯詳細信息的較大圖像。我能夠映射UIVIew的正面紋理,但對於背面紋理,我只能看到白色的屏幕。在OpenGLES視圖的背面映射第二個紋理的白色屏幕

- (void) prepareTexturefrom:(UIView*) fromView To:(UIView*) toView { 
    glLoadIdentity(); 
    glMatrixMode(GL_MODELVIEW); 
    glViewport(0, 0, rect.size.width, rect.size.height); 
    // Turn necessary features on 
     glEnable(GL_TEXTURE_2D); 

     /* Create front Texture in texture[0] , which renders fine*/ 
    ... 
    /*Create back Texture in texture[1], I only get white screen */ 
    maxTextureSize = 1024 
    // Get a image of the screen 
    UIGraphicsBeginImageContext(animateTo.size); 
    [toView.layer renderInContext:UIGraphicsGetCurrentContext()]; 
    UIImage* image2 = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 


    // make space for an RGBA image of the view 
    GLubyte *pixelBuffer = (GLubyte*)calloc(maxTextureSize*4, maxTextureSize); 
    // create a suitable CoreGraphics context 
    CGColorSpaceRef colourSpace = CGColorSpaceCreateDeviceRGB(); 
    CGContextRef textureContext2 = 
    CGBitmapContextCreate(pixelBuffer, 
          maxTextureSize, maxTextureSize, 
          8, 4*maxTextureSize, 
          colourSpace, 
          kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big); 
    CGColorSpaceRelease(colourSpace); 

    CGContextDrawImage(textureContext2, CGRectMake(0, maxTextureSize-animateTo.size.height,animateTo.size.width, animateTo.size.height), 
         image2.CGImage); 
    //CGContextRelease(textureContext); 

    glGenTextures(1, &texture[1]); 
    glBindTexture(GL_TEXTURE_2D, texture[1]); 
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); 
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 
// glTexSubImage2D(GL_TEXTURE_2D, 0, (maxTextureSize - toView.bounds.size.width) /2, //(maxTextureSize - toView.bounds.size.height) /2, toView.bounds.size.width, //toView.bounds.size.height, GL_RGBA, GL_UNSIGNED_BYTE, pixelBuffer); 
    glTexImage2D(GL_TEXTURE_2D, 0, 
       GL_RGBA, 
       toView.bounds.size.width, toView.bounds.size.height, 0, 
       GL_RGBA, GL_UNSIGNED_BYTE, pixelBuffer); 


    // clean up 
    CGContextRelease(textureContext2); 
    //glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, maxTextureSize, maxTextureSize, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixelBuffer); 
    free(pixelBuffer); 
    CADisplayLink *aDisplayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(drawFrame:)]; 
    [aDisplayLink setFrameInterval: 2]; 
    [aDisplayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode]; 
} 

在DisplayLink的功能:

- (void) drawFrame:(CADisplayLink*) displayLink { 
    /* setup */ 
     glClearColor(0.0f, 0.0f, 0.0f, 0.0f); 
      glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 

     const GLfloat zNear = 1, zFar = -1000.0, fieldOfView = 45.0; 
     glMatrixMode(GL_PROJECTION); 
     glLoadIdentity(); 
     glMatrixMode(GL_MODELVIEW); 
     glViewport(0, 0, rect.size.width, rect.size.height); 
     glEnable(GL_CULL_FACE); 

     // Turn necessary features on 
     glEnable(GL_TEXTURE_2D);  



    static const GLfloat vertices[] = { 
     - 300.0/768 , - 266.0/1024 , //bottom left 
     300.0/768, - 266.0/1024 , //bottom right 
     - 300.0/768, 266.0/1024 , //top left 
     300.0/768, 266.0/1024 // top right 
    }; 

GLfloat texcoords[] = { 
     0, 1, 
     1, 1, 
     0, 0, 
     1, 0, 
    }; 

    /* Do Rotate, Scale Transformations */ 
    ...... 


    /* Draw the textures */ 

    glEnableClientState(GL_VERTEX_ARRAY); 
    glTexCoordPointer(2, GL_FLOAT, 0, texcoords); 
     glEnableClientState(GL_TEXTURE_COORD_ARRAY);  
    glBindTexture(GL_TEXTURE_2D, texture[0]); 
    glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); 

    glBindTexture(GL_TEXTURE_2D, texture[1]); 
    glRotatef(180, 0, 1, 0);  
     glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); 


} 
+0

看看這個https://github.com/epatel/EPGLTransitionView – epatel 2011-05-21 21:30:03

+0

@ epatel,感謝您的代碼!我和你的代碼之間的唯一區別似乎是紋理大小(並且我縮放紋理)。可能我沒有正確使用glTexSubImage2D(我的最終紋理需要是600x600,這不是2的冪)。 – Shreesh 2011-05-23 07:56:33

回答

1

glDisable(GL_CULL_FACE)代替glEnable(GL_CULL_FACE);會,以及,禁用背面-剔除,這使得它可以僅繪製是朝向照相機多邊形(用於通常3D對象,這是一個很好的優化,但不是在你的情況)

+0

如何在不使用glEnable(GL_CULL_FACE)的情況下隱藏背面? – Shreesh 2011-05-20 18:24:34

+0

你不能;但我認爲你的問題是你*希望*禁用背面剔除?順便說一句,您可以在一幀內啓用它並將其禁用,這樣您就可以在不剔除的情況下渲染您的方塊,然後重新啓用它。 – Calvin1602 2011-05-20 18:56:36

+0

我面臨的問題是第二個紋理沒有被映射(白色屏幕),如果禁用剔除,我會看到背面紋理的白色屏幕。可能在prepareTexturefrom:函數中出現錯誤。謝謝。 – Shreesh 2011-05-21 17:52:11