Results 1 to 8 of 8

Thread: Targa File Textures Help Needed

  1. #1
    Junior Member Newbie
    Join Date
    Feb 2005
    Location
    Indianapolis, IN
    Posts
    10

    Targa File Textures Help Needed

    Well I have a problem with displaying my tga files for textures. It displays lines and weird colors on my model. I had this same problem when trying to load bmp files that where larger than 64x64

    I would appreciate any help

    texture.h
    Code :
    #ifndef __TEXTURE_H
    #define __TEXTURE_H
     
    #define WIN32_LEAN_AND_MEAN
    #define WIN32_EXTRA_LEAN
     
     
     
    #include <windows.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    #include <gl/gl.h>
    #include <gl/glu.h>
     
    typedef struct
    {
    	unsigned char imageTypeCode;
    	short int imageWidth;
    	short int imageHeight;
    	unsigned char bitCount;
    } TGAHEADER;
     
    class Texture
    {
    private:
    	//******************
    	// private Variables
    	//******************
     
     
     
    	//******************
    	// private Functions
    	// *****************
    	unsigned char *LoadTGAFile(char *filename, TGAHEADER *tgaHeader);
    	void LoadTGATexture(char *filename);
     
    public:
     
    	unsigned char tgaImageCode;	// 0 = not TGA image, 2 = color, 3 = greyscale
     
    	int width;
    	int height;
    	int bitDepth;
    	unsigned int texID;
     
    	unsigned char *data;
     
     
     
     
    	Texture(){data = NULL;};
    	~Texture(){};
     
    	void Load(char *FileName);
    	void Unload()
    	{
    		glDeleteTextures(1, &amp;texID);
     
    		if (data != NULL)
    			free(data);
    	}
     
     
    };
     
    #endif __TEXTURE_H
    texture.cpp
    Code :
    #include "Texture.h"
     
     
     
     
    unsigned char *Texture::LoadTGAFile(char *filename, TGAHEADER *tgaHeader)
    {
    	FILE *filePtr;
    	unsigned 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
    	unsigned char colorSwap;	// swap variable
    	unsigned char *imageData;	// the TGA data
     
    	// open the TGA file
    	filePtr = fopen(filename, "rb");
    	if (!filePtr)
    		return NULL;
     
    	// read first two bytes of garbage
    	fread(&amp;ucharBad, sizeof(unsigned char), 1, filePtr);
    	fread(&amp;ucharBad, sizeof(unsigned char), 1, filePtr);
     
    	// read in the image type
    	fread(&amp;tgaHeader->imageTypeCode, sizeof(unsigned char), 1, filePtr);
     
    	// for our purposes, the image type should be either a 2 or a 3
    	if ((tgaHeader->imageTypeCode != 2) &amp;&amp; (tgaHeader->imageTypeCode != 3))
    	{
    		fclose(filePtr);
    		return NULL;
    	}
     
    	// read 13 bytes of garbage data
    	fread(&amp;sintBad, sizeof(short int), 1, filePtr);
    	fread(&amp;sintBad, sizeof(short int), 1, filePtr);
    	fread(&amp;ucharBad, sizeof(unsigned char), 1, filePtr);
    	fread(&amp;sintBad, sizeof(short int), 1, filePtr);
    	fread(&amp;sintBad, sizeof(short int), 1, filePtr);
     
    	// read image dimensions
    	fread(&amp;tgaHeader->imageWidth, sizeof(short int), 1, filePtr);
    	fread(&amp;tgaHeader->imageHeight, sizeof(short int), 1, filePtr);
     
    	// read bit depth
    	fread(&amp;tgaHeader->bitCount, sizeof(unsigned char), 1, filePtr);
     
    	// read garbage
    	fread(&amp;ucharBad, sizeof(unsigned char), 1, filePtr);
     
    	// colormode -> 3 = BGR, 4 = BGRA
    	colorMode = tgaHeader->bitCount / 8;
    	imageSize = tgaHeader->imageWidth * tgaHeader->imageHeight * colorMode;
     
    	// allocate memory for image data
    	imageData = (unsigned char*)malloc(sizeof(unsigned char)*imageSize);
     
    	// read image data
    	fread(imageData, sizeof(unsigned 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;
    }
     
    void Texture::LoadTGATexture(char *filename)
    {	
    	TGAHEADER tga;		// BMP header
     
    	// store BMP data in texture
    	data = LoadTGAFile(filename, &amp;tga);
    	if (data == NULL)
    	{
    		free(data);
    	}
     
    	// store texture information
    	width = tga.imageWidth;
    	height = tga.imageHeight;
    	//palette = NULL;
    	//scaledHeight = 0;
    	//scaledWidth = 0;
    	tgaImageCode = tga.imageTypeCode;
    	bitDepth = tga.bitCount;
    	//textureType = TGA;
    }
     
    void Texture::Load(char *fileName)
    {
    	LoadTGATexture(fileName);
    }
    SetupModelTexture

    Code :
    void Model::SetupTexture()
    {
    	// set the proper parameters for an MD2 texture
         glGenTextures(0, &amp;P38Texture.texID);
    	 glBindTexture(GL_TEXTURE_2D, P38Texture.texID);
         glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_CLAMP);
         glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_CLAMP);
         glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
         glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);
     
    	 gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB, P38Texture.width, P38Texture.height, 
                   GL_RGB, GL_UNSIGNED_BYTE, P38Texture.data);
     
    }
    Model.cpp
    Code :
    void Model::Draw()
    {
     
    	//MessageBox(NULL, "Drawing Model", "Test", MB_OK);
    	int face1, face2, face3;
     
    	glPushMatrix();
    	glBindTexture(GL_TEXTURE_2D, P38Texture.texID);
    		for(int i = 0; i < MaxPolys; i++)
    		{
    			face1 = myPolys[i].Vertex1 - 1;
    			face2 = myPolys[i].Vertex2 - 1;
    			face3 = myPolys[i].Vertex3 - 1;
    			glBegin(GL_TRIANGLES);
    			//glColor3f(1.0f, 0.0f, 0.0f);
    			glTexCoord2f(TextPoint[face1].pointX,
    						 TextPoint[face1].pointY);
    			glVertex3f(Verticies[face1].pointX,
    					   Verticies[face1].pointY,
    					   Verticies[face1].pointZ);
    			glTexCoord2f(TextPoint[face2].pointX,
    						 TextPoint[face2].pointY);
    			glVertex3f(Verticies[face2].pointX,
    				       Verticies[face2].pointY,
    			    	   Verticies[face2].pointZ);
    			glTexCoord2f(TextPoint[face3].pointX,
    						 TextPoint[face3].pointY);
    			glVertex3f(Verticies[face3].pointX,
    					   Verticies[face3].pointY,
    					   Verticies[face3].pointZ);
    			glEnd();
    		}
    	glPopMatrix();
     
    }
    (-------------(_ __ _ ()
    Just having a good time
    "YA RIGHT!!!"

  2. #2
    Junior Member Newbie
    Join Date
    Feb 2005
    Location
    Indianapolis, IN
    Posts
    10

    Re: Targa File Textures Help Needed

    BTY I have GL_TEXTURE_2D Enabled and the texture load function is called.
    (-------------(_ __ _ ()
    Just having a good time
    "YA RIGHT!!!"

  3. #3
    Senior Member Regular Contributor
    Join Date
    Nov 2004
    Location
    San Diego, CA, USA
    Posts
    122

    Re: Targa File Textures Help Needed

    I don't see any place in your code where you pass the texture data to OpenGL, as by glTexImage2D.

  4. #4
    Junior Member Newbie
    Join Date
    Feb 2005
    Location
    Indianapolis, IN
    Posts
    10

    Re: Targa File Textures Help Needed

    that is done by the glGenTextures() code...
    (-------------(_ __ _ ()
    Just having a good time
    "YA RIGHT!!!"

  5. #5
    Senior Member Regular Contributor remdul's Avatar
    Join Date
    Mar 2004
    Location
    The Netherlands
    Posts
    346

    Re: Targa File Textures Help Needed

    I think you mean gluBuild2DMipmaps()...

    If 64x64 sized images work fine, maybe you have an overflow somewhere? Are these larger images power-of -two sized?

  6. #6
    Senior Member Regular Contributor
    Join Date
    Nov 2004
    Location
    San Diego, CA, USA
    Posts
    122

    Re: Targa File Textures Help Needed

    Maybe I missed something again, but your TGA loading code is prepared for an alpha channel, while your gluBuild2DMipmaps call assumes GL_RGB.

  7. #7
    Junior Member Newbie
    Join Date
    Feb 2005
    Location
    Indianapolis, IN
    Posts
    10

    Re: Targa File Textures Help Needed

    Yes, I was trying 1024 and 2048 image sizes but somehow they seem to be working now. But I'm having problems exporting objects from blender and getting the texture working right. They seem to be overlapping on the mesh..
    (-------------(_ __ _ ()
    Just having a good time
    "YA RIGHT!!!"

  8. #8
    Member Newbie Carl Jokl's Avatar
    Join Date
    Jan 2007
    Location
    Redditch, Worcestershire, United Kingdon
    Posts
    47

    Re: Targa File Textures Help Needed

    Are you still having problems loading textures? It looks like you might be sorted now, at least for that part anyway. I have working BMP and TGA image loading classes for my own coursework if you wanted to look at them but if you have got things up and running then you won't need them.
    Check out my dimensions, length, width and for a limited time only..depth!

Similar Threads

  1. Problems Loading and Displaying TARGA Textures
    By Darlos9D in forum OpenGL: Basic Coding
    Replies: 5
    Last Post: 08-31-2010, 10:05 PM
  2. My Targa file loading function isn't working
    By Joe the Programmer in forum OpenGL: Basic Coding
    Replies: 2
    Last Post: 02-06-2006, 11:36 PM
  3. Data file with 3D vertices needed
    By evian in forum OpenGL: Advanced Coding
    Replies: 5
    Last Post: 07-08-2005, 02:17 PM
  4. Data file with 3D-vertices needed
    By evian in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 07-07-2005, 11:36 AM
  5. Emergency! Helpp Needed@ Can anyone compile the file?
    By jojozhong in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 09-06-2000, 01:16 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean