0

I have two different buffers (QBuffer), one containing vertex coordinates and second one containing vertex normals. In some cases they are organized the same so I can use one index buffer, but is it possible to use 2 different index buffers if data is organized somehow different? One index buffer for vertex coordinates and second index buffer for normals. Maybe even third buffer for UV coordinates.

Reason: plane or triangle has same amount of vertices and normals so one index buffer should do the job, but basic cube with 8 vertices has 24 normals. Every vertex has 3 different normals for each face it is part of, so in this case I would need second index buffer for normals.

I would like to do something like

custom_geo.addAttribute(vtx_pos_attribute)          // vertex coordinates
custom_geo.addAttribute(vtx_pos_index_attribute)    // index buffer for vertex coordinates
custom_geo.addAttribute(vtx_normal_attribute)       // normals
custom_geo.addAttribute(vtx_normal_index_attribute) // index buffer for normals

but I think that my second index attribute just overrides previous one. Anyone has any idea how to fix this? Maybe this is not fixable, and only one index buffer is supported?

eyllanesc
  • 190,383
  • 15
  • 87
  • 142
  • Have you looked at the [Qt code to construct a cuboid mesh](https://github.com/qt/qt3d/blob/dev/src/extras/geometries/qcuboidgeometry.cpp)? 1. It seems like they use tangents instead of 3 normals. 2. Couldn't you just define 3 normal attributes? I don't understand how this would be a problem. Name the attributes, define their stride and continue to use the "normal" index buffer. – Florian Blume Mar 03 '21 at 09:07
  • If I am not mistaken they just repeat the vertex coordinates, just as I'm doing right now. I have vertex coords buffer and normals buffer and index buffers for each. There are 8 vertices and 24 normals. I don't want to combine those 2 buffers in one buffer and repeat each vertex coord 3 times. If that is necessary, I would just write VBO from scratch and ignore their API completely. I don't think they didn't think about it, so I'm probably missing something – Nemanja Stojanovic Mar 03 '21 at 10:32
  • I'm not sure I can follow what you're saying. They don't repeat anything. They create each vertex once: position, tex, normal & tangent. **All in one buffer** (`m_vertexBuffer`). Then they index into that buffer using their index buffer. You don't have to repeat anything either. Just add the vertex 3D coordinates followed by its 3 normals to the vertex buffer and then the next one. Normal 1 attribute then has a certain offset, normal 2 attribute has another offset and normal 3 again another one (into the vertex buffer). – Florian Blume Mar 03 '21 at 10:42
  • Ah ok I think I understand what you mean, the vertices on the edges are duplicated? – Florian Blume Mar 03 '21 at 10:47
  • Yes. Each vertex coord is repeated 3 times in this case. If I could use 2 different index buffers for vertex coord buffer and normals buffer then this would be very simple, but if have to merge these two buffers then it is much better not to use their API at all. – Nemanja Stojanovic Mar 03 '21 at 10:59
  • I don't think each vertex is repeated 3 times but the ones on the edges are present twice and corner vertices are present 3 times. So I guess it's a question of whether you'd be willing to have those vertices in the buffer multiple times not duplicating all vertices. Of course, in your example where you only define the outline of the cube you have to repeat the vertices three times but the amount of data in total is still negligible. – Florian Blume Mar 03 '21 at 13:15
  • I'm also wondering how you would use two separate index buffers with OpenGL (which is the underlying driver of Qt3D, at least for now), because [`glDrawElements`](https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/glDrawElements.xhtml) only accepts one index buffer. Other than that regarding my previous comment I would think it to be an extreme move to not use Qt3D because you have to duplicate vertices on the edges (assuming that you want to use it because of its flexibility, etc.). – Florian Blume Mar 03 '21 at 13:18
  • Yeah, I think that is the limitation of OpenGL. But I'm not sure why Qt 3D offers an option to have different separate buffers for coords and normals and uvs if they don't allow more index buffers. It is like a half feature. We should just write VBO then. The reason why I have this issue is that I want to create meshes procedurally and apply modifiers multiple times and if this is the case, I would spend more time on these organization of data than actually creating vtx_pos, normals and uvs buffers in the first place – Nemanja Stojanovic Mar 03 '21 at 15:06
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/229468/discussion-between-florian-blume-and-nemanja-stojanovic). – Florian Blume Mar 03 '21 at 19:04

0 Answers0