2012-08-16 25 views
0

我是iphone开发新手。目前我正在使用opengl的项目,我需要为视图的一部分设置动画。为此,我拍摄了屏幕截图,然后创建了纹理。如何在开放的gl iphone中为动画制作纹理?

这是我的代码

glEnable(GL_TEXTURE_2D); 
glEnable(GL_BLEND); 
glBlendFunc(GL_ONE, GL_SRC_COLOR); 

UIGraphicsBeginImageContext(self.introductionTextLabel.frame.size); 
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()]; 
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 

GLuint  texture[1]; 
glGenTextures(1, &texture[0]); 

glBindTexture(GL_TEXTURE_2D, texture[0]); 

glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); 
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); 

GLuint width = CGImageGetWidth(viewImage.CGImage); 
GLuint height = CGImageGetHeight(viewImage.CGImage); 
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); 
void *imageData = malloc(height * width * 4); 
CGContextRef contextTexture = CGBitmapContextCreate(imageData, width, height, 8, 4 * width, colorSpace, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big); 

CGColorSpaceRelease(colorSpace); 
CGContextClearRect(contextTexture, CGRectMake(0, 0, width, height)); 
CGContextTranslateCTM(contextTexture, 0, height - height); 
CGContextDrawImage(contextTexture, CGRectMake(0, 0, width, height), viewImage.CGImage); 

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, imageData); 


CGContextRelease(contextTexture); 

free(imageData); 

我寻觅了很多,但无法找到一个发出动画纹理的好方法。

任何人都可以建议我很好的方法。如果我做错了,请指出。

在此先感谢。

回答

0

我可以推荐您修改纹理的唯一方法是使用glTexImage2D()进行全帧更新,并使用glTexSubImage2D()进行部分更新。当然,如果你使用预加载和压缩类型的纹理会更好......