glTexCoordPointer and memory allocation problem

Started by
5 comments, last by rip-off 12 years, 8 months ago
There is something that puzzles me about using glTexCoordPointer and the way I allocate memory. Take a look at the following code:


float **terrainTexCoords; // dynamic array
float terrainTexCoords2[128*128][2]; // static array

terrainTexCoords = new float*[gridSize*gridSize];
for(int i = 0; i < gridSize*gridSize; ++i)
{
terrainTexCoords = new float[2];
} // this initialize the dynamic array


// now I fill both the static and dynamic array with values
for (int i=0; i<gridSize; i++)
for (int j=0; j<gridSize; j++)
{
terrainTexCoords[i*gridSize+j][0]=(float(j+0)/float(gridSize-1));
terrainTexCoords[i*gridSize+j][1]=1.0 - (float(i+0)/float(gridSize-1));

terrainTexCoords2[i*gridSize+j][0]=(float(j+0)/float(gridSize-1));
terrainTexCoords2[i*gridSize+j][1]=1.0 - (float(i+0)/float(gridSize-1));
}

Now, if I use the static array glTexCoordPointer(2, GL_FLOAT, 0, terrainTexCoords2); everything renders fine, if I use the dynamic array glTexCoordPointer(2, GL_FLOAT, 0, terrainTexCoords); texture coordinates are messed up.

I'm puzzled because as I wrote earlier, both arrays have the same identical values.
Advertisement
glTexCoordPointer, and any other other vertex array functions, take a pointer to a continuous memory block that contains your vertex data. It does not take a pointer to an array of pointers to the array data. The two memory layouts you have are not the same, and the static array one is the correct layout. You have to drop the second layer of pointers from the dynamic array, because OpenGL can't handle it.
The two are completely different in terms of memory layout. For "dynamic array", terrainTexCoords points to an array of pointers to float arrays. For "static array", terrainTexCoords2 is a dense contiguous array of floats.
Thanks for the clarification. And if I redesign the dynamic array making it monodimensional, like this:

float *terrainTexCoords; // dynamic array
terrainTexCoords = new float*[gridSize*gridSize*2];

Would it work ?
Yes. Although drop the asterisk in the type you allocate; you want to allocate an array of float, not an array of float pointers.

But you don't have to make the type one dimensional just to get continuous memory.
[source]
float (*terrainTexCoords)[2] = new float[128*128][2];
[/source]
That will allocate an array of 128*128 arrays of 2 floats, just like your static array, but the memory is dynamic instead static. The limitation, though, is that only the first dimension can be of dynamic size, the rest has to be of static size.
Thanks a lot, now I got it ! :wink:
You can also use std::vector, and avoid memory leaking and get bounds checking in Debug mode:

std::vector<float> terrainTexCoords(128 * 128 * 2);

// Or

struct TexCoord
{
float u, v;
};

std::vector<TexCoord> terrainTexCoords(128 * 128);

You can use &terrainTexCoords.front() to get a pointer to the data, which is guaranteed to be contiguous.

This topic is closed to new replies.

Advertisement