Results 1 to 6 of 6

Thread: Little code (comented) to load an image and show it. It doesnt show well the image.

  1. #1
    Member Contributor
    Join Date
    Apr 2007
    Posts
    80

    Little code (comented) to load an image and show it. It doesnt show well the image.

    Hi,

    the code shows an image with vertical stripes.

    Code :
    #include "stdafx.h"
    #include "GL/glut.h"
    #include "GL/glaux.h"
    #include "GL/glu.h"
    #include "stdio.h"
     
    typedef struct // We will use this struct
    {
    GLubyte *imageData;
    GLuint bpp;
    GLuint width;
    GLuint height;
    GLuint texID;
    } Image;
    /*
    ---------------------
    Load_TGA
    This function loads a TGA. TGA must be in this way:
    24 bits + ALPHA channel. (32 bits) and without compression (RLE)
    The image must be square (x=y) and 32x32 or 64x64 or 128x128 or 256x256
    It returns a pointer to the image and the size (tam variable) of the image.
    -----------------------
    */
    GLubyte *LoadTGA(char *filename,int *tam)
    {
    GLubyte TGAheader[12]={0,0,2,0,0,0,0,0,0,0,0,0};
     
    GLubyte TGAcompare[12];
     
    GLubyte header[6];
     
    GLuint bytesPerPixel;
     
    GLuint imageSize;
     
    GLuint temp,i;
     
    GLuint type=GL_RGBA;
     
    Image texture;
     
    GLubyte *aux;
     
    FILE *file = fopen(filename, "rb");
     
    if (file == NULL)
    printf("Error1");
     
     
    /* It opens the image and checks if its a TGA */
    fread(TGAcompare,1,sizeof(TGAcompare),file);
    if (memcmp(TGAheader,TGAcompare,sizeof(TGAheader))!=0)
    printf("Its not a TGA file");
     
     
    /* Reads the header*/
    fread(header,1,sizeof(header),file);
     
     
     
    /* The size of the image */
    texture.width = header[1] * 256 + header[0];
    texture.height = header[3] * 256 + header[2];
     
     
    /* See the features if they are correct*/
    if( texture.width <=0 &amp;#0124;&amp;#0124;texture.height <=0 &amp;#0124;&amp;#0124;texture.width >256 &amp;#0124;&amp;#0124;texture.height !=texture.width &amp;#0124;&amp;#0124;( header[4]!=32))
    {
    fclose(file);
    printf("Error - > Las caracteristicas de la imagen no son las correctas.");
    }
     
     
    /* Calculate the memory that will be necessary */
    texture.bpp = header[4];
    bytesPerPixel = texture.bpp/8;
    imageSize = texture.width*texture.height*bytesPerPixel;
     
    /* Reserve memory */
    texture.imageData=(GLubyte *)malloc(imageSize);
     
     
    /* Load and check some things */
    if( texture.imageData==NULL &amp;#0124;&amp;#0124;
    fread(texture.imageData, 1, imageSize, file)!=imageSize)
    {
    if(texture.imageData!=NULL)
    free(texture.imageData);
    fclose(file);
    printf("Error6");
    }
     
     
    /* TGA file comes in format BGR, we convert it to RGB */
    for(i=0; i<(GLuint)(imageSize); i+=bytesPerPixel)
    {
    temp=texture.imageData[i];
    texture.imageData[i] = texture.imageData[i + 2];
    texture.imageData[i + 2] = temp;
    }
    fclose (file);
     
     
    /* Now, change the order of the lines, as a vertical flip. */
    aux=(GLubyte *)malloc(imageSize);
    for(i=0; i<texture.height; i++)
    memcpy(&amp;aux[imageSize-((i+1)*texture.width*4)],&amp;texture.imageData[i*texture.width*4],texture.width*4);
     
    /* tam returns the size */
    *tam=texture.width;
     
     
     
    free(texture.imageData);
     
     
     
    return aux;
     
     
    }
     
     
     
    void RenderScene (void)
    {
     
    GLubyte *pImage = NULL;
    GLint iWidth;
    GLenum eFormat;
     
    // Clear the window with current clearing color
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
     
    // Targa's are 1 byte aligned
    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
     
    // Load the TGA file, get width, height, and component/format information
    pImage = LoadTGA("tga con alfa.tga", &amp;iWidth);
     
    // Use Window coordinates to set raster position
    glRasterPos2i(0, 0);
     
    // Write a block of pixels to the frame buffer
    if(pImage != NULL)
    glDrawPixels(iWidth, iWidth, GL_RGB, GL_UNSIGNED_BYTE, pImage);
     
    // Don't need the image data anymore
    free(pImage);
     
    // Do the buffer Swap
    glutSwapBuffers();
     
    }
     
     
    void SetupRC(void)
    {
     
    //Blue color to delete the window
    glClearColor(0.0f,0.0f,1.0f,1.0f);
    }
     
     
     
    void main (void)
    {
     
     
     
    glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);
     
     
    glutCreateWindow("Simple");
     
     
    glutDisplayFunc(RenderScene);
     
     
    SetupRC();
     
     
    glutMainLoop();
     
     
     
    }

  2. #2
    Senior Member OpenGL Guru Relic's Avatar
    Join Date
    Apr 2000
    Posts
    2,450

    Re: Little code (comented) to load an image and show it. It doesnt show well the image.

    I would have expected sheared stripes.

    You didn't pay attention to the image data format:
    "This function loads a TGA. TGA must be in this way:24 bits + ALPHA channel. (32 bits) and without compression (RLE)"
    and
    glDrawPixels(iWidth, iWidth, GL_RGB, GL_UNSIGNED_BYTE, pImage);

    GL_RGB is not 32 bits with RGB and Alpha.
    Use GL_RGBA in the glDrawPixels call or change the loader to ignore alpha.
    Or use a better TGA loader and decide what the format is dynamically.
    For example I wonder why the routine flips the image. TGA images and OpenGL both have the origin at the bottom left normally. glDrawPixels or glTexImage2D can just use the input data.

    It's also not a good practice to load and free the image on each repaint. Your redraw performance will suck if you drag another window over your OpenGL window.

  3. #3
    Senior Member Frequent Contributor
    Join Date
    Feb 2006
    Location
    Sweden
    Posts
    744

    Re: Little code (comented) to load an image and show it. It doesnt show well the image.

    Interesting to see that about 90% of the code is copied from nehe.gamedev.net and you didn't even bother to fix the buggs in there, in fact, you added more, i won't even bother to comment them, Relic got some of them, but i can say that this code will never load a TGA created in a recent version of photoshop or paint shop pro correctly.

    Loading type 2 TGAs is easy, it takes about a page worth of code (including whitespace and comments), mode 10 is another page of code.

  4. #4
    Senior Member OpenGL Pro k_szczech's Avatar
    Join Date
    Feb 2006
    Location
    Poland
    Posts
    1,107

    Re: Little code (comented) to load an image and show it. It doesnt show well the image.


  5. #5
    Member Contributor
    Join Date
    Apr 2007
    Posts
    80

    Re: Little code (comented) to load an image and show it. It doesnt show well the image.

    glDrawPixels(iWidth, iWidth, GL_RGB, GL_UNSIGNED_BYTE, pImage);

    GL_RGB is not 32 bits with RGB and Alpha.
    Use GL_RGBA in the glDrawPixels call or change the loader to ignore alpha.
    It works thanks!

  6. #6
    Member Contributor
    Join Date
    Apr 2007
    Posts
    80

    Re: Little code (comented) to load an image and show it. It doesnt show well the image.

    Relic said:

    For example I wonder why the routine flips the image. TGA images and OpenGL both have the origin at the bottom left normally. glDrawPixels or glTexImage2D can just use the input data.
    I wonder also, but if you delete that part of the code gives an error in execution and doesnt show the image.

    And if you set it the image is showed flipped.

Similar Threads

  1. glBitmap doesnt show anything on windows 7
    By rf_daniel in forum OpenGL: Windows
    Replies: 5
    Last Post: 12-13-2010, 06:13 AM
  2. Want to show image in 3D
    By nayan in forum OpenGL: Basic Coding
    Replies: 3
    Last Post: 01-20-2009, 12:43 AM
  3. multitexture, second texture doesnt show
    By bumby in forum OpenGL: Basic Coding
    Replies: 6
    Last Post: 01-12-2005, 01:08 PM
  4. how to show the grey-scale image?
    By in forum OpenGL: Basic Coding
    Replies: 7
    Last Post: 09-30-2003, 01:37 PM
  5. why only one side of image show on the screen?
    By havana in forum OpenGL: Advanced Coding
    Replies: 5
    Last Post: 01-08-2003, 08:16 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