PDA

View Full Version : glBindTexture() problem...



Sephiroth
12-15-2003, 08:39 PM
I have a slight problem binding to a texture for mip-map generation. It causes my app to crahs every time. Here's the source.



//basic run-down

typedef struct tagTEXDATA
{
int numTextures;
unsigned int *Texture;
} sTEXDATA;

sTEXDATA sTexData;

...
glGentextures(261, sTexData.Texture);
sTexData.numTextures = 0;
More = true;
while(More)
{
...
glBindTexture(GL_TEXTURE_2D, sTexData.Texture[sTexData.numTextures]);
...
sTexData.numTextures++;
}

Why is the bind code causing the app to crash? I'm lost on this one.

cwc36
12-15-2003, 11:08 PM
Hi,

Have you break from your while loop?

CeccOman
12-15-2003, 11:28 PM
Hiya,

you should try changing the following:

glGentextures(261, sTexData.Texture);
sTexData.numTextures = 0;

into something similar to:

for (int i=0; i<261; i++) glGentextures(i, sTexData.numTexture);

You won't have a chance to create 261 textures at once with id pointing to a pointer within a struct. Moreover you should also pay some attention at the way you are allocating the memory for the texture data (i.e. the *Texture pointer)

Cheers!

Sephiroth
12-16-2003, 03:11 AM
CeccOman, that code would crash. The "numTextures" member isn't even the right variable type, let alone a pointer. On top of that, I never heard of glGenTextures() working like a 'realloc()' function, so if I call it on a previously allocated block of memory, it'll either crash or create a new location for memory while the old one now has no pointer and floats around, unable to be freed. There's a way to alocate more than 261 textures, I mean look at Unreal II or UT2003! Both have GL support and both load a ton of textures on certain maps, now I need to figure out how, IF that's my problem.

Relic
12-16-2003, 04:37 AM
glGenTextures fills a previously allocated block of unsigned ints with texture object names, that's all.
If you haven't allocated the memory which is pointed to by sTexData.Texture you should get a crash immediately when calling glGenTextures.

If not I guess you're "More" loop is exceeding the number of allocated textures.

Repeating what CeccOman also pointed out,
valid code could look like this:
sTexData.Texture = (unsigned int *) malloc(261 * sizeof(unsigned int));
if (sTexData.Texture != NULL)
{
glGentextures(261, sTexData.Texture);
sTexData.numTextures = 261;
for (i = 0; i < sTexData.numTextures; i++)
{
glBindTexture(GL_TEXTURE_2D, sTexData.Texture[i]);
// do stuff
}
}

CeccOman
12-17-2003, 03:13 AM
You're right Sephiroth, my line doesn't make too much sense. Sorry for having mislead the solution to your problem.

Sephiroth
12-17-2003, 07:01 AM
No prob Cecco, I imagine you just glanced at it for a sec and misundersttod part of it. No biggy.

Relic, I'd never allocated the pointer, but glGenTextures wasn't crashing, so that's probably it. I'll give it a shot after work and post the results.