PDA

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



tirengarfio
08-01-2006, 07:05 AM
Hi,

the code shows an image with vertical stripes.



#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();



}

Relic
08-01-2006, 07:21 AM
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.

zeoverlord
08-01-2006, 12:26 PM
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.

k_szczech
08-01-2006, 04:10 PM
Just use this:
http://freeimage.sourceforge.net/

tirengarfio
08-02-2006, 12:43 AM
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!

tirengarfio
08-02-2006, 12:59 AM
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.