PDA

View Full Version : Targa File Textures Help Needed



Erfdain
05-01-2007, 11:51 PM
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

#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

#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


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_FILTE R,GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTE R,GL_NEAREST);

gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB, P38Texture.width, P38Texture.height,
GL_RGB, GL_UNSIGNED_BYTE, P38Texture.data);

}Model.cpp

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();

}

Erfdain
05-01-2007, 11:53 PM
BTY I have GL_TEXTURE_2D Enabled and the texture load function is called.

James W. Walker
05-02-2007, 05:14 PM
I don't see any place in your code where you pass the texture data to OpenGL, as by glTexImage2D.

Erfdain
05-04-2007, 05:58 PM
that is done by the glGenTextures() code...

remdul
05-05-2007, 12:12 PM
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?

James W. Walker
05-07-2007, 03:17 PM
Maybe I missed something again, but your TGA loading code is prepared for an alpha channel, while your gluBuild2DMipmaps call assumes GL_RGB.

Erfdain
05-09-2007, 09:54 AM
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..

Carl Jokl
05-09-2007, 11:51 AM
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.