Question

I most recently had great progress in getting Vertex buffer objects to work. So I moved on to Element arrays and I figured with such implemented I could then load vertices and face data from an obj.

I'm not too good at reading files in c++ so I wrote a python doc to parse the obj and write 2 separate txts to give me a vertex array and face indices and pasted them directly in my code. Which is like 6000 lines but it works (without compiling errors). And Here's what it looks like

.

I think they're wrong. I'm not sure. The order of the vertices and faces aren't changed just extracted from the obj because I don't have normals or textures working for buffer objects yet. I kinda do if you look at the cube but not really.

Heres the render code

void Mesh_handle::DrawTri(){
    glBindBuffer(GL_ARRAY_BUFFER,vertexbufferid);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,elementbufferid);
    int index1=glGetAttribLocation(bound_program,"inputvertex");
    int index2=glGetAttribLocation(bound_program,"inputcolor");
    int index3=glGetAttribLocation(bound_program,"inputtexcoord");

    glEnableVertexAttribArray(index1);
    glVertexAttribPointer(index1,3,GL_FLOAT,GL_FALSE,9*sizeof(float),0);

    glEnableVertexAttribArray(index2);
    glVertexAttribPointer(index2,4,GL_FLOAT,GL_FALSE,9*sizeof(float),(void*)(3*sizeof(float)));

    glEnableVertexAttribArray(index3);
    glVertexAttribPointer(index3,2,GL_FLOAT,GL_FALSE,9*sizeof(float),(void*)(7*sizeof(float)));

    glDrawArrays(GL_TRIANGLE_STRIP,0,elementcount);
    //glDrawElements(GL_TRIANGLE_STRIP,elementcount,GL_UNSIGNED_INT,0);
}

My python parser which just writes the info into a file: source

The object is Ezreal from League of Legends

I'm not sure if I'm reading the faces wrong or if their not even what I thought they were. Am I suppose to use GL_TRIANGLE_STRIP or something else. Any hints or request more info.

Was it helpful?

Solution

First, as Andreas stated, .obj files use 1-based indices, so you need to convert them to 0-based indices.

Second:

glDrawArrays(GL_TRIANGLE_STRIP,0,elementcount);
//glDrawElements(GL_TRIANGLE_STRIP,elementcount,GL_UNSIGNED_INT,0);

Unless you did some special work to turn the face list you were given in your .obj file into a triangle strip, you don't have triangle strips. You should be rendering GL_TRIANGLES, not strips.

OTHER TIPS

Indices in obj-files are 1 based, so you have to subtract 1 from all indices in order to use them with OpenGL.

From the image for sure your verticies are messed up. It looks like you specified a stride of 9*sizeof(float) in your glGetAttribLocation but from what I can tell from your code your array is tightly packed.

glEnableVertexAttribArray(index1);
glVertexAttribPointer(index1,3,GL_FLOAT,GL_FALSE,0,0);

Also remove stride from color/texture coords.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top