Archived

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

Marty666

darn texture loading

Recommended Posts

Marty666    145
Hi all this is the code of my texture loader. It''s supposed to load a texture and return the numer to bind when using it. numtextures starts at one, because is returns null if something went wrong. Whatever texture I use i allways get the same one. I can''t seem to find what''s wrong. Marty here''s the code:

#include "header.h"

GLuint textures[MAX_TEXTURES];
int numtextures = 1;

AUX_RGBImageRec *LoadBMP(char *Filename)
{
	FILE *File=NULL; // is there a filename?
	if (!Filename){return NULL;} // if not, return NULL
	File=fopen(Filename,"r"); // does the file exist?
	if (File) 
		{	
			fclose(File);	// then close it 
			return auxDIBImageLoad(Filename); // and return a pointer
		} 
	return NULL; // else return null
}

int LoadTexture(char *filename)
{
	if (!(numtextures>MAX_TEXTURES))
	{
		AUX_RGBImageRec * TextureImage[1]; // storagespace for 1 texture
		memset(TextureImage,0,sizeof(void *)*1); // clear the mem
		if (TextureImage[0] = LoadBMP(filename)) // load the file
		{
			glGenTextures(1, &textures[numtextures]); // create space for texture
			glBindTexture(GL_TEXTURE_2D, textures[numtextures]); // bind the loaded texture
			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); // set params
			glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); // set params
		}
		if (TextureImage[0])
		{
			if (TextureImage[0]->data) {free(TextureImage[0]->data);} // free the loaded bmp
			free(TextureImage[0]); // and the memory
		}
		numtextures++;
		return (textures[numtextures - 1]);	// return texnum
	}
	return NULL;						// if max is reached return 0
}
 

Share this post


Link to post
Share on other sites
Marty666    145
Ok, i changed the code, it still doesn''t work This should return a GLuint that i can bind to use the texture, right?


#include "NukeM.h"


AUX_RGBImageRec *LoadBMP(char *Filename)
{
FILE *File=NULL; // is there a filename?
if (!Filename){return NULL;} // if not, return NULL
File=fopen(Filename,"r"); // does the file exist?
if (File)
{
fclose(File); // then close it
return auxDIBImageLoad(Filename); // and return a pointer
}
return NULL; // else return null
}

void LoadTexture(char *filename, GLuint *holder)
{
AUX_RGBImageRec * TextureImage[1]; // storagespace for 1 texture
memset(TextureImage,0,sizeof(void *)*1); // clear the mem
TextureImage[0] = LoadBMP(filename); // load the file
glGenTextures(1, holder); // create space for texture
glBindTexture(GL_TEXTURE_2D, *holder); // bind the loaded texture
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); // set params
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); // set params
if (TextureImage[0])
{
if (TextureImage[0]->data)
{
free(TextureImage[0]->data); // free the loaded bmp
}
free(TextureImage[0]); // and the memory
}
}

Share this post


Link to post
Share on other sites
Marty666    145

NOTE: by return i mean when i do this:

GLuint myholder;
LoadTexture("filename.bmp", &myholder);

then myholder should contain a number to bind, right?

Share this post


Link to post
Share on other sites
Enokh    122
It should contain an unsigned integer that acts as an OpenGL texture ID to your texture. That way before you display your mesh or whatever that you need textured you simply tell it to bind a texture by it''s ID (UINT).

Share this post


Link to post
Share on other sites