Stereo Camera

Post those lines of code you feel like sharing or find what you require for your project here; or simply use them as tutorials.
Acki
Posts: 3496
Joined: Tue Jun 29, 2004 12:04 am
Location: Nobody's Place (Venlo NL)
Contact:

Stereo Camera

Post by Acki »

Hi,
I saw some threads about stereo rendering/camera...
so I thought to do some experiments about this...
I thought about the principle of stereo cameras that also is used as the principle for vr-goggles... ;)
so I created a split screen with 2 cameras (one for each eye)...
you'll have to squint on the screen until the 2 screens overlap...
or in other words: you have to look at the left screen with your right eye and at the right screen with your left eye... ;)
if you do it right then you'll see 3 screens and the middle screen is in real 3d !!! 8)
unfortunately you can't do this for a longer time because you'll get a headache then... :cry:

ok, no more words, here is the code:

Code: Select all

#include <irrlicht.h>
using namespace irr;
using namespace core;
using namespace scene;
using namespace video;
using namespace io;
using namespace gui;

int main(){
  IrrlichtDevice *device = createDevice(EDT_DIRECT3D9, dimension2d<u32>(800, 600), 16, true, false, false, 0);
  IVideoDriver* driver = device->getVideoDriver();
  ISceneManager* smgr = device->getSceneManager();
  IGUIEnvironment* guienv = device->getGUIEnvironment();

  // load the quake map
  device->getFileSystem()->addZipFileArchive("media/map-20kdm2.pk3");
  IAnimatedMesh* mesh = smgr->getMesh("20kdm2.bsp");
  ISceneNode* node = smgr->addOctreeSceneNode(mesh->getMesh(0), 0, -1, 128);
  node->setPosition(core::vector3df(-1350,-90,-1400));
  ITriangleSelector* selector = smgr->createOctreeTriangleSelector(mesh->getMesh(0), node, 128);
  node->setTriangleSelector(selector);
  selector->drop();

  // load a faerie
  IAnimatedMesh* faerie = smgr->getMesh("media/faerie.md2");
  IAnimatedMeshSceneNode* node2 = smgr->addAnimatedMeshSceneNode(faerie);
  node2->setMaterialTexture(0, driver->getTexture("media/faerie2.bmp"));
  node2->setMaterialFlag(EMF_LIGHTING, false);
  node2->setPosition(vector3df(-70,0,-30));
  node2->setMD2Animation(EMAT_SALUTE);

  // setup and run Irrlicht render loop
  ICameraSceneNode* camLeft = smgr->addCameraSceneNodeFPS();
  ICameraSceneNode* camRight = smgr->addCameraSceneNode(camLeft);
  // we set the right cam to the left because we need to squint
  camRight->setPosition(vector3df(-5,0,0));

  device->getCursorControl()->setVisible(false);
  line3d<f32> line;
  vector3df intersection;
  triangle3df tri;
  while(device->run()){
    // get the focus point
    line.start = camLeft->getPosition();
    line.end = line.start + (camLeft->getTarget() - line.start).normalize() * 1000.0f;
    if(smgr->getSceneCollisionManager()->getSceneNodeAndCollisionPointFromRay(line, intersection, tri, 0, 0)){
      camRight->setTarget(intersection);
    }
    // begin scene
    driver->setViewPort(rect<s32>(0,0,800,600));
    driver->beginScene(true,true,SColor(0,100,100,100));
    // draw right camera
    smgr->setActiveCamera(camRight);
    driver->setViewPort(rect<s32>(401,0,800,600));
    smgr->drawAll();
    // draw left camera
    smgr->setActiveCamera(camLeft);
    driver->setViewPort(rect<s32>(0,0,399,600));
    smgr->drawAll();
    // end scene
    driver->endScene();
  }
  device->drop();
  return 0;
}
as an example you can try this image:
Image
just squint on it so your left eye looks at the right image and your right eye looks at the left image...
and don't get to close to the screen, if you're farther away it's easier and you can do it for a longer time... :lol:
Last edited by Acki on Mon Apr 12, 2010 8:48 pm, edited 2 times in total.
while(!asleep) sheep++;
IrrExtensions:Image
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
andrei25ni
Posts: 326
Joined: Wed Dec 14, 2005 10:08 pm

Post by andrei25ni »

that's so cool ! :D
JP
Posts: 4526
Joined: Tue Sep 13, 2005 2:56 pm
Location: UK
Contact:

Post by JP »

That really is awesome, must compile the code and try it out with motion and all, though bit busy at the moment!

EDIT: compiled it and unfortunately it's not particularly easy to see the 3D on a full screen and with motion... not for me anyway, the pic shows the effect better!

But i guess all you need to do now is render the left viewport to texture in red and the right one to a texture in green and then combine them and use 3D glasses?
Image Image Image
Acki
Posts: 3496
Joined: Tue Jun 29, 2004 12:04 am
Location: Nobody's Place (Venlo NL)
Contact:

Post by Acki »

JP wrote:compiled it and unfortunately it's not particularly easy to see the 3D on a full screen and with motion... not for me anyway, the pic shows the effect better!
yes, in full screen mode it's harder, especially if you have a big monitor, because you have to squint extremly or go farther away from the monitor...
in windowed mode it should be easier... ;)
JP wrote:But i guess all you need to do now is render the left viewport to texture in red and the right one to a texture in green and then combine them and use 3D glasses?
I also thought about this...
but I'm not sure how to do this...
I don't know how to filter the colors and I have no clue about shaders, althogh I'm not sure if shaders are neccessary to do this...
then one of the textures have to be transparent and rendered over the other, that should be easy to do I think... ;)
while(!asleep) sheep++;
IrrExtensions:Image
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
psychophoniac
Posts: 101
Joined: Wed Dec 03, 2008 5:33 pm
Location: ger

Post by psychophoniac »

ha going far away from my monitor did it... ^^
but as far as i know are those 2 cams you have in a stereopicture layed over each other, so you get one picture?
(maybe i'm wrong, but this is, what i have in mind->)
http://www.well.com/user/jimg/stereo/stereo_list.html
or this(wait a minute for it to load, so you can see the effect ! ) :
http://ziza.ru/2006/12/05/svezhie_stere ... 17_mb.html

...sorry for pushing this up, but i think its very interesting, I'll definitely have a go on that next week.
i love skateboarding!
skumar
Posts: 201
Joined: Thu Feb 14, 2008 6:24 pm
Location: kerala state india
Contact:

Post by skumar »

Great work acki......

I like such headaches after a virtual reality.....since we are fooling our brains!!!! :D

At some places one viewport fails to update with the other... When you look to sky..ie...non mesh...i know the reason....you use the collision of line on the mesh.....

Any way good work
skumar
bitplane
Admin
Posts: 3204
Joined: Mon Mar 28, 2005 3:45 am
Location: England
Contact:

Post by bitplane »

Pretty cool. Does anyone have a pair of 3D glasses? It would be fun to try combining the two images :D
Submit bugs/patches to the tracker!
Need help right now? Visit the chat room
MasterGod
Posts: 2061
Joined: Fri May 25, 2007 8:06 pm
Location: Israel
Contact:

Post by MasterGod »

So cool - I love it.
Image
Dev State: Abandoned (For now..)
Requirements Analysis Doc: ~87%
UML: ~0.5%
Dragonazul
Posts: 24
Joined: Sun Sep 23, 2007 9:45 pm
Location: Spain

Post by Dragonazul »

Hi All,
I’m very interested in a stereo camera.
I have tried it into a good 3D helmet and into a Power Wall.

You can see the stereo but it isn’t complete correct some parts into the edges of the image look strange.
If you separate the cameras you should have more stereo feeling but this don´t happen. Too all that is between the camera and the common target point should be out of the screen but I can see this effect.

I think one of the problems is that this implementation is a good aproach but not a real stereo.
Here is a link with the stereo render definition.
http://local.wasp.uwa.edu.au/~pbourke/m ... reorender/

This is the best approach. I don’t know if this can be implement in Irrlicht.

Image

I have tried it with projection prespective Fov with not too much lucky for the moment.

If I get something I will told you.

Will be a good new feature for the engine: Stereo render…

P.D: Good start Acki
zeroZshadow
Posts: 43
Joined: Mon Dec 01, 2008 6:35 pm

Post by zeroZshadow »

why not make an anaglyph camera class ?? that would be awsome >_>
SSG
Posts: 33
Joined: Sun Jan 11, 2009 1:16 pm

Post by SSG »

Nice!

I'm a bit concerned that the right camera doesn't always do what the left camera is doing. Perhaps this is an Irrlicht bug?

Anyway, I made the following mods:
  • Use a window instead of fullscreen
  • Reduce the resolution so I could see the effect
  • Change the aspect ratio of both the cameras so the scene doesn't look squished.
Here's the code:

Code: Select all

#include <irrlicht.h>
using namespace irr;
using namespace core;
using namespace scene;
using namespace video;
using namespace io;
using namespace gui;

#ifdef _IRR_WINDOWS_
#pragma comment(lib, "Irrlicht.lib")
#endif

int main(){
  IrrlichtDevice *device = createDevice(EDT_DIRECT3D9, dimension2d<s32>(320, 120), 16, false, false, false, 0);
  IVideoDriver* driver = device->getVideoDriver();
  ISceneManager* smgr = device->getSceneManager();
  IGUIEnvironment* guienv = device->getGUIEnvironment();

  // load the quake map
  device->getFileSystem()->addZipFileArchive("../../media/map-20kdm2.pk3");
  IAnimatedMesh* mesh = smgr->getMesh("20kdm2.bsp");
  ISceneNode* node = smgr->addOctTreeSceneNode(mesh->getMesh(0), 0, -1, 128);
  node->setPosition(core::vector3df(-1350,-90,-1400));
  ITriangleSelector* selector = smgr->createOctTreeTriangleSelector(mesh->getMesh(0), node, 128);
  node->setTriangleSelector(selector);
  selector->drop();

  // load a faerie
  IAnimatedMesh* faerie = smgr->getMesh("media/faerie.md2");
  IAnimatedMeshSceneNode* node2 = smgr->addAnimatedMeshSceneNode(faerie);
  node2->setMaterialTexture(0, driver->getTexture("media/faerie2.bmp"));
  node2->setMaterialFlag(EMF_LIGHTING, false);
  node2->setPosition(vector3df(-70,0,-30));
  node2->setMD2Animation(EMAT_SALUTE);

  // setup and run Irrlicht render loop
  ICameraSceneNode* camLeft = smgr->addCameraSceneNodeFPS();
  ICameraSceneNode* camRight = smgr->addCameraSceneNode(camLeft);
  // we set the right cam to the left because we need to squint
  camRight->setPosition(vector3df(-5,0,0));

  camLeft->setAspectRatio(160.f/120.f);
  camRight->setAspectRatio(160.f/120.f);

  device->getCursorControl()->setVisible(false);
  line3d<f32> line;
  vector3df intersection;
  triangle3df tri;
  while(device->run()){
    // get the focus point
    line.start = camLeft->getPosition();
    line.end = line.start + (camLeft->getTarget() - line.start).normalize() * 1000.0f;
    if(smgr->getSceneCollisionManager()->getCollisionPoint(line, selector, intersection, tri)){
      camRight->setTarget(intersection);
    }
    // begin scene
    driver->setViewPort(rect<s32>(0,0,320,120));
    driver->beginScene(true,true,SColor(0,100,100,100));
    // draw right camera
    smgr->setActiveCamera(camRight);
    driver->setViewPort(rect<s32>(161,0,320,120));
    smgr->drawAll();
    // draw left camera
    smgr->setActiveCamera(camLeft);
    driver->setViewPort(rect<s32>(0,0,159,120));
    smgr->drawAll();
    // end scene
    driver->endScene();
  }
  device->drop();
  return 0;
}
Acki
Posts: 3496
Joined: Tue Jun 29, 2004 12:04 am
Location: Nobody's Place (Venlo NL)
Contact:

Post by Acki »

SSG wrote:I'm a bit concerned that the right camera doesn't always do what the left camera is doing. Perhaps this is an Irrlicht bug?
no, it's a small bug with my code... ;)
the cameras are focusing on the collision point and when you look (e.g.) in the air there is nothing to focus/collide... :lol:
(skumar already mentioned this correctly)
while(!asleep) sheep++;
IrrExtensions:Image
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
jpfelgueiras
Posts: 2
Joined: Mon Apr 12, 2010 6:45 pm

Post by jpfelgueiras »

i love this kind of projects...

can you tell me what release of irrlicht are you using?

when i try to compile i got 2 errors :

main.cpp(10) : error C2664: 'irr::createDevice' : cannot convert parameter 2 from 'irr::core::dimension2d<T>' to 'const irr::core::dimension2d<T> &'


and


main.cpp(46) : error C2660: 'irr::scene::ISceneCollisionManager::getCollisionPoint' : function does not take 4 arguments


im using Visual C++ 2008 express edition and irrlicht v 1.7.1

keep the good job
:D
Acki
Posts: 3496
Joined: Tue Jun 29, 2004 12:04 am
Location: Nobody's Place (Venlo NL)
Contact:

Post by Acki »

jpfelgueiras wrote:can you tell me what release of irrlicht are you using?
don't know, what version was up to date 2 years ago ??? :lol:
jpfelgueiras wrote:when i try to compile i got 2 errors :

main.cpp(10) : error C2664: 'irr::createDevice' : cannot convert parameter 2 from 'irr::core::dimension2d<T>' to 'const irr::core::dimension2d<T> &'
yup, createDevice now takes an u32 dimension (older versions used to use s32)
jpfelgueiras wrote:main.cpp(46) : error C2660: 'irr::scene::ISceneCollisionManager::getCollisionPoint' : function does not take 4 arguments
yup this was also changed, use getSceneNodeAndCollisionPointFromRay instead... ;)

well, I updated the main post and it should now work with Irrlicht 1.7+ !!! 8)
while(!asleep) sheep++;
IrrExtensions:Image
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
jpfelgueiras
Posts: 2
Joined: Mon Apr 12, 2010 6:45 pm

Post by jpfelgueiras »

MANY TKS :D

the only thing that i had change is adding

Code: Select all

#ifdef _IRR_WINDOWS_
#pragma comment(lib, "Irrlicht.lib")
#pragma comment(linker, "/subsystem:windows /ENTRY:mainCRTStartup")
#endif
and redefining the paths for the models.
Post Reply