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 ||texture.height <=0 ||texture.width >256 ||texture.height !=texture.width ||( 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 ||
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(&aux[imageSize-((i+1)*texture.width*4)],&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", &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();
}