Jump to content
  • Advertisement
Sign in to follow this  
MARS_999

BufferDataARB and multidim arrays

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am creating a dynamic multidim array such as Array **array; and I have it setup correctly I am looking at the spec for BufferDataARB() and it says only const void *data? Will sending a 2 or 3 dimension array still work? And if so does it work cause its continuious memory and not broke up into diffferent addresses? Thanks
void BufferDataARB(enum target, sizeiptrARB size, const void *data,
                       enum usage);


Share this post


Link to post
Share on other sites
Advertisement
the memory for the inital array of pointers will be contigous, yes, however each element points to another section of memory, OpenGL isnt expect that so you cant just pass the array in, you'd have to pass each element of the array in instead.

Share this post


Link to post
Share on other sites
Quote:
Original post by phantom
the memory for the inital array of pointers will be contigous, yes, however each element points to another section of memory, OpenGL isnt expect that so you cant just pass the array in, you'd have to pass each element of the array in instead.


Ok, you lost me. ;) Here is what I have


glBufferDataARB(GL_ARRAY_BUFFER_ARB, vbo_object_size_array, &terrain[0][0], GL_STATIC_DRAW_ARB);



and here is my terrain pointer


Vertex **terrain;


and yes I have allocated it correclty. I think my issue is with the bufferData function. When I called the function with this array type

Vertex terrain[129][129];


it ran fine...

Share this post


Link to post
Share on other sites
the two declartions of terrain are functionally the same when it comes to retriving the data, the only difference is one is fixed in size and the other is dynamically allocated.

Personally, I avoid 2D arrays as much as I can, just allocate it as a 1D array and work out the offset as given to you in your other thread about 2D arrays in the General programming forum,iirc.

Share this post


Link to post
Share on other sites
So you are saying that I would have to do this


glBufferDataARB(GL_ARRAY_BUFFER_ARB, vbo_object_size_array, &terrain[0][0], GL_STATIC_DRAW_ARB);
glBufferDataARB(GL_ARRAY_BUFFER_ARB, vbo_object_size_array, &terrain[0][1], GL_STATIC_DRAW_ARB);
glBufferDataARB(GL_ARRAY_BUFFER_ARB, vbo_object_size_array, &terrain[0][2], GL_STATIC_DRAW_ARB);





That would take a ton of calls to send the whole array into my VBO

Share this post


Link to post
Share on other sites
Well, thats why 'for' loops exist, however it wouldnt do what you want anyways, as it would overwrite the data each time with that call, if you wanted todo it like that then glSubBufferData() is the call you want, however as I've said, using a normal flat array (or even an std::vector) is pretty much a better way to go as its easier to work with and you can upload the data in one go.

Share this post


Link to post
Share on other sites
Quote:
Original post by phantom
Well, thats why 'for' loops exist, however it wouldnt do what you want anyways, as it would overwrite the data each time with that call, if you wanted todo it like that then glSubBufferData() is the call you want, however as I've said, using a normal flat array (or even an std::vector) is pretty much a better way to go as its easier to work with and you can upload the data in one go.


Ugh... Thats the whole problem. I dont' want to use one dim, due to all my code is based on the terrain[][] to make looking for data for a specific point easier... So the only way with dynamic memory is a single array and you have to offset it like this?

terrain[z * arraySize + x]


Share this post


Link to post
Share on other sites
yep, thats just how you do it, its just as easy using the 2D method but you dont have to deal with pointers to pointers and the lack of contigous memory allocation when uploading data.

Share this post


Link to post
Share on other sites
Hmm not working get a crash when I try to setup the VBOS now...

terrain[z * mapData.map_Z + x].vx = x;
terrain[z * mapData.map_Z + x].vz = z;



Thats how I am setting up my array for my terrain mesh

glBufferDataARB(GL_ARRAY_BUFFER_ARB, vbo_object_size_array, terrain, GL_STATIC_DRAW_ARB);

is the call to upload the data

Share this post


Link to post
Share on other sites
Just write a thin wrapper to give a one-dimensional array a two-dimensional interface:
#include <iostream>
#include <iterator>
#include <vector>

template < typename TYPE >
class twodarray
{

private:

class reference
{

public:

reference(std::vector< TYPE > & data, unsigned int index)
:
data_(data),
index_(index)
{
}

TYPE & operator[](unsigned int index)
{
return data_[index_ + index];
}

TYPE const & operator[](unsigned int index) const
{
return data_[index_ + index];
}

private:

std::vector< TYPE > & data_;
unsigned int index_;

};

public:

twodarray(unsigned int width, unsigned int height)
:
data_(width * height),
height_(height)
{
}

reference operator[](unsigned int index)
{
return reference(data_, height_ * index);
}

reference const operator[](unsigned int index) const
{
return reference(data_, height_ * index);
}

TYPE * array()
{
return &data_[0];
}

TYPE const * array() const
{
return &data_[0];
}

private:

std::vector< TYPE > data_;
unsigned int height_;

};

int main()
{
twodarray< float > mytwodarray(8, 16);
for (int i = 0; i < 8; ++i)
{
for (int j = 0; j < 16; ++j)
{
mytwodarray[j] = (i * 1000) + j;
}
}
std::copy(mytwodarray.array(), mytwodarray.array() + (8 * 16), std::ostream_iterator< float >(std::cout, " "));
}

Enigma

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!