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, &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(&ucharBad, sizeof(unsigned char), 1, filePtr);
fread(&ucharBad, sizeof(unsigned char), 1, filePtr);
// read in the image type
fread(&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) && (tgaHeader->imageTypeCode != 3))
{
fclose(filePtr);
return NULL;
}
// read 13 bytes of garbage data
fread(&sintBad, sizeof(short int), 1, filePtr);
fread(&sintBad, sizeof(short int), 1, filePtr);
fread(&ucharBad, sizeof(unsigned char), 1, filePtr);
fread(&sintBad, sizeof(short int), 1, filePtr);
fread(&sintBad, sizeof(short int), 1, filePtr);
// read image dimensions
fread(&tgaHeader->imageWidth, sizeof(short int), 1, filePtr);
fread(&tgaHeader->imageHeight, sizeof(short int), 1, filePtr);
// read bit depth
fread(&tgaHeader->bitCount, sizeof(unsigned char), 1, filePtr);
// read garbage
fread(&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, &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, &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
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();
}