ovis.cpp 24.4 KB
Newer Older
1 2 3 4 5 6 7 8 9
// This file is part of OpenCV project.
// It is subject to the license terms in the LICENSE file found in the top-level directory
// of this distribution and at http://opencv.org/license.html.

#include "precomp.hpp"

#include <OgreApplicationContext.h>
#include <OgreCameraMan.h>
#include <OgreRectangle2D.h>
10
#include <OgreCompositorManager.h>
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26

#include <opencv2/calib3d.hpp>

namespace cv
{
namespace ovis
{
using namespace Ogre;

const char* RESOURCEGROUP_NAME = "OVIS";
Ptr<Application> _app;

static const char* RENDERSYSTEM_NAME = "OpenGL 3+ Rendering Subsystem";
static std::vector<String> _extraResourceLocations;

// convert from OpenCV to Ogre coordinates:
27
static Quaternion toOGRE(Degree(180), Vector3::UNIT_X);
28 29 30 31 32 33
static Vector2 toOGRE_SS = Vector2(1, -1);

WindowScene::~WindowScene() {}

void _createTexture(const String& name, Mat image)
{
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
    PixelFormat format;
    switch(image.type())
    {
    case CV_8UC4:
        format = PF_BYTE_BGRA;
        break;
    case CV_8UC3:
        format = PF_BYTE_BGR;
        break;
    case CV_8UC1:
        format = PF_BYTE_L;
        break;
    default:
        CV_Error(Error::StsBadArg, "currently only CV_8UC1, CV_8UC3, CV_8UC4 textures are supported");
        break;
    }

51 52 53 54
    TextureManager& texMgr = TextureManager::getSingleton();
    TexturePtr tex = texMgr.getByName(name, RESOURCEGROUP_NAME);

    Image im;
55
    im.loadDynamicImage(image.ptr(), image.cols, image.rows, 1, format);
56 57 58 59 60 61 62 63 64 65 66 67

    if (tex)
    {
        // update
        PixelBox box = im.getPixelBox();
        tex->getBuffer()->blitFromMemory(box, box);
        return;
    }

    texMgr.loadImage(name, RESOURCEGROUP_NAME, im);
}

68
static void _convertRT(InputArray rot, InputArray tvec, Quaternion& q, Vector3& t, bool invert = false)
69
{
70 71
    CV_Assert_N(rot.empty() || rot.rows() == 3 || rot.size() == Size(3, 3),
                tvec.empty() || tvec.rows() == 3);
72

73
    q = Quaternion::IDENTITY;
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
    t = Vector3::ZERO;

    if (!rot.empty())
    {
        Mat _R;

        if (rot.size() == Size(3, 3))
        {
            _R = rot.getMat();
        }
        else
        {
            Rodrigues(rot, _R);
        }

        Matrix3 R;
        _R.copyTo(Mat_<Real>(3, 3, R[0]));
91
        q = Quaternion(R);
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117

        if (invert)
        {
            q = q.Inverse();
        }
    }

    if (!tvec.empty())
    {
        tvec.copyTo(Mat_<Real>(3, 1, t.ptr()));

        if(invert)
        {
            t = q * -t;
        }
    }
}

static void _setCameraIntrinsics(Camera* cam, InputArray _K, const Size& imsize)
{
    CV_Assert(_K.size() == Size(3, 3));

    cam->setAspectRatio(float(imsize.width) / imsize.height);

    Matx33f K = _K.getMat();

fegorsch's avatar
fegorsch committed
118 119 120 121 122
    float zNear = cam->getNearClipDistance();
    float top = zNear * K(1, 2) / K(1, 1);
    float left = -zNear * K(0, 2) / K(0, 0);
    float right = zNear * (imsize.width - K(0, 2)) / K(0, 0);
    float bottom = -zNear * (imsize.height - K(1, 2)) / K(1, 1);
123 124 125 126 127 128

    // use frustum extents instead of setFrustumOffset as the latter
    // assumes centered FOV, which is not the case
    cam->setFrustumExtents(left, right, top, bottom);

    // top and bottom parts of the FOV
129 130 131 132
    float fovy = atan2(K(1, 2), K(1, 1)) + atan2(imsize.height - K(1, 2), K(1, 1));
    cam->setFOVy(Radian(fovy));
}

133
static SceneNode& _getSceneNode(SceneManager* sceneMgr, const String& name)
134 135 136 137 138
{
    MovableObject* mo = NULL;

    try
    {
139
        mo = sceneMgr->getMovableObject(name, "Camera");
140 141 142 143

        // with cameras we have an extra CS flip node
        if(mo)
            return *mo->getParentSceneNode()->getParentSceneNode();
144 145 146 147 148 149 150 151 152
    }
    catch (ItemIdentityException&)
    {
        // ignore
    }

    try
    {
        if (!mo)
153
            mo = sceneMgr->getMovableObject(name, "Light");
154 155 156 157 158 159 160
    }
    catch (ItemIdentityException&)
    {
        // ignore
    }

    if (!mo)
161
        mo = sceneMgr->getMovableObject(name, "Entity"); // throws if not found
162

163
    return *mo->getParentSceneNode();
164 165
}

166
struct Application : public OgreBites::ApplicationContext, public OgreBites::InputListener
167
{
168
    Ptr<LogManager> logMgr;
169 170 171 172
    Ogre::SceneManager* sceneMgr;
    Ogre::String title;
    uint32_t w;
    uint32_t h;
173
    int key_pressed;
174
    int flags;
175

176
    Application(const Ogre::String& _title, const Size& sz, int _flags)
177
        : OgreBites::ApplicationContext("ovis", false), sceneMgr(NULL), title(_title), w(sz.width),
178
          h(sz.height), key_pressed(-1), flags(_flags)
179
    {
180 181 182
        logMgr.reset(new LogManager());
        logMgr->createLog("ovis.log", true, true, true);
        logMgr->setLogDetail(LL_LOW);
183 184 185 186 187 188 189
    }

    void setupInput(bool /*grab*/)
    {
        // empty impl to show cursor
    }

190
    bool keyPressed(const OgreBites::KeyboardEvent& evt) CV_OVERRIDE
191 192 193 194 195
    {
        key_pressed = evt.keysym.sym;
        return true;
    }

196
    bool oneTimeConfig() CV_OVERRIDE
197 198 199 200 201 202 203 204
    {
        Ogre::RenderSystem* rs = getRoot()->getRenderSystemByName(RENDERSYSTEM_NAME);
        CV_Assert(rs);
        getRoot()->setRenderSystem(rs);
        return true;
    }

    OgreBites::NativeWindowPair createWindow(const Ogre::String& name, uint32_t _w, uint32_t _h,
205
                                             NameValuePairList miscParams = NameValuePairList()) CV_OVERRIDE
206 207 208 209 210 211 212 213
    {
        Ogre::String _name = name;
        if (!sceneMgr)
        {
            _w = w;
            _h = h;
            _name = title;
        }
214 215 216 217

        if (flags & SCENE_AA)
            miscParams["FSAA"] = "4";

218 219
        miscParams["vsync"] = "true";

220 221 222 223 224
        OgreBites::NativeWindowPair ret =
            OgreBites::ApplicationContext::createWindow(_name, _w, _h, miscParams);
        addInputListener(ret.native, this); // handle input for all windows

        return ret;
225 226
    }

227
    void locateResources() CV_OVERRIDE
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
    {
        OgreBites::ApplicationContext::locateResources();
        ResourceGroupManager& rgm = ResourceGroupManager::getSingleton();
        rgm.createResourceGroup(RESOURCEGROUP_NAME);

        for (size_t i = 0; i < _extraResourceLocations.size(); i++)
        {
            String loc = _extraResourceLocations[i];
            String type = StringUtil::endsWith(loc, ".zip") ? "Zip" : "FileSystem";

            if (!FileSystemLayer::fileExists(loc))
            {
                loc = FileSystemLayer::resolveBundlePath(getDefaultMediaDir() + "/" + loc);
            }

            rgm.addResourceLocation(loc, type, RESOURCEGROUP_NAME);
        }
    }

247
    void setup() CV_OVERRIDE
248 249 250 251 252 253 254 255 256
    {
        OgreBites::ApplicationContext::setup();

        MaterialManager& matMgr = MaterialManager::getSingleton();
        matMgr.setDefaultTextureFiltering(TFO_ANISOTROPIC);
        matMgr.setDefaultAnisotropy(16);
    }
};

257
class WindowSceneImpl : public WindowScene
258 259 260 261 262 263 264 265 266
{
    String title;
    Root* root;
    SceneManager* sceneMgr;
    SceneNode* camNode;
    RenderWindow* rWin;
    Ptr<OgreBites::CameraMan> camman;
    Ptr<Rectangle2D> bgplane;

267
    Ogre::RenderTarget* frameSrc;
268
    Ogre::RenderTarget* depthRTT;
269 270
public:
    WindowSceneImpl(Ptr<Application> app, const String& _title, const Size& sz, int flags)
271
        : title(_title), root(app->getRoot()), depthRTT(NULL)
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
    {
        if (!app->sceneMgr)
        {
            flags |= SCENE_SEPERATE;
        }

        if (flags & SCENE_SEPERATE)
        {
            sceneMgr = root->createSceneManager("DefaultSceneManager", title);
            RTShader::ShaderGenerator& shadergen = RTShader::ShaderGenerator::getSingleton();
            shadergen.addSceneManager(sceneMgr); // must be done before we do anything with the scene

            sceneMgr->setAmbientLight(ColourValue(.1, .1, .1));
            _createBackground();
        }
        else
        {
            sceneMgr = app->sceneMgr;
        }

        if(flags & SCENE_SHOW_CS_CROSS)
        {
            sceneMgr->setDisplaySceneNodes(true);
        }

        Camera* cam = sceneMgr->createCamera(title);
        cam->setNearClipDistance(0.5);
        cam->setAutoAspectRatio(true);
        camNode = sceneMgr->getRootSceneNode()->createChildSceneNode();
301
        camNode->setOrientation(toOGRE);
302 303 304 305 306 307
        camNode->attachObject(cam);

        if (flags & SCENE_INTERACTIVE)
        {
            camman.reset(new OgreBites::CameraMan(camNode));
            camman->setStyle(OgreBites::CS_ORBIT);
308
            camNode->setFixedYawAxis(true, Vector3::NEGATIVE_UNIT_Y);
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
        }

        if (!app->sceneMgr)
        {
            app->sceneMgr = sceneMgr;
            rWin = app->getRenderWindow();
            if (camman)
                app->addInputListener(camman.get());
        }
        else
        {
            OgreBites::NativeWindowPair nwin = app->createWindow(title, sz.width, sz.height);
            rWin = nwin.render;
            if (camman)
                app->addInputListener(nwin.native, camman.get());
        }

        rWin->addViewport(cam);
327 328 329 330 331 332 333 334 335 336 337 338
        frameSrc = rWin;

        if (flags & SCENE_RENDER_FLOAT)
        {
            // also render into an offscreen texture
            // currently this draws everything twice, but we spare the float->byte conversion for display
            TexturePtr tex = TextureManager::getSingleton().createManual(
                title + "_rt", RESOURCEGROUP_NAME, TEX_TYPE_2D, sz.width, sz.height, 0, PF_FLOAT32_RGBA,
                TU_RENDERTARGET);
            frameSrc = tex->getBuffer()->getRenderTarget();
            frameSrc->addViewport(cam);
        }
339 340
    }

341
    void setBackground(InputArray image) CV_OVERRIDE
342
    {
343
        CV_Assert(bgplane);
344 345 346 347 348 349 350 351 352 353 354

        String name = sceneMgr->getName() + "_Background";

        _createTexture(name, image.getMat());

        // correct for pixel centers
        Vector2 pc(0.5 / image.cols(), 0.5 / image.rows());
        bgplane->setUVs(pc, Vector2(pc[0], 1 - pc[1]), Vector2(1 - pc[0], pc[1]), Vector2(1, 1) - pc);

        Pass* rpass = bgplane->getMaterial()->getBestTechnique()->getPasses()[0];
        rpass->getTextureUnitStates()[0]->setTextureName(name);
355 356 357

        // ensure bgplane is visible
        bgplane->setVisible(true);
358 359
    }

360
    void setCompositors(const std::vector<String>& names) CV_OVERRIDE
361 362
    {
        CompositorManager& cm = CompositorManager::getSingleton();
363 364
        // this should be applied to all owned render targets
        Ogre::RenderTarget* targets[] = {frameSrc, rWin, depthRTT};
365

366
        for(int j = (frameSrc == rWin); j < 3; j++) // skip frameSrc if it is the same as rWin
367
        {
368
            Ogre::RenderTarget* tgt = targets[j];
369
            if(!tgt) continue;
370 371 372 373 374 375 376 377 378 379 380

            Viewport* vp = tgt->getViewport(0);
            cm.removeCompositorChain(vp); // remove previous configuration

            for(size_t i = 0; i < names.size(); i++)
            {
                if (!cm.addCompositor(vp, names[i])) {
                    LogManager::getSingleton().logError("Failed to add compositor: " + names[i]);
                    continue;
                }
                cm.setCompositorEnabled(vp, names[i], true);
381 382 383 384
            }
        }
    }

385
    void setBackground(const Scalar& color) CV_OVERRIDE
386
    {
387 388 389 390 391 392 393 394
        // hide background plane
        bgplane->setVisible(false);

        // BGRA as uchar
        ColourValue _color = ColourValue(color[2], color[1], color[0], color[3]) / 255;
        rWin->getViewport(0)->setBackgroundColour(_color);
        if(frameSrc != rWin)
            frameSrc->getViewport(0)->setBackgroundColour(_color);
395 396
    }

397
    void createEntity(const String& name, const String& meshname, InputArray tvec, InputArray rot) CV_OVERRIDE
398 399 400 401 402
    {
        Entity* ent = sceneMgr->createEntity(name, meshname, RESOURCEGROUP_NAME);

        Quaternion q;
        Vector3 t;
403
        _convertRT(rot, tvec, q, t);
404 405 406
        SceneNode* node = sceneMgr->getRootSceneNode()->createChildSceneNode(t, q);
        node->attachObject(ent);
    }
407

408
    void removeEntity(const String& name) CV_OVERRIDE {
409 410
        SceneNode& node = _getSceneNode(sceneMgr, name);
        node.getAttachedObject(name)->detachFromParent();
411 412 413

        // only one of the following will do something
        sceneMgr->destroyLight(name);
414
        sceneMgr->destroyEntity(name);
415 416
        sceneMgr->destroyCamera(name);

417
        sceneMgr->destroySceneNode(&node);
418
    }
419 420

    Rect2d createCameraEntity(const String& name, InputArray K, const Size& imsize, float zFar,
421
                              InputArray tvec, InputArray rot) CV_OVERRIDE
422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438
    {
        MaterialPtr mat = MaterialManager::getSingleton().create(name, RESOURCEGROUP_NAME);
        Pass* rpass = mat->getTechniques()[0]->getPasses()[0];
        rpass->setEmissive(ColourValue::White);

        Camera* cam = sceneMgr->createCamera(name);
        cam->setMaterial(mat);

        cam->setVisible(true);
        cam->setDebugDisplayEnabled(true);
        cam->setNearClipDistance(1e-9);
        cam->setFarClipDistance(zFar);

        _setCameraIntrinsics(cam, K, imsize);

        Quaternion q;
        Vector3 t;
439
        _convertRT(rot, tvec, q, t);
440
        SceneNode* node = sceneMgr->getRootSceneNode()->createChildSceneNode(t, q);
441 442
        node = node->createChildSceneNode();
        node->setOrientation(toOGRE); // camera mesh is oriented by OGRE conventions by default
443 444 445 446 447 448 449 450 451 452 453
        node->attachObject(cam);

        RealRect ext = cam->getFrustumExtents();
        float scale = zFar / cam->getNearClipDistance(); // convert to ext at zFar

        return Rect2d(toOGRE_SS[0] * (ext.right - ext.width() / 2) * scale,
                      toOGRE_SS[1] * (ext.bottom - ext.height() / 2) * scale, ext.width() * scale,
                      ext.height() * scale);
    }

    void createLightEntity(const String& name, InputArray tvec, InputArray rot, const Scalar& diffuseColour,
454
                           const Scalar& specularColour) CV_OVERRIDE
455 456 457 458 459 460 461 462 463
    {
        Light* light = sceneMgr->createLight(name);
        light->setDirection(Vector3::NEGATIVE_UNIT_Z);
        // convert to BGR
        light->setDiffuseColour(ColourValue(diffuseColour[2], diffuseColour[1], diffuseColour[0]));
        light->setSpecularColour(ColourValue(specularColour[2], specularColour[1], specularColour[0]));

        Quaternion q;
        Vector3 t;
464
        _convertRT(rot, tvec, q, t);
465 466 467 468
        SceneNode* node = sceneMgr->getRootSceneNode()->createChildSceneNode(t, q);
        node->attachObject(light);
    }

469
    void updateEntityPose(const String& name, InputArray tvec, InputArray rot) CV_OVERRIDE
470
    {
471
        SceneNode& node = _getSceneNode(sceneMgr, name);
472 473 474
        Quaternion q;
        Vector3 t;
        _convertRT(rot, tvec, q, t);
475 476
        node.rotate(q, Ogre::Node::TS_LOCAL);
        node.translate(t, Ogre::Node::TS_LOCAL);
477 478
    }

479
    void setEntityPose(const String& name, InputArray tvec, InputArray rot, bool invert) CV_OVERRIDE
480
    {
481
        SceneNode& node = _getSceneNode(sceneMgr, name);
482 483
        Quaternion q;
        Vector3 t;
484
        _convertRT(rot, tvec, q, t, invert);
485 486
        node.setOrientation(q);
        node.setPosition(t);
487 488
    }

489
    void setEntityProperty(const String& name, int prop, const String& value) CV_OVERRIDE
490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508
    {
        CV_Assert(prop == ENTITY_MATERIAL);
        SceneNode& node = _getSceneNode(sceneMgr, name);

        MaterialPtr mat = MaterialManager::getSingleton().getByName(value, RESOURCEGROUP_NAME);
        CV_Assert(mat && "material not found");

        Camera* cam = dynamic_cast<Camera*>(node.getAttachedObject(name));
        if(cam)
        {
            cam->setMaterial(mat);
            return;
        }

        Entity* ent = dynamic_cast<Entity*>(node.getAttachedObject(name));
        CV_Assert(ent && "invalid entity");
        ent->setMaterial(mat);
    }

509
    void setEntityProperty(const String& name, int prop, const Scalar& value) CV_OVERRIDE
510 511 512 513 514 515
    {
        CV_Assert(prop == ENTITY_SCALE);
        SceneNode& node = _getSceneNode(sceneMgr, name);
        node.setScale(value[0], value[1], value[2]);
    }

516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543
    void _createBackground()
    {
        String name = "_" + sceneMgr->getName() + "_DefaultBackground";

        Mat_<Vec3b> img = (Mat_<Vec3b>(2, 1) << Vec3b(2, 1, 1), Vec3b(240, 120, 120));
        _createTexture(name, img);

        MaterialPtr mat = MaterialManager::getSingleton().create(name, RESOURCEGROUP_NAME);
        Pass* rpass = mat->getTechniques()[0]->getPasses()[0];
        rpass->setLightingEnabled(false);
        rpass->setDepthCheckEnabled(false);
        rpass->setDepthWriteEnabled(false);
        rpass->createTextureUnitState(name);

        bgplane.reset(new Rectangle2D(true));
        bgplane->setCorners(-1.0, 1.0, 1.0, -1.0);

        // correct for pixel centers
        Vector2 pc(0.5 / img.cols, 0.5 / img.rows);
        bgplane->setUVs(pc, Vector2(pc[0], 1 - pc[1]), Vector2(1 - pc[0], pc[1]), Vector2(1, 1) - pc);

        bgplane->setMaterial(mat);
        bgplane->setRenderQueueGroup(RENDER_QUEUE_BACKGROUND);
        bgplane->setBoundingBox(AxisAlignedBox(AxisAlignedBox::BOX_INFINITE));

        sceneMgr->getRootSceneNode()->createChildSceneNode()->attachObject(bgplane.get());
    }

544
    void getScreenshot(OutputArray frame) CV_OVERRIDE
545
    {
546 547 548 549
        PixelFormat src_type = frameSrc->suggestPixelFormat();
        int dst_type = src_type == PF_BYTE_RGB ? CV_8UC3 : CV_32FC4;

        frame.create(frameSrc->getHeight(), frameSrc->getWidth(), dst_type);
550 551

        Mat out = frame.getMat();
552 553 554 555 556
        PixelBox pb(frameSrc->getWidth(), frameSrc->getHeight(), 1, src_type, out.ptr());
        frameSrc->copyContentsToMemory(pb, pb);

        // convert to OpenCV channel order
        cvtColor(out, out, dst_type == CV_8UC3 ? COLOR_RGB2BGR : COLOR_RGBA2BGRA);
557 558
    }

559
    void getDepth(OutputArray depth) CV_OVERRIDE
560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590
    {
        Camera* cam = sceneMgr->getCamera(title);
        if (!depthRTT)
        {
            // render into an offscreen texture
            // currently this draws everything twice as OGRE lacks depth texture attachments
            TexturePtr tex = TextureManager::getSingleton().createManual(
                title + "_Depth", RESOURCEGROUP_NAME, TEX_TYPE_2D, frameSrc->getWidth(),
                frameSrc->getHeight(), 0, PF_DEPTH, TU_RENDERTARGET);
            depthRTT = tex->getBuffer()->getRenderTarget();
            depthRTT->addViewport(cam);
            depthRTT->setAutoUpdated(false); // only update when requested
        }

        Mat tmp(depthRTT->getHeight(), depthRTT->getWidth(), CV_16U);
        PixelBox pb(depthRTT->getWidth(), depthRTT->getHeight(), 1, PF_DEPTH, tmp.ptr());
        depthRTT->update(false);
        depthRTT->copyContentsToMemory(pb, pb);

        // convert to NDC
        double alpha = 2.0/std::numeric_limits<uint16>::max();
        tmp.convertTo(depth, CV_64F, alpha, -1);

        // convert to linear
        float n = cam->getNearClipDistance();
        float f = cam->getFarClipDistance();
        Mat ndc = depth.getMat();
        ndc = -ndc * (f - n) + (f + n);
        ndc = (2 * f * n) / ndc;
    }

591
    void fixCameraYawAxis(bool useFixed, InputArray _up) CV_OVERRIDE
592
    {
593
        Vector3 up = Vector3::NEGATIVE_UNIT_Y;
594 595 596 597 598
        if (!_up.empty())
        {
            _up.copyTo(Mat_<Real>(3, 1, up.ptr()));
        }

599
        camNode->setFixedYawAxis(useFixed, up);
600 601
    }

602
    void setCameraPose(InputArray tvec, InputArray rot, bool invert) CV_OVERRIDE
603 604 605
    {
        Quaternion q;
        Vector3 t;
606
        _convertRT(rot, tvec, q, t, invert);
607 608

        if (!rot.empty())
609
            camNode->setOrientation(q*toOGRE);
610 611

        if (!tvec.empty())
612
            camNode->setPosition(t);
613 614
    }

615
    void getCameraPose(OutputArray R, OutputArray tvec, bool invert) CV_OVERRIDE
616 617
    {
        Matrix3 _R;
618 619
        // toOGRE.Inverse() == toOGRE
        (camNode->getOrientation()*toOGRE).ToRotationMatrix(_R);
620 621 622 623 624 625 626 627

        if (invert)
        {
            _R = _R.Transpose();
        }

        if (tvec.needed())
        {
628
            Vector3 _tvec = camNode->getPosition();
629 630 631 632 633 634 635 636 637 638 639 640 641 642 643

            if (invert)
            {
                _tvec = _R * -_tvec;
            }

            Mat_<Real>(3, 1, _tvec.ptr()).copyTo(tvec);
        }

        if (R.needed())
        {
            Mat_<Real>(3, 3, _R[0]).copyTo(R);
        }
    }

644
    void setCameraIntrinsics(InputArray K, const Size& imsize, float zNear, float zFar) CV_OVERRIDE
645 646
    {
        Camera* cam = sceneMgr->getCamera(title);
647 648 649 650

        if(zNear >= 0) cam->setNearClipDistance(zNear);
        if(zFar >= 0) cam->setFarClipDistance(zFar);
        if(!K.empty()) _setCameraIntrinsics(cam, K, imsize);
651 652
    }

653
    void setCameraLookAt(const String& target, InputArray offset) CV_OVERRIDE
654 655 656 657 658 659 660 661 662 663
    {
        SceneNode* tgt = sceneMgr->getEntity(target)->getParentSceneNode();

        Vector3 _offset = Vector3::ZERO;

        if (!offset.empty())
        {
            offset.copyTo(Mat_<Real>(3, 1, _offset.ptr()));
        }

664
        camNode->lookAt(tgt->_getDerivedPosition() + _offset, Ogre::Node::TS_WORLD);
665 666 667 668 669 670 671 672 673
    }
};

CV_EXPORTS_W void addResourceLocation(const String& path) { _extraResourceLocations.push_back(path); }

Ptr<WindowScene> createWindow(const String& title, const Size& size, int flags)
{
    if (!_app)
    {
674
        _app = makePtr<Application>(title.c_str(), size, flags);
675 676 677 678 679 680
        _app->initApp();
    }

    return makePtr<WindowSceneImpl>(_app, title, size, flags);
}

681
CV_EXPORTS_W int waitKey(int delay)
682 683 684
{
    CV_Assert(_app);

685
    _app->key_pressed = -1;
686
    _app->getRoot()->renderOneFrame();
687 688 689 690 691 692

    // 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;
693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730
}

void setMaterialProperty(const String& name, int prop, const Scalar& val)
{
    CV_Assert(_app);

    MaterialPtr mat = MaterialManager::getSingleton().getByName(name, RESOURCEGROUP_NAME);

    CV_Assert(mat);

    Pass* rpass = mat->getTechniques()[0]->getPasses()[0];
    ColourValue col;

    switch (prop)
    {
    case MATERIAL_POINT_SIZE:
        rpass->setPointSize(val[0]);
        break;
    case MATERIAL_OPACITY:
        col = rpass->getDiffuse();
        col.a = val[0];
        rpass->setDiffuse(col);
        rpass->setSceneBlending(SBT_TRANSPARENT_ALPHA);
        rpass->setDepthWriteEnabled(false);
        break;
    case MATERIAL_EMISSIVE:
        col = ColourValue(val[2], val[1], val[0]) / 255; // BGR as uchar
        col.saturate();
        rpass->setEmissive(col);
        break;
    default:
        CV_Error(Error::StsBadArg, "invalid or non Scalar property");
        break;
    }
}

void setMaterialProperty(const String& name, int prop, const String& value)
{
731
    CV_Assert_N(prop >= MATERIAL_TEXTURE0, prop <= MATERIAL_TEXTURE3, _app);
732 733 734 735 736 737

    MaterialPtr mat = MaterialManager::getSingleton().getByName(name, RESOURCEGROUP_NAME);
    CV_Assert(mat);

    Pass* rpass = mat->getTechniques()[0]->getPasses()[0];

738 739 740 741
    size_t texUnit = prop - MATERIAL_TEXTURE0;
    CV_Assert(texUnit <= rpass->getTextureUnitStates().size());

    if (rpass->getTextureUnitStates().size() <= texUnit)
742 743 744 745 746
    {
        rpass->createTextureUnitState(value);
        return;
    }

747
    rpass->getTextureUnitStates()[texUnit]->setTextureName(value);
748
}
749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803

static bool setShaderProperty(const GpuProgramParametersSharedPtr& params, const String& prop,
                              const Scalar& value)
{
    const GpuConstantDefinition* def = params->_findNamedConstantDefinition(prop, false);

    if(!def)
        return false;

    Vec4f valf = value;

    switch(def->constType)
    {
    case GCT_FLOAT1:
        params->setNamedConstant(prop, valf[0]);
        return true;
    case GCT_FLOAT2:
        params->setNamedConstant(prop, Vector2(valf.val));
        return true;
    case GCT_FLOAT3:
        params->setNamedConstant(prop, Vector3(valf.val));
        return true;
    case GCT_FLOAT4:
        params->setNamedConstant(prop, Vector4(valf.val));
        return true;
    default:
        CV_Error(Error::StsBadArg, "currently only float[1-4] uniforms are supported");
        return false;
    }
}

void setMaterialProperty(const String& name, const String& prop, const Scalar& value)
{
    CV_Assert(_app);

    MaterialPtr mat = MaterialManager::getSingleton().getByName(name, RESOURCEGROUP_NAME);
    CV_Assert(mat);

    Pass* rpass = mat->getTechniques()[0]->getPasses()[0];
    bool set = false;
    if(rpass->hasGpuProgram(GPT_VERTEX_PROGRAM))
    {
        GpuProgramParametersSharedPtr params = rpass->getVertexProgramParameters();
        set = setShaderProperty(params, prop, value);
    }

    if(rpass->hasGpuProgram(GPT_FRAGMENT_PROGRAM))
    {
        GpuProgramParametersSharedPtr params = rpass->getFragmentProgramParameters();
        set = set || setShaderProperty(params, prop, value);
    }

    if(!set)
        CV_Error_(Error::StsBadArg, ("shader parameter named '%s' not found", prop.c_str()));
}
804 805 806 807 808 809 810 811

void updateTexture(const String& name, InputArray image)
{
    CV_Assert(_app);
    TexturePtr tex = TextureManager::getSingleton().getByName(name, RESOURCEGROUP_NAME);
    CV_Assert(tex);
    _createTexture(name, image.getMat());
}
812 813
}
}