# LDraw.org Discussion Forums

Full Version: Misunderstanding transform (line type 1)
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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 0 -4 0 0 0  0 6 0 0  0 0 1```

Applying this to the first vertex:
Code:
```6  0 0 0    1    6 0 -4 0 0 x  1 = -4 = (6,-4,0) 0  0 6 0    0    0 0  0 0 1    1    1```

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...
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.
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.)