Texture problems(SOLVED, but exiting error)

Started by
7 comments, last by yellowstar 16 years, 6 months ago
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;
			tgaimage-&gt;imageData = temp;

			temp = tgaimage-&gt;imageData;
			tgaimage-&gt;imageData = tgaimage-&gt;imageData;
			tgaimage-&gt;imageData = temp;
		}
	}

    

    <span class="cpp-keyword">if</span>(bytesPerPixel != <span class="cpp-number">2</span> &amp;&amp; bytesPerPixel != <span class="cpp-number">4</span>)
    {
	tgaimage-&gt;image = (color*)malloc(<span class="cpp-keyword">sizeof</span>(color)*(tgaimage-&gt;width*tgaimage-&gt;height));
    }
    <span class="cpp-keyword">else</span>
    {
    tgaimage-&gt;imageA = (colorA*)malloc(<span class="cpp-keyword">sizeof</span>(colorA)*(tgaimage-&gt;width*tgaimage-&gt;height));
    }
<span class="cpp-keyword">int</span> I = <span class="cpp-number">0</span>;
color Temp;
colorA Temp2;

	<span class="cpp-keyword">for</span>(<span class="cpp-keyword">int</span> i=<span class="cpp-number">0</span>; i &lt; (<span class="cpp-keyword">int</span>)(tgaimage-&gt;width*tgaimage-&gt;height); i++)
	{

            <span class="cpp-keyword">if</span>(bytesPerPixel==<span class="cpp-number">1</span>){
			Temp.r = tgaimage-&gt;imageData;
            Temp.g = tgaimage-&gt;imageData;
            Temp.b = tgaimage-&gt;imageData;
            }
            <span class="cpp-keyword">if</span>(bytesPerPixel==<span class="cpp-number">2</span>){
			
            Temp2.r = tgaimage-&gt;imageData;
            Temp2.g = tgaimage-&gt;imageData;
            Temp2.b = tgaimage-&gt;imageData;
            Temp2.a = tgaimage-&gt;imageData;
            }

            <span class="cpp-keyword">if</span>(bytesPerPixel==<span class="cpp-number">3</span>){
            
            Temp.r = tgaimage-&gt;imageData;
            Temp.g = tgaimage-&gt;imageData;
            Temp.b = tgaimage-&gt;imageData;
            }
            <span class="cpp-keyword">if</span>(bytesPerPixel==<span class="cpp-number">4</span>)
            {
            Temp2.r = tgaimage-&gt;imageData;
            Temp2.g = tgaimage-&gt;imageData;
            Temp2.b = tgaimage-&gt;imageData;
            Temp2.a = tgaimage-&gt;imageData;
            }
            
	I+=bytesPerPixel;
	
	<span class="cpp-keyword">if</span>(bytesPerPixel != <span class="cpp-number">2</span> &amp;&amp; bytesPerPixel != <span class="cpp-number">4</span>)
	tgaimage-&gt;image<span style="font-weight:bold;"> = Temp;
	
	<span class="cpp-keyword">if</span>(bytesPerPixel == <span class="cpp-number">2</span> || bytesPerPixel == <span class="cpp-number">4</span>)
	tgaimage-&gt;imageA<span style="font-weight:bold;"> = Temp2;


	}
	
<span class="cpp-keyword">if</span>(!orgin)
{


	color temp;
    colorA temp2;

	<span class="cpp-keyword">int</span> y1, y2;
	y1 = <span class="cpp-number">0</span>;
	y2 = (<span class="cpp-keyword">int</span>)tgaimage-&gt;height-<span class="cpp-number">1</span>;

	

	<span class="cpp-keyword">if</span>(bytesPerPixel != <span class="cpp-number">2</span> &amp;&amp; bytesPerPixel != <span class="cpp-number">4</span>)
	{
	    <span class="cpp-keyword">while</span>((y1 != y2) &amp;&amp; (y1 &lt; y2))
	    {

		          <span class="cpp-keyword">for</span>(<span class="cpp-keyword">int</span> w=<span class="cpp-number">0</span>; w&lt; (<span class="cpp-keyword">int</span>)tgaimage-&gt;width; w++)
		          {
			
			      temp = tgaimage-&gt;image[(y1 * (<span class="cpp-keyword">int</span>)tgaimage-&gt;width) + w];
                  
			      tgaimage-&gt;image[(y1 * (<span class="cpp-keyword">int</span>)tgaimage-&gt;width) + w] =
				  tgaimage-&gt;image[(y2 * (<span class="cpp-keyword">int</span>)tgaimage-&gt;width) + w];
                  
			      tgaimage-&gt;image[(y2 * (<span class="cpp-keyword">int</span>)tgaimage-&gt;width) + w] = temp;
		          }

            y1++;
		    y2–;

	     }
    }
    <span class="cpp-keyword">else</span>
    {
        <span class="cpp-keyword">while</span>((y1 != y2) &amp;&amp; (y1 &lt; y2))
	    {

		          <span class="cpp-keyword">for</span>(<span class="cpp-keyword">int</span> w=<span class="cpp-number">0</span>; w&lt; (<span class="cpp-keyword">int</span>)tgaimage-&gt;width; w++)
		          {
			
			      temp2 = tgaimage-&gt;imageA[(y1 * (<span class="cpp-keyword">int</span>)tgaimage-&gt;width) + w];
                  
			      tgaimage-&gt;imageA[(y1 * (<span class="cpp-keyword">int</span>)tgaimage-&gt;width) + w] =
				  tgaimage-&gt;imageA[(y2 * (<span class="cpp-keyword">int</span>)tgaimage-&gt;width) + w];
                  
			      tgaimage-&gt;imageA[(y2 * (<span class="cpp-keyword">int</span>)tgaimage-&gt;width) + w] = temp2;
		          }

            y1++;
		    y2–;

	     }
    }


	

}

	createTexture(tgaimage);

	<span class="cpp-keyword">return</span> <span class="cpp-number">1</span>;
}




</pre></div><!–ENDSCRIPT–> 

<!–EDIT–><span class=editedby><!–/EDIT–>[Edited by - yellowstar on October 19, 2007 4:24:54 PM]<!–EDIT–></span><!–/EDIT–>
My Website:link(Not hosted by Gamedev)
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;}

My Website:link(Not hosted by Gamedev)
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.)

My Website:link(Not hosted by Gamedev)
I have gotten my LoadTexture function
to stop returning zero.

But, it still won't work.

I copyed this into
a seperate program.
It worked.
My Website:link(Not hosted by Gamedev)
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.
My Website:link(Not hosted by Gamedev)
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.
My Website:link(Not hosted by Gamedev)
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.
My Website:link(Not hosted by Gamedev)
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.
My Website:link(Not hosted by Gamedev)
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.


My Website:link(Not hosted by Gamedev)

This topic is closed to new replies.

Advertisement