Hello, I'm a newbie in iphone development so plz don't mind. I'm trying to draw PNG on my screen using libpng but its just displaying nothing on the screen. The code I'm using is:
// Sets up matrices and transforms for OpenGL ES
glViewport(0, 0, backingWidth, backingHeight);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrthof(0.0f, 320.0f, 0.0f,480.0f, -1.0f, 1.0f);
glMatrixMode(GL_MODELVIEW);
// Clears the view with black
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
// Sets up pointers and enables states needed for using vertex arrays and textures
glVertexPointer(2, GL_FLOAT, 0, spriteVertices);
glEnableClientState(GL_VERTEX_ARRAY);
glTexCoordPointer(2, GL_SHORT, 0, spriteTexcoords);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
[EAGLContext setCurrentContext:context];
void *data = NULL;
int width = 0;
int height = 0;
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"figure" ofType:@"png"];
loadPNG((char *) [filePath UTF8String], &width, &height, &data);
GLuint texName = 1;
glGenTextures(1, &texName);
glBindTexture(GL_TEXTURE_2D, texName);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
NSLog(@"[setupView] Width: %d and Height: %d", width, height);
// 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);
// Make sure that you are drawing to the current context
[EAGLContext setCurrentContext:context];
glBindFramebufferOES(GL_FRAMEBUFFER_OES, viewFramebuffer);
//glRotatef(3.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer);
[context presentRenderbuffer:GL_RENDERBUFFER_OES];
And the implementation of loadPNG function is:
void loadPNG(char *path, int *width3, int *height3, void *data)
{
png_uint_32 width, height, width2, height2;
int bits = 0;
FILE *png_file = fopen(path, "rb");
if(!png_file) {
perror("PNG does not exist");
exit(-1);
}
uint8_t header[PNG_SIG_BYTES];
fread(header, 1, PNG_SIG_BYTES, png_file);
if(png_sig_cmp(header, 0, PNG_SIG_BYTES)) {
perror("Unknown file format");
exit(-2);
}
png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
png_infop info_ptr = png_create_info_struct(png_ptr);
png_infop end_info = png_create_info_struct(png_ptr);
png_init_io(png_ptr, png_file);
png_set_sig_bytes(png_ptr, PNG_SIG_BYTES);
png_read_info(png_ptr, info_ptr);
width = png_get_image_width(png_ptr, info_ptr);
height = png_get_image_height(png_ptr, info_ptr);
int bit_depth, color_type;
bit_depth = png_get_bit_depth(png_ptr, info_ptr);
color_type = png_get_color_type(png_ptr, info_ptr);
if( color_type == PNG_COLOR_TYPE_PALETTE )
png_set_palette_to_rgb( png_ptr );
if( color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8 )
png_set_expand_gray_1_2_4_to_8( png_ptr );
if( png_get_valid( png_ptr, info_ptr, PNG_INFO_tRNS ) )
png_set_tRNS_to_alpha (png_ptr);
if( bit_depth == 16 )
png_set_strip_16( png_ptr );
else if( bit_depth < 8 )
png_set_packing( png_ptr );
png_read_update_info(png_ptr, info_ptr);
png_get_IHDR( png_ptr, info_ptr,
&width, &height, &bit_depth, &color_type,
NULL, NULL, NULL );
switch( color_type )
{
case PNG_COLOR_TYPE_GRAY:
bits = 1;
break;
case PNG_COLOR_TYPE_GRAY_ALPHA:
bits = 2;
break;
case PNG_COLOR_TYPE_RGB:
bits = 3;
break;
case PNG_COLOR_TYPE_RGB_ALPHA:
bits = 4;
break;
}
// width2 and height2 are the power of 2 versions of width and height
height2 = height;
width2 = width;
unsigned int i = 0;
if((width2 != 1) && (width2 & (width2 - 1))) {
i = 1;
while( i < width2)
i *= 2;
width2 = i;
}
if((height2 != 1) && (height2 & (height2 - 1))) {
i = 1;
while(i < height2)
i *= 2;
height2 = i;
}
png_byte* pixels = calloc( width2 * height2 * bits, sizeof(png_byte) );
png_byte** row_ptrs = malloc(height * sizeof(png_bytep));
// since Texture2D loads the image "upside-down", there's no need
// to flip the image here
for (i=0; i<height; i++)
row_ptrs[i] = pixels + i*width2*bits;
png_read_image(png_ptr, row_ptrs);
png_read_end( png_ptr, NULL );
png_destroy_read_struct( &png_ptr, &info_ptr, &end_info );
free( row_ptrs );
fclose(png_file);
data = pixels;
*width3 = width2;
*height3 = height2;
NSLog(@"[loadPNG] Width: %d and Height: %d", width2, height2);
}
const GLfloat spriteVertices[] = {
0.0f, 0.0f,
256.0f, 0.0f,
0.0f, 256.0f,
256.0f, 256.0f,
};
// Sets up an array of values for the texture coordinates.
const GLshort spriteTexcoords[] = {
0, 0,
1, 0,
0, 1,
1, 1,
};
Any guesses about the mistake I'm doing?