Example #1
0
int main(int argc, char** argv) {
    if (argc < 3)
        return -1;

    capture = cvCreateCameraCapture(atoi(argv[1]));
    if (!capture) {
        std::cerr << "Create camera capture failed" << std::endl;
        return -1;
    }

    CvarTemplate template_;
    cvarLoadTemplateTag(&template_, argv[2]);
    templates.push_back(template_);

    if (argc < 4) {
        cvarReadCamera(argv[3], &camera);
    } else {
        IplImage* frame = cvQueryFrame(capture);
        cvarReadCamera(NULL, &camera);
        cvarCameraScale(&camera, frame->width, frame->height);
    }

    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
    glutInitWindowSize(640, 480);
    glutCreateWindow("AR");
    glEnable(GL_CULL_FACE);
    glCullFace(GL_BACK);

    glutDisplayFunc(display);
    glutReshapeFunc(reshape);
    glutKeyboardFunc(keyboard);
    glutIdleFunc(idle);

    glEnable(GL_DEPTH_TEST);
    glutMainLoop();
    cvReleaseCapture(&capture);

    return 0;
}
Example #2
0
int main(int argc, char** argv)
{
    // use an ArgumentParser object to manage the program arguments.
    osg::ArgumentParser arguments(&argc,argv);

    // set up the usage document, in case we need to print out how to use this program.
    arguments.getApplicationUsage()->setApplicationName(arguments.getApplicationName());
    arguments.getApplicationUsage()->setDescription(arguments.getApplicationName()+" example demonstrates the use of ImageStream for rendering movies as textures.");
    arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options] filename ...");
    arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information");
    arguments.getApplicationUsage()->addCommandLineOption("--texture2D","Use Texture2D rather than TextureRectangle.");
    arguments.getApplicationUsage()->addCommandLineOption("--shader","Use shaders to post process the video.");
    arguments.getApplicationUsage()->addCommandLineOption("--interactive","Use camera manipulator to allow movement around movie.");
    arguments.getApplicationUsage()->addCommandLineOption("--flip","Flip the movie so top becomes bottom.");


    // construct the viewer.
    osgViewer::Viewer viewer(arguments);

    if (arguments.argc()<1)
    {
        arguments.getApplicationUsage()->write(std::cout,osg::ApplicationUsage::COMMAND_LINE_OPTION);
        return 1;
    }

    osg::ref_ptr<osg::Group> root = new osg::Group;



    /*    osg::Light* light = new osg::Light();
    light->setPosition(osg::Vec4d(-500.0, 1000.0, 500.0, 1.0));
    light->setDirection(osg::Vec3d(5.0, -10.0, -5.0));
    light->setSpotCutoff(70);
    light->setAmbient(osg::Vec4d(0.05, 0.05, 0.05, 1.0));
    light->setDiffuse(osg::Vec4d(0.5, 0.5, 0.5, 1.0));
    //light->setQuadraticAttenuation(0.001);

    osg::LightSource* lightSource = new osg::LightSource();
    lightSource->setLight(light);

    //osg::Light * attachedlight = lightSource->getLight();

    //attache light to root group
    root->addChild(lightSource);

	//activate light
	osg::StateSet* stateSet = root->getOrCreateStateSet();
    lightSource->setStateSetModes(*stateSet, osg::StateAttribute::ON);

    osg::StateSet* stateset = root->getOrCreateStateSet();
    stateset->setMode(GL_LIGHTING,osg::StateAttribute::ON);
*/
    osg::ref_ptr<osg::Geode> geode = new osg::Geode;



    //OpenCV-AR
    CvCapture* cameraCapture;
    CvCapture* fileCapture;
    //cameraCapture = cvCreateCameraCapture(0);

    fileCapture = cvCreateFileCapture("video/whal.avi");
    cameraCapture = fileCapture;

    if(!cameraCapture) {
		fprintf(stderr,"OpenCV: Create camera capture failed\n");
		return 1;
	}

    //printf("%f\n", cvGetCaptureProperty(cameraCapture, CV_CAP_PROP_FPS));

    //cvSetCaptureProperty(cameraCapture, CV_CAP_PROP_FRAME_WIDTH, 1280);
    //cvSetCaptureProperty(cameraCapture, CV_CAP_PROP_FRAME_HEIGHT, 960);
    //cvSetCaptureProperty(cameraCapture, CV_CAP_PROP_FPS, 15);

	IplImage* frame = cvQueryFrame(cameraCapture);
	IplImage* flipFrame = cvCreateImage(cvGetSize(frame), frame->depth, frame->nChannels);



    //osg::Image* image = osgDB::readImageFile("aclib-large.png");
    osg::Image* image = new osg::Image();
    //image->setPixelBufferObject( new osg::PixelBufferObject(image));

    image->setDataVariance( osg::Object::DYNAMIC );
    iplImageToOsgImage(flipFrame, image);

    //load model
    osg::ref_ptr<osg::PositionAttitudeTransform> modelPat = new osg::PositionAttitudeTransform();

    //osg::ref_ptr<osg::Node> loadedModel = osgDB::readNodeFile("models/Cars/AstonMartin-DB9.3ds");
    osg::ref_ptr<osg::Node> loadedModel = osgDB::readNodeFile("models/ferrari_car_2.osg");
    modelPat->addChild(loadedModel);
    modelPat->setScale(osg::Vec3(0.5, 0.5, 0.5));
    modelPat->setAttitude(osg::Quat(3.14 / 2, osg::Vec3d(-1.0, 0.0, 0.0)));

    if (!loadedModel) {
    	std::cout << "No model data loaded" << std::endl;
    	return 1;
    }



    //C_BODY

    std::vector<osg::MatrixTransform*> topMtList = getMatrixTransformListByName("C_TOP", loadedModel);
    std::vector<osg::MatrixTransform*> leftDoorMtList = getMatrixTransformListByName("C_LDOOR", loadedModel);
    std::vector<osg::MatrixTransform*> rightDoorMtList = getMatrixTransformListByName("C_RDOOR", loadedModel);
    std::vector<osg::MatrixTransform*> leftWheelsMtList = getMatrixTransformListByName("C_LWS", loadedModel);
    std::vector<osg::MatrixTransform*> rightWheelsMtList = getMatrixTransformListByName("C_RWS", loadedModel);
    std::vector<osg::MatrixTransform*> forwardBumperMtList = getMatrixTransformListByName("C_BUMP_F", loadedModel);
    std::vector<osg::MatrixTransform*> backBumperMtList = getMatrixTransformListByName("C_BUMP_B", loadedModel);
    std::vector<osg::MatrixTransform*> engineMtList = getMatrixTransformListByName("C_ENGINE", loadedModel);
    std::vector<osg::MatrixTransform*> bodyMtList = getMatrixTransformListByName("C_BODY", loadedModel);
    std::vector<osg::MatrixTransform*> salonMtList = getMatrixTransformListByName("C_SALON", loadedModel);

    /*    //findNodeVisitor findNode("C_BODY");
    FindNamedNode findNode("C_BODY");
    loadedModel->accept(findNode);

    std::vector<osg::Node*> foundNodeList = findNode.getNodeList();
    int listCount = foundNodeList.size();
    printf("%d\n", listCount);
    std::vector<osg::MatrixTransform*> bodyMtList;

    //vector<int>::const_iterator i;
    for(int i = 0; i < listCount; i++) {
        bodyMtList.push_back(new osg::MatrixTransform());
        //obj4Mt->setName("obj4Mt");

        osg::Group* foundNodeParent = foundNodeList[i]->getParent(0);

        bodyMtList[i]->addChild(foundNodeList[i]);

        foundNodeParent->addChild(bodyMtList[i]);

        foundNodeParent->removeChild(foundNodeList[i]);
    }
*/
    osg::Matrix translateMatrix;


    //osg::Node* foundNode = NULL;
    //foundNode = findNamedNode("obj5", loadedModel);

    //osg::ref_ptr<osg::MatrixTransform> obj5Mt = new osg::MatrixTransform();
    //obj4Mt->setName("obj5Mt");

    //osg::Group* foundNodeParent = foundNode->getParent(0);

    //obj5Mt->addChild(foundNode);

    //foundNodeParent->addChild(obj5Mt);

    //foundNodeParent->removeChild(foundNode);


    osg::Matrix rotateMatrix;
    float theta(M_PI * 0.1f);
    osg::Vec3f axis (1.0, 1.0, 0.1);
    osg::Quat wheelAxis( theta, axis);



    osg::BoundingSphere modelBoundingSphere = modelPat->getBound();
    printf("%f\n", modelBoundingSphere.radius());
    modelBoundingSphere.radius() *= 1.5f;

    osg::BoundingBox modelBoundingBox;
    modelBoundingBox.expandBy(modelBoundingSphere);



    //Light group

    //create light
    root->addChild(createLights(modelBoundingBox, root->getOrCreateStateSet()));


    //collect scene

    // only clear the depth buffer
    viewer.getCamera()->setClearMask(GL_DEPTH_BUFFER_BIT);

    // create a HUD as slave camera attached to the master view.

    viewer.setUpViewAcrossAllScreens();

    osgViewer::Viewer::Windows windows;
    viewer.getWindows(windows);

    if (windows.empty()) return 1;

    osg::Camera* screenCamera = createScreen(image);

    // set up cameras to rendering on the first window available.
    screenCamera->setGraphicsContext(windows[0]);
    screenCamera->setViewport(0,0,windows[0]->getTraits()->width, windows[0]->getTraits()->height);
    //screenCamera->setViewport(0, 0, 6.4, 4.8);


    viewer.addSlave(screenCamera, false);


    //root->addChild( geode.get());
    //root->addChild( createPyramid());
    //root->addChild( createScreen());//100.0, 100.0, image));
    root->addChild(modelPat);
    //root->addChild(objectPat);

    // set the scene to render
    viewer.setSceneData(root.get());



        viewer.realize();
        
        viewer.getCamera()->setClearColor(osg::Vec4(0.0f,0.0f,0.0f,1.0f));
/*
        //viewer.getCamera()->setProjameraCaptureectionMatrixAsOrtho(topleft.x(),bottomright.x(),topleft.y(),bottomright.y(), -10, 10);
        //viewer.getCamera()->setProjectionMatrixAsPerspective(60.0, screenAspectRatio, 100.0, -1.0);
*/
        viewer.getCamera()->setViewMatrixAsLookAt(osg::Vec3d(100.0, 100.0, 100.0), osg::Vec3d(0.0, 0.0, 0.0), osg::Vec3d(0.0, 1.0, 0.0));






        //Define vector of OpenCV-AR template
        vector<CvarTemplate> openCvArTemplateList;


        //load template
        CvarTemplate openCvArTemplate1;
        cvarLoadTemplateTag(&openCvArTemplate1,"aclib.png");
        //cvarLoadTemplateTag(&openCvArTemplate1,"markers/431.jpg");
        openCvArTemplateList.push_back(openCvArTemplate1);

        CvarTemplate openCvArTemplate2;
        cvarLoadTemplate(&openCvArTemplate2,"aclib.png",1);
        //cvarLoadTemplate(&openCvArTemplate2,"markers/431.jpg", 1);
        openCvArTemplateList.push_back(openCvArTemplate2);

        //Define OpenCV-AR marker;
        vector<CvarMarker> openCvArMarker;

        //Create OpenCV-AR camera
        CvarCamera openCvArCamera;
        //IplImage* frame = osgImage2IplImage(image);

        //cvarReadCamera("camera.yml", &openCvArCamera);
        cvarReadCamera(NULL, &openCvArCamera);
        cvarCameraScale(&openCvArCamera,frame->width,frame->height);
        viewer.getCamera()->setProjectionMatrix(osg::Matrixd(openCvArCamera.projection));


        //CvarOpticalFlow *flow;

       // srand(time(NULL));

        //int thresh = 60;
        double matchThresh = 0.7;
        //int state = 0;


        int counter = 0;
        while(!viewer.done())
        {
        	counter++;

        	char c = 0;//cvWaitKey(33);
        	//printf("%d\n", c);
        	if (c == 27) { // нажата ESC
        		printf("esc\n");
        		break;
        	}
        	if (c == 107) { // matchThresh up
        	  	matchThresh = matchThresh + 0.01;
        	}
        	if (c == 106) { // matchThresh down
        		matchThresh = matchThresh - 0.01;
        	}


        	if ((counter >= 300) and (counter < 310)) { // matchThresh down
        		//Top
        		translateMatrixTransformList(topMtList, 0.0, -1.2, 0.0);

        		//Engine
        		translateMatrixTransformList(engineMtList, 0.0, -1.0, 0.0);

        		//Body
        		translateMatrixTransformList(bodyMtList, 0.0, -0.8, 0.0);

        		//Salon
        		translateMatrixTransformList(salonMtList, 0.0, -0.4, 0.0);


        		//leftWeels
        		translateMatrixTransformList(leftWheelsMtList, -0.5, 0.0, 0.0);

        		//rightWeels
        		translateMatrixTransformList(rightWheelsMtList, 0.5, 0.0, 0.0);

        		//Left doors
        		translateMatrixTransformList(leftDoorMtList, -0.5, 0.0, 0.0);

        		//Right doors
           		translateMatrixTransformList(rightDoorMtList, 0.5, 0.0, 0.0);


           		//Forward bumper
        		translateMatrixTransformList(forwardBumperMtList, 0.0, 0.0, 0.5);

        		//back bumper
        		translateMatrixTransformList(backBumperMtList, 0.0, 0.0, -0.5);


        	}


        	//rotateMatrix.makeRotate(rotateMatrix.getRotate() * wheelAxis);
        	//obj5Mt->setMatrix(rotateMatrix);

        	//thresh = rand() % 256;
        	//printf("Match thresh value: %f\n", matchThresh);
        	frame = cvQueryFrame(cameraCapture);

        	cvCopy(frame, flipFrame);

        	cvFlip(flipFrame, flipFrame);
        	//cvNamedWindow("Original", 1);
        	//cvShowImage("Original", frame);
        	iplImageToOsgImage(frame, image);

        	image->dirty();

        	//osg::Image* = osg::Image(*image);


        	//frame = osgImage2IplImage(image);
        	//AR detection
        	//GLdouble modelview[16] = {0};

        	//Detect marker
        	int arDetect = cvarArMultRegistration(flipFrame,&openCvArMarker,openCvArTemplateList,&openCvArCamera, 60, 0.91);
			//printf("Marker found: %d\n",  arDetect);

			viewer.getCamera()->setViewMatrixAsLookAt(osg::Vec3d(0.0, 0.0, 100.0), osg::Vec3d(0.0, 0.0, 1000.0), osg::Vec3d(0.0, 1.0, 0.0));

			for(int i=0;i<arDetect;i++) {
				//if(openCvArMarker[i].tpl == 0);
				osg::Matrixf osgModelViewMatrix;
				for (int column = 0; column < 4; column++)	{
					for (int row = 0; row < 4; row++)	{
						osgModelViewMatrix(column, row) = openCvArMarker[i].modelview[column * 4 + row];
					}
				}

				viewer.getCamera()->setViewMatrix(osgModelViewMatrix);
			}

			viewer.frame();
        }
        return 0;

}