castle-engine / castle-model-viewer

Viewer for many 3D and 2D model formats: glTF, X3D, VRML, Collada, 3DS, MD3, Wavefront OBJ, STL, Spine JSON, sprite sheets in Cocos2D and Starling XML formats
https://castle-engine.io/castle-model-viewer
90 stars 19 forks source link

Errors when reading simple Wavefront Object file (.obj+.mtl) #40

Closed elmkni closed 3 years ago

elmkni commented 3 years ago

Hi Michalis,

it's me again, Elmar...

I just found a problem when loading a simple Wavefront .obj/.mtl file that I exported from MeshLab 2021.07 (https://github.com/cnr-isti-vclab/meshlab/releases):

#### ccube_meshlab.obj ######################################################
#
# OBJ File Generated by Meshlab
#
####
# Object ccube_meshlab.obj
#
# Vertices: 8
# Faces: 6
#
####
mtllib ./ccube_meshlab.obj.mtl

v -1.000000 -1.000000 -1.000000
v 1.000000 -1.000000 -1.000000
v -1.000000 1.000000 -1.000000
v 1.000000 1.000000 -1.000000
v -1.000000 -1.000000 1.000000
v 1.000000 -1.000000 1.000000
v -1.000000 1.000000 1.000000
v 1.000000 1.000000 1.000000
# 8 vertices, 0 vertices normals

usemtl material_0
f 2 1 3 4

usemtl material_1
f 3 1 5 7

usemtl material_2
f 5 1 2 6

usemtl material_3
f 6 8 7 5

usemtl material_4
f 7 8 4 3

usemtl material_5
f 4 8 6 2
# 6 faces, 0 coords texture

# End of File ###############################################################
#### ccube_meshlab.obj.mtl ##################################################
#
# Wavefront material file
# Converted by Meshlab Group
#

newmtl material_0
Ka 0.200000 0.200000 0.200000
Kd 1.000000 0.000000 0.000000
Ks 1.000000 1.000000 1.000000
Tr 1.000000
illum 2
Ns 0.000000

newmtl material_1
Ka 0.200000 0.200000 0.200000
Kd 0.000000 1.000000 0.000000
Ks 1.000000 1.000000 1.000000
Tr 1.000000
illum 2
Ns 0.000000

newmtl material_2
Ka 0.200000 0.200000 0.200000
Kd 0.000000 0.000000 1.000000
Ks 1.000000 1.000000 1.000000
Tr 1.000000
illum 2
Ns 0.000000

newmtl material_3
Ka 0.200000 0.200000 0.200000
Kd 1.000000 0.000000 1.000000
Ks 1.000000 1.000000 1.000000
Tr 1.000000
illum 2
Ns 0.000000

newmtl material_4
Ka 0.200000 0.200000 0.200000
Kd 0.000000 1.000000 1.000000
Ks 1.000000 1.000000 1.000000
Tr 1.000000
illum 2
Ns 0.000000

newmtl material_5
Ka 0.200000 0.200000 0.200000
Kd 1.000000 1.000000 0.000000
Ks 1.000000 1.000000 1.000000
Tr 1.000000
illum 2
Ns 0.000000

# End of File ###############################################################

View3DScene (view3dscene-4.1.0-win64-x86_64.zip | 10-Sep-2021 22:42 | 7270675) issues the following:

16 warnings:

Wavefront material "material_0" specifies specular exponent (Ns) as zero, ignoring
Wavefront material "material_1" specifies specular exponent (Ns) as zero, ignoring
Wavefront material "material_2" specifies specular exponent (Ns) as zero, ignoring
Wavefront material "material_3" specifies specular exponent (Ns) as zero, ignoring
Wavefront material "material_4" specifies specular exponent (Ns) as zero, ignoring
Wavefront material "material_5" specifies specular exponent (Ns) as zero, ignoring
X3D: Invalid number of items in a normal or texture coordinate array for shape "IndexedFaceSet": Invalid index: 6, but we have 5 items
X3D: Invalid number of items in a normal or texture coordinate array for shape "IndexedFaceSet": Invalid index: 5, but we have 5 items
X3D: Invalid number of items in a normal or texture coordinate array for shape "IndexedFaceSet": Invalid index: 5, but we have 5 items
X3D: Invalid number of items in a normal or texture coordinate array for shape "IndexedFaceSet": Invalid index: 6, but we have 5 items
X3D: Invalid number of items in a normal or texture coordinate array for shape "IndexedFaceSet": Invalid index: 7, but we have 5 items
X3D: Invalid number of items in a normal or texture coordinate array for shape "IndexedFaceSet": Invalid index: 6, but we have 5 items
X3D: Invalid number of items in a normal or texture coordinate array for shape "IndexedFaceSet": Invalid index: 5, but we have 5 items
X3D: Invalid number of items in a normal or texture coordinate array for shape "IndexedFaceSet": Invalid index: 5, but we have 5 items
X3D: Invalid number of items in a normal or texture coordinate array for shape "IndexedFaceSet": Invalid index: 6, but we have 5 items
X3D: Invalid number of items in a normal or texture coordinate array for shape "IndexedFaceSet": Invalid index: 7, but we have 5 items

Scene URL: "file:///C:/Temp/ccube_meshlab.obj".

Please help...

with best regards,

Elmar

I attach the .obj+.mtl files and the view3dscene.log: view3dscene_issue_40.zip

michaliskambi commented 3 years ago

Fixed! Thank you for the report.

The

Invalid number of items in a normal or texture coordinate array for shape "IndexedFaceSet"...

was quite serious problem underneath. It occurred during a refactor of a piece of renderer code (I think a few months ago), I'm ashamed it wasn't caught earlier.

It's fixed now, in Castle Game Engine and view3dscene. If you use the precompiled view3dscene, then take the latest view3dscene "snapshot" version from https://castle-engine.io/view3dscene.php in an ~hour from now, it will contain the fix.

Note: the warnings

Wavefront material "xxx" specifies specular exponent (Ns) as zero, ignoring

will remain. This is indeed an invalid thing in the OBJ file, and we ignore Ns 0 in Wavefront materials after https://github.com/castle-engine/castle-engine/issues/178 .

elmkni commented 3 years ago

Hi Michalis,

thanks for the quick fix!

I've tested with view3dscene-4.1.0-win64-x86_64.zip | 15-Sep-2021 22:59 and it works!

with best regards,

Elmar