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

General discussion about LÖVE, Lua, game development, puns, and unicorns.
ThatCodingGuy78
Prole
Posts: 16
Joined: Thu May 27, 2021 12:32 am

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

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
(BTW, the rendered shape when you execute the .love file is only getting the vertex positions, the lines are predefined so it actually renders)
"There is no such thing as a mistake, only happy accidents"
grump
Party member
Posts: 863
Joined: Sat Jul 22, 2017 7:43 pm

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

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.
ThatCodingGuy78
Prole
Posts: 16
Joined: Thu May 27, 2021 12:32 am

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

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.
"There is no such thing as a mistake, only happy accidents"
darkfrei
Party member
Posts: 437
Joined: Sat Feb 08, 2020 11:09 pm

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

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
Falling in LÖVE
I Löve Lua and Love2D
ThatCodingGuy78
Prole
Posts: 16
Joined: Thu May 27, 2021 12:32 am

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

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!
"There is no such thing as a mistake, only happy accidents"

### Who is online

Users browsing this forum: No registered users and 37 guests