0033661: Data Exchange, Step Import - Tessellated GDTs are not imported
[occt.git] / samples / webgl / WasmOcctView.h
CommitLineData
565baee6 1// Copyright (c) 2019 OPEN CASCADE SAS
2//
3// This file is part of the examples of the Open CASCADE Technology software library.
4//
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:
11//
12// The above copyright notice and this permission notice shall be included in all
13// copies or substantial portions of the Software.
14//
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
21
22#ifndef _WasmOcctView_HeaderFile
23#define _WasmOcctView_HeaderFile
24
25#include <AIS_InteractiveContext.hxx>
26#include <AIS_ViewController.hxx>
27#include <V3d_View.hxx>
28
29#include <emscripten.h>
30#include <emscripten/html5.h>
31
32class AIS_ViewCube;
33
34//! Sample class creating 3D Viewer within Emscripten canvas.
35class WasmOcctView : protected AIS_ViewController
36{
37public:
5de4b704 38
39 //! Return global viewer instance.
40 static WasmOcctView& Instance();
41
42public: //! @name methods exported by Module
43
44 //! Set cubemap background.
45 //! File will be loaded asynchronously.
46 //! @param theImagePath [in] image path to load
47 static void setCubemapBackground (const std::string& theImagePath);
48
49 //! Clear all named objects from viewer.
50 static void removeAllObjects();
51
52 //! Fit all/selected objects into view.
53 //! @param theAuto [in] fit selected objects (TRUE) or all objects (FALSE)
54 static void fitAllObjects (bool theAuto);
55
56 //! Remove named object from viewer.
57 //! @param theName [in] object name
58 //! @return FALSE if object was not found
59 static bool removeObject (const std::string& theName);
60
61 //! Temporarily hide named object.
62 //! @param theName [in] object name
63 //! @return FALSE if object was not found
64 static bool eraseObject (const std::string& theName);
65
66 //! Display temporarily hidden object.
67 //! @param theName [in] object name
68 //! @return FALSE if object was not found
69 static bool displayObject (const std::string& theName);
70
71 //! Show/hide ground.
72 //! @param theToShow [in] show or hide flag
73 static void displayGround (bool theToShow);
74
75 //! Open object from the given URL.
76 //! File will be loaded asynchronously.
77 //! @param theName [in] object name
78 //! @param theModelPath [in] model path
79 static void openFromUrl (const std::string& theName,
80 const std::string& theModelPath);
81
82 //! Open object from memory.
83 //! @param theName [in] object name
84 //! @param theBuffer [in] pointer to data
85 //! @param theDataLen [in] data length
86 //! @param theToFree [in] free theBuffer if set to TRUE
87 //! @return FALSE on reading error
88 static bool openFromMemory (const std::string& theName,
89 uintptr_t theBuffer, int theDataLen,
90 bool theToFree);
91
92 //! Open BRep object from memory.
93 //! @param theName [in] object name
94 //! @param theBuffer [in] pointer to data
95 //! @param theDataLen [in] data length
96 //! @param theToFree [in] free theBuffer if set to TRUE
97 //! @return FALSE on reading error
98 static bool openBRepFromMemory (const std::string& theName,
99 uintptr_t theBuffer, int theDataLen,
100 bool theToFree);
101
102public:
103
565baee6 104 //! Default constructor.
105 WasmOcctView();
106
107 //! Destructor.
108 virtual ~WasmOcctView();
109
110 //! Main application entry point.
111 void run();
112
113 //! Return interactive context.
114 const Handle(AIS_InteractiveContext)& Context() const { return myContext; }
115
116 //! Return view.
117 const Handle(V3d_View)& View() const { return myView; }
118
119 //! Return device pixel ratio for handling high DPI displays.
120 float DevicePixelRatio() const { return myDevicePixelRatio; }
121
5de4b704 122 //! Request view redrawing.
123 void UpdateView();
124
565baee6 125private:
126
127 //! Create window.
128 void initWindow();
129
130 //! Create 3D Viewer.
131 bool initViewer();
132
133 //! Fill 3D Viewer with a DEMO items.
134 void initDemoScene();
135
136 //! Application event loop.
137 void mainloop();
138
565baee6 139 //! Flush events and redraw view.
140 void redrawView();
141
142 //! Handle view redraw.
143 virtual void handleViewRedraw (const Handle(AIS_InteractiveContext)& theCtx,
144 const Handle(V3d_View)& theView) override;
145
f9ab9f7f 146 //! Schedule processing of window input events with the next repaint event.
147 virtual void ProcessInput() override;
148
149 //! Handle key down event.
150 virtual void KeyDown (Aspect_VKey theKey,
151 double theTime,
152 double thePressure) override;
153
154 //! Handle key up event.
155 virtual void KeyUp (Aspect_VKey theKey,
156 double theTime) override;
157
565baee6 158 //! Dump WebGL context information.
159 void dumpGlInfo (bool theIsBasic);
160
161 //! Initialize pixel scale ratio.
162 void initPixelScaleRatio();
163
565baee6 164//! @name Emscripten callbacks
165private:
166 //! Window resize event.
167 EM_BOOL onResizeEvent (int theEventType, const EmscriptenUiEvent* theEvent);
168
169 //! Mouse event.
170 EM_BOOL onMouseEvent (int theEventType, const EmscriptenMouseEvent* theEvent);
171
172 //! Scroll event.
173 EM_BOOL onWheelEvent (int theEventType, const EmscriptenWheelEvent* theEvent);
174
175 //! Touch event.
176 EM_BOOL onTouchEvent (int theEventType, const EmscriptenTouchEvent* theEvent);
177
178 //! Key down event.
179 EM_BOOL onKeyDownEvent (int theEventType, const EmscriptenKeyboardEvent* theEvent);
180
181 //! Key up event.
182 EM_BOOL onKeyUpEvent (int theEventType, const EmscriptenKeyboardEvent* theEvent);
183
5f69cfa7 184 //! Focus change event.
185 EM_BOOL onFocusEvent (int theEventType, const EmscriptenFocusEvent* theEvent);
186
565baee6 187//! @name Emscripten callbacks (static functions)
188private:
189
190 static EM_BOOL onResizeCallback (int theEventType, const EmscriptenUiEvent* theEvent, void* theView)
191 { return ((WasmOcctView* )theView)->onResizeEvent (theEventType, theEvent); }
192
193 static void onRedrawView (void* theView)
194 { return ((WasmOcctView* )theView)->redrawView(); }
195
196 static EM_BOOL onMouseCallback (int theEventType, const EmscriptenMouseEvent* theEvent, void* theView)
197 { return ((WasmOcctView* )theView)->onMouseEvent (theEventType, theEvent); }
198
199 static EM_BOOL onWheelCallback (int theEventType, const EmscriptenWheelEvent* theEvent, void* theView)
200 { return ((WasmOcctView* )theView)->onWheelEvent (theEventType, theEvent); }
201
202 static EM_BOOL onTouchCallback (int theEventType, const EmscriptenTouchEvent* theEvent, void* theView)
203 { return ((WasmOcctView* )theView)->onTouchEvent (theEventType, theEvent); }
204
205 static EM_BOOL onKeyDownCallback (int theEventType, const EmscriptenKeyboardEvent* theEvent, void* theView)
206 { return ((WasmOcctView* )theView)->onKeyDownEvent (theEventType, theEvent); }
207
208 static EM_BOOL onKeyUpCallback (int theEventType, const EmscriptenKeyboardEvent* theEvent, void* theView)
209 { return ((WasmOcctView* )theView)->onKeyUpEvent (theEventType, theEvent); }
210
5f69cfa7 211 static EM_BOOL onFocusCallback (int theEventType, const EmscriptenFocusEvent* theEvent, void* theView)
212 { return ((WasmOcctView* )theView)->onFocusEvent (theEventType, theEvent); }
213
e03a03fd 214private:
215
216 //! Register hot-keys for specified Action.
217 void addActionHotKeys (Aspect_VKey theAction,
218 unsigned int theHotKey1 = 0,
219 unsigned int theHotKey2 = 0,
220 unsigned int theHotKey3 = 0,
221 unsigned int theHotKey4 = 0,
222 unsigned int theHotKey5 = 0)
223 {
224 if (theHotKey1 != 0) { myNavKeyMap.Bind (theHotKey1, theAction); }
225 if (theHotKey2 != 0) { myNavKeyMap.Bind (theHotKey2, theAction); }
226 if (theHotKey3 != 0) { myNavKeyMap.Bind (theHotKey3, theAction); }
227 if (theHotKey4 != 0) { myNavKeyMap.Bind (theHotKey4, theAction); }
228 if (theHotKey5 != 0) { myNavKeyMap.Bind (theHotKey5, theAction); }
229 }
230
231 //! Handle navigation keys.
232 bool navigationKeyModifierSwitch (unsigned int theModifOld,
233 unsigned int theModifNew,
234 double theTimeStamp);
235
236 //! Handle hot-key.
237 bool processKeyPress (Aspect_VKey theKey);
238
565baee6 239private:
240
5de4b704 241 NCollection_IndexedDataMap<TCollection_AsciiString, Handle(AIS_InteractiveObject)> myObjects; //!< map of named objects
242
e03a03fd 243 NCollection_DataMap<unsigned int, Aspect_VKey> myNavKeyMap; //!< map of Hot-Key (key+modifiers) to Action
244
565baee6 245 Handle(AIS_InteractiveContext) myContext; //!< interactive context
246 Handle(V3d_View) myView; //!< 3D view
247 Handle(Prs3d_TextAspect) myTextStyle; //!< text style for OSD elements
248 Handle(AIS_ViewCube) myViewCube; //!< view cube object
249 TCollection_AsciiString myCanvasId; //!< canvas element id on HTML page
f9ab9f7f 250 Graphic3d_Vec2i myWinSizeOld;
565baee6 251 float myDevicePixelRatio; //!< device pixel ratio for handling high DPI displays
e3dae4a9 252 unsigned int myNbUpdateRequests; //!< counter for unhandled update requests
565baee6 253
254};
255
256#endif // _WasmOcctView_HeaderFile