Hi, first of all thank you all for responding… indeed, the Unix/system runs on Sun workstations (not the same system as the .PC I was loading the tga files…) My code is below, sorry about the length… If the issue is endianess, I have no idea how to deal with it Never came accross it, until now, so any help would be greatly appreciated.
The whole texture.h is below.
Again, the tga files were saved on PC on photoshop. tga loading works perfect on PC. When I take it on Sun workstation the textures do not load… (Sun workstation and Mesa Opengl for graphics). How can I fix this endian issue (if that is it…) never dealt with it before… Huge thanks for all your help.
#ifndef __TEXTURE_H
#define __TEXTURE_H
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <GL/glut.h>
/************************************************************************
texture.h
Implements loading of tga textures. The tga files can be 24 or 32 bit.
This implementation is based on the code from “Opengl game programming”
book by Kevin Hawkins.
Supports alpha channels in the texture.
Limitations: will not load compressed tga textures
************************************************************************/
/************************************************************************
Standard tga header.
Only part of the tga header is used here.
See specifications of tga file.
*************************************************************************/
typedef struct {
char imageTypeCode;
short int imageWidth;
short int imageHeight;
char bitCount;
} TGAHEADER;
/************************************************************************
Holds the texture object
*************************************************************************/
typedef struct {
long int scaledWidth;
long int scaledHeight;
char tgaImageCode; /* 0 = not TGA image, 2 = color, 3 = greyscale*/
int width; /* width (in pixels) of the texture */
int height; /* height (in pixels) of the texture */
int bitDepth; /* holds the big depth of image (16, 24 or 32 bits supported)*/
int texID; /* Opengl texture id for texture object support */
char *data; /* Actual pixel data */
} Texture;
/**************************** FUNCTIONS ********************************/
/ Clears the texture /
void UnloadTexture(Texture texture) {
glDeleteTextures(1, texture->texID);
if (texture->data != NULL)
free(texture->data);
texture->data = NULL;
}
/************************************************************************
Loads TGA file, since we are only using part of the header,
a lot of bytes will be skipped (defined as ‘garbage’ here.
*************************************************************************/
char* LoadTGAFile(char *filename, TGAHEADER *tgaHeader){
FILE *filePtr;
char ucharBad; /* garbage data*/
short int sintBad; /* garbage data*/
long imageSize; /* size of TGA image*/
int colorMode; /* 4 for RGBA, 3 for RGB*/
long imageIdx; /* counter variable*/
char colorSwap; /* swap variable*/
char *imageData; /* the TGA data*/
/* open the TGA file */
filePtr = fopen(filename, "rb");
if (!filePtr)
return NULL;
/* read first two bytes of garbage */
fread(&ucharBad, sizeof( char), 1, filePtr);
fread(&ucharBad, sizeof( char), 1, filePtr);
/*read in the image type*/
fread(&tgaHeader->imageTypeCode, sizeof( char), 1, filePtr);
/* for our purposes, the image type should be either a 2 or a 3*/
if ((tgaHeader->imageTypeCode != 2) && (tgaHeader->imageTypeCode != 3))
{
fclose(filePtr);
return NULL;
}
/* read 13 bytes of garbage data */
fread(&sintBad, sizeof(short int), 1, filePtr);
fread(&sintBad, sizeof(short int), 1, filePtr);
fread(&ucharBad, sizeof( char), 1, filePtr);
fread(&sintBad, sizeof(short int), 1, filePtr);
fread(&sintBad, sizeof(short int), 1, filePtr);
/* read image dimensions */
fread(&tgaHeader->imageWidth, sizeof(short int), 1, filePtr);
fread(&tgaHeader->imageHeight, sizeof(short int), 1, filePtr);
/* read bit depth */
fread(&tgaHeader->bitCount, sizeof( char), 1, filePtr);
/* read garbage */
fread(&ucharBad, sizeof( char), 1, filePtr);
/* colormode -> 3 = BGR, 4 = BGRA */
colorMode = tgaHeader->bitCount / 8;
imageSize = tgaHeader->imageWidth * tgaHeader->imageHeight * colorMode;
/* allocate memory for image data */
imageData = ( char*)malloc(sizeof( char)*imageSize);
/* read image data */
fread(imageData, sizeof( char), imageSize, filePtr);
/* change BGR to RGB so OpenGL can use the data */
for (imageIdx = 0; imageIdx < imageSize; imageIdx += colorMode)
{
colorSwap = imageData[imageIdx];
imageData[imageIdx] = imageData[imageIdx+2];
imageData[imageIdx + 2] = colorSwap;
}
/* close the file */
fclose(filePtr);
return imageData;
}
/************************************************************************
Loads a tga file into a texture structure
************************************************************************/
void LoadTGATexture(Texture texture, char *filename) {
TGAHEADER tga;
texture->data = (char*)malloc(sizeof(char));
texture->data = LoadTGAFile(filename, &tga);
if (texture->data == NULL)
{
free(texture->data);
}
/* store texture information */
texture->width = tga.imageWidth;
texture->height = tga.imageHeight;
texture->scaledHeight = 0;
texture->scaledWidth = 0;
texture->tgaImageCode = tga.imageTypeCode;
texture->bitDepth = tga.bitCount;
}
#endif