Jump to content
  • Advertisement
Sign in to follow this  
Andrew1979

Loading Textures...help

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


int LoadGLTextures()									// Load Bitmaps And Convert To Textures
{
	int Status=FALSE;									// Status Indicator
	int count = 0;
	int texture_count = 0;

	AUX_RGBImageRec *TextureImage[MAX_TEXTURES];		// Create Storage Space For The Texture

	memset(TextureImage,0,sizeof(void *)*1);           	// Set The Pointer To NULL

	// Load bitmaps for texture mapping
	
	// Load grass texture
	TextureImage[0] = LoadBMP("../Textures/grass.bmp");

	// Create the textures
	glGenTextures(1, &texture[0]);
	
	// Typical Texture Generation Using Data From The Bitmap
	glBindTexture(GL_TEXTURE_2D, texture[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);
		
		if (TextureImage[0])									// If Texture Exists
		{
			if (TextureImage[0]->data)							// If Texture Image Exists
			{
				free(TextureImage[0]->data);					// Free The Texture Image Memory
			}

			free(TextureImage[0]);								// Free The Image Structure
		}
		
		count++;
	
return 1;										// Return The Status
}


The above function is called from my InitGL function and slows down my program dramatically to unplayable speed. I only get slight improvement using Vertex arrays and Display Lists. When I remove the calling of the above function, my program runs at perfect speed. What am I missing when doing the above? What extra do I need to do? Do I just need to call the above function somewhere else other than InitGL function? Thank you in advance for suggestions. Andrew

Share this post


Link to post
Share on other sites
Advertisement
Can you check if the texture itself is loaded properly? If the speed drops that much there must be an error somewhere. Make sure the sizeX/Y are ok. In case you use old hardware, you might need to have "Power Of Two" dimensions as well (2,4,8,16,32,64,128,256, ...).

I don't know if it helps, but you might need to enable/disable GL_TEXTURE_2D while creating the texture:

glEnable( GL_TEXTURE_2D );
glBindTexture(GL_TEXTURE_2D, texture[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);
glDisable( GL_TEXTURE_2D );


Another possible bug, the third parameter in glTexImage2D is set to 3... I don't think that's a proper format. Use the target format, GL_RGB(A), for example.

Greetings,
Rick

Share this post


Link to post
Share on other sites
The texture loads and maps correctly onto the object I'm wanting to go it onto however it is in that function that slows it down. I remove the function and get no texture as they would not be loaded but program runs at normal speed. It's really fustrating.

Share this post


Link to post
Share on other sites
Have you tried changing the third parameter to GL_RGB(A) (or eventually a floating point format), and enable/disable GL_TEXTURE_2D before and after generating the texture?

Rick

Share this post


Link to post
Share on other sites
Yes tried both and both failed, however I have identified the exact lines causing the slow down. However if I remove them I don't get my textures. The culpret lines are:



glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);


Share this post


Link to post
Share on other sites
Strange. Recently I had problems with textures as well. In some cases mipmaps were not supported, but you don't use them. The lines you are pointing out should not do any 'damage' normally. In my case it went wrong because the wrong target was binded at the time before loading (GL_TEXTURE_3D instead of 2D).

I don't think this will help much too, but you can try to place these 2 lines before "glTexImage2D". Your complete code could look something like this:

glEnable( GL_TEXTURE_2D );
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);

glGenTextures(1, &texture[0]);
glBindTexture(GL_TEXTURE_2D, texture[0]);

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
TextureImage[0]->sizeX, TextureImage[0]->sizeY,
0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[0]->data);

glDisable( GL_TEXTURE_2D );


Since the texture itself is ok, I guess the width/height, formats and data buffers are all ok. What are you doing before this loading procedure starts? Have you already created or messed around with buffers/textures? Sometimes the previous state is not finished/closed properly, and therefore can mess up everything that follows.

Rick

Share this post


Link to post
Share on other sites
I'm guessing spek is correct when he suggested you may be using non-power-of-2 textures. What are the dimensions of the image you are trying to load?

Share this post


Link to post
Share on other sites
256 x 256 is fine. Nowadays you can use any resolution. However, when using older hardware there were restrictions. You could still use other sizes, but that required the GL_TEXTURE_RECT extension. But I don't think you'll have to worry about it, unless you using an ancient or a really exotic videocard...

I assume you tried all the tricks above. Especially that format thing. In your first attempt you tried to use '3', which is wrong (unless the constant GL_RGB == 3, but I don't think so). I think something is going wrong before you start creating that texture. Can you write down the current state of the texture generation part, and explain what you do before the texture loading starts? I'm asking because you'll often see stupid little bugs while writing it down. At least, that always happens to me :) Checked my code a billion times, get desperate and ask on gamedev, then suddenly notices a tiny bug while writing the code down...

Rick

Share this post


Link to post
Share on other sites
Now I can be more specific:

glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);

This line of code seems to cause my program to slow down but without I don't get my textures.

My texture images are 256 x 256 in dimensions. The texture loads correctly but as I have said, there is a very severe slow down in the program because of this (unplayable) even when using vertex arrays and display lists.

The code that the function is shown in, is the first and only function to deal with setting up textures. Displaying them is done via display maps.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

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

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!