ovis.hpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. // This file is part of OpenCV project.
  2. // It is subject to the license terms in the LICENSE file found in the top-level directory
  3. // of this distribution and at http://opencv.org/license.html.
  4. #ifndef _OPENCV_OVIS_H_
  5. #define _OPENCV_OVIS_H_
  6. #include <opencv2/core.hpp>
  7. /**
  8. @defgroup ovis OGRE 3D Visualiser
  9. ovis is a simplified rendering wrapper around [ogre3d](https://www.ogre3d.org/).
  10. The [Ogre terminology](https://ogrecave.github.io/ogre/api/latest/_the-_core-_objects.html) is used in the API
  11. and [Ogre Script](https://ogrecave.github.io/ogre/api/latest/_scripts.html) is assumed to be used for advanced customization.
  12. Besides the API you see here, there are several environment variables that control the behavior of ovis.
  13. They are documented in @ref createWindow.
  14. ## Loading geometry
  15. You can create geometry [on the fly](@ref createTriangleMesh) or by loading Ogre `.mesh` files.
  16. ### Blender
  17. For converting/ creating geometry [Blender](https://www.blender.org/) is recommended.
  18. - Blender 2.7x is better tested, but Blender 2.8x should work too
  19. - install [blender2ogre](https://github.com/OGRECave/blender2ogre) matching your Blender version
  20. - download the [Ogre MSVC SDK](https://www.ogre3d.org/download/sdk/sdk-ogre) which contains `OgreXMLConverter.exe` (in `bin/`) and set the path in the blender2ogre settings
  21. - get [ogre-meshviewer](https://github.com/OGRECave/ogre-meshviewer) to enable the preview function in blender2ogre as well as for verifying the exported files
  22. - in case the exported materials are not exactly how you want them, consult the [Ogre Manual](https://ogrecave.github.io/ogre/api/latest/_material-_scripts.html)
  23. ### Assimp
  24. When using Ogre 1.12.9 or later, enabling the Assimp plugin allows to load arbitrary geometry.
  25. Simply pass `bunny.obj` instead of `bunny.mesh` as `meshname` in @ref WindowScene::createEntity.
  26. You should still use ogre-meshviewer to verify that the geometry is converted correctly.
  27. */
  28. namespace cv {
  29. namespace ovis {
  30. //! @addtogroup ovis
  31. //! @{
  32. enum SceneSettings
  33. {
  34. /// the window will use a separate scene. The scene will be shared otherwise.
  35. SCENE_SEPARATE = 1,
  36. /// allow the user to control the camera.
  37. SCENE_INTERACTIVE = 2,
  38. /// draw coordinate system crosses for debugging
  39. SCENE_SHOW_CS_CROSS = 4,
  40. /// Apply anti-aliasing. The first window determines the setting for all windows.
  41. SCENE_AA = 8,
  42. /// Render off-screen without a window. Allows separate AA setting. Requires manual update via @ref WindowScene::update
  43. SCENE_OFFSCREEN = 16,
  44. /// Enable real-time shadows in the scene. All entities cast shadows by default. Control via @ref ENTITY_CAST_SHADOWS
  45. SCENE_SHADOWS = 32
  46. };
  47. enum MaterialProperty
  48. {
  49. MATERIAL_POINT_SIZE,
  50. MATERIAL_LINE_WIDTH,
  51. MATERIAL_OPACITY,
  52. MATERIAL_EMISSIVE,
  53. MATERIAL_DIFFUSE,
  54. MATERIAL_TEXTURE0,
  55. MATERIAL_TEXTURE = MATERIAL_TEXTURE0,
  56. MATERIAL_TEXTURE1,
  57. MATERIAL_TEXTURE2,
  58. MATERIAL_TEXTURE3,
  59. };
  60. enum EntityProperty
  61. {
  62. ENTITY_MATERIAL,
  63. ENTITY_SCALE,
  64. ENTITY_AABB_WORLD,
  65. ENTITY_ANIMBLEND_MODE,
  66. ENTITY_CAST_SHADOWS
  67. };
  68. /**
  69. * A 3D viewport and the associated scene
  70. */
  71. class CV_EXPORTS_W WindowScene {
  72. public:
  73. virtual ~WindowScene();
  74. /**
  75. * set window background to custom image/ color
  76. * @param image
  77. */
  78. CV_WRAP virtual void setBackground(InputArray image) = 0;
  79. /// @overload
  80. CV_WRAP_AS(setBackgroundColor) virtual void setBackground(const Scalar& color) = 0;
  81. /**
  82. * enable an ordered chain of full-screen post processing effects
  83. *
  84. * this way you can add distortion or SSAO effects.
  85. * The effects themselves must be defined inside Ogre .compositor scripts.
  86. * @param names compositor names that will be applied in order of appearance
  87. * @see addResourceLocation
  88. */
  89. CV_WRAP virtual void setCompositors(const std::vector<String>& names) = 0;
  90. /**
  91. * place an entity of a mesh in the scene
  92. *
  93. * the mesh needs to be created beforehand. Either programmatically
  94. * by e.g. @ref createPointCloudMesh or by placing the respective file in a resource location.
  95. * @param name entity name
  96. * @param meshname mesh name
  97. * @param tvec translation
  98. * @param rot @ref Rodrigues vector or 3x3 rotation matrix
  99. * @see addResourceLocation
  100. */
  101. CV_WRAP virtual void createEntity(const String& name, const String& meshname,
  102. InputArray tvec = noArray(), InputArray rot = noArray()) = 0;
  103. /**
  104. * remove an entity from the scene
  105. * @param name entity name
  106. */
  107. CV_WRAP virtual void removeEntity(const String& name) = 0;
  108. /**
  109. * set the property of an entity to the given value
  110. * @param name entity name
  111. * @param prop @ref EntityProperty
  112. * @param value the value
  113. * @param subEntityIdx index of the sub-entity (default: all)
  114. */
  115. CV_WRAP virtual void setEntityProperty(const String& name, int prop, const String& value,
  116. int subEntityIdx = -1) = 0;
  117. /// @overload
  118. CV_WRAP virtual void setEntityProperty(const String& name, int prop, const Scalar& value) = 0;
  119. /**
  120. * get the property of an entity
  121. * @param name entity name
  122. * @param prop @ref EntityProperty
  123. * @param value the value
  124. */
  125. CV_WRAP virtual void getEntityProperty(const String& name, int prop, OutputArray value) = 0;
  126. /**
  127. * convenience method to visualize a camera position
  128. *
  129. * @param name entity name
  130. * @param K intrinsic matrix
  131. * @param imsize image size
  132. * @param zFar far plane in camera coordinates
  133. * @param tvec translation
  134. * @param rot @ref Rodrigues vector or 3x3 rotation matrix
  135. * @param color line color
  136. * @return the extents of the Frustum at far plane, where the top left corner denotes the principal
  137. * point offset
  138. */
  139. CV_WRAP virtual Rect2d createCameraEntity(const String& name, InputArray K, const Size& imsize,
  140. float zFar, InputArray tvec = noArray(),
  141. InputArray rot = noArray(),
  142. const Scalar& color = Scalar::all(1)) = 0;
  143. /**
  144. * creates a point light in the scene
  145. * @param name entity name
  146. * @param tvec translation
  147. * @param rot @ref Rodrigues vector or 3x3 rotation matrix
  148. * @param diffuseColor
  149. * @param specularColor
  150. */
  151. CV_WRAP virtual void createLightEntity(const String& name, InputArray tvec = noArray(),
  152. InputArray rot = noArray(),
  153. const Scalar& diffuseColor = Scalar::all(1),
  154. const Scalar& specularColor = Scalar::all(1)) = 0;
  155. /**
  156. * update entity pose by transformation in the parent coordinate space. (pre-rotation)
  157. * @param name entity name
  158. * @param tvec translation
  159. * @param rot @ref Rodrigues vector or 3x3 rotation matrix
  160. */
  161. CV_WRAP virtual void updateEntityPose(const String& name, InputArray tvec = noArray(),
  162. InputArray rot = noArray()) = 0;
  163. /**
  164. * set entity pose in the world coordinate space.
  165. * @param name enitity name
  166. * @param tvec translation
  167. * @param rot @ref Rodrigues vector or 3x3 rotation matrix
  168. * @param invert use the inverse of the given pose
  169. */
  170. CV_WRAP virtual void setEntityPose(const String& name, InputArray tvec = noArray(),
  171. InputArray rot = noArray(), bool invert = false) = 0;
  172. /**
  173. * Retrieves the current pose of an entity
  174. * @param name entity name
  175. * @param R 3x3 rotation matrix
  176. * @param tvec translation vector
  177. * @param invert return the inverted pose
  178. */
  179. CV_WRAP virtual void getEntityPose(const String& name, OutputArray R = noArray(), OutputArray tvec = noArray(),
  180. bool invert = false) = 0;
  181. /**
  182. * get a list of available entity animations
  183. * @param name entity name
  184. * @param out the animation names
  185. */
  186. CV_WRAP virtual void getEntityAnimations(const String& name, std::vector<String>& out) = 0;
  187. /**
  188. * play entity animation
  189. * @param name entity name
  190. * @param animname animation name
  191. * @param loop enable or disable animation loop
  192. * @see getEntityAnimations
  193. */
  194. CV_WRAP virtual void playEntityAnimation(const String& name, const String& animname,
  195. bool loop = true) = 0;
  196. /**
  197. * stop entity animation
  198. * @param name enitity name
  199. * @param animname animation name
  200. */
  201. CV_WRAP virtual void stopEntityAnimation(const String& name, const String& animname) = 0;
  202. /**
  203. * read back the image generated by the last call to @ref waitKey
  204. */
  205. CV_WRAP virtual void getScreenshot(OutputArray frame) = 0;
  206. /**
  207. * read back the texture of an active compositor
  208. * @param compname name of the compositor
  209. * @param texname name of the texture inside the compositor
  210. * @param mrtIndex if texture is a MRT, specifies the attachment
  211. * @param out the texture contents
  212. */
  213. CV_WRAP virtual void getCompositorTexture(const String& compname, const String& texname,
  214. OutputArray out, int mrtIndex = 0) = 0;
  215. /**
  216. * get the depth for the current frame.
  217. *
  218. * return the per pixel distance to the camera in world units
  219. */
  220. CV_WRAP virtual void getDepth(OutputArray depth) = 0;
  221. /**
  222. * convenience method to force the "up" axis to stay fixed
  223. *
  224. * works with both programmatic changes and SCENE_INTERACTIVE
  225. * @param useFixed whether to enforce the fixed yaw axis
  226. * @param up the axis to be fixed
  227. */
  228. CV_WRAP virtual void fixCameraYawAxis(bool useFixed, InputArray up = noArray()) = 0;
  229. /**
  230. * Sets the current camera pose
  231. * @param tvec translation
  232. * @param rot @ref Rodrigues vector or 3x3 rotation matrix
  233. * @param invert use the inverse of the given pose
  234. */
  235. CV_WRAP virtual void setCameraPose(InputArray tvec = noArray(), InputArray rot = noArray(),
  236. bool invert = false) = 0;
  237. /**
  238. * convenience method to orient the camera to a specific entity
  239. * @param target entity name
  240. * @param offset offset from entity centre
  241. */
  242. CV_WRAP virtual void setCameraLookAt(const String& target, InputArray offset = noArray()) = 0;
  243. /**
  244. * convenience method to orient an entity to a specific entity.
  245. * If target is an empty string the entity looks at the given offset point
  246. * @param origin entity to make look at
  247. * @param target name of target entity
  248. * @param offset offset from entity centre
  249. */
  250. CV_WRAP virtual void setEntityLookAt(const String& origin, const String& target, InputArray offset = noArray()) = 0;
  251. /**
  252. * Retrieves the current camera pose
  253. * @param R 3x3 rotation matrix
  254. * @param tvec translation vector
  255. * @param invert return the inverted pose
  256. */
  257. CV_WRAP virtual void getCameraPose(OutputArray R = noArray(), OutputArray tvec = noArray(),
  258. bool invert = false) = 0;
  259. /**
  260. * set intrinsics of the camera
  261. *
  262. * @param K intrinsic matrix or noArray(). If noArray() is specified, imsize
  263. * is ignored and zNear/ zFar can be set separately.
  264. * @param imsize image size
  265. * @param zNear near clip distance or -1 to keep the current
  266. * @param zFar far clip distance or -1 to keep the current
  267. */
  268. CV_WRAP virtual void setCameraIntrinsics(InputArray K, const Size& imsize,
  269. float zNear = -1,
  270. float zFar = -1) = 0;
  271. /**
  272. * render this window, but do not swap buffers. Automatically called by @ref ovis::waitKey
  273. */
  274. CV_WRAP virtual void update() = 0;
  275. };
  276. /**
  277. * Add an additional resource location that is search for meshes, textures and materials
  278. *
  279. * must be called before the first createWindow. If give path does not exist, retries inside
  280. * Ogre Media Directory.
  281. * @param path folder or Zip archive.
  282. */
  283. CV_EXPORTS_W void addResourceLocation(const String& path);
  284. /**
  285. * create a new rendering window/ viewport
  286. * @param title window title
  287. * @param size size of the window
  288. * @param flags a combination of @ref SceneSettings
  289. *
  290. * Furthermore, the behavior is controlled by the following environment variables
  291. * - OPENCV_OVIS_VERBOSE_LOG: print all of OGRE log output
  292. * - OPENCV_OVIS_RENDERSYSTEM: the name of the OGRE RenderSystem to use
  293. * - OPENCV_OVIS_NOVSYNC: disable VSYNC for all windows
  294. */
  295. CV_EXPORTS_W Ptr<WindowScene> createWindow(const String& title, const Size& size,
  296. int flags = SCENE_INTERACTIVE | SCENE_AA);
  297. /**
  298. * update all windows and wait for keyboard event
  299. *
  300. * @param delay 0 is the special value that means "forever".
  301. * Any positive number returns after sync to blank (typically 16ms).
  302. * @return the code of the pressed key or -1 if no key was pressed
  303. */
  304. CV_EXPORTS_W int waitKey(int delay = 0);
  305. /**
  306. * set the property of a material to the given value
  307. * @param name material name
  308. * @param prop @ref MaterialProperty
  309. * @param value the value
  310. */
  311. CV_EXPORTS_W void setMaterialProperty(const String& name, int prop, const Scalar& value);
  312. /// @overload
  313. CV_EXPORTS_W void setMaterialProperty(const String& name, int prop, const String& value);
  314. /**
  315. * set the texture of a material to the given value
  316. * @param name material name
  317. * @param prop @ref MaterialProperty
  318. * @param value the texture data
  319. */
  320. CV_EXPORTS_AS(setMaterialTexture) void setMaterialProperty(const String& name, int prop, InputArray value);
  321. /**
  322. * set the shader property of a material to the given value
  323. * @param name material name
  324. * @param prop property name
  325. * @param value the value
  326. */
  327. CV_EXPORTS_W void setMaterialProperty(const String& name, const String& prop, const Scalar& value);
  328. /**
  329. * create a 2D plane, X right, Y down, Z up
  330. *
  331. * creates a material with the same name
  332. * @param name name of the mesh
  333. * @param size size in world units
  334. * @param image optional texture
  335. */
  336. CV_EXPORTS_W void createPlaneMesh(const String& name, const Size2f& size, InputArray image = noArray());
  337. /**
  338. * creates a point cloud mesh
  339. *
  340. * creates a material with the same name
  341. * @param name name of the mesh
  342. * @param vertices float vector of positions
  343. * @param colors uchar vector of colors
  344. */
  345. CV_EXPORTS_W void createPointCloudMesh(const String& name, InputArray vertices, InputArray colors = noArray());
  346. /**
  347. * creates a grid
  348. *
  349. * creates a material with the same name
  350. * @param name name of the mesh
  351. * @param size extents of the grid
  352. * @param segments number of segments per side
  353. */
  354. CV_EXPORTS_W void createGridMesh(const String& name, const Size2f& size, const Size& segments = Size(1, 1));
  355. /**
  356. * creates a triangle mesh from vertex-vertex or face-vertex representation
  357. *
  358. * creates a material with the same name
  359. * @param name name of the mesh
  360. * @param vertices float vector of positions
  361. * @param normals float vector of normals
  362. * @param indices int vector of indices
  363. */
  364. CV_EXPORTS_W void createTriangleMesh(const String& name, InputArray vertices, InputArray normals = noArray(), InputArray indices = noArray());
  365. /// @deprecated use setMaterialProperty
  366. CV_EXPORTS_W void updateTexture(const String& name, InputArray image);
  367. //! @}
  368. }
  369. }
  370. #endif