Weird texture behaviour after switching from vertexarray to vertexbuffer+indexbuffer

Hi!

I started with OpenGL ES 2.0 about two weeks ago. In the beginning using Vertexarrays seemed fine but now that I want to load .obj files I need to work with an indexed system.

I got the vertex- and indexbuffer code from an OpenGL ES starter book.

I tried to draw a square and everything works perfectly. Then I tried to add texture coordinates to my vertexdata the same way I did before with the vertexarray. The texture on the square only consists of weird lines and I have no idea why.
Both ways use the same basic texture shader and both can draw the vertices.

The error I get says: A vertex attribute index out of boundary is detected.

This is my indexed Table class:

I would greatly appreciate any help!

Hi Andion,
Neat code.
Have you checked if the ‘stacking’ of vertex-attributes are ok? There could be a 4 byte boundary to respect …

1 Like

Note that OBJ’s indexing isn’t the same as OpenGL’s. The two major differences are:

  1. An OBJ vertex has separate indices for position, normal and texture coordinates. An OpenGL vertex has a single index.
  2. OBJ indices are one-based, OpenGL indices are zero-based (as are array indices in most modern programming languages).

So when you load an OBJ file, first you need to iterate over the list of faces and create a table of every distinct combination of position/normal/texture coordinate indices. Typically you have an associative array (e.g. std::map or std::unordered_map in C++) which maps a tuple of OBJ indices to a single OpenGL index, and an array (e.g. std::vector in C++) which holds the reverse mapping. Once you have that data, you use the forward mapping (associative array) to transform the list of faces (for each face vertex, map its tuple of OBJ indices to the corresponding OpenGL index) and the reverse mapping to generate the OpenGL attribute arrays from the OBJ arrays (which will typically be different sizes).

That’s what I’d expect if you tried to use the OBJ attribute arrays and indices as-is.

1 Like

Thanks for the reply!

I don’t think that there should be any form of limitation considering that you should be able to ‘stack’ vertex coordinates, texture coordinates, normals, etc.

If I missunderstood you, please feel free to correct me.

Do you have any other ideas?

Thank you very much for your very detailed and informative answer! This knowledge will undoubtedly be useful for me in the future.

The problem is: This is just a test of the buffer system. I am not actually loading from an OBJ file yet.

Do you have any other ideas?

hm … test out the glDrawElements(…). The elements-count might not be the number you figure. Using, thinking and talking about indices is sometimes surprisingly confusing.