LDraw.org Discussion Forums
Writing .dat to .obj converter in JS, but having BFC problems - 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: Writing .dat to .obj converter in JS, but having BFC problems (/thread-7542.html)



Writing .dat to .obj converter in JS, but having BFC problems - Mike "Pomax" Kamermans - 2012-12-30

I'm writing a .dat to .obj converter in JavaScript so I can do LEGO modelling using WebGL, and the following the documentation for data interpreting is pretty straight forward, except I'm gettting weird looking inverted faces when loading several parts.

So, I took part 75535 (technic pin joiner round) and commented off the parts after the initial two inner cylindrical surfaces. This turns it into the following extremely simple .dat file:

75535.dat reduced version Wrote:0 BFC CERTIFY CCW
0 BFC INVERTNEXT
1 16 18 0 0 0 -16 0 6 0 0 0 0 6 4-4cyli.dat
0 BFC INVERTNEXT
1 16 -18 0 0 0 16 0 6 0 0 0 0 6 4-4cyli.dat

They're both inner cylinders, with the same definition, so they should both face the same direction. However, one part faces inward (correctly) and the other part faces outward (incorrectly): http://www.int13h.com/webgl/matviewer/testParse.html (four meshes, twice, visualisation of this .obj definition is at http://www.int13h.com/webgl/matviewer for the duration of this thread)

Viewing the .dat file in LDView shows two cylinders, facing inward, so the .dat file is interpreted correctly by .dat readers. However, the converted .obj data is very wrong. Somehow my parser is screwing up, but at this point the only thing I can think of is that the RT matrix for the include operation is doing something really funky, flipping the vertices to effect a reversed winding or something. I'm kind of at my wits end, following the documentation is apparently not enough to interpet the .dat format... what am I still missing here?

- Pomax

N.B: parser code is in http://www.int13h.com/webgl/matviewer/DATParser.js


Re: Writing .dat to .obj converter in JS, but having BFC problems - Max Martin Richter - 2012-12-30

Maybe you have a problem with the 16 and -16 scaling in the rotation matrix?

/Max


Re: Writing .dat to .obj converter in JS, but having BFC problems - Mike "Pomax" Kamermans - 2012-12-30

quite possibly, and then the question is "how can I tell from the RT matrix whether or not it'll invert the face?" I'm not the greatest at 3d matrices (it's been a long time since I worked with them) so if you happen to know the formula/conditional offhand, that'd be extremely helpful.


Re: Writing .dat to .obj converter in JS, but having BFC problems - Roland Melkert - 2012-12-30

You also need to flip winding when the matrix determinant is negative.

This might be confusing at some times especially when there is an invertnext preceding (causing the flips to cancel each other out)


Re: Writing .dat to .obj converter in JS, but having BFC problems - Michael Heidemann - 2012-12-30

Please find below my 0.01 cent for this discussion. That is the code I use for inlineing in my applications. It is wirtten in visual basic net. I am sure there is a more elegant way so solve but this is mine.
The sub Inline and Reversewinding are what you need. I hope this is not too complicated to understand. For me each line is an object and both sub are methods of the line object.

Code:
''' <summary>
    ''' Manipulates the current matrix values according the values from the given Originalline
    ''' </summary>
    ''' <param name="objOriginalLine">Line that calls this subpart</param>
    ''' <param name="reverse">Does nothing so far</param>
    ''' <remarks></remarks>
    Public Sub Inline(ByVal objOriginalLine As cls_LDraw_Line, Optional ByVal reverse As Boolean = False)
        'Originalline is the line that should be further extracted.
        If objOriginalLine.LineType <> LDraw_DatLineType.Subpart Then Exit Sub
        Select Case Me.LineType
            Case LDraw_DatLineType.Subpart
                Dim tmpDatLinePoint As New cls_LDraw_Point
                Dim tmpDatMatrix As New cls_LDraw_Matrix '3x3

                tmpDatLinePoint.X = varDatLinePoint1.X
                tmpDatLinePoint.Y = varDatLinePoint1.Y
                tmpDatLinePoint.Z = varDatLinePoint1.Z

                tmpDatMatrix.TransX.X = varDatMatrix.TransX.X
                tmpDatMatrix.TransX.Y = varDatMatrix.TransX.Y
                tmpDatMatrix.TransX.Z = varDatMatrix.TransX.Z
                tmpDatMatrix.TransY.X = varDatMatrix.TransY.X
                tmpDatMatrix.TransY.Y = varDatMatrix.TransY.Y
                tmpDatMatrix.TransY.Z = varDatMatrix.TransY.Z
                tmpDatMatrix.TransZ.X = varDatMatrix.TransZ.X
                tmpDatMatrix.TransZ.Y = varDatMatrix.TransZ.Y
                tmpDatMatrix.TransZ.Z = varDatMatrix.TransZ.Z


                varDatLinePoint1.X = _
                tmpDatLinePoint.X * objOriginalLine.TransformMatrix.TransX.X + _
                tmpDatLinePoint.Y * objOriginalLine.TransformMatrix.TransX.Y + _
                tmpDatLinePoint.Z * objOriginalLine.TransformMatrix.TransX.Z + _
                1 * objOriginalLine.Coordinate.X

                varDatLinePoint1.Y = _
                tmpDatLinePoint.X * objOriginalLine.TransformMatrix.TransY.X + _
                tmpDatLinePoint.Y * objOriginalLine.TransformMatrix.TransY.Y + _
                tmpDatLinePoint.Z * objOriginalLine.TransformMatrix.TransY.Z + _
                1 * objOriginalLine.Coordinate.Y

                varDatLinePoint1.Z = _
                tmpDatLinePoint.X * objOriginalLine.TransformMatrix.TransZ.X + _
                tmpDatLinePoint.Y * objOriginalLine.TransformMatrix.TransZ.Y + _
                tmpDatLinePoint.Z * objOriginalLine.TransformMatrix.TransZ.Z + _
                1 * objOriginalLine.Coordinate.Z

                varDatMatrix.TransX.X = _
                tmpDatMatrix.TransX.X * objOriginalLine.TransformMatrix.TransX.X + _
                tmpDatMatrix.TransY.X * objOriginalLine.TransformMatrix.TransX.Y + _
                tmpDatMatrix.TransZ.X * objOriginalLine.TransformMatrix.TransX.Z + _
                0 * objOriginalLine.Coordinate.X
                'ok
                varDatMatrix.TransX.Y = _
                tmpDatMatrix.TransX.Y * objOriginalLine.TransformMatrix.TransX.X + _
                tmpDatMatrix.TransY.Y * objOriginalLine.TransformMatrix.TransX.Y + _
                tmpDatMatrix.TransZ.Y * objOriginalLine.TransformMatrix.TransX.Z + _
                0 * objOriginalLine.Coordinate.X
                'ok
                varDatMatrix.TransX.Z = _
                tmpDatMatrix.TransX.Z * objOriginalLine.TransformMatrix.TransX.X + _
                tmpDatMatrix.TransY.Z * objOriginalLine.TransformMatrix.TransX.Y + _
                tmpDatMatrix.TransZ.Z * objOriginalLine.TransformMatrix.TransX.Z + _
                0 * objOriginalLine.Coordinate.X
                'ok

                varDatLinePoint2 = varDatMatrix.TransX

                varDatMatrix.TransY.X = _
                tmpDatMatrix.TransX.X * objOriginalLine.TransformMatrix.TransY.X + _
                tmpDatMatrix.TransY.X * objOriginalLine.TransformMatrix.TransY.Y + _
                tmpDatMatrix.TransZ.X * objOriginalLine.TransformMatrix.TransY.Z + _
                0 * objOriginalLine.Coordinate.Y
                'ok

                varDatMatrix.TransY.Y = _
                tmpDatMatrix.TransX.Y * objOriginalLine.TransformMatrix.TransY.X + _
                tmpDatMatrix.TransY.Y * objOriginalLine.TransformMatrix.TransY.Y + _
                tmpDatMatrix.TransZ.Y * objOriginalLine.TransformMatrix.TransY.Z + _
                0 * objOriginalLine.Coordinate.Y
                'ok

                varDatMatrix.TransY.Z = _
                tmpDatMatrix.TransX.Z * objOriginalLine.TransformMatrix.TransY.X + _
                tmpDatMatrix.TransY.Z * objOriginalLine.TransformMatrix.TransY.Y + _
                tmpDatMatrix.TransZ.Z * objOriginalLine.TransformMatrix.TransY.Z + _
                0 * objOriginalLine.Coordinate.Y
                'ok

                varDatLinePoint3 = varDatMatrix.TransY

                varDatMatrix.TransZ.X = _
                tmpDatMatrix.TransX.X * objOriginalLine.TransformMatrix.TransZ.X + _
                tmpDatMatrix.TransY.X * objOriginalLine.TransformMatrix.TransZ.Y + _
                tmpDatMatrix.TransZ.X * objOriginalLine.TransformMatrix.TransZ.Z + _
                0 * objOriginalLine.Coordinate.Z
                'ok

                varDatMatrix.TransZ.Y = _
                tmpDatMatrix.TransX.Y * objOriginalLine.TransformMatrix.TransZ.X + _
                tmpDatMatrix.TransY.Y * objOriginalLine.TransformMatrix.TransZ.Y + _
                tmpDatMatrix.TransZ.Y * objOriginalLine.TransformMatrix.TransZ.Z + _
                0 * objOriginalLine.Coordinate.Z
                'ok

                varDatMatrix.TransZ.Z = _
                tmpDatMatrix.TransX.Z * objOriginalLine.TransformMatrix.TransZ.X + _
                tmpDatMatrix.TransY.Z * objOriginalLine.TransformMatrix.TransZ.Y + _
                tmpDatMatrix.TransZ.Z * objOriginalLine.TransformMatrix.TransZ.Z + _
                0 * objOriginalLine.Coordinate.Z
                'ok

                varDatLinePoint4 = varDatMatrix.TransZ

                If varDatLineColor = "16" Then
                    varDatLineColor = objOriginalLine.LineColor
                End If
            Case LDraw_DatLineType.Line
                'für linetype2
                Dim x1, x2, y1, y2, z1, z2 As Double

                'Die Werte für X sind ok aber nicht y und z, da bereits x verändert wurde.:-(
                x1 = varDatLinePoint1.X
                x2 = varDatLinePoint2.X
                y1 = varDatLinePoint1.Y
                y2 = varDatLinePoint2.Y
                z1 = varDatLinePoint1.Z
                z2 = varDatLinePoint2.Z

                varDatLinePoint1.X = _
                x1 * objOriginalLine.TransformMatrix.TransX.X + _
                y1 * objOriginalLine.TransformMatrix.TransX.Y + _
                z1 * objOriginalLine.TransformMatrix.TransX.Z + _
                objOriginalLine.Coordinate.X

                varDatLinePoint1.Y = _
                x1 * objOriginalLine.TransformMatrix.TransY.X + _
                y1 * objOriginalLine.TransformMatrix.TransY.Y + _
                z1 * objOriginalLine.TransformMatrix.TransY.Z + _
                objOriginalLine.Coordinate.Y

                varDatLinePoint1.Z = _
                x1 * objOriginalLine.TransformMatrix.TransZ.X + _
                y1 * objOriginalLine.TransformMatrix.TransZ.Y + _
                z1 * objOriginalLine.TransformMatrix.TransZ.Z + _
                objOriginalLine.Coordinate.Z

                varDatLinePoint2.X = _
                x2 * objOriginalLine.TransformMatrix.TransX.X + _
                y2 * objOriginalLine.TransformMatrix.TransX.Y + _
                z2 * objOriginalLine.TransformMatrix.TransX.Z + _
                objOriginalLine.Coordinate.X


                varDatLinePoint2.Y = _
                x2 * objOriginalLine.TransformMatrix.TransY.X + _
                y2 * objOriginalLine.TransformMatrix.TransY.Y + _
                z2 * objOriginalLine.TransformMatrix.TransY.Z + _
                objOriginalLine.Coordinate.Y

                varDatLinePoint2.Z = _
                x2 * objOriginalLine.TransformMatrix.TransZ.X + _
                y2 * objOriginalLine.TransformMatrix.TransZ.Y + _
                z2 * objOriginalLine.TransformMatrix.TransZ.Z + _
                objOriginalLine.Coordinate.Z

                If varDatLineColor = "16" Then
                    varDatLineColor = objOriginalLine.LineColor
                End If
            Case LDraw_DatLineType.Triangle
                'für linetype3
                Dim x1, x2, x3, y1, y2, y3, z1, z2, z3 As Double

                x1 = varDatLinePoint1.X
                x2 = varDatLinePoint2.X
                x3 = varDatLinePoint3.X
                y1 = varDatLinePoint1.Y
                y2 = varDatLinePoint2.Y
                y3 = varDatLinePoint3.Y
                z1 = varDatLinePoint1.Z
                z2 = varDatLinePoint2.Z
                z3 = varDatLinePoint3.Z

                varDatLinePoint1.X = x1 * objOriginalLine.TransformMatrix.TransX.X + _
                                    y1 * objOriginalLine.TransformMatrix.TransX.Y + _
                                    z1 * objOriginalLine.TransformMatrix.TransX.Z + _
                                    objOriginalLine.Coordinate.X
                varDatLinePoint1.Y = x1 * objOriginalLine.TransformMatrix.TransY.X + _
                                    y1 * objOriginalLine.TransformMatrix.TransY.Y + _
                                    z1 * objOriginalLine.TransformMatrix.TransY.Z + _
                                    objOriginalLine.Coordinate.Y
                varDatLinePoint1.Z = x1 * objOriginalLine.TransformMatrix.TransZ.X + _
                                    y1 * objOriginalLine.TransformMatrix.TransZ.Y + _
                                    z1 * objOriginalLine.TransformMatrix.TransZ.Z + _
                                    objOriginalLine.Coordinate.Z

                varDatLinePoint2.X = x2 * objOriginalLine.TransformMatrix.TransX.X + _
                                    y2 * objOriginalLine.TransformMatrix.TransX.Y + _
                                    z2 * objOriginalLine.TransformMatrix.TransX.Z + _
                                    objOriginalLine.Coordinate.X
                varDatLinePoint2.Y = x2 * objOriginalLine.TransformMatrix.TransY.X + _
                                    y2 * objOriginalLine.TransformMatrix.TransY.Y + _
                                    z2 * objOriginalLine.TransformMatrix.TransY.Z + _
                                    objOriginalLine.Coordinate.Y
                varDatLinePoint2.Z = x2 * objOriginalLine.TransformMatrix.TransZ.X + _
                                    y2 * objOriginalLine.TransformMatrix.TransZ.Y + _
                                    z2 * objOriginalLine.TransformMatrix.TransZ.Z + _
                                    objOriginalLine.Coordinate.Z

                varDatLinePoint3.X = x3 * objOriginalLine.TransformMatrix.TransX.X + _
                                    y3 * objOriginalLine.TransformMatrix.TransX.Y + _
                                    z3 * objOriginalLine.TransformMatrix.TransX.Z + _
                                    objOriginalLine.Coordinate.X
                varDatLinePoint3.Y = x3 * objOriginalLine.TransformMatrix.TransY.X + _
                                    y3 * objOriginalLine.TransformMatrix.TransY.Y + _
                                    z3 * objOriginalLine.TransformMatrix.TransY.Z + _
                                    objOriginalLine.Coordinate.Y
                varDatLinePoint3.Z = x3 * objOriginalLine.TransformMatrix.TransZ.X + _
                                    y3 * objOriginalLine.TransformMatrix.TransZ.Y + _
                                    z3 * objOriginalLine.TransformMatrix.TransZ.Z + _
                                    objOriginalLine.Coordinate.Z

                If varDatLineColor = "16" Then
                    varDatLineColor = objOriginalLine.LineColor
                End If

                If reverse Then
                    Me.ReverseWinding()
                End If
            Case LDraw_DatLineType.Quad
                Dim x1, x2, x3, x4, y1, y2, y3, y4 As Double

                x1 = varDatLinePoint1.X
                x2 = varDatLinePoint2.X
                x3 = varDatLinePoint3.X
                x4 = varDatLinePoint4.X
                y1 = varDatLinePoint1.Y
                y2 = varDatLinePoint2.Y
                y3 = varDatLinePoint3.Y
                y4 = varDatLinePoint4.Y

                varDatLinePoint1.X = varDatLinePoint1.X * objOriginalLine.TransformMatrix.TransX.X + _
                                    varDatLinePoint1.Y * objOriginalLine.TransformMatrix.TransX.Y + _
                                    varDatLinePoint1.Z * objOriginalLine.TransformMatrix.TransX.Z + _
                                    objOriginalLine.Coordinate.X
                varDatLinePoint1.Y = x1 * objOriginalLine.TransformMatrix.TransY.X + _
                                    varDatLinePoint1.Y * objOriginalLine.TransformMatrix.TransY.Y + _
                                    varDatLinePoint1.Z * objOriginalLine.TransformMatrix.TransY.Z + _
                                    objOriginalLine.Coordinate.Y
                varDatLinePoint1.Z = x1 * objOriginalLine.TransformMatrix.TransZ.X + _
                                    y1 * objOriginalLine.TransformMatrix.TransZ.Y + _
                                    varDatLinePoint1.Z * objOriginalLine.TransformMatrix.TransZ.Z + _
                                    objOriginalLine.Coordinate.Z

                varDatLinePoint2.X = varDatLinePoint2.X * objOriginalLine.TransformMatrix.TransX.X + _
                                    varDatLinePoint2.Y * objOriginalLine.TransformMatrix.TransX.Y + _
                                    varDatLinePoint2.Z * objOriginalLine.TransformMatrix.TransX.Z + _
                                    objOriginalLine.Coordinate.X
                varDatLinePoint2.Y = x2 * objOriginalLine.TransformMatrix.TransY.X + _
                                    varDatLinePoint2.Y * objOriginalLine.TransformMatrix.TransY.Y + _
                                    varDatLinePoint2.Z * objOriginalLine.TransformMatrix.TransY.Z + _
                                    objOriginalLine.Coordinate.Y
                varDatLinePoint2.Z = x2 * objOriginalLine.TransformMatrix.TransZ.X + _
                                    y2 * objOriginalLine.TransformMatrix.TransZ.Y + _
                                    varDatLinePoint2.Z * objOriginalLine.TransformMatrix.TransZ.Z + _
                                    objOriginalLine.Coordinate.Z

                varDatLinePoint3.X = varDatLinePoint3.X * objOriginalLine.TransformMatrix.TransX.X + _
                                    varDatLinePoint3.Y * objOriginalLine.TransformMatrix.TransX.Y + _
                                    varDatLinePoint3.Z * objOriginalLine.TransformMatrix.TransX.Z + _
                                    objOriginalLine.Coordinate.X
                varDatLinePoint3.Y = x3 * objOriginalLine.TransformMatrix.TransY.X + _
                                    varDatLinePoint3.Y * objOriginalLine.TransformMatrix.TransY.Y + _
                                    varDatLinePoint3.Z * objOriginalLine.TransformMatrix.TransY.Z + _
                                    objOriginalLine.Coordinate.Y
                varDatLinePoint3.Z = x3 * objOriginalLine.TransformMatrix.TransZ.X + _
                                    y3 * objOriginalLine.TransformMatrix.TransZ.Y + _
                                    varDatLinePoint3.Z * objOriginalLine.TransformMatrix.TransZ.Z + _
                                    objOriginalLine.Coordinate.Z

                varDatLinePoint4.X = varDatLinePoint4.X * objOriginalLine.TransformMatrix.TransX.X + _
                                    varDatLinePoint4.Y * objOriginalLine.TransformMatrix.TransX.Y + _
                                    varDatLinePoint4.Z * objOriginalLine.TransformMatrix.TransX.Z + _
                                    objOriginalLine.Coordinate.X
                varDatLinePoint4.Y = x4 * objOriginalLine.TransformMatrix.TransY.X + _
                                    varDatLinePoint4.Y * objOriginalLine.TransformMatrix.TransY.Y + _
                                    varDatLinePoint4.Z * objOriginalLine.TransformMatrix.TransY.Z + _
                                    objOriginalLine.Coordinate.Y
                varDatLinePoint4.Z = x4 * objOriginalLine.TransformMatrix.TransZ.X + _
                                    y4 * objOriginalLine.TransformMatrix.TransZ.Y + _
                                    varDatLinePoint4.Z * objOriginalLine.TransformMatrix.TransZ.Z + _
                                    objOriginalLine.Coordinate.Z

                If varDatLineColor = "16" Then
                    varDatLineColor = objOriginalLine.LineColor
                End If

                If reverse Then

                    Me.ReverseWinding()

                End If

            Case LDraw_DatLineType.OptLine
                Dim x1, x2, x3, x4, y1, y2, y3, y4 As Double

                x1 = varDatLinePoint1.X
                x2 = varDatLinePoint2.X
                x3 = varDatLinePoint3.X
                x4 = varDatLinePoint4.X
                y1 = varDatLinePoint1.Y
                y2 = varDatLinePoint2.Y
                y3 = varDatLinePoint3.Y
                y4 = varDatLinePoint4.Y

                varDatLinePoint1.X = x1 * objOriginalLine.TransformMatrix.TransX.X + _
                                    y1 * objOriginalLine.TransformMatrix.TransX.Y + _
                                    varDatLinePoint1.Z * objOriginalLine.TransformMatrix.TransX.Z + _
                                    objOriginalLine.Coordinate.X
                varDatLinePoint1.Y = x1 * objOriginalLine.TransformMatrix.TransY.X + _
                                    y1 * objOriginalLine.TransformMatrix.TransY.Y + _
                                    varDatLinePoint1.Z * objOriginalLine.TransformMatrix.TransY.Z + _
                                    objOriginalLine.Coordinate.Y
                varDatLinePoint1.Z = x1 * objOriginalLine.TransformMatrix.TransZ.X + _
                                    y1 * objOriginalLine.TransformMatrix.TransZ.Y + _
                                    varDatLinePoint1.Z * objOriginalLine.TransformMatrix.TransZ.Z + _
                                    objOriginalLine.Coordinate.Z

                varDatLinePoint2.X = varDatLinePoint2.X * objOriginalLine.TransformMatrix.TransX.X + _
                                    y2 * objOriginalLine.TransformMatrix.TransX.Y + _
                                    varDatLinePoint2.Z * objOriginalLine.TransformMatrix.TransX.Z + _
                                    objOriginalLine.Coordinate.X
                varDatLinePoint2.Y = x2 * objOriginalLine.TransformMatrix.TransY.X + _
                                    y2 * objOriginalLine.TransformMatrix.TransY.Y + _
                                    varDatLinePoint2.Z * objOriginalLine.TransformMatrix.TransY.Z + _
                                    objOriginalLine.Coordinate.Y
                varDatLinePoint2.Z = x2 * objOriginalLine.TransformMatrix.TransZ.X + _
                                    y2 * objOriginalLine.TransformMatrix.TransZ.Y + _
                                    varDatLinePoint2.Z * objOriginalLine.TransformMatrix.TransZ.Z + _
                                    objOriginalLine.Coordinate.Z

                varDatLinePoint3.X = varDatLinePoint3.X * objOriginalLine.TransformMatrix.TransX.X + _
                                    y3 * objOriginalLine.TransformMatrix.TransX.Y + _
                                    varDatLinePoint3.Z * objOriginalLine.TransformMatrix.TransX.Z + _
                                    objOriginalLine.Coordinate.X
                varDatLinePoint3.Y = x3 * objOriginalLine.TransformMatrix.TransY.X + _
                                    y3 * objOriginalLine.TransformMatrix.TransY.Y + _
                                    varDatLinePoint3.Z * objOriginalLine.TransformMatrix.TransY.Z + _
                                    objOriginalLine.Coordinate.Y
                varDatLinePoint3.Z = x3 * objOriginalLine.TransformMatrix.TransZ.X + _
                                    y3 * objOriginalLine.TransformMatrix.TransZ.Y + _
                                    varDatLinePoint3.Z * objOriginalLine.TransformMatrix.TransZ.Z + _
                                    objOriginalLine.Coordinate.Z

                varDatLinePoint4.X = varDatLinePoint4.X * objOriginalLine.TransformMatrix.TransX.X + _
                                    y4 * objOriginalLine.TransformMatrix.TransX.Y + _
                                    varDatLinePoint4.Z * objOriginalLine.TransformMatrix.TransX.Z + _
                                    objOriginalLine.Coordinate.X
                varDatLinePoint4.Y = x4 * objOriginalLine.TransformMatrix.TransY.X + _
                                    y4 * objOriginalLine.TransformMatrix.TransY.Y + _
                                    varDatLinePoint4.Z * objOriginalLine.TransformMatrix.TransY.Z + _
                                    objOriginalLine.Coordinate.Y
                varDatLinePoint4.Z = x4 * objOriginalLine.TransformMatrix.TransZ.X + _
                                    y4 * objOriginalLine.TransformMatrix.TransZ.Y + _
                                    varDatLinePoint4.Z * objOriginalLine.TransformMatrix.TransZ.Z + _
                                    objOriginalLine.Coordinate.Z

                If varDatLineColor = "16" Then
                    varDatLineColor = objOriginalLine.LineColor
                End If

        End Select
    End Sub

    ''' <summary>
    ''' Method to reversewind this line
    ''' </summary>
    ''' <remarks></remarks>
    Public Sub ReverseWinding()
        Dim x1, x2, z1, y1, y2, z2 As Double
        Dim x3, x4, z3, y3, y4, z4 As Double
        Try
            Select Case Me.LineType
                Case LDraw_DatLineType.Comment
                Case LDraw_DatLineType.Line
                Case LDraw_DatLineType.OptLine
                Case LDraw_DatLineType.Quad
                    'save values
                    x1 = varDatLinePoint1.X
                    y1 = varDatLinePoint1.Y
                    z1 = varDatLinePoint1.Z
                    x2 = varDatLinePoint2.X
                    y2 = varDatLinePoint2.Y
                    z2 = varDatLinePoint2.Z
                    x3 = varDatLinePoint3.X
                    y3 = varDatLinePoint3.Y
                    z3 = varDatLinePoint3.Z
                    x4 = varDatLinePoint4.X
                    y4 = varDatLinePoint4.Y
                    z4 = varDatLinePoint4.Z

                    'Change BFC of that line
                    If varDatLineBFC = "CW" Then
                        varDatLineBFC = "CCW"
                    Else
                        varDatLineBFC = "CW"
                    End If

                    'Change the point of that line
                    varDatLinePoint1.X = x4
                    varDatLinePoint1.Y = y4
                    varDatLinePoint1.Z = z4

                    varDatLinePoint2.X = x3
                    varDatLinePoint2.Y = y3
                    varDatLinePoint2.Z = z3

                    varDatLinePoint3.X = x2
                    varDatLinePoint3.Y = y2
                    varDatLinePoint3.Z = z2

                    varDatLinePoint4.X = x1
                    varDatLinePoint4.Y = y1
                    varDatLinePoint4.Z = z1

                Case LDraw_DatLineType.Subpart
                    'Nothing to do
                Case LDraw_DatLineType.Triangle
                    'save values
                    x1 = varDatLinePoint1.X
                    y1 = varDatLinePoint1.Y
                    z1 = varDatLinePoint1.Z
                    x2 = varDatLinePoint3.X
                    y2 = varDatLinePoint3.Y
                    z2 = varDatLinePoint3.Z

                    'Change BFC of that line
                    If varDatLineBFC = "CW" Then
                        varDatLineBFC = "CCW"
                    Else
                        varDatLineBFC = "CW"
                    End If

                    'Change the point of that line
                    varDatLinePoint1.X = x2
                    varDatLinePoint1.Y = y2
                    varDatLinePoint1.Z = z2
                    varDatLinePoint3.X = x1
                    varDatLinePoint3.Y = y1
                    varDatLinePoint3.Z = z1
            End Select
        Catch ex As Exception
            MessageBox.Show("Error in cls_LDraw_Line in ReverseWinding" & ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub



Re: Writing .dat to .obj converter in JS, but having BFC problems - Mike "Pomax" Kamermans - 2012-12-30

ah, negative determinant I can work with! Not too bothered about the compound inverse, since I'm dealing with that already anyway for flipping based on clockwise/counterclockwise winding. Another boolean flip'll be pretty easy to add in.

Thanks!


Re: Writing .dat to .obj converter in JS, but having BFC problems - Reuben Pearse - 2012-12-31

Hi there!

I've emailed Rolf Redford about this - he's the author of LdrDat2Obj

In his reply he said....

My solution was pretty simple.

My program sets invert flag when it reads invertnext line. It sets it false when it finish processing that line.

When it writes out, it does this

For triangle:

Code:
if (!inverted)
                writetype3obj(color, p1, p2, p3, writefile);
            else
                writetype3obj(color, p1, p3, p2, writefile);
or for rectangle:

            if (!inverted)
                writetype4obj(color, p1, p2, p3, p4, writefile);
            else
                writetype4obj(color, p1, p4, p3, p2, writefile);


I hope this helps.

Reuben

=====================================
Download 3D Lego models and other resources from:
http://www.pearse.co.uk/lego
=====================================


Re: Writing .dat to .obj converter in JS, but having BFC problems - Mike "Pomax" Kamermans - 2012-12-31

Yeah, with the negative determinant rule in there things work a treat. I stuck my code on https://github.com/Pomax/WebGLdraw for anyone that's interested, and I'll make a new post for it it's not lost in a "how do I..." thread. Seeing LEGO pieces rendered using WebGL is pretty cool =)

- Pomax