PDA

View Full Version : Program crashing at 'glTexImage2D'



opcode
03-09-2004, 12:16 PM
This problem is really causing me distress. The following code crashes on line 9 with basically a null pointer error.

01) /* Bind the texture using the data in tex_data */
02) glGenTextures(1, &NewNode->tex);
03) glBindTexture(GL_TEXTURE_2D, NewNode->tex);
04) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
05) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
06) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
07) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
08) printf("glTexImage2D...");
09) glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, NewNode->size, NewNode->size, 0, GL_RGB, GL_FLOAT, NewNode->tex_data);
10) printf("done\n");

This never happened before when I was using a compile time three-demensional array such as tex_data=float[512][512][3], However, I wanted them to be dynamic so I wrote the code below to deal with that.

/* Use calloc to build a dynamic three-demensional array */
NewNode->tex_data=(float***)calloc(NewNode->size, sizeof(float*));
for(hh=NewNode->size-1;hh>=0;hh--) {
NewNode->tex_data[hh]=(float**)calloc(NewNode->size, sizeof(float*));
for(ww=0;ww<NewNode->size;ww++) {
NewNode->tex_data[hh][ww]=(float*)calloc(3, sizeof(float));
}
}

The runtime allocated array is accessable just like a compile time array, however, when I send it to glTexImage2D it crashes. If I switch back to the compile time array the program does not crash.

So, why would glTexImage2D not like the array I sent to it? Is their some difference between a three-demensioanl array to float *** of the same sort?

dorbie
03-09-2004, 12:53 PM
No, :-)

The memory allocation for the image data is simply newnode->size * newnode->size * etc..

It's not an array of pointers it's a block of contiguous data representing an array with a single pointer.

opcode
03-09-2004, 01:02 PM
Arg, I was afraid of that. Thanks I guess I'll tinker with trying to figure out how dynamically create the arrays of arrays of arrays which I need. Unless, of course, you have the solution hehe.

Aeluned
03-09-2004, 02:07 PM
The solution was actually given above...you don't need an array of pointers, just one large continguous chunk of data.

So declare it like this NewNode->data = new BYTE[NewNode->size*NewNode->size];

opcode
03-09-2004, 03:57 PM
Yeah, now that I re-read it, I see what he is trying to tell me. Thanks!