Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8900143
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T01:02:10+00:00 2026-06-15T01:02:10+00:00

I’m learning vertex array objects in OpenGL. Running codeblocks with mingw gcc as my

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T01:02:11+00:00Added an answer on June 15, 2026 at 1:02 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I am currently running into a problem where an element is coming back from
I have a text area in my form which accepts all possible characters from
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I have a bunch of posts stored in text files formatted in yaml/textile (from
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have an array which has BIG numbers and small numbers in it. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.