I've finally figured out how to get the data from a .obj file and store the vertex positions x,y,z into a structure called Points with members x y z which are of type float. I want to know how to get this data onto the screen.

Here is my attempt at doing so:

//make a fileobject and store list and the index of that list in a c string
ifstream file (list[index].c_str() );

Point p;
int face[4];

while ( !file.eof() ) 
    char  modelbuffer[10000];
    //Get lines and store it in line string
    file.getline(modelbuffer, 10000);
        case 'v' :
            sscanf(modelbuffer, "v %f %f %f",  &p.x, &p.y, &p.z);
            cout << "Getting Vertex Positions" << endl;
            cout << "v" << p.x << endl;
            cout << "v" << p.y << endl;
            cout << "v" << p.z << endl;
        case 'f':
            sscanf(modelbuffer, "f %d %d %d %d", face, face+1, face+2,  face+3 );
            cout << face[0] << endl;
            cout << face[1] << endl;
            cout << face[2] << endl;
            cout << face[3] << endl;
    GLuint vertexbuffer;
    glGenBuffers(1, &vertexbuffer);
    glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
    glBufferData(GL_ARRAY_BUFFER, points.size(), points.data(), GL_STATIC_DRAW);
    //glBufferData(GL_ARRAY_BUFFER,sizeof(points), &(points[0]), GL_STATIC_DRAW);

    glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 0, 0);
    glVertexPointer(3, GL_FLOAT, sizeof(points),points.data());
    glIndexPointer(GL_DOUBLE, 0, faces.data());
    glDrawArrays(GL_QUADS, 0, points.size());
    glDrawElements(GL_QUADS, faces.size(), GL_UNSIGNED_INT,  faces.data());

As you can see I've clearly failed the end part but I really don't know why its not rendering the data onto the frustum? Does anyone have a solution for this?


2 Answers 2


I'm going to leave my other answer in place, because I think it's more valuable general advice, and people can consider it separately.

Looking in more detail at your code raises the following questions, all of which need answered before you'll get working code.

  • Why are you calling glDrawArrays and glDrawElements? You should be doing one or the other, depending on how you've organised your data.
  • Why are you performing a draw every line? Only some lines (faces) contain primitives that you can actually draw, and even then you want to collect them all up then render, not add one, then render, then add another, then render. I'm going to assume that everything from the 'vertexbuffer' declaration is actually supposed to be outside the loop and it's just a typo transcribing into the question, because otherwise the code is just broken.
  • In glBufferData, why are you passing the number of points instead of the size of the data buffer (in bytes) as specified?
  • In glVertexAttribPointer, why are you telling it that the Point structure consists of 4 GL_FLOATs when you only have 3?
  • In fact, why are you using glVertexAttribPointer at all (given that you've not called glEnableVertexAttribArray to enable it, and you go on to use glVertexPointer)
  • Why are you telling glVertexPointer that you have a stride equal to the size of the points array in bytes, instead of the 0 bytes you actually have?
  • Have you enabled the vertex array client state using glEnableClientState(GL_VERTEX_ARRAY, GL_TRUE) somewhere else? And GL_INDEX_ARRAY?
  • In glDrawArrays, why are you passing the number of vertices, instead of 4 * the number of quads?
  • Why are your indices ints, and not unsigned ints like you're telling glDrawElements?

All of these I found in 15 minutes just looking at the OpenGL function reference. I think you'd be better served by stepping back and going through a few tutorials which illustrate the use of vertex / index arrays and other basic GL functions before you jump to trying to write a .OBJ renderer. Alternatively I'd recommend Googling for other existing OpenGL .OBJ implementations (of which there must be dozens) to look at for reference.


In every case where I have some bare bones geometry rendering but no real infrastructure, it's been something simple and obvious, and the geometry rendering part has actually been fine. Check:

  • Your camera /view matrix is actually looking at the object (move either the object or your camera around until you see something - a dirty animation loop where you orbit the camera or object will usually suffice)
  • Your projection matrix is sensible (FOV isn't zero, or just plain uninitialised)
  • Your scene is lit (you're not rendering a black object on a black background - change your background colour)
  • \$\begingroup\$ I tried all those things I believe I'm not doing the rendering right with the functions messing something up somewhere but not really knowing what I'm messing up. \$\endgroup\$ Apr 9, 2012 at 22:33

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .