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 7996183
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T14:38:38+00:00 2026-06-04T14:38:38+00:00

I have got a Wavefront OBJ Parser up running but after spending endless hours

  • 0

I have got a Wavefront OBJ Parser up running but after spending endless hours trying to figure out how i load the data in OpenGL i finally had to give up.

This is how the code is built:

struct ObjMeshVertex{
    Vector3f pos;
    Vector2f texcoord;
    Vector3f normal;
};

struct ObjMeshFace{
    ObjMeshVertex vertices[3];
};

struct ObjMesh{
    std::vector<ObjMeshFace> faces;
};

ObjMesh myMesh;

for(size_t i = 0; i < faces.size(); ++i){
    ObjMeshFace face;
    for(size_t j = 0; j < 3; ++j){
        face.vertices[j].pos        = positions[faces[i].pos_index[j] - 1];
        face.vertices[j].texcoord   = texcoords[faces[i].tex_index[j] - 1];
        face.vertices[j].normal     = normals[faces[i].nor_index[j] - 1];
    }
    myMesh.faces.push_back(face);
}

Normally when i create a simple cube, the array looks something like this: {posX, posY, posZ, normX, normY, normZ}.

And by using the offset with glVertexAttribPointer its pretty simple to load the data.

But i have no clue how this is done?

EDIT
Setup:

    glGenVertexArraysOES(1, &_boxVAO);
    glBindVertexArrayOES(_boxVAO);

    int sizeOfFaces = myMesh.faces.size() * sizeof(ObjMeshFace);
    glGenBuffers(1, &_boxBuffer);
    glBindBuffer(GL_ARRAY_BUFFER, _boxBuffer);
    glBufferData(GL_ARRAY_BUFFER, sizeOfFaces, &(myMesh.faces[0]), GL_STATIC_DRAW);


    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(ObjMeshVertex), 0);
    glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(ObjMeshVertex), (void*)offsetof(ObjMeshVertex, texcoord));        
    glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE ,sizeof(ObjMeshVertex), (void*)offsetof(ObjMeshVertex, normal));

    glEnableVertexAttribArray(0);
    glEnableVertexAttribArray(1);
    glEnableVertexAttribArray(2);

    glBindVertexArrayOES(0);

Draw:

glBindVertexArrayOES(_boxVAO);
glDrawArrays(GL_TRIANGLES, 0, indicesize);  

Where indicesize = myMesh.faces.size()

  • 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-04T14:38:39+00:00Added an answer on June 4, 2026 at 2:38 pm

    If you’re just asking how to send the data to glBufferData, then you just get the pointer to the first face in myMesh, and call glBufferData on that (assuming that everything is tightly packed).

    int sizeOfFaces = myMesh.faces.size() * sizeof(ObjMeshFace);
    glBufferData(GL_ARRAY_BUFFER, sizeOfFaces, &(myMesh.faces[0]), usage);
    

    Then you can just set up the pointers to this interleaved structure:

    glVertexPointer(3, GL_FLOAT, sizeof(ObjMeshVertex), 0);
    glTexCoordPointer(2, GL_FLOAT, sizeof(ObjMeshVertex), (void*)(sizeof(Vector3f)));
    glNormalPointer(3, GL_FLOAT, sizeof(ObjMeshVertex), (void*)(sizeof(Vector3f) + sizeof(Vector2f)));
    

    One thing you might need to check is that your structs aren’t being padded (I’m less sure about how to enable/disable this).

    Just double check and assert that:

    sizeof(ObjMeshVertex) == 2 * sizeOf(Vector3f) + sizeOf(Vector2f);
    sizeof(ObjMeshFace) == 3 * sizeOf(ObjMeshVertex);
    

    If this isn’t true you might need to make some adjustments for struct padding.

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

Sidebar

Related Questions

Have got an NSString *str = @12345.6789 and want to find out if there
I have got this code: XDocument xdoc = XDocument.Load(URI); XElement root = xdoc.Element(forecast); //get
I have got a field that has been setup to allow for Null, but
I have got an application developed with Monotouch 4.0.7. This app is running on
I have got my own JAR () running via separated class loaders and one
I have got some code to load an assembly and get all types, which
have got a problem with do this kind of code , can't figure how
I have got experience working in Spring framework, but i find it very difficult
I have got one app running on two servers, one works and one doesnt
I have got a path to some external program in my pom.xml, but this

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.