- (CGImageRef) getPicture
{
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
// Create and init the textures
Texture2D *_textures[2];
_textures[0] = [[Texture2D alloc] initWithImage: [UIImage imageNamed:@"clean.png"]];
_textures[1] = [[Texture2D alloc] initWithImage: [UIImage imageNamed:@"testbkgnd1.jpg"]];
// Get the drawing in the screen
glBindTexture(GL_TEXTURE_2D,[_textures[0] name]);
glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, kScreenWidth, kScreenHeight);
// Redraw the scene
[_textures[1] drawInRect:[self bounds]];
[_textures[0] drawInRect:[self bounds]];
// Entire data length
NSInteger myDataLength = kScreenWidth * kScreenHeight * 4;
// allocate array and read pixels into it.
static GLubyte *buffer = NULL;
static GLubyte *invertedBuffer = NULL;
if(buffer == NULL)
buffer = (GLubyte *) malloc(myDataLength);
if(invertedBuffer == NULL)
invertedBuffer = (GLubyte *) malloc(myDataLength);
glReadPixels(0, 0, kScreenWidth, kScreenHeight, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
// reverse the height as opengl draws it upside down
for(int y = 0; y <kScreenHeight; y++)
{
for(int x = 0; x <kScreenWidth * 4; x++)
{
invertedBuffer[((kScreenHeight-1) - y) * kScreenWidth * 4 + x] = buffer[y * 4 * kScreenWidth + x];
}
}
// make data provider with data.
CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, invertedBuffer, myDataLength, NULL);
// prep the ingredients
int bitsPerComponent = 8;
int bitsPerPixel = 32;
int bytesPerRow = 4 * kScreenWidth;
CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault;
CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault;
// make the cgimage
CGImageRef imageRef = CGImageCreate(kScreenWidth, kScreenHeight, bitsPerComponent, bitsPerPixel, bytesPerRow, colorSpaceRef, bitmapInfo, provider, NULL, NO, renderingIntent);
// Redraw the drawings w/o the background
[self erase];
[_textures[0] drawInRect:[self bounds]];
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
[self swapBuffers];
// Bind the brush stoke texture to the default one
glBindTexture(GL_TEXTURE_2D, brushTexture[0]);
// Release the textures
[ _textures[0] release];
[ _textures[1] release];
return imageRef;
}
This is my code... but still get a darker _texture[1] with higher contrast.
and when i Redraw the drawings w/o the background the drawings were also dark w/o any additive drawings also...
comments please.
Saran.
|