# Adjacency to index buffer from trimesh

**URL:** <https://discourse.libcinder.org/t/adjacency-to-index-buffer-from-trimesh/163>\
**Category:** Using Cinder\
**Created:** [July 19, 2016, 9:40pm UTC](https://discourse.libcinder.org/t/adjacency-to-index-buffer-from-trimesh/163 "2016-07-19T21:40:43Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![mettrelapaix](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.libcinder.org/mettrelapaix/32/88_2.png) [@mettrelapaix](https://discourse.libcinder.org/u/mettrelapaix)\
**Post date:** [July 19, 2016, 9:40pm UTC](https://discourse.libcinder.org/t/adjacency-to-index-buffer-from-trimesh/163/1 "2016-07-19T21:40:43Z")

</div>

hey everyone!

I would like to generate an index buffer with adjacency information to send to a geometry shader for silhouette extraction (along the lines of openGL cookbook and P. Rideout’s blog). I have an .obj file loaded with the objLoader into a trimesh and… that’s about it at the moment. building the shader looks to be no problem. so my request is for some help generating that index buffer.

here is a screenshot of the foundation.

 ![](https://canada1.discourse-cdn.com/flex030/uploads/libcinder/original/1X/5ed910d388b2a30a5b3703d4df1f0213328b56ba.jpg)

textured plane: check.  
cel shaded bird from .obj file (complete with randomized, bird-like hopping behavior): check.  
wouldn’t it be great to help me check off stylized bold outlines?

throw me a bone, anyone?

---

<div class="post-metadata">

**Author:** ![paul.houx](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.libcinder.org/paul.houx/32/31_2.png) [@paul.houx](https://discourse.libcinder.org/u/paul.houx)\
**Post date:** [July 19, 2016, 11:20pm UTC](https://discourse.libcinder.org/t/adjacency-to-index-buffer-from-trimesh/163/2 "2016-07-19T23:20:39Z")

</div>

Hi,

a `TriMesh`, by default, is not really suitable for that kind of thing. It only supports `GL_TRIANGLES`, meaning that for each triangle there are 3 indices in the index buffer. There is no adjacency information available, because this is not stored in an OBJ file, anyway.

The way I see it you have 2 options:

1. Create your own mesh from scratch.
2. Parse the `TriMesh` one triangle at a time and try to reconstruct adjacency info from it. If two triangles share two vertices, you know they are adjacent to each other. Then build a new index buffer and set the primitive type to `GL_TRIANGLES_ADJACENCY`.

-Paul

---

<div class="post-metadata">

**Author:** ![mettrelapaix](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.libcinder.org/mettrelapaix/32/88_2.png) [@mettrelapaix](https://discourse.libcinder.org/u/mettrelapaix)\
**Post date:** [July 19, 2016, 11:55pm UTC](https://discourse.libcinder.org/t/adjacency-to-index-buffer-from-trimesh/163/3 "2016-07-19T23:55:45Z")

</div>

Thanks @paul.houx !

I see. I found something to get me started with option 1: a VBOmeshAdjacency loader that takes an obj file…

for option 2, how can I access the triangles of the trimesh? I think I have seen a method for calculating those shared edges given a vector of faces.

---

<div class="post-metadata">

**Author:** ![mike](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.libcinder.org/mike/32/56_2.png) [@mike](https://discourse.libcinder.org/u/mike)\
**Post date:** [July 20, 2016, 12:06am UTC](https://discourse.libcinder.org/t/adjacency-to-index-buffer-from-trimesh/163/4 "2016-07-20T00:06:25Z")

</div>

You can call `TriMesh::getNumTriangles` and then iterate through, calling `TriMesh::getTriangleVertices` for each triangle ID. You can also call `TriMesh::getIndices`, which will return a `std::vector<uint32_t>`, representing the indices of the mesh. You can index into this vector using the triangle ID from above.

---

<div class="post-metadata">

**Author:** ![mettrelapaix](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.libcinder.org/mettrelapaix/32/88_2.png) [@mettrelapaix](https://discourse.libcinder.org/u/mettrelapaix)\
**Post date:** [July 20, 2016, 12:14am UTC](https://discourse.libcinder.org/t/adjacency-to-index-buffer-from-trimesh/163/5 "2016-07-20T00:14:17Z")

</div>

thank you so much. this is so helpful!

---

<div class="post-metadata">

**Author:** ![mettrelapaix](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.libcinder.org/mettrelapaix/32/88_2.png) [@mettrelapaix](https://discourse.libcinder.org/u/mettrelapaix)\
**Post date:** [July 25, 2016, 5:05pm UTC](https://discourse.libcinder.org/t/adjacency-to-index-buffer-from-trimesh/163/6 "2016-07-25T17:05:57Z")

</div>

ok, so I have extracted some stuff from the objLoader and from a Rideout example to build a parser that returns a vector of indices with adjacency information (happy to share it with anyone that wants it… setting up my github now!).

from here, is it best to construct a vbomesh in cinder? or is there a way to replace the trimesh primitive type with GL\_TRIANGLES\_ADJACENCY and the indices with my new ones? either way, I can’t figure out how to build a new index buffer at the moment or what to associate it with.

---

<div class="post-metadata">

**Author:** ![paul.houx](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.libcinder.org/paul.houx/32/31_2.png) [@paul.houx](https://discourse.libcinder.org/u/paul.houx)\
**Post date:** [July 25, 2016, 6:36pm UTC](https://discourse.libcinder.org/t/adjacency-to-index-buffer-from-trimesh/163/7 "2016-07-25T18:36:09Z")

</div>

Hi,

as far as I know (and I briefly checked Cinder’s source code), a `TriMesh` only supports `GL_TRIANGLES`, so you won’t be able to change its primitive type to `GL_TRIANGLES_ADJACENCY`. Doing so would cause most of its utility functions to stop working correctly. But you can relatively easily create a `VboMesh` using the positions, normals, texcoords etc. from the `TriMesh` and the index buffer with adjacency you created from parsing.

See [this](https://github.com/cinder/Cinder/blob/583e67cc41efc7cb68c6769108ff6baa9c13e12f/include/cinder/gl/VboMesh.h#L87) `VboMesh` constructor, for example. See also the [bufferAttrib](https://github.com/cinder/Cinder/blob/583e67cc41efc7cb68c6769108ff6baa9c13e12f/include/cinder/gl/VboMesh.h#L125) method and [this sample](https://github.com/cinder/Cinder/blob/583e67cc41efc7cb68c6769108ff6baa9c13e12f/samples/_opengl/PickingFBO/src/PickingFBOApp.cpp).

-Paul

---

<div class="post-metadata">

**Author:** ![mettrelapaix](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.libcinder.org/mettrelapaix/32/88_2.png) [@mettrelapaix](https://discourse.libcinder.org/u/mettrelapaix)\
**Post date:** [July 28, 2016, 8:51pm UTC](https://discourse.libcinder.org/t/adjacency-to-index-buffer-from-trimesh/163/8 "2016-07-28T20:51:11Z")

</div>

yes!!!

the picking FBO sample was one I had overlooked. the construction method there was super helpful. so the overall process goes like this (for anyone interested):

extract vertices from ‘v’ tags in the obj file and the indices from the ‘f’ tags of the same file. both go into std::vectors.  
make a new vector mapping the vertices from the index vector:

```
    std::vector<vec3> verts=mAdjCalc.mVals.mVertices;
    std::vector<GLuint> inds=mAdjCalc.mVals.mVertIndices;
    std::vector<vec3> indexedVerts;
    for(auto idx:inds)
    {
        indexedVerts.push_back(verts[idx]);
    }

```

run the adjacency calculation from the glsl 4.0 cookbook: [https://github.com/daw42/glslcookbook/blob/master/ingredients/vbomeshadj.cpp#L17](https://github.com/daw42/glslcookbook/blob/master/ingredients/vbomeshadj.cpp#L17)

finally create the vbomesh:

```
    layout.usage( GL_STATIC_DRAW ).attrib( geom::POSITION, 3 );
    mVBOMesh=gl::VboMesh::create(indexedVerts.size(),GL_TRIANGLES_ADJACENCY,{ layout } );
    mVBOMesh->bufferAttrib( geom::POSITION, indexedVerts.size() * sizeof( vec3 ), indexedVerts.data() );

```

the geometry shader from the same book works nicely. the result without texture (I haven’t incorporated this into the main program yet):  
 ![](https://canada1.discourse-cdn.com/flex030/uploads/libcinder/original/1X/361763a0cedae356d3cae353b352f76efd78c1ba.jpg)

big thanks to @paul.houx and @mike for help on this one!

---

<div class="post-metadata">

**Author:** ![paul.houx](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.libcinder.org/paul.houx/32/31_2.png) [@paul.houx](https://discourse.libcinder.org/u/paul.houx)\
**Post date:** [July 29, 2016, 12:07am UTC](https://discourse.libcinder.org/t/adjacency-to-index-buffer-from-trimesh/163/9 "2016-07-29T00:07:20Z")

</div>

Congratulations! Perhaps we should add this adjacency stuff to Cinder, might come in handy. A `gl::VboMeshAdj` that takes a `TriMesh`, `gl::VboMesh` or even a `geom::Source` as input, for instance.

---

<div class="post-metadata">

**Author:** ![mettrelapaix](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.libcinder.org/mettrelapaix/32/88_2.png) [@mettrelapaix](https://discourse.libcinder.org/u/mettrelapaix)\
**Post date:** [July 29, 2016, 4:09pm UTC](https://discourse.libcinder.org/t/adjacency-to-index-buffer-from-trimesh/163/10 "2016-07-29T16:09:03Z")

</div>

for sure, I would be happy to contribute there! I actually tried to cobble together something from the trimesh and vbomesh sources at first but got a little lost.

---

<div class="post-metadata">

**Author:** ![khuxley](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.libcinder.org/khuxley/32/91_2.png) [@khuxley](https://discourse.libcinder.org/u/khuxley)\
**Post date:** [August 2, 2016, 9:03am UTC](https://discourse.libcinder.org/t/adjacency-to-index-buffer-from-trimesh/163/11 "2016-08-02T09:03:56Z")

</div>

Hi @mettrelapaix,

Congratulations on solving this difficult problem. I’ve been Googling and hammering away for many hours trying to come up with a similar solution for shape2d outlines in Cinder PoScene.

I’ve created a post describing the problem I’m having and how far I’ve gotten solving it: [Outline shape2d VboMesh with stroke in PoScene](http://discourse.libcinder.org/t/outline-shape2d-vbomesh-with-stroke-in-poscene/191).

Do you have a Cinder project demonstrating your solution that you’d be willing to share? I read through your post and the logic and code snippets make sense but I’m not sure how to put it all together, especially integrating the adjacency calculation code and it’s dependencies from the glsl 4.0 cookbook into my Cinder project.

Many thanks for any help on this.

Kind regards,

Ken
