Advertisement

Using PNGs as textures

Started by November 16, 2004 02:19 AM
4 comments, last by GameDev.net 20 years ago
Does anyone here use PNGs as textures?
ah found it!

[SOURCE]// This is a source i donwloaded from a forum for loading PNG FilesFILE *File = fopen("Filename.png","r");if (File== NULL)  return 0;png_structp png_ptr = png_create_read_struct (PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);if (!png_ptr){return 0;};png_infop info_ptr = png_create_info_struct(png_ptr);if (!info_ptr) {png_destroy_read_struct(&png_ptr, (png_infopp)NULL, (png_infopp)NULL);        return 0;}png_infop end_info = png_create_info_struct(png_ptr);if (!end_info) {png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL);return 0;}if (setjmp(png_ptr->jmpbuf)) {        png_destroy_read_struct(&png_ptr, &info_ptr,&end_info);        return 0;}// Get Information about PNGpng_init_io(png_ptr, file);png_read_info(png_ptr, info_ptr);int width = png_get_image_width(png_ptr, info_ptr),height = png_get_image_height(png_ptr, info_ptr),color_type = png_get_color_type(png_ptr, info_ptr);if (pWidth) *pWidth = width;if (pHeight) *pHeight = height;if (pColorType) *pColorType = color_type;// Convert, if wrong formatif (color_type == PNG_COLOR_TYPE_PALETTE && png_get_bit_depth(png_ptr, info_ptr) <= 8) png_set_expand(png_ptr);if (color_type == PNG_COLOR_TYPE_GRAY && png_get_bit_depth(png_ptr, info_ptr) < 8) png_set_expand(png_ptr);if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) png_set_expand(png_ptr);if (png_get_bit_depth(png_ptr, info_ptr) == 16) png_set_strip_16(png_ptr);if (color_type == PNG_COLOR_TYPE_GRAY || color_type == PNG_COLOR_TYPE_GRAY_ALPHA) png_set_gray_to_rgb(png_ptr);png_read_update_info(png_ptr, info_ptr);// Request neccessary memoryunsigned char *Data = new unsigned char[ height * png_get_rowbytes(png_ptr, info_ptr)];unsigned char *DataPtr = Data;unsigned char **lines;lines = new unsigned char*[height];if (lines == NULL) return 0;for (int i = 0; i < height; i++){lines = DataPtr;DataPtr += png_get_rowbytes(png_ptr, info_ptr);}// read the imagepng_read_image(png_ptr, lines);// free resourcespng_read_end(png_ptr, end_info);png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);delete[] lines;fclose(File);return Data;[/SOURCE]


by the way create a png totaly transparent, use it as the 3dsmax max's background enviroment. Now every image u render to a png has transparency in.

Very usefull for explosions and then u dont need a mask.

I never had time to do the blend functions in ogl though
----------------------------

http://djoubert.co.uk
Advertisement
Well that reads like it needs some supporting files.

I am interested in how people use the alpha channel in pngs.
No ur right, i never did get that code to work

http://www.devlib.org/glpng_L20680/
http://www.fifi.org/doc/libglpng-dev/glpng.html

follow the link to get what i use
----------------------------

http://djoubert.co.uk
Cool that does look like what I'm after I just hope it doesn't require too many dlls.

I will give it a go later

thanks for your help.



You can also load a PCX image tha uses 256colors. It has transparancy built in also...Good for explosions.

This topic is closed to new replies.

Advertisement