3 #include "WasmOcctView.h"
6 #include <Message_Messenger.hxx>
7 #include <Message_PrinterSystemLog.hxx>
8 #include <OSD_MemInfo.hxx>
9 #include <OSD_Parallel.hxx>
11 #include <AIS_Shape.hxx>
12 #include <BRepTools.hxx>
13 #include <BRep_Builder.hxx>
14 #include <Standard_ArrayStreamBuffer.hxx>
16 #include <emscripten.h>
17 #include <emscripten/html5.h>
19 //! Global viewer instance.
20 static WasmOcctView aViewer;
22 //! Dummy main loop callback for a single shot.
23 extern "C" void onMainLoop()
25 // do nothing here - viewer updates are handled on demand
26 emscripten_cancel_main_loop();
29 //! File data read event.
30 extern "C" void onFileDataRead (void* theOpaque, void* theBuffer, int theDataLen)
32 const char* aName = theOpaque != NULL ? (const char* )theOpaque : "";
34 AIS_ListOfInteractive aShapes;
35 aViewer.Context()->DisplayedObjects (AIS_KOI_Shape, -1, aShapes);
36 for (AIS_ListOfInteractive::Iterator aShapeIter (aShapes); aShapeIter.More(); aShapeIter.Next())
38 aViewer.Context()->Remove (aShapeIter.Value(), false);
42 Standard_ArrayStreamBuffer aStreamBuffer ((const char* )theBuffer, theDataLen);
43 std::istream aStream (&aStreamBuffer);
45 BRep_Builder aBuilder;
46 BRepTools::Read (aShape, aStream, aBuilder);
48 Handle(AIS_Shape) aShapePrs = new AIS_Shape (aShape);
49 aShapePrs->SetMaterial (Graphic3d_NOM_SILVER);
50 aViewer.Context()->Display (aShapePrs, AIS_Shaded, 0, false);
51 aViewer.View()->FitAll (0.01, false);
52 aViewer.View()->Redraw();
53 Message::DefaultMessenger()->Send (TCollection_AsciiString("Loaded file ") + aName, Message_Info);
54 Message::DefaultMessenger()->Send (OSD_MemInfo::PrintInfo(), Message_Trace);
57 //! File read error event.
58 static void onFileReadFailed (void* theOpaque)
60 const char* aName = (const char* )theOpaque;
61 Message::DefaultMessenger()->Send (TCollection_AsciiString("Error: unable to load file ") + aName, Message_Fail);
66 Message::DefaultMessenger()->Printers().First()->SetTraceLevel (Message_Trace);
67 Handle(Message_PrinterSystemLog) aJSConsolePrinter = new Message_PrinterSystemLog ("webgl-sample", Message_Trace);
68 Message::DefaultMessenger()->AddPrinter (aJSConsolePrinter); // open JavaScript console within the Browser to see this output
69 Message::DefaultMessenger()->Send (TCollection_AsciiString("NbLogicalProcessors: ") + OSD_Parallel::NbLogicalProcessors(), Message_Trace);
71 // setup a dummy single-shot main loop callback just to shut up a useless Emscripten error message on calling eglSwapInterval()
72 emscripten_set_main_loop (onMainLoop, -1, 0);
75 Message::DefaultMessenger()->Send (OSD_MemInfo::PrintInfo(), Message_Trace);
78 emscripten_async_wget_data ("samples/Ball.brep", (void* )"samples/Ball.brep", onFileDataRead, onFileReadFailed);