Commit 4595ada0 authored by Alexander Alekhin's avatar Alexander Alekhin

Merge pull request #1539 from paroj:ovisup

parents 7d4a4526 e52c4dbe
...@@ -49,11 +49,18 @@ public: ...@@ -49,11 +49,18 @@ public:
*/ */
CV_WRAP virtual void setBackground(InputArray image) = 0; CV_WRAP virtual void setBackground(InputArray image) = 0;
/// @overload
CV_WRAP_AS(setBackgroundColor) virtual void setBackground(const Scalar& color) = 0;
/** /**
* place an entity of an mesh in the scene * place an entity of an mesh in the scene
*
* the mesh needs to be created beforehand. Either programmatically
* by e.g. @ref createPointCloudMesh or by placing an Ogre .mesh file in a resource location.
* @see addResourceLocation
* @param name entity name * @param name entity name
* @param meshname mesh name * @param meshname mesh name
* @param rot Rodrigues vector or 3x3 rotation matrix * @param rot @ref Rodrigues vector or 3x3 rotation matrix
* @param tvec translation * @param tvec translation
*/ */
CV_WRAP virtual void createEntity(const String& name, const String& meshname, CV_WRAP virtual void createEntity(const String& name, const String& meshname,
...@@ -67,7 +74,7 @@ public: ...@@ -67,7 +74,7 @@ public:
* @param K intrinsic matrix * @param K intrinsic matrix
* @param imsize image size * @param imsize image size
* @param zFar far plane in camera coordinates * @param zFar far plane in camera coordinates
* @param rot Rodrigues vector or 3x3 rotation matrix * @param rot @ref Rodrigues vector or 3x3 rotation matrix
* @param tvec translation * @param tvec translation
* @return the extents of the Frustum at far plane, where the top left corner denotes the principal * @return the extents of the Frustum at far plane, where the top left corner denotes the principal
* point offset * point offset
...@@ -79,7 +86,7 @@ public: ...@@ -79,7 +86,7 @@ public:
/** /**
* creates a point light in the scene * creates a point light in the scene
* @param name entity name * @param name entity name
* @param rot Rodrigues vector or 3x3 rotation matrix * @param rot @ref Rodrigues vector or 3x3 rotation matrix
* @param tvec translation * @param tvec translation
* @param diffuseColor * @param diffuseColor
* @param specularColor * @param specularColor
...@@ -92,7 +99,7 @@ public: ...@@ -92,7 +99,7 @@ public:
/** /**
* update entity pose by transformation in the parent coordinate space. (pre-rotation) * update entity pose by transformation in the parent coordinate space. (pre-rotation)
* @param name entity name * @param name entity name
* @param rot Rodrigues vector or 3x3 rotation matrix * @param rot @ref Rodrigues vector or 3x3 rotation matrix
* @param tvec translation * @param tvec translation
*/ */
CV_WRAP virtual void updateEntityPose(const String& name, InputArray tvec = noArray(), CV_WRAP virtual void updateEntityPose(const String& name, InputArray tvec = noArray(),
...@@ -101,7 +108,7 @@ public: ...@@ -101,7 +108,7 @@ public:
/** /**
* set entity pose in the world coordinate space. * set entity pose in the world coordinate space.
* @param name enitity name * @param name enitity name
* @param rot Rodrigues vector or 3x3 rotation matrix * @param rot @ref Rodrigues vector or 3x3 rotation matrix
* @param tvec translation * @param tvec translation
* @param invert use the inverse of the given pose * @param invert use the inverse of the given pose
*/ */
...@@ -109,7 +116,7 @@ public: ...@@ -109,7 +116,7 @@ public:
InputArray rot = noArray(), bool invert = false) = 0; InputArray rot = noArray(), bool invert = false) = 0;
/** /**
* read back image of last call to @ref renderOneFrame * read back the image generated by the last call to @ref waitKey
*/ */
CV_WRAP virtual void getScreenshot(OutputArray frame) = 0; CV_WRAP virtual void getScreenshot(OutputArray frame) = 0;
...@@ -124,7 +131,7 @@ public: ...@@ -124,7 +131,7 @@ public:
/** /**
* Sets the current camera pose * Sets the current camera pose
* @param rot Rodrigues vector or 3x3 rotation matrix * @param rot @ref Rodrigues vector or 3x3 rotation matrix
* @param tvec translation * @param tvec translation
* @param invert use the inverse of the given pose * @param invert use the inverse of the given pose
*/ */
...@@ -168,21 +175,24 @@ CV_EXPORTS_W void addResourceLocation(const String& path); ...@@ -168,21 +175,24 @@ CV_EXPORTS_W void addResourceLocation(const String& path);
* create a new rendering window/ viewport * create a new rendering window/ viewport
* @param title window title * @param title window title
* @param size size of the window * @param size size of the window
* @param flags @see SceneSettings * @param flags a combination of @ref SceneSettings
*/ */
CV_EXPORTS_W Ptr<WindowScene> createWindow(const String& title, const Size& size, CV_EXPORTS_W Ptr<WindowScene> createWindow(const String& title, const Size& size,
int flags = SCENE_INTERACTIVE); int flags = SCENE_INTERACTIVE);
/** /**
* update all windows * update all windows and wait for keyboard event
* @return true if this functian can be called again (i.e. continue rendering). false otherwise. *
* @param delay 0 is the special value that means "forever".
* Any positive number returns after sync to blank (typically 16ms).
* @return the code of the pressed key or -1 if no key was pressed
*/ */
CV_EXPORTS_W bool renderOneFrame(); CV_EXPORTS_W int waitKey(int delay = 0);
/** /**
* set the property of a material to the given value * set the property of a material to the given value
* @param name material name * @param name material name
* @param prop property @ref MaterialProperty * @param prop @ref MaterialProperty
* @param value the value * @param value the value
*/ */
CV_EXPORTS_W void setMaterialProperty(const String& name, int prop, const Scalar& value); CV_EXPORTS_W void setMaterialProperty(const String& name, int prop, const Scalar& value);
......
...@@ -13,6 +13,7 @@ K = cv.getDefaultNewCameraMatrix(np.diag([800, 800, 1]), imsize, True) ...@@ -13,6 +13,7 @@ K = cv.getDefaultNewCameraMatrix(np.diag([800, 800, 1]), imsize, True)
cv.ovis.addResourceLocation("packs/Sinbad.zip") # shipped with Ogre cv.ovis.addResourceLocation("packs/Sinbad.zip") # shipped with Ogre
win = cv.ovis.createWindow("arucoAR", imsize, flags=0) win = cv.ovis.createWindow("arucoAR", imsize, flags=0)
win.setCameraIntrinsics(K, imsize)
win.createEntity("figure", "Sinbad.mesh", (0, 0, -5), (-1.57, 0, 0)) win.createEntity("figure", "Sinbad.mesh", (0, 0, -5), (-1.57, 0, 0))
win.createLightEntity("sun", (0, 0, -100)) win.createLightEntity("sun", (0, 0, -100))
...@@ -21,7 +22,7 @@ cap = cv.VideoCapture(0) ...@@ -21,7 +22,7 @@ cap = cv.VideoCapture(0)
cap.set(cv.CAP_PROP_FRAME_WIDTH, imsize[0]) cap.set(cv.CAP_PROP_FRAME_WIDTH, imsize[0])
cap.set(cv.CAP_PROP_FRAME_HEIGHT, imsize[1]) cap.set(cv.CAP_PROP_FRAME_HEIGHT, imsize[1])
while cv.ovis.renderOneFrame(): while cv.ovis.waitKey(1) != 27:
img = cap.read()[1] img = cap.read()[1]
win.setBackground(img) win.setBackground(img)
corners, ids = cv.aruco.detectMarkers(img, adict)[:2] corners, ids = cv.aruco.detectMarkers(img, adict)[:2]
......
...@@ -23,6 +23,6 @@ iwin.createEntity("figure", "Sinbad.mesh", (0, -5, 0)) ...@@ -23,6 +23,6 @@ iwin.createEntity("figure", "Sinbad.mesh", (0, -5, 0))
iwin.createLightEntity("sun", (0, 0, -100)) iwin.createLightEntity("sun", (0, 0, -100))
iwin.setCameraIntrinsics(K, imsize) iwin.setCameraIntrinsics(K, imsize)
while cv.ovis.renderOneFrame(): while cv.ovis.waitKey(1) != 27:
R, t = iwin.getCameraPose() R, t = iwin.getCameraPose()
owin.setEntityPose("cam", t, R) owin.setEntityPose("cam", t, R)
...@@ -136,17 +136,18 @@ static SceneNode* _getSceneNode(SceneManager* sceneMgr, const String& name) ...@@ -136,17 +136,18 @@ static SceneNode* _getSceneNode(SceneManager* sceneMgr, const String& name)
return mo->getParentSceneNode(); return mo->getParentSceneNode();
} }
struct Application : public OgreBites::ApplicationContext struct Application : public OgreBites::ApplicationContext, public OgreBites::InputListener
{ {
Ptr<LogManager> logMgr; Ptr<LogManager> logMgr;
Ogre::SceneManager* sceneMgr; Ogre::SceneManager* sceneMgr;
Ogre::String title; Ogre::String title;
uint32_t w; uint32_t w;
uint32_t h; uint32_t h;
int key_pressed;
Application(const Ogre::String& _title, const Size& sz) Application(const Ogre::String& _title, const Size& sz)
: OgreBites::ApplicationContext("ovis", false), sceneMgr(NULL), title(_title), w(sz.width), : OgreBites::ApplicationContext("ovis", false), sceneMgr(NULL), title(_title), w(sz.width),
h(sz.height) h(sz.height), key_pressed(-1)
{ {
logMgr.reset(new LogManager()); logMgr.reset(new LogManager());
logMgr->createLog("ovis.log", true, true, true); logMgr->createLog("ovis.log", true, true, true);
...@@ -158,6 +159,12 @@ struct Application : public OgreBites::ApplicationContext ...@@ -158,6 +159,12 @@ struct Application : public OgreBites::ApplicationContext
// empty impl to show cursor // empty impl to show cursor
} }
bool keyPressed(const OgreBites::KeyboardEvent& evt)
{
key_pressed = evt.keysym.sym;
return true;
}
bool oneTimeConfig() bool oneTimeConfig()
{ {
Ogre::RenderSystem* rs = getRoot()->getRenderSystemByName(RENDERSYSTEM_NAME); Ogre::RenderSystem* rs = getRoot()->getRenderSystemByName(RENDERSYSTEM_NAME);
...@@ -179,7 +186,11 @@ struct Application : public OgreBites::ApplicationContext ...@@ -179,7 +186,11 @@ struct Application : public OgreBites::ApplicationContext
miscParams["FSAA"] = "4"; miscParams["FSAA"] = "4";
miscParams["vsync"] = "true"; miscParams["vsync"] = "true";
return OgreBites::ApplicationContext::createWindow(_name, _w, _h, miscParams); OgreBites::NativeWindowPair ret =
OgreBites::ApplicationContext::createWindow(_name, _w, _h, miscParams);
addInputListener(ret.native, this); // handle input for all windows
return ret;
} }
void locateResources() void locateResources()
...@@ -212,7 +223,7 @@ struct Application : public OgreBites::ApplicationContext ...@@ -212,7 +223,7 @@ struct Application : public OgreBites::ApplicationContext
} }
}; };
class WindowSceneImpl : public WindowScene, public OgreBites::InputListener class WindowSceneImpl : public WindowScene
{ {
String title; String title;
Root* root; Root* root;
...@@ -266,8 +277,6 @@ public: ...@@ -266,8 +277,6 @@ public:
{ {
app->sceneMgr = sceneMgr; app->sceneMgr = sceneMgr;
rWin = app->getRenderWindow(); rWin = app->getRenderWindow();
app->addInputListener(this);
if (camman) if (camman)
app->addInputListener(camman.get()); app->addInputListener(camman.get());
} }
...@@ -277,8 +286,6 @@ public: ...@@ -277,8 +286,6 @@ public:
rWin = nwin.render; rWin = nwin.render;
if (camman) if (camman)
app->addInputListener(nwin.native, camman.get()); app->addInputListener(nwin.native, camman.get());
app->addInputListener(nwin.native, this);
} }
rWin->addViewport(cam); rWin->addViewport(cam);
...@@ -300,6 +307,12 @@ public: ...@@ -300,6 +307,12 @@ public:
rpass->getTextureUnitStates()[0]->setTextureName(name); rpass->getTextureUnitStates()[0]->setTextureName(name);
} }
void setBackground(const Scalar& color)
{
Mat img(1, 1, CV_8UC3, color);
setBackground(img);
}
void createEntity(const String& name, const String& meshname, InputArray tvec, InputArray rot) void createEntity(const String& name, const String& meshname, InputArray tvec, InputArray rot)
{ {
Entity* ent = sceneMgr->createEntity(name, meshname, RESOURCEGROUP_NAME); Entity* ent = sceneMgr->createEntity(name, meshname, RESOURCEGROUP_NAME);
...@@ -415,14 +428,6 @@ public: ...@@ -415,14 +428,6 @@ public:
rWin->copyContentsToMemory(pb, pb); rWin->copyContentsToMemory(pb, pb);
} }
bool keyPressed(const OgreBites::KeyboardEvent& evt)
{
if (evt.keysym.sym == SDLK_ESCAPE)
root->queueEndRendering();
return true;
}
void fixCameraYawAxis(bool useFixed, InputArray _up) void fixCameraYawAxis(bool useFixed, InputArray _up)
{ {
Vector3 up = Vector3::UNIT_Y; Vector3 up = Vector3::UNIT_Y;
...@@ -521,12 +526,18 @@ Ptr<WindowScene> createWindow(const String& title, const Size& size, int flags) ...@@ -521,12 +526,18 @@ Ptr<WindowScene> createWindow(const String& title, const Size& size, int flags)
return makePtr<WindowSceneImpl>(_app, title, size, flags); return makePtr<WindowSceneImpl>(_app, title, size, flags);
} }
CV_EXPORTS_W bool renderOneFrame() CV_EXPORTS_W int waitKey(int delay)
{ {
CV_Assert(_app); CV_Assert(_app);
_app->key_pressed = -1;
_app->getRoot()->renderOneFrame(); _app->getRoot()->renderOneFrame();
return not _app->getRoot()->endRenderingQueued();
// wait for keypress, using vsync instead of sleep
while(!delay && _app->key_pressed == -1)
_app->getRoot()->renderOneFrame();
return (_app->key_pressed != -1) ? (_app->key_pressed & 0xff) : -1;
} }
void setMaterialProperty(const String& name, int prop, const Scalar& val) void setMaterialProperty(const String& name, int prop, const Scalar& val)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment