在OpenGLES视图背面映射第二个纹理的白色屏幕

时间:2011-05-20 10:15:12

标签: iphone opengl-es texture-mapping

我想要的是一个翻转动画,其中正面是一个专辑缩略图,它旋转并缩放为更大的图像显示专辑详细信息。我可以从UIVIew映射前面的纹理,但对于背面纹理,我只能得到一个白色的屏幕。

    - (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);


}

1 个答案:

答案 0 :(得分:1)

glDisable(GL_CULL_FACE)而不是glEnable(GL_CULL_FACE);将禁用背面剔除,这样就可以只绘制朝向相机的多边形(对于通常的3D对象,这是一个很好的优化,但是不在你的情况下)