简体   繁体   中英

Passing A Two Dimensional Array Into A Function That Only Takes A One Dimensional Array (C++)

I'm learning vertex array objects in OpenGL. Running codeblocks with mingw gcc as my compiler. I loaded data from a text file using this (just a bunch of quads to be used as walls)

void setup_world()                                                          // Sets up the world using the world.txt file
{
    float x, y, z, u, v;
    FILE *filein;
    char oneline[255];
    filein = fopen("Data/world.txt", "rt");
    readstr(filein, oneline);
    sscanf(oneline, "NUMPOLLIES %d\n", &numpolygons);
    int cols = 13;
    int rows = numpolygons+1;
    polygondata = new float*[rows];
    for(int i = 0; i < rows; i++)
        polygondata[i] = new float[cols];
    polygondata[0][0] = 69.01;
    for (int loop = 0; loop < numpolygons; loop++)
    {
        readstr(filein, oneline);
        sscanf(oneline, "%f %f %f %f %f %f", &x, &y, &z, &u, &v);
        polygondata[loop][0] = x;
        polygondata[loop][1] = y;
        polygondata[loop][2] = z;
        readstr(filein, oneline);
        sscanf(oneline, "%f %f %f %f %f %f", &x, &y, &z, &u, &v);
        polygondata[loop][3] = x;
        polygondata[loop][4] = y;
        polygondata[loop][5] = z;
        readstr(filein, oneline);
        sscanf(oneline, "%f %f %f %f %f %f", &x, &y, &z, &u, &v);
        polygondata[loop][6] = x;
        polygondata[loop][7] = y;
        polygondata[loop][8] = z;
        readstr(filein, oneline);
        sscanf(oneline, "%f %f %f %f %f %f", &x, &y, &z, &u, &v);
        polygondata[loop][9] = x;
        polygondata[loop][10] = y;
        polygondata[loop][11] = z;
    }
    fclose(filein);
    return;
}

I know it's kind of redundant but it will do until I get around to rewriting the part that stores all the data into the array.
Typically, data is passed to OpenGL for vertex array objects using

glBufferData(GL_ARRAY_BUFFER, sizeof(g_vertex_buffer_data), g_vertex_buffer_data, GL_STATIC_DRAW);

OpenGL would usually take in an array similar to this, generating a quad

float g_vertex_buffer_data[] =
{
    -1.0, -1.0,-5.0,
    1.0, -1.0, -5.0,
    1.0,  1.0, -5.0,
    -1.0,  1.0, -5.0,
};

The function expects g_vertex_buffer_data to be a one-dimensional arrary, however i stored all my vertex data into a two dimensional array, the first dimension used for storage of several polygons (in this case QUADS) and the second dimension is used to store all 12 vertexs of for a quad.

What would be the best way to approach this?

A proposed solution that i came up with was creating another array (single dimension, however) that would temporarly store the vertex data from one polygon at a time. Directly after I would pass this single dimension array to the glBufferData function (since glBufferData has no problem receiving single dimension arrays). I would repeat this for every polygon effectively traversing my entire two dimensional array and passing the to the function data into glBufferData polygon by polygon. I know im going to have to put each polygon in a seperate vertex buffer no need to remind me. I just need to know if it's possible to pass in one part of my two dimensional array!
Is there an easier and more efficient way to go about doing this.

Here's a much better idea that doesn't involve... what you did.

std::vector<GLfloat> polygondata;
polygondata.reserve(numpolygons * 4 * 3);
for (int loop = 0; loop < numpolygons * 4 * 3; loop++)
{
    readstr(filein, oneline);
    sscanf(oneline, "%f %f %f %f %f %f", &x, &y, &z, &u, &v);
    polygondata.push_back(x);
    polygondata.push_back(y);
    polygondata.push_back(z);
}

Not only are pointless memory allocations removed, you now have your data in a 1D array, which you can index in a 1D fashion. If you want to access vertex X of polygon Y, you use Y * numpolygons + X for the index. And you can upload the data to OpenGL using a simple glBufferData(..., sizeof(GLfloat) * polygondata.size(), &polygondata[0], ...) command.

You can pass just 1 row of your 2D array with giving one of two indices, It will then refer to just that particular polygons data.

You can see similar usage at https://stackoverflow.com/a/13427008/1520364

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM