Skip to main content
GameDev.net gamedev.net
🔒 Locked

textures?

Started by CodeBox Nov 29, 2005 at 9:02 PM 6 replies 650+ views
Original Post
CodeBox
CodeBox
I have this code for my textures but if I use 30 textures at once then the computer runs out of memory, or says DIB convertion error or something! I was wondering if there was a better way or some code to stop this? Code below! Thanks for any help in advance!

GLuint LoadTexture(char *filename) { 
	GLuint texID; 

	AUX_RGBImageRec *image = auxDIBImageLoad(filename); 

	glGenTextures(1, &texID); 
	glBindTexture(GL_TEXTURE_2D, texID); 
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST); 
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); 
	gluBuild2DMipmaps(GL_TEXTURE_2D, 3, image->sizeX, image->sizeY, GL_RGB, GL_UNSIGNED_BYTE, image->data); 

	return texID; 
} 
static void background(){
	glEnable(GL_TEXTURE_2D); 
	glBindTexture(GL_TEXTURE_2D,  LoadTexture("water.bmp")); 
	glBegin(GL_QUADS); 
	glColor3f(1,1,1); 
	glTexCoord2f(10.0f, 10.0f); glVertex2d(winL,winT);
	glTexCoord2f(10.0f, 0.0f); glVertex2d(winL,winB);
	glTexCoord2f(0.0f, 0.0f); glVertex2d(winR,winB);
	glTexCoord2f(0.0f, 10.0f); glVertex2d(winR,winT);
	glEnd(); 
}

Dave Hunt
Dave Hunt
If you are calling background() every frame, then you are loading a copy of the water.bmp every frame. That's a very bad idea. You should load the texture once during initialization and save the id. Then bind it using that id in your background() function.

The same would be true of any other textures you are using. Load them once and use the saved ids to bind and draw using them in your render functions.
CodeBox
CodeBox
ok, I got this on a tutorial and I did not really understand it.

it draws like 30 images every frame I guess how do I stop this?

what is the render function?
Dave Hunt
Dave Hunt
Somewhere in your initialization code, after opengl is set up, do something like:
waterId = LoadTexture("water.bmp");

where waterId is a global variable.

Then, in your background() function, do this:
glBindTexture(GL_TEXTURE_2D,  waterId);


For each image you want to draw, create a variable for it like the waterId variable and load the image in your initialization code. Then use the appropriate variable whenever you call glBindTexture.

A "render" function would be any function where you call OpenGL drawing functions like you have in background().
CodeBox
CodeBox
OK, cool I did that I think i'll do it again now, but I did it and it was giving me an error one sec it will take me two mins to do that!

Thanks for helping!

P.s what variable formate is best? int? double? char?
Dave Hunt
Dave Hunt
Since your LoadTexture function returns a GLuint, then your variables should be GLuint as well.

By the way, have you checked out the OpenGL tutorials available at NeHe Productions? They are an excellent starting point.
CodeBox
CodeBox
What like this? If I put the GLuint below the variables it goes nuts and brings up loads of errors and at the memont I get this error?

go fatal error LNK1201: error writing to program database 'c:\Documents and Settings\Stephen Kelly\My Documents\uni work\programming\Gorillas\go\Debug\go.pdb'; check for insufficient disk space, invalid path, or insufficient privilege

GLuint LoadTexture(char *filename) { 	GLuint texID; 	AUX_RGBImageRec *image = auxDIBImageLoad(filename); 	glGenTextures(1, &texID); 	glBindTexture(GL_TEXTURE_2D, texID); 	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST); 	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); 	gluBuild2DMipmaps(GL_TEXTURE_2D, 3, image->sizeX, image->sizeY, GL_RGB, GL_UNSIGNED_BYTE, image->data); 	return texID; } GLuint waterId=LoadTexture("water.bmp");GLuint redBuildingId=LoadTexture("orangebuild.bmp");GLuint blueBuildingId=LoadTexture("water.bmp");GLuint orangeBuildingId=LoadTexture("water.bmp");


Edit*
I had a quick look but it just confused me the black background don't help neither, (i am dyslexic you see)
*
CodeBox
CodeBox
Ok, I closed and re-opened it and I don't get one error now? stupid Visual weired studio???

but there is still a problem if I have glColor3d(1,1,1); or any colour before the image it ever makes it darker or changes it really strange?

Edit*

attually it has nothing to do with that at all I had some old code and it was not doing it via the way you said anyways iyt does not seem to be working just brings up the default colour I had before?

You think I should put the GLuint variables in the init or idle?

it's p**sing me off now!!! I put them global and nothing I put them in the same file hey then bang wam slow down of the centry and poo and memory fails haha, so why when I put this outside the void do them not show?


GLuint redBuildingId=LoadTexture("redbuild.bmp");
GLuint blueBuildingId=LoadTexture("bluebuild.bmp");
GLuint orangeBuildingId=LoadTexture("orangebuild.bmp");

*

[Edited by - CodeBox on November 29, 2005 10:53:23 PM]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.