Page 1 of 1

How would I write an algorithm to convert a OBJ face to triangles?

Posted: Tue Jun 29, 2021 6:54 am
by ThatCodingGuy78
As the title says, how would I write an algorithm to convert a OBJ face to triangles?

Example: You have a cube.obj file that has 6 faces, but they have 4 vertexes (Per face), and you can only render in 3D triangles. How would you convert the data to a set of points and lines defined as the points to connect?

Here's my .love file in case it's relevant
Game Name.love
(9.74 KiB) Downloaded 181 times
(BTW, the rendered shape when you execute the .love file is only getting the vertex positions, the lines are predefined so it actually renders)

Re: How would I write an algorithm to convert a OBJ face to triangles?

Posted: Tue Jun 29, 2021 9:04 am
by grump
Some general pointers, may or may not apply to your problem:

A quad on a plane is trivially converted to two triangles

Code: Select all

0---3
|   |
1---2
{ 0, 1, 3 } is triangle #1, and { 1, 2, 3 } is triangle #2. In your cube example you would ideally represent this as an indexed triangle list, to keep the number of vertices low.

Any convex polygon on a plane is easily triangulated using fanning: insert a vertex at center of polygon, then go around counter-clockwise and connect vertices to center vertex.

There's also love.math.triangulate that you could use for more complex cases, but that has its limitations.

The best and common solution is to only export triangulated meshes so you don't have to deal with triangulation yourself. Every 3D modeling software that exports OBJ has this option.

Re: How would I write an algorithm to convert a OBJ face to triangles?

Posted: Tue Jun 29, 2021 9:11 am
by ThatCodingGuy78
grump wrote: Tue Jun 29, 2021 9:04 am Some general pointers, may or may not apply to your problem:

A quad on a plane is trivially converted to two triangles

Code: Select all

0---3
|   |
1---2
{ 0, 1, 3 } is triangle #1, and { 1, 2, 3 } is triangle #2. In your cube example you would ideally represent this as an indexed triangle list, to keep the number of vertices low.

Any convex polygon on a plane is easily triangulated using fanning: insert a vertex at center of polygon, then go around counter-clockwise and connect vertices to center vertex.

There's also love.math.triangulate that you could use for more complex cases, but that has its limitations.

The best and common solution is to only export triangulated meshes so you don't have to deal with triangulation yourself. Every 3D modeling software that exports OBJ has this option.
Ah. I'll search for the option on my modeling software I guess.

Re: How would I write an algorithm to convert a OBJ face to triangles?

Posted: Tue Jun 29, 2021 10:28 am
by darkfrei
ThatCodingGuy78 wrote: Tue Jun 29, 2021 9:11 am Ah. I'll search for the option on my modeling software I guess.
blender: Edit mode --> Select faces and Ctrl+T to triangulate it.
https://docs.blender.org/manual/en/late ... faces.html

Re: How would I write an algorithm to convert a OBJ face to triangles?

Posted: Tue Jun 29, 2021 10:36 am
by ThatCodingGuy78
darkfrei wrote: Tue Jun 29, 2021 10:28 am
ThatCodingGuy78 wrote: Tue Jun 29, 2021 9:11 am Ah. I'll search for the option on my modeling software I guess.
blender: Edit mode --> Select faces and Ctrl+T to triangulate it.
https://docs.blender.org/manual/en/late ... faces.html
Thanks!