Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 5 of 5

Thread: Loading a texture

  1. #1
    Junior Member Newbie
    Join Date
    Jan 2011
    Posts
    23

    Loading a texture

    I am tearing my hair out over something that originally appeared a simple task, displaying an image in OpenGL.

    It's not drawing the image on-screen that I find difficult, it's loading the image in the first place.

    My texture is stored in PNG format and I read it using libpng, loosely following the example here.

    Before I post my code, I would greatly appreciate it if somebody could send me a working example of displaying a PNG image (cross platform too, please ).

    Segmentation error occurs near the bottom where the comment is:

    Code :
    GLuint LoadTexture(char * path)
    {
        FILE * file = fopen(path, "rb");
        if (!file)
        {
            ReportAssetError((char*) "missing texture", path, 0);
        }
        else
        {
            unsigned char * header;
            header = (unsigned char *) malloc(8);
            fread(header, 1, 8, file);
            bool is_png = !png_sig_cmp(header, 0, 8);
            delete header;
            if (is_png)
            {
                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, file);
                png_set_sig_bytes(png_ptr, 8);
                png_read_info(png_ptr, info_ptr);
                int bit_depth, color_type;
                png_uint_32 width, height;
                png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, &color_type, NULL, NULL, NULL);
                png_read_update_info(png_ptr, info_ptr);
                int row_bytes = png_get_rowbytes(png_ptr, info_ptr);
                png_bytep * row_pointers = new png_bytep[height];
                png_byte * image_data = new png_byte[row_bytes * height];
                if(!image_data)
                {
                    // error
                    png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
                    fclose(file);
                    ReportAssetError((char*)"error reading file", path, 0);
                }
                for(int i = 0; i < height; i++)
                {
                    row_pointers[height - 1 - i] = image_data + i * row_bytes;
                }
                png_read_image(png_ptr, row_pointers);
                GLuint texture;
                glGenTextures(1, &amp;texture);
                glBindTexture(GL_TEXTURE_2D, texture);
                glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, (GLvoid*)image_data); // Segmentation error
                glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
                png_destroy_read_struct(&amp;png_ptr, &amp;info_ptr, &amp;end_info);
                delete[] image_data;
                delete[] row_pointers;
                //fclose(file);
                return texture;
            }
            else
            {
                ReportAssetError((char*)"unsupported format", path, 0);
            }
        }
    }

  2. #2
    Junior Member Regular Contributor
    Join Date
    Mar 2009
    Posts
    153

    Re: Loading a texture

    I recommend using stb_image lib for png loading it is much simpler. Follow link in my signature to see example code.

  3. #3
    Junior Member Newbie
    Join Date
    Jan 2011
    Posts
    23

    Re: Loading a texture

    Alrighty, thank you.

  4. #4
    Senior Member OpenGL Pro
    Join Date
    Jan 2007
    Posts
    1,761

    Re: Loading a texture

    What value are you getting for bit_depth? I'm not too familiar with libpng, but it sounds an awful lot like you're loading a 24-bit image and overflowing a buffer. (Confirm by changing the format parameter of your glTexImage2D to GL_RGB.)

  5. #5
    Junior Member Newbie
    Join Date
    Jan 2011
    Posts
    23

    Re: Loading a texture

    Oh, bit late now, sorry.

    Got started on deciphering stb_image as I posted last, managed to load the image but when I try and display it I just get a white square.

    I'll take your advice into account for when I decide to re-write what I've already done which will probably be pretty soon.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •