# glTexCoordPointer and memory allocation problem

This topic is 2649 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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.

##### Share on other sites
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.

##### Share on other sites
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.

##### Share on other sites
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 ?

##### Share on other sites
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.

##### Share on other sites
Thanks a lot, now I got it !

##### Share on other sites
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.

1. 1
Rutin
42
2. 2
3. 3
4. 4
5. 5

• 9
• 27
• 20
• 14
• 14
• ### Forum Statistics

• Total Topics
633384
• Total Posts
3011598
• ### Who's Online (See full list)

There are no registered users currently online

×