frenchtoast747 / webgl-obj-loader

A simple OBJ model loader to help facilitate the learning of WebGL.
http://frenchtoast747.github.io/webgl-obj-loader/
MIT License
283 stars 59 forks source link

Add support for more than 4 vertices per face #45

Closed mnoven closed 5 years ago

mnoven commented 6 years ago

Right now the lib seems to convert quads to triangles and thus assumes 3 or 4 vertices per face. The .obj format has however no limit on how many vertices there should be per face and some models break because of this.

An example of an .obj which sometimes has more than 4 vertices per face rendered in my engine using gl.drawElements:

broken

(Model taken from https://casual-effects.com/g3d/data10/research/model/breakfast_room/breakfast_room.zip)

qtip commented 6 years ago

That's absolutely something that the lib should support, thank you for filing this issue. It's non-trivial, but there are some well known algorithms to do this (see https://en.wikipedia.org/wiki/Polygon_triangulation).

@frenchtoast747: The code for the quad triangulation should be tossed and a generic triangulation function should take its place:

for each face in faces:
    for each triangle in triangulate(faces):
        // ....

Here are baby steps:

tckxjzc commented 6 years ago

+1

dekdevy commented 5 years ago

+1

frenchtoast747 commented 5 years ago

Added v2.0.3 which provides initial support for N-gons. I will create another issue to provide a better triangulate() algorithm.

dekdevy commented 5 years ago

Much appreciated. I will report back with any potential issues.