Program crashing at 'glTexImage2D'

This problem is really causing me distress. The following code crashes on line 9 with basically a null pointer error.

  1. /* Bind the texture using the data in tex_data */
  2. glGenTextures(1, &NewNode->tex);
  3. glBindTexture(GL_TEXTURE_2D, NewNode->tex);
  4. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  5. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  6. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  7. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  8. printf(“glTexImage2D…”);
  9. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, NewNode->size, NewNode->size, 0, GL_RGB, GL_FLOAT, NewNode->tex_data);
  10. printf("done
    ");

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?

No, :slight_smile:

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.

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.

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];

Yeah, now that I re-read it, I see what he is trying to tell me. Thanks!