Sign in to follow this  

Texturing Problem

This topic is 3857 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 making an asteroids game, and I have 2 different 3ds models loaded. I load the textures from .tga's and try to texture both models with their respective images. For some reason I have it texturing both with the same texture. Also, it is not even using the texture it is just using the main color out of the texture. Can someone help me solve these problems? Here's some source to help you figure out what is going on.
//TGA class
class zTGAImage
{

	public :


	// UBYTE pointer to hold the entire image 
	unsigned char *image;
	// no. of bytes per pixel (3 for RGB, 4 for RGBA)
    int channels;
	// image ID
	unsigned int ID;
	//image width and height
	int imageWidth;
    int imageHeight;
	
	// function prototypes
	zTGAImage();
	bool Load(char*);
	void Free();
	~zTGAImage();

};

//TGA Loader
bool zTGAImage::Load(char *fileName)
{
	Free(); // free any previous image loaded

	FILE *image_file;
	unsigned char tempColor;
	unsigned char bitCount;
	long tgaSize;
	unsigned char UncompressedHeader[12] = {0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0};
	unsigned char tgaHeader[12];
	unsigned char imageHeader[6];
	long index;

	if(!fileName)
	{
		// output error at console
		return false;
	}

	image_file = fopen(fileName, "rb");

	if(!image_file)
	{
		// output error at console
		return false;
	}



	// read the header
	fread(tgaHeader, 1, sizeof(tgaHeader), image_file);

	// make sure it is uncompressed tga
	if(memcmp(UncompressedHeader, tgaHeader, sizeof(UncompressedHeader))!=0)
	{
		// output error to console
		fclose(image_file);
		return false;
	}

	// read image header
    fread(imageHeader, 1, sizeof(imageHeader), image_file);

	// convert from base 256 to decimal base
	imageWidth = imageHeader[1] * 256 + imageHeader[0];
	imageHeight = imageHeader[3] * 256 + imageHeader[2];

	// read the bits per pixel
	bitCount = imageHeader[4];

	channels = bitCount / 8; // BGR or BGRA

	// calculate the number of bytes taken by the image
	tgaSize = imageWidth * imageHeight * channels;

    // allocate memory for the image and load it
    image = new unsigned char[sizeof(unsigned char) * tgaSize];
    fread(image, sizeof(unsigned char), tgaSize, image_file);


    // convert BGR to RGB
    for(index = 0; index < tgaSize; index += channels)
	{
		tempColor = image[index];
		image[index] = image[index + 2];
		image[index + 2] = tempColor;
	}
	glPushMatrix();
    glGenTextures(1, &ID);
    glBindTexture(GL_TEXTURE_2D, ID);
    glTexImage2D(GL_TEXTURE_2D, 0, 3, imageWidth, imageHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, image);
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);	
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);	
    glPopMatrix();
	// close the image
	fclose(image_file);

	// if all went ok, return true
	return true;
}

//In an init function

shipimage.Load("ship.tga");
rockimage.Load("rocktexture.tga");
//In a rendering function
shipmodel.Render(shipimage.ID)
rockmodel.Render(rockmodel.ID)

//The rendering function
void z3DSModel::Render(unsigned char texID)
{
    glPushMatrix();
	// set color as white
	glColor3f(1.0f, 1.0f, 1.0f);

	// begin rendering triangles
	glBegin(GL_TRIANGLES);

	// loop through all the meshes
	for(int i=0; i<totalMeshes; i++)
	{

        // Draw each triangle of this mesh.
		for(unsigned int f=0; f<meshList[i].totalFaces; f++)
		{
			// Get pointers to make the below code cleaner.
			stMesh *pMesh = &meshList[i];
			stFace *pFace = pMesh->pFaces;
			stTexCoord *pTexCoord = pMesh->pTexCoords;
			
			// If one or both or NULL, we got a problem.
			if(!pMesh || !pFace) continue;

			
			// Draw the a triangle, with texture coordinates and normals for
			// each vertex.
            glBindTexture(GL_TEXTURE_2D, texID);
			glNormal3f(pFace[f].normal.x, pFace[f].normal.y,
					   pFace[f].normal.z);

			int index1 = pFace[f].indices[0], index2 = pFace[f].indices[1], index3 = pFace[f].indices[2];
			
			if(pTexCoord)
				glTexCoord2f(pTexCoord[index1].tu, pTexCoord[index1].tv);
			
			glVertex3f(pMesh->pVertices[index1].x, pMesh->pVertices[index1].y, pMesh->pVertices[index1].z); 
		    
			if(pTexCoord)
				glTexCoord2f(pTexCoord[index2].tu, pTexCoord[index2].tv);
			
			glVertex3f(pMesh->pVertices[index2].x, pMesh->pVertices[index2].y, pMesh->pVertices[index2].z); 
			
			if(pTexCoord)
				glTexCoord2f(pTexCoord[index3].tu, pTexCoord[index3].tv);
			
			glVertex3f(pMesh->pVertices[index3].x, pMesh->pVertices[index3].y, pMesh->pVertices[index3].z); 
		}
	
	}

	// end drawing triangles
	glEnd();
	glPopMatrix();
}

Thanks if anyone can help. =)

Share this post


Link to post
Share on other sites
Try this:


//TGA class
class zTGAImage
{

public :


// UBYTE pointer to hold the entire image
unsigned char *image;
// no. of bytes per pixel (3 for RGB, 4 for RGBA)
int channels;
// image ID
unsigned int ID;
//image width and height
int imageWidth;
int imageHeight;

// function prototypes
zTGAImage();
bool Load(char*);
void Free();
~zTGAImage();

};

//TGA Loader
bool zTGAImage::Load(char *fileName)
{
Free(); // free any previous image loaded

FILE *image_file;
unsigned char tempColor;
unsigned char bitCount;
long tgaSize;
unsigned char UncompressedHeader[12] = {0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0};
unsigned char tgaHeader[12];
unsigned char imageHeader[6];
long index;

if(!fileName)
{
// output error at console
return false;
}

image_file = fopen(fileName, "rb");

if(!image_file)
{
// output error at console
return false;
}



// read the header
fread(tgaHeader, 1, sizeof(tgaHeader), image_file);

// make sure it is uncompressed tga
if(memcmp(UncompressedHeader, tgaHeader, sizeof(UncompressedHeader))!=0)
{
// output error to console
fclose(image_file);
return false;
}

// read image header
fread(imageHeader, 1, sizeof(imageHeader), image_file);

// convert from base 256 to decimal base
imageWidth = imageHeader[1] * 256 + imageHeader[0];
imageHeight = imageHeader[3] * 256 + imageHeader[2];

// read the bits per pixel
bitCount = imageHeader[4];

channels = bitCount / 8; // BGR or BGRA

// calculate the number of bytes taken by the image
tgaSize = imageWidth * imageHeight * channels;

// allocate memory for the image and load it
image = new unsigned char[sizeof(unsigned char) * tgaSize];
fread(image, sizeof(unsigned char), tgaSize, image_file);


// convert BGR to RGB
for(index = 0; index < tgaSize; index += channels)
{
tempColor = image[index];
image[index] = image[index + 2];
image[index + 2] = tempColor;
}
glPushMatrix();
glGenTextures(1, &ID);
glBindTexture(GL_TEXTURE_2D, ID);
glTexImage2D(GL_TEXTURE_2D, 0, 3, imageWidth, imageHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, image);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glPopMatrix();
// close the image
fclose(image_file);

// if all went ok, return true
return true;
}

//In an init function

shipimage.Load("ship.tga");
rockimage.Load("rocktexture.tga");
//In a rendering function
shipmodel.Render(shipimage.ID)
rockmodel.Render(rockmodel.ID)

//The rendering function
void z3DSModel::Render(unsigned char texID)
{
glPushMatrix();
// set color as white
glColor3f(1.0f, 1.0f, 1.0f);

glBindTexture(GL_TEXTURE_2D, texID);
// begin rendering triangles
glBegin(GL_TRIANGLES);

// loop through all the meshes
for(int i=0; i<totalMeshes; i++)
{

// Draw each triangle of this mesh.
for(unsigned int f=0; f<meshList[i].totalFaces; f++)
{
// Get pointers to make the below code cleaner.
stMesh *pMesh = &meshList[i];
stFace *pFace = pMesh->pFaces;
stTexCoord *pTexCoord = pMesh->pTexCoords;

// If one or both or NULL, we got a problem.
if(!pMesh || !pFace) continue;


// Draw the a triangle, with texture coordinates and normals for
// each vertex.

glNormal3f(pFace[f].normal.x, pFace[f].normal.y,
pFace[f].normal.z);

int index1 = pFace[f].indices[0], index2 = pFace[f].indices[1], index3 = pFace[f].indices[2];

if(pTexCoord)
glTexCoord2f(pTexCoord[index1].tu, pTexCoord[index1].tv);

glVertex3f(pMesh->pVertices[index1].x, pMesh->pVertices[index1].y, pMesh->pVertices[index1].z);

if(pTexCoord)
glTexCoord2f(pTexCoord[index2].tu, pTexCoord[index2].tv);

glVertex3f(pMesh->pVertices[index2].x, pMesh->pVertices[index2].y, pMesh->pVertices[index2].z);

if(pTexCoord)
glTexCoord2f(pTexCoord[index3].tu, pTexCoord[index3].tv);

glVertex3f(pMesh->pVertices[index3].x, pMesh->pVertices[index3].y, pMesh->pVertices[index3].z);
}

}

// end drawing triangles
glEnd();
glPopMatrix();
}



don't use function glBindTexture() betwen glBegin() and glEnd()

Share this post


Link to post
Share on other sites

This topic is 3857 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this