开发者

white screen on mapping second texture on the back side of OpenGLES view

开发者 https://www.devze.com 2023-03-07 01:28 出处:网络
What I am trying to get is a flip animation where the front face is an album thumbnail and it rotates and scales to a larger Image showing album details. I am able to map the front texture from UIVIew

What I am trying to get is a flip animation where the front face is an album thumbnail and it rotates and scales to a larger Image showing album details. I am able to map the front texture from UIVIew, but for the back texture I only get a white screen.

    - (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];
}

In the DisplayLink Function:

    - (void) drawFrame:(CADisplayLink*) displayLink {
     /* setup */
        glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
          glClear(GL_COLOR_BU开发者_开发技巧FFER_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);


}


glDisable(GL_CULL_FACE) instead of glEnable(GL_CULL_FACE); will, well, disable backface-culling, which makes it possible to only draw polygons that are towards the camera ( for usual 3D objects, this is a great optimisation, but not in your case )

0

精彩评论

暂无评论...
验证码 换一张
取 消