Misunderstanding transform (line type 1) - Printable Version +- LDraw.org Discussion Forums ( https://forums.ldraw.org)+-- Forum: LDraw Programs ( https://forums.ldraw.org/forum-7.html)+--- Forum: LDraw File Processing and Conversion ( https://forums.ldraw.org/forum-22.html)+--- Thread: Misunderstanding transform (line type 1) ( /thread-9519.html) |

Misunderstanding transform (line type 1) - Robert Baruch - 2013-08-07
I think I'm misunderstanding something about the transform matrix for line type 1. Long story short, I've written a program to convert LDRAW files into something that threejs (3D library for JavaScript) can use, but I'm getting surfaces with the wrong normals, and it's not obvious what's going on. So take a look at this line in p/stud.dat: Code: `1 16 0 0 0 6 0 0 0 -4 0 0 0 6 4-4cyli.dat` And now p/4-4cyli, just the first quad: Code: `4 16 1 1 0 0.9239 1 0.3827 0.9239 0 0.3827 1 0 0` So the vertices are (1,1,0) (0.9239,1,0.3827) (0.9239,0,0.3827) and (1,0,0). Plotting these out and assuming CCW winding shows that the normal faces outwards, as expected. Proof for those who care: the first two vectors formed by the first three vertices are (-0.0761,0,0.3827) and (0,-1,0). Crossing the first vector by the second yields (0.3827,0,0.0761), a vector pointing positive along x and z. Now, the transformation matrix in stud.dat transforms the vertices in 4-4cyli by scaling the x-axis by 6, the y-axis by -4, and the z-axis by 6. Unfortunately, scaling the y-axis by -4 reverses the sense of the face, and now its normal points inward. Again, proof for those who care: the transformation matrix is Code: `6 0 0 0` Applying this to the first vertex: Code: `6 0 0 0 1 6` So the first three vertices transformed are (6,-4,0) (5.5434,-4,2.2962) and (5.5434,0,2.2962). The vectors formed by these vertices are (-0.4566,0,2.2962) and (0,4,0). Crossing the first by the second yields (-9.1848,0,-1.8264): a vector pointing along the negative x and z axes. The order of the vertices has not changed, but since y was inverted, the orientation of the face has changed. This means that stud.dat has an inward-facing cylinder, but a disc pointing in the -y direction, which is weird. But is this correct? If so, I'm not sure how stud.dat is supposed to display anything properly... Re: Misunderstanding transform (line type 1) - Robert Baruch - 2013-08-07
Ah, never mind. It appears that when the determinant of the transform matrix is negative, the winding must also be reversed in a separate step, so the transformed cylinder now has its surfaces facing outwards. Re: Misunderstanding transform (line type 1) - Travis Cobbs - 2013-08-07
I would strongly recommend a very careful reading of the BFC spec. The Rendering Engine Guidelines section points the above out, and proper BFC handling has some other gotchas. Note that any geometry that isn't BFC-certifed will probably have to be added to your threejs model twice, with the same geometry for each copy, but reversed winding of the vertices. And if you look closely at the BFC Spec, you will see that a culling can be enabled and disabled at any time inside a file, so fully proper handling requires BFC tracking on a line-by-line basis, not file-by-file. (See the NOCLIP and CLIP commands in the BFC spec.) |