Jump to content
  • Advertisement
Sign in to follow this  
Flopid

How to Pass Arrays to Functions for OGL?

This topic is 2569 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

Hello I am having a bit of confusion with C++ and passing a 1D array for vertices into a function when it will be used to fill in the vertex buffer. If I just do this:


{
GLfloat aVertices[] = {
-0.8f, -0.8f, 0.0f, 1.0f,
0.0f, 0.8f, 0.0f, 1.0f,
0.8f, -0.8f, 0.0f, 1.0f
};

...

CreateVBO(p_vertices, aVertices);

}

void CreateVBO(Vertex* theVerts, GLfloat theGLfloat[])
{
...
glBufferData(GL_ARRAY_BUFFER, sizeof(theGLfloat), theGLfloat, GL_STATIC_DRAW);
...
}


The triangle will not render!!!

but if I insert this code inside the CreateVBO function it works:


void CreateVBO(Vertex* theVerts, GLfloat theGLfloat[])
{
GLfloat newTestVerArr[12];

for(int i = 0; i < 12; i++)
{
newTestVerArr = theGLfloat;
}
...
glBufferData(GL_ARRAY_BUFFER, sizeof(newTestVerArr), newTestVerArr, GL_STATIC_DRAW);
...
}


It seems like a waste of processing power to copy an array again, so I was wondering if there is a syntax I could use to skip this copy loop. I am confused, since as far as I know the "newTestVerArr" is just a pointer to the first element in the array, just like the "theGLfloat" so why it won't work without the copy I am not sure. If you could explain this to me I would appreciate it.

Thanks

Share this post


Link to post
Share on other sites
Advertisement
sizeof() is a compile time construct. When you do sizeof on theGlFloat, you get the size of a pointer, not the size of whatever array you passed to your function. One way to handle this is to pass the array size as an additional argument, or pass a pointer or reference to an array class that contains the size like std::vector.

Share this post


Link to post
Share on other sites
Hidden
It's likely the sizeof operator that's causing your problem. When you pass an array to a function, the function doesn't know how big the array is, its just a pointer to a data block. When you call sizeof on that pointer, it just returns the size of the pointer (4/8 bytes dep. on architecture), not the number of elements in your array.

You should either explicitly pass in the number of elements into your function, or store them in some kind of container which tracks its own size. As it stands your array size information is not transmitted to the function.

Share this post


Link to post
Generally what you'd do is something like this:

// in a real program these wouldn't be globals but let's do it this way for demo purposes
GLfloat g_verts[] = {.... vertex data here ....};
int g_vertexsize = whatever; // size of each vertex in bytes; 4 bytes per float, 3 floats for position, 2 for a typical texcoord, 3 for a normal, etc
int g_numverts = whatever; // number of vertexes

// blah blah blah more stuff here

void CreateVBOGeneric (int numverts, int vertexsize, void *data)
{
// gen, bind, etc
// !!! should also validate params !!!
glBufferData(GL_ARRAY_BUFFER, numverts * vertexsize, data, GL_STATIC_DRAW);
// anything else you need to do here
}

void SetupMyStuff (void)
{
// setup some stuff
CreateVBOGeneric (g_numverts, g_vertexsize, g_verts);
// setup some more stuff
}

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!