Sign in to follow this  
Alessandro

glTexCoordPointer and memory allocation problem

Recommended Posts

There is something that puzzles me about using glTexCoordPointer and the way I allocate memory. Take a look at the following code:


[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[i] = 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));
}
[/code]
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
You can also use std::vector, and avoid memory leaking and get bounds checking in Debug mode:
[code]
std::vector<float> terrainTexCoords(128 * 128 * 2);

// Or

struct TexCoord
{
float u, v;
};

std::vector<TexCoord> terrainTexCoords(128 * 128);
[/code]
You can use &terrainTexCoords.front() to get a pointer to the data, which is guaranteed to be contiguous.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this