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
91 stars 19 forks source link

4th order NurbsPatchSurface node **not** rendered correctly! #17

Open elmkni opened 5 years ago

elmkni commented 5 years ago

Hi Michalis,

I have found a Utah teapot that uses Bezier-patches in Renderman .RIB format here and converted it with the help of Ayam 1.26 and WhiteDune 1.212 and a lot of manual simplification to X3D/X3DOM!

Rendered with X3DOM-1.8.1-dev snapshot it looks like this: uteapot_x3dom

but rendered with View3dscene 3.18.0-20190817 (zoomed to the max) it looks like this: uteapot_view3dscene

Am I missing something?

Because this renders O.K.! -- look here: v3_NurbsPatchSurface_xonon_VIEW3DSCENE

I don't get it! Why does the spaceship (composed of 6 order-4-NurbsPatchSurface-nodes) work and the teapot (composed of 64 order-4-NurbsPatchSurface-nodes) doesn't?

May-be because I omitted the weight-property?

It would be: weight='1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1' for all NurbsPatchSurface-Nodes and seems to be omittable, because according to the spec, the default weight of 1.0 is assumed for each control point.

Please help me!

With best regards,

Elmar

P.S.: I attach the used files: view3dscene_issue_17.zip

michaliskambi commented 5 years ago

Thanks for the report.

I can reproduce it with latest view3dscene from GitHub. Release version looks like on your screenshot, debug version raises assertion errors at NURBS calculation (https://github.com/castle-engine/castle-engine/blob/master/src/3d/castlenurbs.pas#L264) -- so this is definitely a bug in code, something is unexpected.

The weight indeed can be omitted in X3D, this should not be an issue.

I'll look into it and fix soon.

michaliskambi commented 5 years ago

Looks like our NURBS implementation was never ready for "periodic" knots of NURBS. We only implement "non-periodic" knot vectors, which (following the definition in "The NURBS Book", https://books.google.pl/books?id=7dqY5dyAwWkC&pg=PA67&hl=pl&source=gbs_toc_r&cad=3#v=onepage&q=nonperiodic&f=false ) means that the knot vector has Order equal points at the beginning, and similarly Order equal points at the end.

Your example with teapot shows periodic NURBS knots, like

uKnot='0 .142857 .285714 .428571 .571429 .714286 .857143 1'

I'll look into implementing support for periodic NURBS too. I tried a simple approach to it today, but it looks like it will need more serious change of the algorithm.

elmkni commented 5 years ago

Hi Michalis,

I tested my X3D-code the whole night because I thought, the mistake might be on my side, like a typo or a missing '>' or '/' -- But I found nothing...

X3DOM uses the tessalation code of Ayam by Randolf Schultz (randolf.schultz@gmail.com), may-be a look into the X3DOM implementation gives a hint, how to handle 'periodic NURBS'.

I hope you find a way to fix the 'periodic NURBS'-problem soon, because the NURBS-Utah-teapot is a nice show-case for what one can do with NURBS and I would like to be able to use such code also in View3DScene as well!

With best regards,

Elmar

elmkni commented 5 years ago

Hi Michalis,

I have been trying to convert the original Renderman RIB-file of the Utah Teapot to X3D in a way that fits View3DScene's abilities (without having to rewrite the NURBS implementation) and I succeeded!

v3_Teapot_Renderman_View3DScene

I didn't use any other programs than Notepad2 and copied/pasted the original controlPoint-values from the RIB-file into my X3D-file and decorated them with some X3D-NurbsPatchSurface syntax (solid="false" order="4" dimension="4" tessellation="8" etc.), thats all! (no knots! no weight!)

I could even reduce the file-size by 50%, because the original RIB-file draws the teapot actually twice (once for the toon-surface and a second time for the toon-outlines)!

The resulting X3D-file works not only in View3DScene 3.18.0-snapshot, but also in freeWRL-4.3.4, OctagaPlayer-5.0.8 and X3DOM-1.8.1-dev!

v3_Teapot_Renderman_freeWRL v3_Teapot_Renderman_Octaga v3_Teapot_Renderman_X3DOM

Of course it would be cool if View3DScene also supported periodic NURBS knots!

With best regards,

Elmar

P.S.: Here are my files: view3dscene_issue_17-2.zip

michaliskambi commented 5 years ago

Cool, thanks for the information.

If you don't specify the NURBS "knot vector" explicitly in X3D, we internally generate a (non-periodic) knot vector. So it makes sense that it works correctly in this case.

Indeed it would be even better if view3dscene supported periodic NURBS.