Skip to main content
gamedev.net
🔒 Locked

tagra loader

Started by hanker-dev Dec 24, 2001 at 6:09 AM 7 replies 350+ views
Original Post
hanker-dev
hanker-dev
hiho i wrote a function to load .tga images but instead of using them as texture the textures are strange mixtures of strange colors. I think that the error is with the pointer *buffer but i''m not sure. please tell me what''s wrong and woh to correct it. The code: void LoadGLTextures() { FILE *file; unsigned char ImgInfo[6],Pixel[3]; unsigned char *buffer; int ImgWidth,ImgHeight; glGenTextures(maxtexture, &texture[0]); for(int i=0;i,"r+bt"); fseek(file,12,SEEK_SET); fread(&ImgInfo,sizeof(unsigned char),6,file); ImgWidth=ImgInfo[0]+ImgInfo[1]*256; ImgHeight=ImgInfo[2]+ImgInfo[3]*256; for(int j=0;j
=)
Brannon
Brannon
I post this to every image loading question/post.

Use FreeImage. Why rewrite this yourself?

http://www.6ixsoft.com/


-Brannon
-Brannon
terminate
terminate
quote:
Original post by Brannon
I post this to every image loading question/post.

Use FreeImage. Why rewrite this yourself?

http://www.6ixsoft.com/


-Brannon



Many reasons
1) Because you can
2) So you know how
3) Writing your own code is better than relying on someone elses
4) Its more fun
5) You have more control
.
.
. ' Target=_Blank>Link
Those who dance are considered insane by those who cannot hear the music.
Dwarf with Axe
Dwarf with Axe
I am all for writing your own code.

I am not for using someone else''s.

I am for looking at someone else''s for help.


Sir, by telling someone that using things like FreeEdit are NOT cheating, you are encouraging degrowth. We are here to inspire and encourage programmers to do their best and meet challenges head-on...

I especially like the anonymous''s post... If your saying that We are all technically ''cheating'' by using compilers and such, then I guess we should just go ahead and write our own language to speak, because , as you know, English was by some cavemen, and not by us...

zedzeek
zedzeek
>>gluBuild2DMipmaps(GL_TEXTURE_2D,GL_RGB,ImgWidth,ImgHeight,GL_RGB, GL_UNSIGNED_BYTE,buffer);<<

in answere to the original question windows expects colours in BGR format thus use
gluBuild2DMipmaps(GL_TEXTURE_2D,GL_RGB,ImgWidth,ImgHeight,GL_BGR_EXT, GL_UNSIGNED_BYTE,buffer);

also FWIW heres my tga loader ( no compressed tga''s allowwed ) can be improved a bit also (looking at it now
if (header->bitDepth==8)
bd=1;
if (header->bitDepth==16)
bd=2;
hehe )

struct TGAImageHeader
{
// imagetype 2==truecoloour uncompressed, 3==b+w uncompressed (theres no implementational difference between the two)
TGAImageHeader() { memset( this, 0 , sizeof(TGAImageHeader)); }

GLubyte id; // the number of bytes in image ID (comes after imageDescription) + before the actual image data
GLubyte colormap;
GLubyte imageType;
GLubyte colormapSpec[5];

GLubyte xOrigin[2];
GLubyte yOrigin[2];
GLubyte width[2];
GLubyte height[2];
GLubyte bitDepth;
GLubyte imageDescription;

};

GLubyte* loadTGA( char *filename, int &w, int &h, int &bd )
{
FILE *f;
int i,j,k;
GLubyte *image;

// if the filename is not an invalid one ie not empty it sees if it can open that file,
// if it can''t it creates a white texture
// TODO is a 1x1 RGB texture good that is would I ever need a RGBA one or a LUMINANCE one
if ( !strcmp( filename, "" ) || (f = fopen(filename, "rb")) == NULL )
{
printf("ERROR %s texture not found\n", filename );

// make a plain white 1x1 RGB texture

w = h = 1;
bd = 3;
image = new GLubyte[w*h*bd];

for ( i=0; i for ( j=0; j < w; j++)
for ( k=0; k *(image+(i*w*bd)+(j*bd)+k) = 0xff;

return image;
}

TGAImageHeader *header;
header = new TGAImageHeader;
fread(header,1,sizeof(TGAImageHeader),f);

int width = header->width[0] + header->width[1] * 256;
int height = header->height[0] + header->height[1] * 256;

if (header->bitDepth==8)
bd=1;
if (header->bitDepth==16)
bd=2;
if (header->bitDepth==24)
bd=3;
if (header->bitDepth==32)
bd=4;

image = NULL;
image = new GLubyte[width*height*bd];

// skip past the id field (if there is one)
fread( image, sizeof( GLubyte ), header->id, f );
fread( image, sizeof( GLubyte ), width*height*bd, f );

w=width;
h=height;

delete header;
fclose(f);
return image;
}

http://uk.geocities.com/sloppyturds/gotterdammerung.html
WhatEver
WhatEver
Reusing code is good. Knowing how the code works is better. It''s not a necessity to know how everything works. We program with a lot of functions we didn''t make. If we did everything by ourselves we would never work in a team, we would never use OpenGL and we would never use DirectX. There is always a time to use someone else''s code, it''s up to you to decide when.

I was once a person who wanted to do it all. I wouldn''t even touch OpenGL or DirectX. Soon I realized that sometimes you should advance by using others code. Learn to decide when you should use anothers code base.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.