Jump to content
  • Advertisement
Sign in to follow this  
yellowstar

Texture problems(SOLVED, but exiting error)

This topic is 4076 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

I am having problems with my ported TGA loader. At first I created it for NDS Homebrew. But, now I have ported it to PC, for a game I am making. Here's the problem: When I try to draw the texture,(on a cube and ect) it is completly white.(like it failed to load it) But, on the NDS Hombrew version, it works.
bool createTexture(TGAimage *tgaimage)
{
	unsigned int bytesPerPixel = tgaimage->bpp / 8;
    
    glGenTextures(1,tgaimage->texID);
	glBindTexture(GL_TEXTURE_2D, *tgaimage->texID);
    
    if(bytesPerPixel==1 || bytesPerPixel== 3)
	glTexImage2D(GL_TEXTURE_2D,0,3,tgaimage->width, tgaimage->height,0, GL_RGB, GL_UNSIGNED_BYTE,(BYTE*)tgaimage->image);
	
	if(bytesPerPixel==2 || bytesPerPixel== 4)
	glTexImage2D(GL_TEXTURE_2D,0,4,tgaimage->width, tgaimage->height,0, GL_RGBA, GL_UNSIGNED_BYTE,(BYTE*)tgaimage->imageA);
    
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    
	return true;
}




//There is another function, which reads a tga file, and passes the data(and ect), to this function.
bool loadTGA(BYTE *DATA, int size, TGAimage *tgaimage, unsigned int *texID)
{
	int i;
	unsigned char temp;
	u8 *stream = NULL;
	u8 Desc = 0;
	bool orgin = false;//If false, orgin is
	//in lower-left corner, otherwise, it is in the top-left corner.
	bool RLE = false;//Wether or
	//not the image is RLE compressed.
	bool Gray = false;//Wether or not the image is grayscale.
	bool has_palette = false;//Wether or not the image has an color map.(palette)

	if(DATA==NULL){return 0;}

	stream = DATA;

    tgaimage->texID = texID;



    tgaimage->header.ID_Length = *stream;stream++;
    tgaimage->header.colorMapType = *stream;stream++;
    tgaimage->header.imageType = *stream;stream++;
    
    for(int a = 0; a < 5; a++)
    {
    tgaimage->header.ColorMapSpecification[a] = *stream;stream++;
    }
    
    SCpy(stream,&tgaimage->header.xOrigin);stream+=2;
    SCpy(stream,&tgaimage->header.yOrigin);stream+=2;
    SCpy(stream,&tgaimage->header.imageWidth);stream+=2;
    SCpy(stream,&tgaimage->header.imageHeight);stream+=2;
    
    tgaimage->header.pixelDepth = *stream;stream++;

	if(tgaimage->header.imageType !=2 && 
	tgaimage->header.imageType != 10 &&
	tgaimage->header.imageType != 3 &&
	tgaimage->header.imageType != 11 ){
		ERROR
		
	}
	if(tgaimage->header.imageType == 10 || tgaimage->header.imageType == 11){
	RLE = true;//printf("RLE\n");
    }
	
	if(tgaimage->header.imageType != 10 && tgaimage->header.imageType != 11)
	RLE = false;
	
	if(tgaimage->header.imageType == 3 || tgaimage->header.imageType == 11){
	Gray = true;//printf("Grayscale\n");
    }
	
	if(tgaimage->header.imageType != 3 && tgaimage->header.imageType != 11)
	Gray = false;

	tgaimage->width = (unsigned int)tgaimage->header.imageWidth;
	tgaimage->height = (unsigned int)tgaimage->header.imageHeight;
	tgaimage->bpp = tgaimage->header.pixelDepth;

	if (tgaimage->width <= 0 || tgaimage->height <= 0 || (tgaimage->bpp!=24 && tgaimage->bpp!=32 && tgaimage->bpp!=8 && tgaimage->bpp!=16)){
        ERROR
        }

	unsigned int bytesPerPixel = tgaimage->bpp / 8;
	unsigned int imageSize = ((tgaimage->width * tgaimage->height) * bytesPerPixel);
	
	tgaimage->type = GL_RGBA;

	if(tgaimage->bpp==24)
	{
		tgaimage->type = GL_RGB;printf("RGB\n");
    }
    if(tgaimage->bpp==32)
    {
        //printf("RGBA\n");
    }

	if(tgaimage->bpp==8)
	{
		tgaimage->type = GL_RGB;printf("Grayscale\n");
    }
    if(tgaimage->bpp==16)
    {
        //printf("Grayscale with Alpha\n");
    }

    if(tgaimage->header.colorMapType==1)
    {
	has_palette = true;
    }
	else
	{
	has_palette = false;
	}
	
	if(has_palette){ERROR}
    
    tgaimage->imageData = (BYTE*)malloc((size_t)((int)imageSize));
    
    if(tgaimage->imageData==NULL)
    {
    
    ERROR
    }


	Desc = *stream;
	tgaimage->header.ImageDesc = Desc;
	stream++;
	
	if(Desc & BIT(5))//Upper-left hand corner
	{
		orgin = true;
	}
	else
	{
	//lower-left hand corner
	orgin = false;
	}

	if(!RLE)
	{

		for(int I = 0; I < (int)imageSize; I++)
		{
		tgaimage->imageData = *stream;
		stream++;
		}

	}
	
	if(RLE)
	{
	
	int Size;
	Size = size - 18;
	
	int pos = 0;
	u8 temp = 0;
	int impos = 0;
	int repeats = 0;
	u8 r, g, b, a, gvalue;
	r=0;
	g=0;
	b=0;
	a=0;
	gvalue=0;//Grayscale value.
	
	bool raw = false;
	
	
	while(!((*(stream)) & BIT(7))){pos++;stream++;Size++;}
	//Find the first packet of the actual data.
	
	
	
		while(pos < Size)
		{
		
		temp = *(stream);
		repeats = 0;
		 //if(!(temp & BIT(0)))
		
			for(int i=0; i< 7; i++)
			{
			if(temp & BIT(i))
			repeats |= BIT(i);
			
			
			}
			
			//repeats = (int)temp;
			
			//repeats = repeats & ~BIT(7);
			
			repeats++;//The value in the TGA is 1
			//less than the actal pixels.
			//So 0 would be 1 pixel, 1 would mean 2 pixels,
			//ect.
			#ifdef DEBUG2
			//fprintf(log,"Runlength: %d\n",repeats);
			#endif
			
			
			
			raw = (!(*stream & BIT(7)))?true:false;
			
			pos++;
			stream++;
			
			if(!raw)
			{
			
			if(bytesPerPixel>2)
			{

			b = *(stream);//B//
			//In an TGA, the colors values are flipped.
			//That is, it is BGR, instead of RGB
			pos++;
			stream++;
			
			
			g = *(stream);//G
			pos++;
			stream++;
			
			r = *(stream);//R
			pos++;
			stream++;
			
				if(bytesPerPixel==4)
				{
				a = *(stream);//A
				pos++;
				stream++;
				}
			
			}
			else
			{
				
				gvalue = *(stream);
				pos++;
				stream++;
					
					if(bytesPerPixel==2)
					{
					a = *(stream);//A
					pos++;
					stream++;
					}

			}

				if(bytesPerPixel>2)
				{
					for(int i=0; i<repeats; i++)
					{
					tgaimage->imageData[impos + 0] = b;
					tgaimage->imageData[impos + 1] = g;
					tgaimage->imageData[impos + 2] = r;
					
					if(bytesPerPixel==4)
					tgaimage->imageData[impos + 3] = a;
					
					impos+=(int)bytesPerPixel;
					
					}
				}
				else
				{
					for(int i=0; i<repeats; i++)
					{
					tgaimage->imageData[impos + 0] = gvalue;
					
					if(bytesPerPixel==2)
					tgaimage->imageData[impos + 1] = a;
					
					impos+=(int)bytesPerPixel;
					
					}
				}


			
			}
			else
			{
				for(int i=0; i<repeats; i++)
				{

				if(bytesPerPixel>2)
				{
					b = *(stream);//B//
					//In an TGA, the colors values are flipped.
					//That is, it is BGR, instead of RGB
					pos++;
					stream++;
					
					
					g = *(stream);//G
					pos++;
					stream++;
					
					r = *(stream);//R
					pos++;
					stream++;
					
						if(bytesPerPixel==4)
						{
						a = *(stream);//A
						pos++;
						stream++;
						}
						
						tgaimage->imageData[impos + 0] = b;
						tgaimage->imageData[impos + 1] = g;
						tgaimage->imageData[impos + 2] = r;
						
						if(bytesPerPixel==4)
						tgaimage->imageData[impos + 3] = a;
						
						impos+=(int)bytesPerPixel;
					
					}
					else
					{

					gvalue = *(stream);
					pos++;
					stream++;
					
						if(bytesPerPixel==2)
						{
						a = *(stream);//A
						pos++;
						stream++;
						}
						
						tgaimage->imageData[impos + 0] = gvalue;
						
						if(bytesPerPixel==2)
						tgaimage->imageData[impos + 1] = a;
						
						impos+=(int)bytesPerPixel;

					}

				}
			
			}
			
			if((int)impos > (int)imageSize)
			break;//TGAs have
			//some extra data at the end of it,
			//after the image data.
			//This code is there so it dosen't
			//think that data is image data.
		
		}
	
	}


	//printf("Image data\n\n");
	
	//Convert BGR to RGB
	if(!Gray)
	{
		for (i =0; i <(int)imageSize; i+=(int)bytesPerPixel)
		{
		
			temp = tgaimage->imageData;
			tgaimage->imageData = tgaimage->imageData[i + 2];
			tgaimage->imageData[i + 2] = temp;

			temp = tgaimage->imageData[i + 1];
			tgaimage->imageData[i + 1] = tgaimage->imageData[i + 2];
			tgaimage->imageData[i + 2] = temp;
		}
	}

    

    if(bytesPerPixel != 2 && bytesPerPixel != 4)
    {
	tgaimage->image = (color*)malloc(sizeof(color)*(tgaimage->width*tgaimage->height));
    }
    else
    {
    tgaimage->imageA = (colorA*)malloc(sizeof(colorA)*(tgaimage->width*tgaimage->height));
    }
int I = 0;
color Temp;
colorA Temp2;

	for(int i=0; i < (int)(tgaimage->width*tgaimage->height); i++)
	{

            if(bytesPerPixel==1){
			Temp.r = tgaimage->imageData[I + 0];
            Temp.g = tgaimage->imageData[I + 0];
            Temp.b = tgaimage->imageData[I + 0];
            }
            if(bytesPerPixel==2){
			
            Temp2.r = tgaimage->imageData[I + 0];
            Temp2.g = tgaimage->imageData[I + 0];
            Temp2.b = tgaimage->imageData[I + 0];
            Temp2.a = tgaimage->imageData[I + 1];
            }

            if(bytesPerPixel==3){
            
            Temp.r = tgaimage->imageData[I + 0];
            Temp.g = tgaimage->imageData[I + 1];
            Temp.b = tgaimage->imageData[I + 2];
            }
            if(bytesPerPixel==4)
            {
            Temp2.r = tgaimage->imageData[I + 0];
            Temp2.g = tgaimage->imageData[I + 1];
            Temp2.b = tgaimage->imageData[I + 2];
            Temp2.a = tgaimage->imageData[I + 3];
            }
            
	I+=bytesPerPixel;
	
	if(bytesPerPixel != 2 && bytesPerPixel != 4)
	tgaimage->image = Temp;
	
	if(bytesPerPixel == 2 || bytesPerPixel == 4)
	tgaimage->imageA = Temp2;


	}
	
if(!orgin)
{


	color temp;
    colorA temp2;

	int y1, y2;
	y1 = 0;
	y2 = (int)tgaimage->height-1;

	

	if(bytesPerPixel != 2 && bytesPerPixel != 4)
	{
	    while((y1 != y2) && (y1 < y2))
	    {

		          for(int w=0; w< (int)tgaimage->width; w++)
		          {
			
			      temp = tgaimage->image[(y1 * (int)tgaimage->width) + w];
                  
			      tgaimage->image[(y1 * (int)tgaimage->width) + w] =
				  tgaimage->image[(y2 * (int)tgaimage->width) + w];
                  
			      tgaimage->image[(y2 * (int)tgaimage->width) + w] = temp;
		          }

            y1++;
		    y2--;

	     }
    }
    else
    {
        while((y1 != y2) && (y1 < y2))
	    {

		          for(int w=0; w< (int)tgaimage->width; w++)
		          {
			
			      temp2 = tgaimage->imageA[(y1 * (int)tgaimage->width) + w];
                  
			      tgaimage->imageA[(y1 * (int)tgaimage->width) + w] =
				  tgaimage->imageA[(y2 * (int)tgaimage->width) + w];
                  
			      tgaimage->imageA[(y2 * (int)tgaimage->width) + w] = temp2;
		          }

            y1++;
		    y2--;

	     }
    }


	

}

	createTexture(tgaimage);

	return 1;
}




[Edited by - yellowstar on October 19, 2007 4:24:54 PM]

Share this post


Link to post
Share on other sites
Advertisement
I am now having another problem.

When I try to add the following code to the init code,
the LoadTexture function seems to fail.(like loadTGA failed.)
The LoadTexture function is returning 0,(failed)
even when loadTGA succeeded.

glShadeModel(GL_SMOOTH);
glClearColor(0.0f, 0.0f, 0.0f, 0.5f);
glClearDepth(1.0f);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);




//DLLIMPORT means to export that function.(This is in a DLL, for the following
//functions)
DLLIMPORT unsigned int LoadTexture(char *filename)
{
unsigned int TexID;

if(!LoadTGA(filename,&TexID))
{
return 0;
}


return TexID;
}

unsigned int GetFileLength(FILE *file)
{
int l_iSavPos, l_iEnd;

l_iSavPos = ftell(file);
fseek(file, 0, SEEK_END);
l_iEnd = ftell(file);
fseek(file, l_iSavPos, SEEK_SET);

return (unsigned int)l_iEnd;
}

DLLIMPORT bool LoadTGA(char *filename, unsigned int *texID)
{
FILE *f = fopen(filename,"rb");
TGAimage image;
memset(&image,0,sizeof(TGAimage));
unsigned int size = 0;
BYTE *buff = NULL;


if(f==NULL)
return 0;

size = GetFileLength(f);
buff = (BYTE*)malloc(size);
if(buff==NULL)return 0;

fread(buff,1,size,f);
if(!loadTGA(buff,(int)size,&image,texID))
{
free(buff);
fclose(f);




return 0;
}

free(buff);

fclose(f);

return 1;
}


Share this post


Link to post
Share on other sites
I am using a 800x600 TGA.
I tried a 32x32 TGA,
but it still won't work.

I tried a differen't TGA loader,(not mine)
but it won't work for this either.(It works in the program that
it came with.)

Share this post


Link to post
Share on other sites
Never mind,
my LoadTexture function is returning zero again.

I found out I had duplicated init gl code.

I have fixed that, but it still won't work.

The var which contains the texture
is zero. When the LoadTexture function
fails, it returns zero for the texture value.

Share this post


Link to post
Share on other sites
Another thing:

When I try to access the value of the texture var
after loading,
it is a big value.

But, when I access it right in the createtexture function,
it's zero.

Share this post


Link to post
Share on other sites
I have found out why one of the values
was way to big.

It was a result of a mistake in some debugging code.
Now all of it is zero in all cases.

Share this post


Link to post
Share on other sites
I have dumped the converted image,
and had it displayed in a seperate program.
It worked.

So,
the problem is the gl commands,
not the loading code.

That is,
the texture creation gl function calls.

But,
that code is correct.
It works in other programs.

So,
something must be messing it up.

Share this post


Link to post
Share on other sites
I have Solved it!!!!(the texture problem)

I found the problem via a debugger mentioned
in my Ogl debugger topic.(GLintercept)
(Basic settings found it.)

Here,
I was doing ogl init,(enabling textures)
and creating textures,
BEFORE the window and ogl context was created!(Window creation and ect.
was in a function similar to main.
I moved that into a seperate function.)

Once I did the stuff mentioned in the parentheses,
it worked!!!

The colors and ect.
weren't correct.

But,
that's my TGA loader causing that.
I should be able to fix it.
If not,
I'll post here.

I'm having another problem.

On my computer,(tested on XP, works on my 98)
when I exit the program,
it throws the following error:(similar to it)
'The instruction at address 0x7c910f29 refrenced memory at address 0x00000000
The memory could not be "read" '

Click
here

if you want to help with this problem.


Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

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

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!