4

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.

Jules G.M.
  • 3,624
  • 1
  • 21
  • 35
Kaliber64
  • 586
  • 1
  • 5
  • 16
  • 2
    +1 for Modern Art. The top of this...thing... actually looks like Ezrael's hairstyle, if you use your imagination xD – SingerOfTheFall Aug 28 '12 at 10:02
  • Since your problem is almost certainly in your .obj code, rather than your rendering code, why don't you show us where your actual problem is? – Nicol Bolas Aug 28 '12 at 10:22

3 Answers3

4

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

Andreas Brinck
  • 51,293
  • 14
  • 84
  • 114
2

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.

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
  • OK so objs work in triangles I wasn't sure. Thanks a lot. I forgot that once I had the faces changed I needed to drawelements. I don't know what I was thinking with drawarrays it doesn't even use the indexes XD. minus one from faces, use triangles and draw elements. YAY! its so kaleidoscope O.o – Kaliber64 Aug 28 '12 at 10:24
  • @Kaliber64: actually, objs can have any kind of polygons, in general you have to triangulate the data first. – KillianDS Aug 28 '12 at 10:32
  • Ya I've figured a bunch out now. I found one for Quads but what is it if there are only 2 numbers per face? lines? – Kaliber64 Aug 28 '12 at 12:05
  • I rendered it as lines and it worked.Turns out I can't get blender to output objs with triangles or quads. Just wireframes. Max nor Maya will load the obj. says nothing to load :S. – Kaliber64 Aug 28 '12 at 13:05
  • Just compared the exported .obj with Blender indices turned on, and it doesn't match – June Wang Sep 05 '19 at 03:30
  • @JuneWang: They don't match what? – Nicol Bolas Sep 05 '19 at 03:39
  • @NicolBolas i.e. a face in .obj is 'f 2//1 4//1 1//1', subtract 1, you get [1 3 0] as a triangle. But in Blender, [1 3 0] isn't a triangle. – June Wang Sep 05 '19 at 03:47
  • @JuneWang: How do you know that? How are you examining the indices in *Blender* to know which vertices those map to? I wasn't aware that vertices even had indices within Blender itself; they only gain indices when you export them into some format. – Nicol Bolas Sep 05 '19 at 03:53
  • In Blender 2.8, you can turn on indices by pressing the down arrow of 'Viewport Overlay' -> Developer, and check 'indices' option, it'll visualize the indices of vertices. @NicolBolas – June Wang Sep 05 '19 at 04:11
  • @JuneWang: OK, so where in Blender's OBJ exporter is there a statement that the indices in Blender will match the indices output by the exporter? Because I see no reason why there has to be any such correlation. – Nicol Bolas Sep 05 '19 at 04:12
  • @NicolBolas There must be some correlation, otherwise how does Blender know which vertex connects to which. – June Wang Sep 05 '19 at 04:20
  • @JuneWang: "*There must be some correlation, otherwise how does Blender know which vertex connects to which.*" Triangle faces are defined by a set of 3 positions. Indices act as intermediaries between the triangle face and the positions it references. But exactly which index is used to provide the link between position and faces is arbitrary, and need not be consistent or preserved. You can provide any ordering of positions with any indexing thereof while still representing the same set of faces. – Nicol Bolas Sep 05 '19 at 04:34
  • @NicolBolas Yes, but I needed the indices of Blender/obj to be exact match(after subtract 1). Found the solution is to manually check 'keep vert order' in .obj importer/exporter to maintain the same mapping. Thanks. – June Wang Sep 05 '19 at 04:46
0

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.

vasmos
  • 2,472
  • 1
  • 10
  • 21