Jump to content
  • Advertisement

Archived

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

good texture loading procedure please

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

Hey what''s the best texture loading procedure that doesn''t use the NeHe famous: AUX_RGBImageRec form that apparently cannot be dynamically allocated (or its oddly different i dont know)? I need to be able to have the number of textures to be loaded to be dynamic and then have a texture loading procedure load them in. However when i do something like : AUX_RGBImageRec *TextureImage; TextureImage = new AUX_RGBImageRec[size]; it doesn''t work. Am I doing it wrong? What the hell is that thing anyway?? ( yes i''m well aware that glaux sucks i just never had to change it till now)

Share this post


Link to post
Share on other sites
Advertisement
Make a function which can load 1 texture and then call that function for every texture.

A smal example:

GLuint LoadTexture(char *filename)
{
//use nehe''s code
}

If you dont wanna use AUX_RGBImageRec, write your own function for bmp''s , it''s pretty easy.

Share this post


Link to post
Share on other sites
Use libpng www.libpng.org to load PNG files, then pass a pointer to the bits to a texture loading function.

I wrap it all up in a texture class, so you can declare as many textures as necessary.

texture *water = new texture();
water->loadFileRGB ("water.rgb");
water->bind();
//Start drawing

source for part of texture class (yes its sloppy, and it has no error checking -- you are forewarned):
  
class texture
{
protected:
GLuint id;
void release ( void );
void create ( GLubyte *pixels, int size );
public:
texture ( void );
~texture ( void );
void bind ( void );
bool loadFileRGB (const char * filename);
};
texture::texture ( void )
{
id = 0;
}
texture::~texture ( void )
{
release ();
}
void texture::bind ( void )
{
glBindTexture (GL_TEXTURE_2D, id);
}
void texture::release ()
{
glDeleteTextures (1, &id);
id = 0;
}
void texture::create ( GLubyte *pixels, int size )
{
glGenTextures( 1, &id );
glBindTexture (GL_TEXTURE_2D, id);
glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexEnvf (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
glTexImage2D(GL_TEXTURE_2D, 0, 3, size, size, 0, GL_RGB, GL_UNSIGNED_BYTE, pixels);
}

bool texture::loadFileRGB (const char * filename)
{
release ();
std::ifstream in;
in.open (filename, std::ios::binary );

if (in.is_open() == false)
return false;
int length, size;

in.seekg (0, std::ios::end);
length = in.tellg();
in.seekg (0, std::ios::beg);

size = (int) sqrtf(length/3);

GLubyte *pixels = new GLubyte [length];

in.read ((char*)pixels,length);

in.close();

create (pixels, size);
delete [] pixels;
return true;
}


Share this post


Link to post
Share on other sites
Indeed, Aux is not the way to go

I use Targa images, which are great because they have a built-in alpha channel which saves you a lot of work and it doesn''t use the GLaux lib... it''s not really hard to load the files, although it''s a little bit more code than the bitmap loading code of yours probably is. There are 2 tutorials about loading Targa files on NeHe, Lesson 25 (Uncompressed Targa explained very detailed, scissor testing and extensions too (off-topic for you)) and lesson 34, fully dedicated to both uncompressed AND compressed Targa files, but explained a bit less detailed.

My advice is to follow both lessons if you decide to give targa a try...

Else you can also still go for PNG or RAW, or whatever there are lots of choices just decide for yourself what''s best...



''Qui habet aures audiendi audiat'' - Me

A link to the website of my 3D engine will follow as soon as I have something concrete to show... Which will be soon.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!