我想要得到的是一個翻轉動畫,其中正面是一個專輯縮略圖,它旋轉並縮放到顯示專輯詳細信息的較大圖像。我能夠映射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);
}
看看這個https://github.com/epatel/EPGLTransitionView – epatel 2011-05-21 21:30:03
@ epatel,感謝您的代碼!我和你的代碼之間的唯一區別似乎是紋理大小(並且我縮放紋理)。可能我沒有正確使用glTexSubImage2D(我的最終紋理需要是600x600,這不是2的冪)。 – Shreesh 2011-05-23 07:56:33