Virtual camera in AR
Keywords:
vision,
tracking,
marker,
augmented reality
Author(s): Sabine Webel
Date: 2009-07-03
Summary: This tutorial describes the camera handling in Augmented Reality based X3D, or rather instantreality, applications. It is shown how to apply marker tracking to your X3D scene by using a Viewpoint node, what allows you to run an Augmented Reality scene with arbitrary aspect ratio of the window. It also describes how the fit mode (fit to window height, fit to window width) can be changed.
Introduction
As described in the Basic Marker Tracking tutorial, instantvision is a set of visual tracking systems offering a variety of features such as simple marker tracking or markerless tracking. This example focuses on a simple marker tracking example using the VisionLib backend. The Basic Marker Tracking tutorial describes how to track a single marker for a simple Augmented Reality application using a Viewfrustum node. The advantage of that approach is that the projection and modelview matrices can be routed directly to the Viewfrustum node (without applying any further settings or modifications) to control the virtual camera. The problem is that it is quite complicated to run the scene (without distortions) with a different aspect ratio of the window than the predefined size.
This example shows how to handle the virtual camera in a vison based scene while keeping the window size flexible. It also shows how the background image (coming from the camera) can be kept in the correct aspect ratio and how to define if the background image is fittet relative to the window height or width.
IOSensor
As you know the marker tracking is loaded like any other InstantIO device via an IOSensor. To instantvision's fields that are necessary are the following:
- VideoSourceImage (SFImage): Camera image
- TrackedObject1Camera_Position (SFVec3f): Camera's position
- TrackedObject1Camera_Orientation (SFRotation): Camera's orientation
- TrackedObject1Camera_PrincipalPoint (SFVec2f): Camera's principal point
- TrackedObject1Camera_FOV_horizontal (SFFloat): Camera's horizontal field of view
- TrackedObject1Camera_FOV_vertical (SFFloat): Camera's vertical field of view
Code: IOSensor
<IOSensor DEF='VisionLib' type='VisionLib' configFile='TutorialMarkerTrackingVP.pm'>
<field accessType='outputOnly' name='VideoSourceImage' type='SFImage'/>
<field accessType='outputOnly' name='TrackedObject1Camera_Position' type='SFVec3f'/>
<field accessType='outputOnly' name='TrackedObject1Camera_Orientation' type='SFRotation'/>
<field accessType='outputOnly' name='TrackedObject1Camera_PrincipalPoint' type='SFVec2f'/>
<field accessType='outputOnly' name='TrackedObject1Camera_FOV_horizontal' type='SFFloat'/>
<field accessType='outputOnly' name='TrackedObject1Camera_FOV_vertical' type='SFFloat'/>
</IOSensor>
Setting the background and the virtual camera
To set the camera's image in the background we use PolygonBackground node. By setting the PolygonBackground's field fixedImageSize the aspect ratio of the image can be defined. This aspect ratio is also kept when the window is resized. Depending on how you want the background image fit in the window you need to set the mode field to "VERTICAL" or "HORIZONTAL". When set to "VERTICAL" the image fits to the height of the window, when the "mode is set to "HORIZONTAL" it fits to the width of the window.
Code: Camera Image on Polygon BackGround
<PolygonBackground fixedImageSize='640,480' mode='vertical'>
<Appearance positions='0 0, 1 0, 1 1, 0 1' >
<TextureTransform rotation='0' scale='1 -1'/>
<PixelTexture2D DEF='tex' autoScale='false'/>
</Appearance>
</PolygonBackground>
<ROUTE fromNode='VisionLib' fromField='VideoSourceImage' toNode='tex' toField='image'/>
No we add a virtual object (a yellow teapot) to the scene. This object is later placed on the marker, that is used to determine the camera's position and orientation. A transformation translates and rotates the teapot relative to the marker.
Code: Adding a virtual object
<Transform DEF='transfObj1RelativeToMarker' translation='2.5 2.5 1.5' rotation='1 0 0 1.57'>
<Shape>
<Appearance>
<Material emissiveColor='1 0.5 0' />
</Appearance>
<Teapot size='5 5 5' />
</Shape>
</Transform>
To set the position and orientation the camera we route the corresponding values delivered by the IOSensor to our scene camera. Therefor we use a PerspectiveViewpoint. If you decided to fit the background image to the height of the window (mode field of the PolygonBackground is set to "vertical"), you need to set the fovMode field of the PerspectiveViewpoint also to "vertical", otherwise you need to set it to "horizontal". However, both fields should contain the same value. In case the image is fits to the window height, the vertical field of view must be routed to the PerspectiveViewpoint. Otherwise you need to route the horizontal field of view.
Code: Setting up and connecting the scene camera (PerspectiveViewpoint)
<PerspectiveViewpoint DEF='vp' fovMode='vertical'/>
<ROUTE fromNode='VisionLib' fromField='TrackedObject1Camera_Position' toNode='vp' toField='position'/>
<ROUTE fromNode='VisionLib' fromField='TrackedObject1Camera_Orientation' toNode='vp' toField='orientation'/>
<ROUTE fromNode='VisionLib' fromField='TrackedObject1Camera_PrincipalPoint' toNode='vp' toField='principalPoint'/>
<ROUTE fromNode='VisionLib' fromField='TrackedObject1Camera_FOV_vertical' toNode='vp' toField='fieldOfView'/>
To deactive the navigation (what is usually desired in Augmented Reality scenarios) we use a NavigationInfo node.
Code: Deactivating the navigation
<NavigationInfo type='none' />
Now we have created an Augmented Reality scenario.
Comments
This tutorial has no comments.
Add a new comment
Due to excessive spamming we have disabled the comment functionality for tutorials. Please use our forum to post any questions.