Sign in to follow this  
Noxir

GIF loader?

Recommended Posts

Noxir    100
On GIF and PNG the images can be transparent, so there is no background around them that can disturb the stuff the player is walking on. Could anyone tell me how to make a GIF loader? This is a BMP loader (so you know what I mean):
	if (TextureImage[0]=LoadBMP("player.bmp"))
	{
		Status=TRUE;						

		glGenTextures(1, &player[0]);					// Create The Texture

		// Typical Texture Generation Using Data From The Bitmap
		glBindTexture(GL_TEXTURE_2D, player[0]);
		glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[0]->sizeX, TextureImage[0]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[0]->data);
		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
	}
AUX_RGBImageRec *LoadBMP(char *Filename)
{
	FILE *File=NULL;

	if (!Filename)						
	{
		return NULL;		
	}

	File=fopen(Filename,"r");							// Check To See If The File Exists

	if (File)											// Does The File Exist?
	{
		fclose(File);									// Close The Handle
		return auxDIBImageLoad(Filename);				// Load The Bitmap And Return A Pointer
	}

	return NULL;										// If Load Failed Return NULL
}





Many thanks for answer, since the player can't walk on anything without it seems weird.

Share this post


Link to post
Share on other sites
lc_overlord    436
Actuarly, gif files use color keying, so you would do just fine using that on bmp or tga files.

Basicly you need to add a alpha component to the texture data after loading using the color.
It's not hard to do.
i reccomend using tga files though, RLE compressed 24bit color + 8bit alpha beats 8bit indexed collor with 1bit alpha any day, allso they are easy to load.

allso gif files can only have 256 colors, one of them is transparent.

Share this post


Link to post
Share on other sites
lc_overlord    436
when loading you get the TextureImage[0]->data array, it has the size of sizeX*sizeY*3.

if you then create an array with the size of sizeX*sizeY*4 and transfer the data from the TextureImage[0]->data array,and after each group of 3 bytes you add a fourth one witch is either 255 or 0 depending on if the preceding 3 bytes matches the transparent color value or not.
then change this line
glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[0]->sizeX, TextureImage[0]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[0]->data);
to this
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, TextureImage[0]->sizeX, TextureImage[0]->sizeY, 0, GL_RGBA, GL_UNSIGNED_BYTE, --insert your new array here--);


And BTW i realy don't care about spelling, only what is being said with it.

Share this post


Link to post
Share on other sites
Steve132    433
I apologize if this seems harsh, but I am saying this not to hurt your feelings, but to help you get better results in the future. You are also not the only one with this problem, but you are one who happens to be here now.

Anywhere that purports to foster and encourage intellectual thought and growth will respond extremely negatively to "plz just give code." and a gamedev programming forum is no different. You are (supposedly) a programmer; use some brains and figure it out! Helpful advice has been given, and when you continued to have questions, an attempt was made to help you understand. If you have further questions, then do not hesitate to ask, but asking someone else you have never met who is helping you only out of the kindness of his heart to do work on your project for you for nothing after he has already given plenty of advice is very rude. I guarantee kindness and etiquette will be rewarded in this forum, but bad manners will get you nothing (except maybe a low rating.)

Share this post


Link to post
Share on other sites
try_catch_this    373

#include <windows.h>
#include <gl/glu.h>
#include <string>
// rember to link opengl32 and glu32

#define ERROR_CNLT 0xFFFFFF

GLuint LoadGLTextureBitmapFile( std::string path, COLORREF transparent_color )
{
return LoadGLTextureBitmapFile( (char*) path.c_str(), transparent_color );
}

GLuint LoadGLTextureBitmapFile( char path[], COLORREF transparent_color )
{
HBITMAP hBMP;
BITMAP BMP;

GLuint Texture = ERROR_CNLT;
::glGenTextures( 1, &Texture ); // Generate Textures

hBMP = (HBITMAP)::LoadImage( NULL, path, IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION | LR_LOADFROMFILE );
if( hBMP )
{
::GetObject( hBMP, sizeof( BMP ), &BMP );
::glBindTexture( GL_TEXTURE_2D, Texture ); // Bind Our Texture
::glPixelStorei( GL_UNPACK_ALIGNMENT, 4 ); // Pixel Storage Mode ( Word Alignment / 4 Bytes )

::glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR ); // Linear Filtering
::glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR ); // Linear Filtering

unsigned char *data = new unsigned char[ BMP.bmWidth * BMP.bmHeight * 4 ];
if( data == NULL)
return ERROR_CNLT;
unsigned int pixels = BMP.bmWidth * BMP.bmHeight;
unsigned char color[3] = {0,0,0};
unsigned char *bits = (unsigned char*)BMP.bmBits;

for( int i=0; i<pixels ; ++i )
{
color[0] = *(bits+(i*3));
color[1] = *(bits+(i*3)+1);
color[2] = *(bits+(i*3)+2);
if( color[0] == GetBValue(transparent_color) &&
color[1] == GetGValue(transparent_color) &&
color[2] == GetRValue(transparent_color)
)
{
data[ i*4 ] = color[0];
data[ i*4 +1 ] = color[1];
data[ i*4 +2 ] = color[2];
data[ i*4 +3 ] = 0;
}
else
{
data[ i*4 ] = color[0];
data[ i*4 +1 ] = color[1];
data[ i*4 +2 ] = color[2];
data[ i*4 +3 ] = 255;
}
}

// Generate Mipmapped Texture (4 Bytes, Width, Height And Data From The BMP)
::gluBuild2DMipmaps( GL_TEXTURE_2D, 4, BMP.bmWidth, BMP.bmHeight, GL_BGRA_EXT, GL_UNSIGNED_BYTE, data );

::DeleteObject( hBMP ); // Delete The Bitmap Object
delete[] data;

return Texture;
}
// you put in code to get the last system error
return ERROR_CNLT;
}




I load regular bitmaps and assign a a transparent color.
My code manually adds a alpha channel.
eg
GLuint texture = LoadGLTextureBitmapFile( "data/myfile.bmp", RGB( 0, 0, 0 ) );
// black ( 0,0,0) is transparent

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
I've "used the brain" in hours now and I still cant figure it out, I've tried to change everything, every little variable, I've also searched a lot in Google but still can't find anything

This is the only site I think it's possible getting answer
Thanks

Share this post


Link to post
Share on other sites
dawidjoubert    161
Okay what he was saying was
Your gif looks like this after being read from file
ColorObject Color[256]; // Now this is the index list Color Consists of RGBA
// When a certain pixel with a certain index is rendered
byte IndexColor[Width*Height]

So to get the Color of a Pixel You Would go
int GetColor(x,y)
{
return IndexColor[x + (y*width)]
}

And so you convert that format to a
RGB Format

byte *data = new byte[width*hieght*4] // where the 4 stands for four colors

// now when you convert from the one to the other
for (int g=0;g < sizeof(data)/4;g+=4;)
{
data[g+0] = Color[IndexColor[g]].R
data[g+1] = Color[IndexColor[g]].G
data[g+2] = Color[IndexColor[g]].B
if (IndexColor[g] == 255) data[g+3] = 0.0f;
else data[g+3] = 1.0f;

}

// None the Less you still need 2 program and improvise.


Share this post


Link to post
Share on other sites

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