• Advertisement

Archived

This topic is now archived and is closed to further replies.

Help, Targa texture not binding

This topic is 5131 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

For some reason, the texture isn''t showing - I had it working after a fashion - you could see a box that was colored some sort of blue. now you can''t see anything Heres'' teh source :
#ifndef TEXTURE_H
#define TEXTURE_H 

typedef struct
{
	GLubyte *imageData;
	GLint idLength;
	GLint colorMapLength;
	GLint bytesPerPixel;
	GLint width;
	GLint height;
	GLuint texID;
	GLint size;
	GLuint type;
} Texture;

#endif  //TEXTURE_H

#ifndef TARGA_H
#define TARGA_H

#include <GL/gl.h>
#include <fstream>
#include "texture.h"

class LoadTexture
{
	public:
	LoadTexture(){}
	~LoadTexture(){}
	
	typedef struct
	{
		GLbyte idLength;
		GLbyte colourMapType;
		GLbyte imageType;
		//Color Map Specification:

		GLbyte firstEntryIndex[2];
		GLbyte colorMapLength[2];
		GLbyte colorMapEntrySize;
		//image specification

		GLbyte xOrigin[2];
		GLbyte yOrigin[2];
		GLbyte imageWidth[2];
		GLbyte imageHeight[2];
		GLbyte pixelDepth;
		GLbyte imageDescriptor;
	} TGAHeader;
	
	TGAHeader tgaHeader;
	bool LoadTGATexture( char *, Texture * );
	bool LoadUncompressedTexture( TGAHeader *, Texture *, std::fstream * );
	bool LoadCompressedTexture( TGAHeader *, Texture *, std::fstream * );
};

#endif  //TARGA_H

#include "targa.h"
#include <cstdlib>

bool LoadTexture::LoadTGATexture( char *fileName, Texture *texture )
{
	std::fstream file;		//Open the texture file to be read

	file.open( fileName, std::ios::in | std::ios::binary );		//Read in the file in binary mode

	if ( !file ){		//If we failed to read the file

		printf( "Error : Could not open file %s\n", fileName );
		file.close();
		return false;
	}
	else{	//We opened the file succesfully

		printf( "Opened file %s succesfully\n", fileName );
	}
	
	//file.seekg( 0, std::ios::beg );		//Make sure the pointer points to the beginning of the file - paranoid - probably redundant

	if( !( file.read( ( char * )&tgaHeader, sizeof( tgaHeader ) ) ) ){		//If we couldnt read in the header of the Targa image ( 18 bytes BTW )

		printf( "Error : failed to read file header" );
		file.close();
		return false;
	}
	if( ( tgaHeader.imageType == 2) ){	//If it is an uncompressed Targa image

		if( !( LoadUncompressedTexture( &tgaHeader, texture, &file ) ) ){	//If the function failed to load the uncompressed Targa image

			printf( "Error loading texture, aborting" );
			return false;
		}
	}
	else if( ( tgaHeader.imageType == 10 ) ){		//If it is a compressed Targa image

		if( !( LoadCompressedTexture( &tgaHeader, texture, &file ) ) ){		//If the function failed to load the compressed Targa image

			printf( "Error loading texture, aborting" );
			return false;
		}
	}
	else{	//If the file is neither a compressed nor uncompressed Targa image

		printf( "File is not a compressed or uncompressed targa image, aborting\n" );
		file.close();
		return false;
	}
	
	/* Setting up the texture and binding it for OpenGL */
	glGenTextures( 1, &texture[0].texID );
	glBindTexture( GL_TEXTURE_2D, texture[0].texID );
	glTexImage2D( GL_TEXTURE_2D, 0, 3, texture[0].width, texture[0].height, 0, GL_RGB, GL_UNSIGNED_BYTE,texture[0].imageData );
	glTexParameteri( GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR );
	glTexParameteri( GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR );
	
	file.close();		//Close the file since we''re done with it

	delete texture->imageData;		//delete the allocate space for the Targa image data since we''re finished with it

	return true;
}

bool LoadTexture::LoadUncompressedTexture( TGAHeader *tgaHeader, Texture *texture, std::fstream * file )
{
	GLuint swap;	//Variable used later on to swap the byte order from RGB to BGR for OpenGL

	printf( "Loading uncompressed tga image\n" );
	
	/* This next block fills the Texture we are generating with pertinent information from the header we read from the Targa image */
	texture->idLength = tgaHeader->idLength;
	texture->colorMapLength = tgaHeader->firstEntryIndex[0] + tgaHeader->firstEntryIndex[1] + tgaHeader->colorMapEntrySize;
	texture->width = abs( tgaHeader->imageWidth[1] * 256 ) +  abs( tgaHeader->imageWidth[0] );
	texture->height = abs( tgaHeader->imageHeight[1] * 256 ) +  abs( tgaHeader->imageHeight[0] );
	texture->bytesPerPixel = tgaHeader->pixelDepth/8;
	
	if ( tgaHeader->pixelDepth == 24 ){	//If we have a 24 bpp Targa image

		texture->type = GL_RGB;	//Set the type for OpenGL

	}
	else if ( tgaHeader->pixelDepth == 32 ){	//If we have a 32 bpp Targa image

		texture->type = GL_RGBA;	//Set the type for OpenGL

	} else{	//If the Targa image has a different bpp

		printf ( "Error : Bit depth must equal 24 or 32, aborting" );
		return false;
	}
	
	/* This next block calculates, allocates and finally reads in the actual image data from the Targa image and then swaps the byte order */
	texture->size = texture->bytesPerPixel * texture->width * texture->height;
	texture->imageData = new GLubyte[texture->size];
	file->seekg( ( sizeof( texture->idLength ) + sizeof( texture->colorMapLength ) ), std::ios::cur );	//Jump to the byte offset where the actual image data begins

	file->read( ( char * )texture->imageData, sizeof( texture->imageData ) );
	for(swap = 0; swap < ( int )texture->size; swap += ( texture->bytesPerPixel ) )
	{
		texture->imageData[swap] ^= texture->imageData[swap+2] ^=
		texture->imageData[swap] ^= texture->imageData[swap+2];
	}
	
	//delete texture->imageData;

	return true;
}

bool LoadTexture::LoadCompressedTexture( TGAHeader *tgaHeader, Texture *texture, std::fstream * file )
{
	printf( "Loading compressed tga image\n" );
	texture->width = ( abs( tgaHeader->imageWidth[1] * 256 ) +  abs( tgaHeader->imageWidth[0] ) );
	texture->height = ( abs( tgaHeader->imageHeight[1] * 256 ) +  abs( tgaHeader->imageHeight[0] ) );
	texture->bytesPerPixel = tgaHeader->pixelDepth;
	if ( texture->bytesPerPixel == 24 ){
		texture->type = GL_RGB;
	}
	else if ( texture->bytesPerPixel == 32 ){
		texture->type = GL_RGBA;
	} else{
		printf ( "Error : Bit depth must equal 24 or 32, aborting" );
		return false;
	}
	
	return true;
}
#include "seraphgl.h"
#include "targa.h"
#include "texture.h"

LoadTexture loadTexture;
Texture texture[1];

void SeraphGL::LoadGLTextures()
{
}

void SeraphGL::DrawBox( GLuint &boxList )
{
	boxList = glGenLists( 1 );
	glNewList( boxList, GL_COMPILE );
	glBegin( GL_QUADS );
		//Front Face

		glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f,  1.0f);
		glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f,  1.0f);
		glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f,  1.0f,  1.0f);
		glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f,  1.0f,  1.0f);
		// Back Face

		glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
		glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f,  1.0f, -1.0f);
		glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f,  1.0f, -1.0f);
		glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f);
		// Top Face

		glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f,  1.0f, -1.0f);
		glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f,  1.0f,  1.0f);
		glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f,  1.0f,  1.0f);
		glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f,  1.0f, -1.0f);
		// Bottom Face

		glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
		glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, -1.0f, -1.0f);
		glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f,  1.0f);
		glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f,  1.0f);
		// Right face

		glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f);
		glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f,  1.0f, -1.0f);
		glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f,  1.0f,  1.0f);
		glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f,  1.0f);
		// Left Face

		glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
		glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f,  1.0f);
		glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f,  1.0f,  1.0f);
		glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f,  1.0f, -1.0f);
	glEnd();
	glEndList();
}

void SeraphGL::initializeGL()
{
	glShadeModel( GL_SMOOTH );
	qglClearColor( black );
	glClearDepth( 1.0f );
	glEnable( GL_TEXTURE_2D );
	glEnable( GL_DEPTH_TEST );
	glEnable( GL_POLYGON_SMOOTH );
	glDepthFunc(GL_LEQUAL);
	glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST );
	glTranslatef( 0.0, 0.0, 0.0 );
	if( !( loadTexture.LoadTGATexture( "../data/texture.tga", &texture[0] ) ) ){
		printf( "Error initialising LoadTGATexture\n" );
	} else
		printf( "Succesfully initialized LoadTGATexture\n" );
	DrawBox( boxList );
}

void SeraphGL::resizeGL( GLsizei width, GLsizei height )
{
	glViewport( 0, 0, width, height );
	glMatrixMode( GL_PROJECTION );
	glLoadIdentity();
	gluPerspective( 90.0f,(GLfloat)width/(GLfloat)height,0.1f,100.0f);
	glMatrixMode( GL_MODELVIEW );
	glLoadIdentity();
}

void SeraphGL::paintGL()
{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
	glLoadIdentity();
	glTranslatef( 0.0f, 0.0f, -5.0f );
	glRotatef( xRotation, 1.0, 0.0, 0.0 );
	glRotatef( yRotation, 0.0, 1.0, 0.0 );
	glRotatef( zRotation, 0.0, 0.0, 1.0 );
	glBindTexture(GL_TEXTURE_2D, texture[0].texID);
	glCallList( boxList );
}

void SeraphGL::setXRotation( int xRot )
{
	xRotation = (GLint)( xRot % 360 );
	updateGL();
}

void SeraphGL::setYRotation( int yRot )
{
	yRotation = (GLint)( yRot % 360 );
	updateGL();
}

void SeraphGL::setZRotation( int zRot )
{
	zRotation = (GLint)( zRot % 360 );
	updateGL();
}
Any idea why? I appear just to have a black scren now :/

Share this post


Link to post
Share on other sites
Advertisement
Actually, from what I can tell the texture is actually binding - I think what is happening is something else. Either the scene needs to be lit (although from the countless tutorials I examined, they don''t light the scene) or my algorithm for sorting the bits into BGR is screwy, which I doubt since I pasted it

Share this post


Link to post
Share on other sites

  • Advertisement