WebGL export

Info about new implemented features, classes, modules or APIs. Might get technical!
jreinhardt
Posts: 329
Joined: Mon Sep 09, 2013 6:08 pm

Re: WebGL export

Postby jreinhardt » Sun May 11, 2014 9:43 pm

Strange. I get different results when activating phong shading, see attachments. Did you really try the webgl2 branch? I put the phong changes to a new branch, as they are independent from the discretize stuff.

Ok, then I will bring the discretize issue up again, once 0.14 is out of the door. Should I post in this thread again or better open a thread in the pull request forum?
Attachments
cylinder_phong.png
cylinder_phong.png (6.54 KiB) Viewed 3232 times
cylinder_normal.png
cylinder_normal.png (2.67 KiB) Viewed 3232 times
Last edited by jreinhardt on Sun May 11, 2014 9:58 pm, edited 1 time in total.
BOLTS, standard parts library for FreeCAD Thread, Help with Translation
User avatar
yorik
Site Admin
Posts: 8563
Joined: Tue Feb 17, 2009 9:16 pm
Location: São Paulo, Brazil
Contact:

Re: WebGL export

Postby yorik » Sun May 11, 2014 9:56 pm

jreinhardt wrote:Did you really try the webgl2 branch?

Yes, I had the importWebGL.shading option...
jreinhardt wrote:Ok, then I will bring the discretize issue up again, once 0.14 is out of the door. Should I post in this thread again or better open a thread in the pull request forum?

As you prefer!
User avatar
kwahoo
Posts: 186
Joined: Fri Nov 29, 2013 3:09 pm

Re: WebGL export

Postby kwahoo » Tue Jun 24, 2014 5:19 pm

Hi,
I started tinkering with webgl2 branch and I had been a bit disappointed (sorry Johannes!).
A cylinder exported with "phong":
Image

It looks like flat shading - one normal vector for face/3 vertices. Then I found:

Code: Select all

geom.computeFaceNormals();

and added:

Code: Select all

geom.computeVertexNormals();


The effect is even worse:
Image

I'd started analysing Coin3d output, as example for a cube it looks like:

Code: Select all

print FreeCADGui.ActiveDocument.ActiveObject.toString()


#Inventor V2.1 ascii


Separator {

  Transform {
    translation 0 0 0
    rotation 0 0 1  0
    center 0 0 0

  }
  Coordinate3 {
    point [ 0 0 0,
        0 0 10,
        0 10 10,
        0 10 0,
        10 0 0,
        10 0 10,
        10 10 10,
        10 10 0,
        0 0 0,
        10 0 0,
        10 0 10,
        0 0 10,
        0 10 0,
        10 10 0,
        10 10 10,
        0 10 10,
        0 0 0,
        0 10 0,
        10 10 0,
        10 0 0,
        0 0 10,
        0 10 10,
        10 10 10,
        10 0 10,
        0 0 10,
        0 0 0,
        0 10 10,
        0 10 0,
        10 0 10,
        10 0 0,
        10 10 10,
        10 10 0 ]

  }
  Switch {
    whichChild 0

    Separator {

      DEF _+0 Separator {

        Material {
          ambientColor 0.2 0.2 0.2
          diffuseColor 0.098039217 0.098039217 0.098039217
          specularColor 0 0 0
          emissiveColor 0 0 0
          shininess 1
          transparency 0

        }
        DrawStyle {
          style LINES
          lineWidth 2
          linePattern 0xffff

        }
        SoBrepEdgeSet {
          fields [ SFNode vertexProperty, MFInt32 coordIndex, MFInt32 materialIndex, MFInt32 normalIndex, MFInt32 textureCoordIndex, SFInt32 highlightIndex, MFInt32 selectionIndex ]
          coordIndex [ 0, 1, -1, 1, 2, -1, 3, 2,
              -1, 0, 3, -1, 4, 5, -1, 5,
              6, -1, 7, 6, -1, 4, 7, -1,
              8, 9, -1, 11, 10, -1, 12, 13,
              -1, 15, 14, -1 ]
          highlightIndex -1
          selectionIndex [  ]

        }
      }
      PolygonOffset {

      }
      DEF _+1 Separator {

        ShapeHints {
          vertexOrdering COUNTERCLOCKWISE
          shapeType UNKNOWN_SHAPE_TYPE

        }
        MaterialBinding {
          value OVERALL

        }
        Material {
          diffuseColor 0.80000001 0.80000001 0.80000001

        }
        DrawStyle {
          style FILLED

        }
        Normal {
          vector [ -1 0 0,
              -1 0 0,
              -1 0 0,
              -1 0 0,
              1 0 0,
              1 0 0,
              1 0 0,
              1 0 0,
              0 -1 0,
              0 -1 0,
              0 -1 0,
              0 -1 0,
              0 1 0,
              0 1 0,
              0 1 0,
              0 1 0,
              0 0 -1,
              0 0 -1,
              0 0 -1,
              0 0 -1,
              0 0 1,
              0 0 1,
              0 0 1,
              0 0 1 ]

        }
        NormalBinding {
          value PER_VERTEX_INDEXED

        }
        SoBrepFaceSet {
          fields [ SFNode vertexProperty, MFInt32 coordIndex, MFInt32 materialIndex, MFInt32 normalIndex, MFInt32 textureCoordIndex, MFInt32 partIndex, SFInt32 highlightIndex, MFInt32 selectionIndex ]
          coordIndex [ 3, 1, 2, -1, 0, 1, 3, -1,
              5, 7, 6, -1, 5, 4, 7, -1,
              9, 10, 8, -1, 8, 10, 11, -1,
              14, 13, 12, -1, 14, 12, 15, -1,
              17, 18, 16, -1, 16, 18, 19, -1,
              22, 21, 20, -1, 22, 20, 23, -1 ]
          partIndex [ 2, 2, 2, 2, 2, 2 ]
          highlightIndex -1
          selectionIndex [  ]

        }
      }
      DEF _+2 Separator {

        Material {
          ambientColor 0.2 0.2 0.2
          diffuseColor 0.098039217 0.098039217 0.098039217
          specularColor 0 0 0
          emissiveColor 0 0 0
          shininess 1
          transparency 0

        }
        DrawStyle {
          style POINTS
          pointSize 2

        }
        SoBrepPointSet {
          fields [ SFNode vertexProperty, SFInt32 startIndex, SFInt32 numPoints, SFInt32 highlightIndex, MFInt32 selectionIndex ]
          startIndex 24
          highlightIndex -1
          selectionIndex [  ]

        }
      }
    }
    USE _+1
    Separator {

      USE _+0
      USE _+2
    }
    USE _+2
  }
}


I used vertices coordinates, normals per vertex and index in my custom WebGL file, example for the above data:

Code: Select all

    function initBuffers() {

        modelVertexPositionBuffer = gl.createBuffer();
        gl.bindBuffer(gl.ARRAY_BUFFER, modelVertexPositionBuffer);
        vertices = [
            0.0, 0.0, 0.0, //from Coordinate3
            0.0, 0.0, 10.0,
            0.0, 10.0, 10.0,
            0.0, 10.0, 0.0,
            10.0, 0.0, 0.0,
            10.0, 0.0, 10.0,
            10.0, 10.0, 10.0,
            10.0, 10.0, 0.0,
            0.0, 0.0, 0.0,
            10.0, 0.0, 0.0,
            10.0, 0.0, 10.0,
            0.0, 0.0, 10.0,
            0.0, 10.0, 0.0,
            10.0, 10.0, 0.0,
            10.0, 10.0, 10.0,
            0.0, 10.0, 10.0,
            0.0, 0.0, 0.0,
            0.0, 10.0, 0.0,
            10.0, 10.0, 0.0,
            10.0, 0.0, 0.0,
            0.0, 0.0, 10.0,
            0.0, 10.0, 10.0,
            10.0, 10.0, 10.0,
            10.0, 0.0, 10.0,
            0.0, 0.0, 10.0,
            0.0, 0.0, 0.0,
            0.0, 10.0, 10.0,
            0.0, 10.0, 0.0,
            10.0, 0.0, 10.0,
            10.0, 0.0, 0.0,
            10.0, 10.0, 10.0,
            10.0, 10.0, 0.0,
        ];
        gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(vertices), gl.STATIC_DRAW);
        modelVertexPositionBuffer.itemSize = 3;
        modelVertexPositionBuffer.numItems = 32;

        modelVertexNormalBuffer = gl.createBuffer();
        gl.bindBuffer(gl.ARRAY_BUFFER, modelVertexNormalBuffer);
        var vertexNormals = [
                -1.0, 0.0, 0.0, //from Normals
                -1.0, 0.0, 0.0,
                -1.0, 0.0, 0.0,
                -1.0, 0.0, 0.0,
                1.0, 0.0, 0.0,
                1.0, 0.0, 0.0,
                1.0, 0.0, 0.0,
                1.0, 0.0, 0.0,
                0.0, -1.0, 0.0,
                0.0, -1.0, 0.0,
                0.0, -1.0, 0.0,
                0.0, -1.0, 0.0,
                0.0, 1.0, 0.0,
                0.0, 1.0, 0.0,
                0.0, 1.0, 0.0,
                0.0, 1.0, 0.0,
                0.0, 0.0, -1.0,
                0.0, 0.0, -1.0,
                0.0, 0.0, -1.0,
                0.0, 0.0, -1.0,
                0.0, 0.0, 1.0,
                0.0, 0.0, 1.0,
                0.0, 0.0, 1.0,
                0.0, 0.0, 1.0,
        ];
        gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(vertexNormals), gl.STATIC_DRAW);
        modelVertexNormalBuffer.itemSize = 3;
        modelVertexNormalBuffer.numItems = 24;


        modelVertexIndexBuffer = gl.createBuffer();
        gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, modelVertexIndexBuffer);
        var modelVertexIndices = [ //from coordIndex without -1
              3, 1, 2, 0, 1, 3,
              5, 7, 6, 5, 4, 7,
              9, 10, 8, 8, 10, 11,
              14, 13, 12, 14, 12, 15,
              17, 18, 16, 16, 18, 19,
              22, 21, 20, 22, 20, 23, 
        ];
        gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint16Array(modelVertexIndices), gl.STATIC_DRAW);
        modelVertexIndexBuffer.itemSize = 1;
        modelVertexIndexBuffer.numItems = 36;


    }


Finally, I put a cylinder data in my file and... it looks pretty nice, even with Gouraud shading only (I haven't written Phong one yet):

Image
Attachments
fc-cylinder-webgl.zip
(8.77 KiB) Downloaded 68 times
jreinhardt
Posts: 329
Joined: Mon Sep 09, 2013 6:08 pm

Re: WebGL export

Postby jreinhardt » Thu Aug 14, 2014 2:39 pm

I brought up the Wireframe problem up in the developers corner:
viewtopic.php?f=10&t=7316

Werner had a very good proposal how to resolve the issue while preserving backwards compatibility, and also implemented it. Building on that I created a branch where the discretize setting is removed and the new invocations are used to create nice wireframes and small files:

https://github.com/jreinhardt/FreeCAD_s ... discretize
BOLTS, standard parts library for FreeCAD Thread, Help with Translation
User avatar
rockn
Posts: 1433
Joined: Wed Sep 28, 2011 10:39 am
Location: Toulouse, France
Contact:

Re: WebGL export

Postby rockn » Tue Mar 24, 2015 8:51 pm

Hi,
It seem that WebGL export is broken somewhere :

Code: Select all

Traceback (most recent call last):
  File "<string>", line 4, in <module>
  File "/home/jo/Logiciels/FreeCAD/free-cad-code/masterOCE17_build/Mod/Arch/importWebGL.py", line 116, in export
    html = getHTML(exportList)
  File "/home/jo/Logiciels/FreeCAD/free-cad-code/masterOCE17_build/Mod/Arch/importWebGL.py", line 128, in getHTML
    objectsData += getObjectData(obj)
  File "/home/jo/Logiciels/FreeCAD/free-cad-code/masterOCE17_build/Mod/Arch/importWebGL.py", line 174, in getObjectData
    wo = Part.Wire(DraftGeomUtils.sortEdges(w.Edges))
  File "/home/jo/Logiciels/FreeCAD/free-cad-code/masterOCE17_build/Mod/Draft/DraftGeomUtils.py", line 727, in sortEdges
    print("DraftGeomUtils.sortEdges failed - running old version")
<type 'exceptions.AttributeError'>: 'method_descriptor' object has no attribute 'write'
Stack Trace: Traceback (most recent call last):
  File "<string>", line 4, in <module>
  File "/home/jo/Logiciels/FreeCAD/free-cad-code/masterOCE17_build/Mod/Arch/importWebGL.py", line 116, in export
    html = getHTML(exportList)
  File "/home/jo/Logiciels/FreeCAD/free-cad-code/masterOCE17_build/Mod/Arch/importWebGL.py", line 128, in getHTML
    objectsData += getObjectData(obj)
  File "/home/jo/Logiciels/FreeCAD/free-cad-code/masterOCE17_build/Mod/Arch/importWebGL.py", line 174, in getObjectData
    wo = Part.Wire(DraftGeomUtils.sortEdges(w.Edges))
  File "/home/jo/Logiciels/FreeCAD/free-cad-code/masterOCE17_build/Mod/Draft/DraftGeomUtils.py", line 727, in sortEdges
    print("DraftGeomUtils.sortEdges failed - running old version")


I'm just trying to export a Part Box.

OS: Ubuntu 14.04.2 LTS
Word size of OS: 64-bit
Word size of FreeCAD: 64-bit
Version: 0.16.4701 (Git)
Branch: master
Hash: f6d50e6f33d71a533d18707aa3d74d0d45a5fc28
Python version: 2.7.6
Qt version: 4.8.6
Coin version: 4.0.0a
OCC version: 6.8.0.oce-0.17-dev
Formations et tutoriels FreeCAD en Français, soutenez moi : https://www.tipeee.com/formations-freecad
User avatar
yorik
Site Admin
Posts: 8563
Joined: Tue Feb 17, 2009 9:16 pm
Location: São Paulo, Brazil
Contact:

Re: WebGL export

Postby yorik » Fri Mar 27, 2015 5:08 pm

For me it works without error...
This line:

Code: Select all

<type 'exceptions.AttributeError'>: 'method_descriptor' object has no attribute 'write'

makes me think that the file object hasn't been correctly created and therefore has no write method... What is the filepath you're trying to write to?
User avatar
rockn
Posts: 1433
Joined: Wed Sep 28, 2011 10:39 am
Location: Toulouse, France
Contact:

Re: WebGL export

Postby rockn » Sun Mar 29, 2015 5:02 pm

Thanks to look at this.
yorik wrote:makes me think that the file object hasn't been correctly created and therefore has no write method... What is the filepath you're trying to write to?

Nothing particular. But I get this error (no attribute write) here and here. So I investigate in my mind and I remove the last custum workbench I had (CadQuery).
Now the export work but I still get this error in the report view :

Code: Select all

Traceback (most recent call last):
  File "<string>", line 4, in <module>
  File "/home/jo/Logiciels/FreeCAD/free-cad-code/masterOCE17_build/Mod/Arch/importWebGL.py", line 120, in export
    FreeCAD.Console.PrintMessage(translate("Arch","successfully written ")+filename+"\n")
<type 'exceptions.UnicodeDecodeError'>: 'ascii' codec can't decode byte 0xc3 in position 9: ordinal not in range(128)
Stack Trace: Traceback (most recent call last):
  File "<string>", line 4, in <module>
  File "/home/jo/Logiciels/FreeCAD/free-cad-code/masterOCE17_build/Mod/Arch/importWebGL.py", line 120, in export
    FreeCAD.Console.PrintMessage(translate("Arch","successfully written ")+filename+"\n")
Formations et tutoriels FreeCAD en Français, soutenez moi : https://www.tipeee.com/formations-freecad
User avatar
yorik
Site Admin
Posts: 8563
Joined: Tue Feb 17, 2009 9:16 pm
Location: São Paulo, Brazil
Contact:

Re: WebGL export

Postby yorik » Mon Mar 30, 2015 3:23 pm

rockn wrote:I remove the last custum workbench I had (CadQuery). Now the export work

This is weird :!:

The error you are getting in the console is probably because of the translation stuff... Probably someting not encoded the way the Console expects, I'll have a look at it
triplus
Posts: 4857
Joined: Mon Dec 12, 2011 4:45 pm

Re: WebGL export

Postby triplus » Sat Oct 17, 2015 9:50 pm

Today i struggled to understand why i get this issue when using a macro that worked fine in the past:

Code: Select all

object has no attribute 'write'


Luckily i found this forum thread and indeed removing CadQuery module removed that error. Therefore i am guessing i will report this to cadquery-freecad-module developer. Before i do that just one question:

https://github.com/jmwright/cadquery-fr ... InitGui.py

The issue must be introduced in this file?

P.S. As if i start FreeCAD and don't switch to CadQuery WB the issue occurs.
User avatar
microelly2
Posts: 2362
Joined: Tue Nov 12, 2013 4:06 pm
Contact:

Re: WebGL export

Postby microelly2 » Sun Oct 18, 2015 8:45 am

The cause for this error seems to be print calls in the code when the script is executed from a Qt Gui.
I had the same problem last days.
Go through the code and deactivate all prints.
I still have no idea to redirect print in such cases.