Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Whitehair

Anyone knows how to change the loading of .tga format to .raw file format?

This topic is 5732 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Anyone knows how to change the loading of .tga format to .raw file format? this is in main.cpp -------------------------------------------------------------------------------------------------------- void glInit (void) { char buff[15]; tga_t image; glClearColor(0.0f, 0.0f, 0.0f, 0.0f); glEnable(GL_DEPTH_TEST); glEnable(GL_BLEND); glEnable(GL_TEXTURE_2D); // blend func for transparency glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); // load the face textures for (int i = 0; i < 6; i++) { texIds = image.GenId(); // let OpenGL give us a texture id sprintf(buff, "face%d.tga", i + 1); if (image.Load(buff, texIds[i]) != 1) // load image printf("Could not load %s!!\n", buff); } printf("T - Turns off transparency\n"); printf("Mouse Buttons - Rotate\n"); printf("Q - Quits\n"); } --------------------------------------------------------------------------------------------------------- This is in tga.cpp #include "tga.h" #include <math.h> tga_t::tga_t() { lastError = 0; bits = width = height = id = size = 0; data = 0; } tga_t::tga_t(char *name) { lastError = 0; bits = width = height = id = size = 0; data = 0; Load(name); } tga_t::tga_t(char *name, int texId, int upload, GLenum nf) { lastError = 0; bits = width = height = id = size = 0; data = 0; Load(name, texId, upload, nf); } int tga_t::Load(char *name) { byte type[4]; byte info[7]; FILE *iFile; if (!(iFile = fopen(name, "r+bt"))) return Error(notFound, 0); fread(&type, sizeof(byte), 3, iFile); // read in colormap info and image type, byte 0 ignored fseek(iFile, 12, SEEK_SET); // seek past the header and useless info fread(&info, sizeof(byte), 6, iFile); fseek(iFile, type[0], SEEK_CUR); // skip past image identification if (type[1] != 0 || (type[2] != 2 && type[2] != 3)) return Error (badType, iFile); width = info[0] + info[1] * 256; height = info[2] + info[3] * 256; bits = info[4]; size = width * height; // make sure dimension is a power of 2 //if (!CheckSize (width) || !CheckSize (height)) //return Error(badDimension, iFile); // make sure we are loading a supported type if (bits != 32 && bits != 24 && bits != return Error(badBits, iFile); data = GrabData(iFile, size); // no image data if (data == 0) return Error(badData, iFile); fclose(iFile); return 1; } int tga_t::Load(char *name, int texId, int upload, GLenum nf) { int err; if ((err = Load(name)) == 1) { Id(texId); if (nf) format = nf; if (upload == uploadYes) { if ((err == Upload()) != 1) return err; delete [] data; data = 0; } } return err; } int tga_t::Upload(void) { if (!data) return badData; if (!CheckSize (width) || !CheckSize (height)) return badDimension; glBindTexture(GL_TEXTURE_2D, id); glPixelStorei(GL_UNPACK_ALIGNMENT, 1); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, format, GL_UNSIGNED_BYTE, data); return 1; } void tga_t::Reset(void) { if (data != 0) delete [] data; lastError = 0; bits = width = height = id = size = 0; data = 0; } int tga_t::Error(int errNum, FILE *strm) { lastError = errNum; if (strm) fclose(strm); return 0; } int tga_t::CheckSize(int x) { unsigned int i = 0; for ( ; pow(2, i) <= maxTexSize; i++) if (x == pow(2, i)) return 1; return 0; } byte *tga_t::GetRGBA(FILE *strm, int size) { byte *rgba; byte temp; int bread; int i; rgba = new byte[size * 4]; if (rgba == 0) return 0; bread = fread(rgba, sizeof(byte), size * 4, strm); if (bread != size * 4) { delete [] rgba; return 0; } for (i = 0; i < size * 4; i += 4 ) { temp = rgba[i]; rgba[i] = rgba[i + 2]; rgba[i + 2] = temp; } format = GL_RGBA; return rgba; } byte *tga_t::GetRGB(FILE *strm, int size) { byte *rgb; byte temp; int bread; int i; rgb = new byte[size * 3]; if (rgb == 0) return 0; bread = fread (rgb, sizeof(byte), size * 3, strm); if (bread != size * 3) { delete [] rgb; return 0; } for (i = 0; i < size * 3; i += 3) { temp = rgb[i]; rgb[i] = rgb[i + 2]; rgb[i + 2] = temp; } format = GL_RGB; return rgb; } byte *tga_t::GetGray(FILE *strm, int size) { byte *grayData; int bread; grayData = new byte[size]; if (grayData == 0) return 0; bread = fread(grayData, sizeof(byte), size, strm); if (bread != size) { delete [] grayData; return 0; } format = GL_ALPHA; return grayData; } byte *tga_t::GrabData(FILE *strm, int size) { if (bits == 32) return GetRGBA (strm, size); else if (bits == 24) return GetRGB (strm, size); else if (bits == return GetGray (strm, size); return 0; } ------------------------------------------------------------------------------------------------------- This is in tga.h #ifndef TGA_H #define TGA_H #include "utility.h" #define WIN32_LEAN_AND_MEAN #include <windows.h> #include <GL/gl.h> struct tga_t { tga_t(); tga_t(char *name); tga_t(char *name, int texId, int upload = uploadYes, GLenum nf = 0); int Load(char *name); int Load(char *name, int texId, int upload = uploadYes, GLenum nf = 0); void Free(void) {delete [] data;} void Id(int texId) {id = texId;} int Upload(void); void Reset(void); int LastError(void) {return lastError;} int Width(void) {return width;} int Height(void) {return height;} int Bits(void) {return bits;} int Id(void) {return id;} void Format(GLenum nf) {format = nf;} GLenum Format(void) {return format;} byte *Data(void) {return data;} byte operator[](int ndx) {return data[ndx];} int GenId(void) {unsigned int temp; glGenTextures(1, &temp); return temp;} enum { notFound = 1000, badType, badDimension, badBits, badData, badUpload, uploadNo = 1, uploadYes = 0, maxTexSize = 512 }; protected: int Error(int errNum, FILE *strm); int CheckSize(int x); byte *GetRGB(FILE *strm, int size); byte *GetRGBA(FILE *strm, int size); byte *GetGray(FILE *strm, int size); byte *GrabData(FILE *strm, int size); int lastError; int bits; int width; int height; int id; int size; GLenum format; byte *data; }; #endif

Share this post


Link to post
Share on other sites
Advertisement
I would suggest looking at NeHe''s tutorials, there is one on loading .raw and one on loading .tga. Looking at the difference between the two methods you should be able to apply it to your code.

Share this post


Link to post
Share on other sites
Here''s a dump of some old code to get you started.
Notes:
- res_load just loads a file into memory, and indicates its size
- limited to square, power of 2 textures at 16,24, or 32 bpp (otherwise => problems guessing the texture''s dimensions)

  
typedef struct
{
int width, height;
GLenum fmt;
uchar* ptr;
}
TEX_INFO;
static TEX_INFO* ti_array;
static uint ti_size;


uint load_texture(const char* fn)
{
int size;

/* load image */
uchar* ptr = (uchar*)res_load(fn, &size);
if(!ptr)
return 0;

/* alloc */
uint id;
glGenTextures(1, &id);
if(id >= ti_size)
{
ti_size = id*2;
ti_array = (TEX_INFO*)realloc(ti_array, ti_size*sizeof(TEX_INFO));
memset(&ti_array[id], 0, (ti_size-id)*sizeof(TEX_INFO));
}

/*
* guess texture dimensions / format
* width = height = 2^x
*/
static GLenum fmt[5] = { 0, 0, GL_LUMINANCE_ALPHA, GL_RGB, GL_RGBA };
for(int i = 2; i <= 4; i++)
{
int dim = (int)sqrt(size/i);
if(dim % i || (dim & (dim-1)) || dim*dim*i != size)
continue;

TEX_INFO* ti = &ti_array[id];
ti->fmt = fmt[i];
ti->width = ti->height = dim;
ti->ptr = ptr;

return id; /* success */
}

return 0; /* invalid texture dimensions */

}

Share this post


Link to post
Share on other sites
I'll take a look and try to change it.
thanks

By the way, I used glblendfunc to make my images transparent. but it seems if the image on top has a background which is black, the image below it (place at a 0.02f distance from it) cannot be seen...

any idea why this is happening?

[edited by - Whitehair on December 5, 2002 8:11:41 PM]

Share this post


Link to post
Share on other sites
In search of a C/C++/opengl program convert .raw to .tga format.

The dataset that i have is in .raw format and instead of using photoshop to do the conversion one by one. I figured, there must be some programs with source code out there laying around somewhere that is shared to make our life more efficient.

Anyone can help on this?

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!