Create openGL texture

by nik 13. May 2009 13:58

- (void)createGLTexture:(GLuint *)texName fromCGImage:(CGImageRef)img

{

GLubyte *spriteData = NULL;

CGContextRef spriteContext;

GLuint imgW, imgH, texW, texH;

imgW = CGImageGetWidth(img);

imgH = CGImageGetHeight(img);

// Find smallest possible powers of 2 for our texture dimensions

for (texW = 1; texW < imgW; texW *= 2) ;

for (texH = 1; texH < imgH; texH *= 2) ;

// Allocated memory needed for the bitmap context

spriteData = (GLubyte *) calloc(texH, texW * 4);

// Uses the bitmatp creation function provided by the Core Graphics framework. 

spriteContext = CGBitmapContextCreate(spriteData, texW, texH, 8, texW * 4, CGImageGetColorSpace(img), kCGImageAlphaPremultipliedLast);

// Translate and scale the context to draw the image upside-down (conflict in flipped-ness between GL textures and CG contexts)

CGContextTranslateCTM(spriteContext, 0., texH);

CGContextScaleCTM(spriteContext, 1., -1.);

// After you create the context, you can draw the sprite image to the context.

CGContextDrawImage(spriteContext, CGRectMake(0.0, 0.0, imgW, imgH), img);

// You don't need the context at this point, so you need to release it to avoid memory leaks.

CGContextRelease(spriteContext);

// Use OpenGL ES to generate a name for the texture.

glGenTextures(1, texName);

// Bind the texture name. 

glBindTexture(GL_TEXTURE_2D, *texName);

// Speidfy a 2D texture image, provideing the a pointer to the image data in memory

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, texW, texH, 0, GL_RGBA, GL_UNSIGNED_BYTE, spriteData);

// Set the texture parameters to use a minifying filter and a linear filer (weighted average)

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

// Enable use of the texture

glEnable(GL_TEXTURE_2D);

// Set a blending function to use

glBlendFunc(GL_SRC_ALPHA,GL_ONE);

// Enable blending

glEnable(GL_BLEND);

free(spriteData);

}

Draw on UIImage

by alex 4. May 2009 13:15

-(UIImage *)addCircle:(UIImage *)img radius:(CGFloat)radius latCon:(CGFloat)lat lonCon:(CGFloat)lon{

    int w = img.size.width;

    int h = img.size.height; 

    lon = h - lon;

    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();

    CGContextRef context = CGBitmapContextCreate(NULL, w, h, 8, 4 * w, colorSpace, kCGImageAlphaPremultipliedFirst);

    

//draw the circle

CGContextDrawImage(context, CGRectMake(0, 0, w, h), img.CGImage);

CGRect leftOval = {lat- radius/2, lon - radius/2, radius, radius};

CGContextSetRGBFillColor(context, 0.0, 0.0, 1.0, 0.3);

CGContextAddEllipseInRect(context, leftOval);

CGContextFillPath(context);

    CGImageRef imageMasked = CGBitmapContextCreateImage(context);

    CGContextRelease(context);

    CGColorSpaceRelease(colorSpace);

    return [UIImage imageWithCGImage:imageMasked];

}

Powered by BlogEngine.NET 1.4.5.0
Theme by Mads Kristensen