검색결과 리스트
글
OpenGL ES 사용
아이폰
2011. 2. 14. 10:50
이미지 데이터 OpenGL ES 메모리로 로드하기
-(CGSize)loadTextureImage:(NSString*)imageName materialKey:(NSString*)materialKey
{
CGContextRef spriteContext; //context ref for the UIImage
GLubyte *spriteData; // a temporary buffer to hold our image data
size_t width, height;
GLuint textureID; // the ultimate ID for this texture
// grab the image off the file system, jam it into a CGImageRef
UIImage* uiImage = [[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:imageName ofType:nil]];
CGImageRef spriteImage = [uiImage CGImage];
// Get the width and height of the image
width = CGImageGetWidth(spriteImage);
height = CGImageGetHeight(spriteImage);
CGSize imageSize = CGSizeMake(width, height);
// Texture dimensions must be a power of 2. If you write an application that allows users to supply an image,
// you'll want to add code that checks the dimensions and takes appropriate action if they are not a power of 2.
if (spriteImage) {
// Allocated memory needed for the bitmap context
spriteData = (GLubyte *) malloc(width * height * 4);
memset(spriteData, 0, (width * height * 4));
// Uses the bitmatp creation function provided by the Core Graphics framework.
spriteContext = CGBitmapContextCreate(spriteData, width, height, 8, width * 4, CGImageGetColorSpace(spriteImage), kCGImageAlphaPremultipliedLast);
// After you create the context, you can draw the sprite image to the context.
CGContextDrawImage(spriteContext, CGRectMake(0.0, 0.0, (CGFloat)width, (CGFloat)height), spriteImage);
// 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, &textureID);
// Bind the texture name.
glBindTexture(GL_TEXTURE_2D, textureID);
// Specidfy a 2D texture image, provideing the a pointer to the image data in memory
//Convert "RRRRRRRRRGGGGGGGGBBBBBBBBAAAAAAAA" to "RRRRRGGGGBBBBAAAA"
// this will make your images take up half as much space in memory
// but you will lose half of your color depth.
if (BB_CONVERT_TO_4444) {
void* tempData;
unsigned int* inPixel32;
unsigned short* outPixel16;
tempData = malloc(height * width * 2);
inPixel32 = (unsigned int*)spriteData;
outPixel16 = (unsigned short*)tempData;
NSUInteger i;
for(i = 0; i < width * height; ++i, ++inPixel32)
*outPixel16++ = ((((*inPixel32 >> 0) & 0xFF) >> 4) << 12) | ((((*inPixel32 >> 8) & 0xFF) >> 4) << 8) | ((((*inPixel32 >> 16) & 0xFF) >> 4) << 4) | ((((*inPixel32 >> 24) & 0xFF) >> 4) << 0);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_SHORT_4_4_4_4, tempData);
free(tempData);
} else {
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, spriteData);
}
free(spriteData);
// Release the image data
// 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);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
// Enable use of the texture
glEnable(GL_TEXTURE_2D);
// Set a blending function to use
glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
// Enable blending
glEnable(GL_BLEND);
} else {
return CGSizeZero;
}
[uiImage release];
if (materialLibrary == nil) materialLibrary = [[NSMutableDictionary alloc] init];
// now put the texture ID into the library
[materialLibrary setObject:[NSNumber numberWithUnsignedInt:textureID] forKey:materialKey];
return imageSize;
}
정점그리기
-(void)render
{
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glDisable(GL_TEXTURE_2D);
// load arrays into the engine
glVertexPointer(vertexSize, GL_FLOAT, 0, vertexes);
glEnableClientState(GL_VERTEX_ARRAY);
glColorPointer(colorSize, GL_FLOAT, 0, colors);
glEnableClientState(GL_COLOR_ARRAY);
//render
glDrawArrays(renderStyle, 0, vertexCount);
}
텍스처 그리기
-(void)render
{
glVertexPointer(vertexSize, GL_FLOAT, 0, vertexes);
glEnableClientState(GL_VERTEX_ARRAY);
glColorPointer(colorSize, GL_FLOAT, 0, colors);
glEnableClientState(GL_COLOR_ARRAY);
if (materialKey != nil) {
[[BBMaterialController sharedMaterialController] bindMaterial:materialKey];
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glTexCoordPointer(2, GL_FLOAT, 0, uvCoordinates);
}
//render
glDrawArrays(renderStyle, 0, vertexCount);
}
'아이폰' 카테고리의 다른 글
| Sprite Sheet 제작툴 Zwoptex (0) | 2011.03.16 |
|---|---|
| 애플리케이션 응답성 향상을 위해 동시성 사용하기 (0) | 2011.03.15 |
| OpenGL ES 게임 프레임웍 (0) | 2011.02.14 |
| OpenGL ES 사용 (0) | 2011.02.14 |
| OpenGL ES 사용설정 (0) | 2011.02.13 |
| [iPhone] 로깅 프레임웍 cocoalumberjack (0) | 2010.12.21 |