// // GL.m // SpriteGL // #import "GL.h" @implementation GL GLfloat spriteTexcoords[8]; + (GLuint)loadTexture:(StringPtr)cadena { CGImageRef spriteImage; GLuint spriteTexture = 0; // Creates a Core Graphics image from an image file spriteImage = [UIImage imageNamed:cadena].CGImage; if(spriteImage) { // Get the width and height of the image size_t width = CGImageGetWidth(spriteImage); size_t height = CGImageGetHeight(spriteImage); // Allocated memory needed for the bitmap context GLubyte *spriteData = (GLubyte *) malloc(width * height * 4); // Uses the bitmatp creation function provided by the Core Graphics framework. CGContextRef 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, &spriteTexture); // Bind the texture name. glBindTexture(GL_TEXTURE_2D, spriteTexture); // Speidfy a 2D texture image, provideing the a pointer to the image data in memory glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, spriteData); // Release the image data free(spriteData); } return spriteTexture; } // // Establece el color para el dibujado // + (void)setColor:(int)r g:(int)g b:(int)b alpha:(int)alpha { glColor4f((GLfloat)r / 256.0f, (GLfloat)g / 256.0f, (GLfloat)b / 256.0f, (GLfloat)alpha / 256.0f); } // // Dibuja un rectangulo solido sin textura // + (void)fillRec:(float)x1 y1:(float)y1 x2:(float)x2 y2:(float)y2 { GLfloat triVertices[] = { x1, y2, 0.0f, x2, y2, 0.0f, x2, y1, 0.0f, x1, y1, 0.0f }; glVertexPointer(3, GL_FLOAT, 0, triVertices); glEnableClientState(GL_VERTEX_ARRAY); glDrawArrays(GL_TRIANGLE_FAN, 0, 4); } + (void)setSprite:(GLuint)texture frame:(int)frame { glEnable(GL_TEXTURE_2D); glBindTexture(GL_TEXTURE_2D, texture); // 0 - 0, 0 // 1 - 0.5, 0 // 2 - 0, 0.5 // 3 - 0.5, 0.5 // [0] [1] // [2] [3] GLfloat x1 = (frame % 2) / 2.0f; GLfloat y1 = (frame / 2) / 2.0f; GLfloat x2 = x1 + 0.5f; GLfloat y2 = y1 + 0.5f; // preparamos el array con las coordenadas de la textyra spriteTexcoords[0] = x1; spriteTexcoords[1] = y1; spriteTexcoords[2] = x2; spriteTexcoords[3] = y1; spriteTexcoords[4] = x2; spriteTexcoords[5] = y2; spriteTexcoords[6] = x1; spriteTexcoords[7] = y2; } // // dibuja un sprite cuadrado con el indice de la textua activa // + (void)drawSprite:(int)x1 y1:(int)y1 x2:(int)x2 y2:(int)y2 { // Sets up an array of values to use as the sprite vertices. GLfloat spriteVertices[] = { x1, y1, x2, y1, x2, y2, x1, y2 }; glVertexPointer(2, GL_FLOAT, 0, spriteVertices); glEnableClientState(GL_VERTEX_ARRAY); glTexCoordPointer(2, GL_FLOAT, 0, spriteTexcoords); glEnableClientState(GL_TEXTURE_COORD_ARRAY); /* // Sets up an array of values for the texture coordinates. GLfloat spriteTexcoords[] = { 0, 0, .5, 0, .5, .5, 0, .5, }; glTexCoordPointer(2, GL_FLOAT, 0, spriteTexcoords); glEnableClientState(GL_TEXTURE_COORD_ARRAY); */ glDrawArrays(GL_TRIANGLE_FAN, 0, 4); //glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); } @end