🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Lesson 6: Texture Class?

Started by
3 comments, last by Fubator 18 years, 1 month ago
I'm fairly new to classes in C++, and I don't really trust msyelf with them right now ^^;; I was reading over Lesson 6 on textures, and was wanting to be able to simply create a Texture object everytime I wanted a new texture. My end goal is to make a simple 2d game. It would be a lot easier to create things if I could easily create textures. A Map could be an array of Texture object, a character would have a Tezture object, as would NPCs. I attempted to take the snippet in Lesson 6 that created and loaded the texture and put it into a class, but I believe I butchered it pretty bad... I was hoping someone could help me with doing this? Meow =3
Advertisement
I came up with the following texture class once, for an older project. I am not really an expert myself yet, so I'm sure there are hundreds of improvements to be made on the class design and stuff, but it works and might just get you started!

enum	FILTER_TYPE{	FILTER_LINEAR,	FILTER_MIPMAP};class	ctexture{	private:		GLuint	id;	public:		void	init ( char filename[], FILTER_TYPE filter );		void	activate ( void );};AUX_RGBImageRec *load_image ( char *filename )// loads a bitmap file{	FILE *file = NULL;	if ( !filename )		return NULL;	file = fopen ( filename, "r" );	if ( file )	{		fclose ( file );		return auxDIBImageLoad ( filename );	}	return NULL;}void ctexture::init ( char filename[], FILTER_TYPE filter )// loads the texture{	AUX_RGBImageRec *texture_image;	if ( texture_image = load_image ( filename ) )	{		glGenTextures ( 1, &id );		glBindTexture ( GL_TEXTURE_2D, id );				if ( filter == FILTER_LINEAR )		{			glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );			glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );			glTexImage2D ( GL_TEXTURE_2D, 0, 3, texture_image->sizeX, texture_image->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, texture_image->data );				}		else if ( filter == FILTER_MIPMAP )		{			glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );			glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST );			gluBuild2DMipmaps ( GL_TEXTURE_2D, 3, texture_image->sizeX, texture_image->sizeY, GL_RGB, GL_UNSIGNED_BYTE, texture_image->data );		}	}	if ( texture_image )	{		if ( texture_image->data )			free ( texture_image->data );		free ( texture_image );	}}void ctexture::activate ( void )// activates the texture for drawing{	glBindTexture ( GL_TEXTURE_2D, id );}


Now, when you want to get yourself a texture, you only need to define it:
ctexture texture;

Then, initialize it once at the beginning of your application:
// tell the texture what image file to use and to use mipmaps instead of linear// filtering for this texturetexture.init ( "textures\grass.bmp", FILTER_MIPMAP );

And then, right before drawing it, (where you normally would bind the texture using "glBindTexture ( )") you need to activate it now like this:
texture.activate ( );


And that's it. This texture class supports basic *.bmp-files as textures and additionally you can decide for each texture if you want to use faster, but blurry mipmaps or slower, but sharper linear filtering.

Hope this helps and feel free to ask questions if something remains unclear!
First off, I want to thank you! It's so funny, reading over that, it makes perfect sense, but I never would have come up with that on my own.

Anyway, I put that into it's own .cpp file and gave it a header. It compiles and runs fine. I had a little trouble getting the texture to show up at first, the I released I was using the wrong back slash in the file name XD

So instead of searching for "Data/nehe.bmp" it wanted:
"Data
ehe.bmp"

I had a good chuckle with that =P

Thanks a ton, I expect to use this a LOT. I commented some credit in for you aswell. It just gives credit to "d h k" so, if you want a more formal name or anything, I can change it to that ^^

Meow =3
Edit: Problem fixed. My project was just pulling one of the files from an older folder. So it couldn't find my header file in THAT folder. I just readded the file, and bam it worked. It's always the silly msitakes that get ya...

Scratch that, I have a tiny issue. I ran into the Glaux.dll error, and so I searched up the replacement code. I used the code from this forum post:

http://www.gamedev.net/community/forums/topic.asp?topic_id=275238

The problem I'm running into, Is my ctexture.cpp class says BMP.h doesn't exist =/ I have it included in the project and everyhting, and my main.cpp file finds it just fine with no problems. Everything is in the same folder, so there shouldn't be any path mistakes...

Thanks for any help ^^;;

[Edited by - KuroKitten on May 14, 2006 8:37:24 PM]
I had the same problem when using the bmp.cpp/.h loader from there. I just moved the bmp.h into my include folder. Now I dont have to make copies of it to put in each project folder I use it in now either. Just have to do that with the .cpp. It works great tho.

This topic is closed to new replies.

Advertisement