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));
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.
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.
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.