0031668: Visualization - WebGL sample doesn't work on Emscripten 1.39
[occt.git] / samples / webgl / main.cpp
CommitLineData
565baee6 1#include <iostream>
2
3#include "WasmOcctView.h"
4
5#include <Message.hxx>
6#include <Message_Messenger.hxx>
b380b06c 7#include <Message_PrinterSystemLog.hxx>
565baee6 8#include <OSD_MemInfo.hxx>
9#include <OSD_Parallel.hxx>
10
11#include <AIS_Shape.hxx>
12#include <BRepTools.hxx>
13#include <BRep_Builder.hxx>
14#include <Standard_ArrayStreamBuffer.hxx>
15
16#include <emscripten.h>
17#include <emscripten/html5.h>
18
19//! Global viewer instance.
20static WasmOcctView aViewer;
21
7465bfa6 22//! Dummy main loop callback for a single shot.
23extern "C" void onMainLoop()
24{
25 // do nothing here - viewer updates are handled on demand
26 emscripten_cancel_main_loop();
27}
28
565baee6 29//! File data read event.
30extern "C" void onFileDataRead (void* theOpaque, void* theBuffer, int theDataLen)
31{
32 const char* aName = theOpaque != NULL ? (const char* )theOpaque : "";
33 {
34 AIS_ListOfInteractive aShapes;
35 aViewer.Context()->DisplayedObjects (AIS_KOI_Shape, -1, aShapes);
36 for (AIS_ListOfInteractive::Iterator aShapeIter (aShapes); aShapeIter.More(); aShapeIter.Next())
37 {
38 aViewer.Context()->Remove (aShapeIter.Value(), false);
39 }
40 }
41
42 Standard_ArrayStreamBuffer aStreamBuffer ((const char* )theBuffer, theDataLen);
43 std::istream aStream (&aStreamBuffer);
44 TopoDS_Shape aShape;
45 BRep_Builder aBuilder;
46 BRepTools::Read (aShape, aStream, aBuilder);
47
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);
55}
56
57//! File read error event.
58static void onFileReadFailed (void* theOpaque)
59{
60 const char* aName = (const char* )theOpaque;
61 Message::DefaultMessenger()->Send (TCollection_AsciiString("Error: unable to load file ") + aName, Message_Fail);
62}
63
64int main()
65{
66 Message::DefaultMessenger()->Printers().First()->SetTraceLevel (Message_Trace);
b380b06c 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
565baee6 69 Message::DefaultMessenger()->Send (TCollection_AsciiString("NbLogicalProcessors: ") + OSD_Parallel::NbLogicalProcessors(), Message_Trace);
7465bfa6 70
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);
73
565baee6 74 aViewer.run();
75 Message::DefaultMessenger()->Send (OSD_MemInfo::PrintInfo(), Message_Trace);
76
77 // load some file
78 emscripten_async_wget_data ("samples/Ball.brep", (void* )"samples/Ball.brep", onFileDataRead, onFileReadFailed);
79 return 0;
80}