貼圖會存在GLuint *tID;
//把UIView的CALayer畫成圖片
- (void)setOverlayLayer:(CALayer *)overlayLayer {
UIImage* image = nil;
UIGraphicsBeginImageContext(overlayLayer.frame.size);
{
[overlayLayer renderInContext: UIGraphicsGetCurrentContext()];
image = UIGraphicsGetImageFromCurrentImageContext();
}
UIGraphicsEndImageContext();
// Get the inner CGImage from the UIImage wrapper
CGImageRef cgImage = image.CGImage;
// Get the image size
NSInteger width = CGImageGetWidth(cgImage);
NSInteger height = CGImageGetHeight(cgImage);
// Record the number of channels
NSInteger channels = CGImageGetBitsPerPixel(cgImage)/CGImageGetBitsPerComponent(cgImage);
// Generate a CFData object from the CGImage object (a CFData object represents an area of memory)
CFDataRef imageData = CGDataProviderCopyData(CGImageGetDataProvider(cgImage));
unsigned char* pngData = new unsigned char[width * height * channels];
const int rowSize = width * channels;
const unsigned char* pixels = (unsigned char*)CFDataGetBytePtr(imageData);
// Copy the row data from bottom to top
for (int i = 0; i < height; ++i) {
memcpy(pngData + rowSize * i, pixels + rowSize * (height - 1 - i), width * channels);
}
glClearColor(0.0f, 0.0f, 0.0f, QCAR::requiresAlpha() ? 0.0f : 1.0f);
if (!trackingTIDSet) {
glGenTextures(1, &tID);
}
glBindTexture(GL_TEXTURE_2D, tID);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_BGRA_EXT, GL_UNSIGNED_BYTE, (GLvoid*)pngData);
trackingTIDSet = YES;
trackingTextureAvailable = YES;
delete[] pngData;
CFRelease(imageData);
self.renderState = RS_NORMAL;
NSLog(@"setOverlayer");
}
在做這個轉換之前,記得openGL只吃長寬為2的次方的貼圖
因為忽略這個基本觀念,整整花了二天debug得到這個教訓
沒有留言:
張貼留言