Results 1 to 4 of 4

Thread: png texture blend problem

  1. #1
    Newbie Newbie
    Join Date
    Nov 2013
    Posts
    2

    png texture blend problem

    Hi,

    As a beginer i try to load a png texture(with tranparancy)
    After having read some docs and forum i succeeded in loading png (without transparancy) as a texture into a 2D quad.
    Fact is i encounter some troubles with transparancy.
    I saw some important rules to apply : glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) for exemple and gl_enabled(GL_BLEND).
    I still have a badly formated picture.

    Maybe i do something wrong, maybe instructions are not in the good order.

    This is my code, maybe somone could see where the issue come from?

    Code :
     
    int main(int argc, char *argv[]){
      SDL_Event event;
      SDL_Init(SDL_INIT_VIDEO);
      SDL_SetVideoMode(1200, 700, 0, SDL_OPENGL | SDL_HWSURFACE | SDL_NOFRAME);
     
      glViewport(0, 0, 1200, 700);
      // glClearColor(0.0, 0.0, 0.0, 1.0);
    //glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
      glMatrixMode(GL_PROJECTION);
      glMatrixMode(GL_MODELVIEW);
      //glEnable(GL_DEPTH_TEST);
     
      int width(191);
      int height(240);
     
       texture = LoadTexture_png("pmario.png",width,height);
        SDL_EnableKeyRepeat(10,10);
        while(SDL_WaitEvent(&event))
        {
            switch(event.type)
            {
                case SDL_QUIT:
                     exit(0);
                     break;
                case SDL_KEYDOWN: 
                switch (event.key.keysym.sym)
                {
                case SDLK_ESCAPE:
                         exit(0);
                         break;     
                }
            }
     
     
     
     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
     glMatrixMode( GL_MODELVIEW );
     glLoadIdentity( );
     glEnable(GL_BLEND);
     glDisable(GL_DEPTH_TEST);
     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
     
     
     glEnable(GL_TEXTURE_2D); 
     
      glBegin(GL_QUADS);
          glBindTexture(GL_TEXTURE_2D, texture);
          glTexCoord2d(0,1);glVertex2f(-0.8, 0.8);
     
          glTexCoord2d(1,1); glVertex2f(-0.6, 0.8);
          glTexCoord2d(1,0);glVertex2f(-0.6, 0.4);
          glTexCoord2d(0,0); glVertex2f(-0.8, 0.4);
      glEnd();  
      glDisable(GL_TEXTURE_2D);
      glDisable(GL_BLEND);
     
      SDL_GL_SwapBuffers();
     
     
      }

    For instance i use a loading png function i found on the web :

    Code :
     
    GLuint LoadTexture_png(const char* filename, int &width, int &height) 
     {
       //header for testing if it is a png
       png_byte header[8];
     
       //open file as binary
       FILE *fp = fopen(filename, "rb");
       if (!fp) {
         return TEXTURE_LOAD_ERROR;
       }
     
       //read the header
       fread(header, 1, 8, fp);
     
       //test if png
       int is_png = !png_sig_cmp(header, 0, 8);
       if (!is_png) {
         fclose(fp);
         return TEXTURE_LOAD_ERROR;
       }
     
       //create png struct
       png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL,
           NULL, NULL);
       if (!png_ptr) {
         fclose(fp);
         return (TEXTURE_LOAD_ERROR);
       }
     
       //create png info struct
       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);
         fclose(fp);
         return (TEXTURE_LOAD_ERROR);
       }
     
       //create png info struct
       png_infop end_info = png_create_info_struct(png_ptr);
       if (!end_info) {
         png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp) NULL);
         fclose(fp);
         return (TEXTURE_LOAD_ERROR);
       }
     
       //png error stuff, not sure libpng man suggests this.
       if (setjmp(png_jmpbuf(png_ptr))) {
         png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
         fclose(fp);
         return (TEXTURE_LOAD_ERROR);
       }
     
       //init png reading
       png_init_io(png_ptr, fp);
     
       //let libpng know you already read the first 8 bytes
       png_set_sig_bytes(png_ptr, 8);
     
       // read all the info up to the image data
       png_read_info(png_ptr, info_ptr);
     
       //variables to pass to get info
       int bit_depth, color_type;
       png_uint_32 twidth, theight;
     
       // get info about png
       png_get_IHDR(png_ptr, info_ptr, &twidth, &theight, &bit_depth, &color_type,
           NULL, NULL, NULL);
     
       //update width and height based on png info
       width = twidth;
       height = theight;
     
       // Update the png info struct.
       png_read_update_info(png_ptr, info_ptr);
     
       // Row size in bytes.
       int rowbytes = png_get_rowbytes(png_ptr, info_ptr);
     
       // Allocate the image_data as a big block, to be given to opengl
       png_byte *image_data = new png_byte[rowbytes * height];
       if (!image_data) {
         //clean up memory and close stuff
         png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
         fclose(fp);
         return TEXTURE_LOAD_ERROR;
       }
     
       //row_pointers is for pointing to image_data for reading the png with libpng
       png_bytep *row_pointers = new png_bytep[height];
       if (!row_pointers) {
         //clean up memory and close stuff
         png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
         delete[] image_data;
         fclose(fp);
         return TEXTURE_LOAD_ERROR;
       }
       // set the individual row_pointers to point at the correct offsets of image_data
       for (int i = 0; i < height; ++i)
         row_pointers[height - 1 - i] = image_data + i * rowbytes;
     
       //read the png into image_data through row_pointers
       png_read_image(png_ptr, row_pointers);
     
       //Now generate the OpenGL texture object
       GLuint texture;
       glGenTextures(1, &texture);
       glBindTexture(GL_TEXTURE_2D, texture);
       glTexImage2D(GL_TEXTURE_2D,0, GL_RGBA, width, height, 0,
           GL_RGB, GL_UNSIGNED_BYTE, (GLvoid*) image_data);
     
       glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
     
       //clean up memory and close stuff
       png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
       delete[] image_data;
       delete[] row_pointers;
       fclose(fp);
     
       return texture;
     }

    The png i use :

    RGBA (i checked it, it is a png with alpha for transparancy)
    color depth 32
    size 191 X 240
    Thanks a lot for your help,

    Gilles

  2. #2
    Senior Member OpenGL Pro
    Join Date
    Jan 2012
    Location
    Australia
    Posts
    1,117
    I still have a badly formated picture.

    Can you be more specific

  3. #3
    Newbie Newbie
    Join Date
    Nov 2013
    Posts
    2
    Hi,

    Fact is i was using an old opengl Version. I assume with opengl3 or more, it will be ok.
    Image was unreadable(visually). I have difficulties to fond the english word it sounds like picture was badly rendered (like a tv with bad setting ).

    Thanks,

    Gilles

  4. #4
    Senior Member OpenGL Pro
    Join Date
    Jan 2012
    Location
    Australia
    Posts
    1,117
    Code :
    glTexImage2D(GL_TEXTURE_2D,0, GL_RGBA, width, height, 0,
           GL_RGB, GL_UNSIGNED_BYTE, (GLvoid*) image_data);

    You have to check it the image is RGB or RGBA. This state says that the image is RGB which is unlikely if you have an alpha value.

Similar Threads

  1. Problem in blend texture to draw translucent line
    By anoop.singh.knit in forum OpenGL ES
    Replies: 0
    Last Post: 12-26-2011, 12:09 AM
  2. Problem in blend texture to draw translucent line
    By Anoop in forum OpenGL: Advanced Coding
    Replies: 1
    Last Post: 12-25-2011, 11:34 PM
  3. Blend Problem
    By imported_epsxk82 in forum OpenGL: Advanced Coding
    Replies: 3
    Last Post: 10-22-2010, 12:26 AM
  4. how to blend from texture to texture (without using color buffers)
    By Thomas Krog in forum OpenGL: Advanced Coding
    Replies: 3
    Last Post: 09-03-2003, 05:57 AM
  5. texture map and blend
    By yqs2002 in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 11-21-2002, 01:04 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean