1 // Copyright (c) 2019 OPEN CASCADE SAS
3 // This file is part of the examples of the Open CASCADE Technology software library.
5 // Permission is hereby granted, free of charge, to any person obtaining a copy
6 // of this software and associated documentation files (the "Software"), to deal
7 // in the Software without restriction, including without limitation the rights
8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 // copies of the Software, and to permit persons to whom the Software is
10 // furnished to do so, subject to the following conditions:
12 // The above copyright notice and this permission notice shall be included in all
13 // copies or substantial portions of the Software.
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE
22 #ifndef _WasmOcctView_HeaderFile
23 #define _WasmOcctView_HeaderFile
25 #include <AIS_InteractiveContext.hxx>
26 #include <AIS_ViewController.hxx>
27 #include <V3d_View.hxx>
29 #include <emscripten.h>
30 #include <emscripten/html5.h>
34 //! Sample class creating 3D Viewer within Emscripten canvas.
35 class WasmOcctView : protected AIS_ViewController
38 //! Default constructor.
42 virtual ~WasmOcctView();
44 //! Main application entry point.
47 //! Return interactive context.
48 const Handle(AIS_InteractiveContext)& Context() const { return myContext; }
51 const Handle(V3d_View)& View() const { return myView; }
53 //! Return device pixel ratio for handling high DPI displays.
54 float DevicePixelRatio() const { return myDevicePixelRatio; }
64 //! Fill 3D Viewer with a DEMO items.
67 //! Application event loop.
70 //! Request view redrawing.
73 //! Flush events and redraw view.
76 //! Handle view redraw.
77 virtual void handleViewRedraw (const Handle(AIS_InteractiveContext)& theCtx,
78 const Handle(V3d_View)& theView) override;
80 //! Dump WebGL context information.
81 void dumpGlInfo (bool theIsBasic);
83 //! Initialize pixel scale ratio.
84 void initPixelScaleRatio();
86 //! Return point from logical units to backing store.
87 Graphic3d_Vec2d convertPointToBacking (const Graphic3d_Vec2d& thePnt) const
89 return thePnt * myDevicePixelRatio;
92 //! Return point from logical units to backing store.
93 Graphic3d_Vec2i convertPointToBacking (const Graphic3d_Vec2i& thePnt) const
95 Graphic3d_Vec2d aPnt = Graphic3d_Vec2d (thePnt) * myDevicePixelRatio + Graphic3d_Vec2d (0.5);
96 return Graphic3d_Vec2i (aPnt);
99 //! @name Emscripten callbacks
101 //! Window resize event.
102 EM_BOOL onResizeEvent (int theEventType, const EmscriptenUiEvent* theEvent);
105 EM_BOOL onMouseEvent (int theEventType, const EmscriptenMouseEvent* theEvent);
108 EM_BOOL onWheelEvent (int theEventType, const EmscriptenWheelEvent* theEvent);
111 EM_BOOL onTouchEvent (int theEventType, const EmscriptenTouchEvent* theEvent);
114 EM_BOOL onKeyDownEvent (int theEventType, const EmscriptenKeyboardEvent* theEvent);
117 EM_BOOL onKeyUpEvent (int theEventType, const EmscriptenKeyboardEvent* theEvent);
119 //! @name Emscripten callbacks (static functions)
122 static EM_BOOL onResizeCallback (int theEventType, const EmscriptenUiEvent* theEvent, void* theView)
123 { return ((WasmOcctView* )theView)->onResizeEvent (theEventType, theEvent); }
125 static void onRedrawView (void* theView)
126 { return ((WasmOcctView* )theView)->redrawView(); }
128 static EM_BOOL onMouseCallback (int theEventType, const EmscriptenMouseEvent* theEvent, void* theView)
129 { return ((WasmOcctView* )theView)->onMouseEvent (theEventType, theEvent); }
131 static EM_BOOL onWheelCallback (int theEventType, const EmscriptenWheelEvent* theEvent, void* theView)
132 { return ((WasmOcctView* )theView)->onWheelEvent (theEventType, theEvent); }
134 static EM_BOOL onTouchCallback (int theEventType, const EmscriptenTouchEvent* theEvent, void* theView)
135 { return ((WasmOcctView* )theView)->onTouchEvent (theEventType, theEvent); }
137 static EM_BOOL onKeyDownCallback (int theEventType, const EmscriptenKeyboardEvent* theEvent, void* theView)
138 { return ((WasmOcctView* )theView)->onKeyDownEvent (theEventType, theEvent); }
140 static EM_BOOL onKeyUpCallback (int theEventType, const EmscriptenKeyboardEvent* theEvent, void* theView)
141 { return ((WasmOcctView* )theView)->onKeyUpEvent (theEventType, theEvent); }
145 Handle(AIS_InteractiveContext) myContext; //!< interactive context
146 Handle(V3d_View) myView; //!< 3D view
147 Handle(Prs3d_TextAspect) myTextStyle; //!< text style for OSD elements
148 Handle(AIS_ViewCube) myViewCube; //!< view cube object
149 TCollection_AsciiString myCanvasId; //!< canvas element id on HTML page
150 Aspect_Touch myClickTouch; //!< single touch position for handling clicks
151 OSD_Timer myDoubleTapTimer; //!< timer for handling double tap
152 float myDevicePixelRatio; //!< device pixel ratio for handling high DPI displays
153 unsigned int myUpdateRequests; //!< counter for unhandled update requests
157 #endif // _WasmOcctView_HeaderFile