Curious glitches when moving the camera

Dec 29, 2010 at 2:03 PM

I'm seeing some curious glitches when I move the camera.

I'm trying to figure out how the various positioning mechanisms work in Balder and I've started with a simple example that positions an object near the origin and moves the camera around it.

It works almost exactly as I'd expect, as I move the camera the view shifts smoothly and sensibly. But there are various positions where the object disappears from the screen, and others where the camera position seems to jump (i.e. if I change a given coordinate through 17, 18, 19 the view at 18 is substantially different to the views at 17 or 19).

It is hard to describe exactly how the problem behaves, but it seems as if there are planes in my scene where the camera misbehaves. I have very little experience of 3D rendering and what I have is a few decades old, but this reminds me of the symptoms of arithmetic overflow. More likely it's me doing something stupid setting up the scene, but I can't see what. Here is the code showing this problem, cut down from the original SLAR Balder demo:

<UserControl x:Class="BalderCameraDemo.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:execution="clr-namespace:Balder.Execution;assembly=Balder"
    xmlns:geometries="clr-namespace:Balder.Objects.Geometries;assembly=Balder"
    xmlns:lighting="clr-namespace:Balder.Lighting;assembly=Balder"
    xmlns:view="clr-namespace:Balder.View;assembly=Balder"
    mc:Ignorable="d"
    d:DesignHeight="480" d:DesignWidth="640">

    <Grid x:Name="LayoutRoot" Background="Black">
        <StackPanel Width="640" Height="700">
            <TextBlock FontSize="12" Text="Augmented Reality Balder 3D Demo" Style="{StaticResource TextBlockStyle}" HorizontalAlignment="Center" VerticalAlignment="Stretch" />
            <StackPanel Orientation="Horizontal">
                <Grid Name="ViewportContainer" Width="640" Height="480" >
                    <Grid Name="VideoContainer" RenderTransformOrigin="0.5,0.5" >
                        <Rectangle Name="Video"/>
                        <Image Name="ViewportOverlay"/>
                    </Grid>
                    <execution:Game x:Name="Game" Width="640" Height="480" Background="Transparent" RenderTransformOrigin="0.5,0.5" >
                        <lighting:OmniLight Name="Light1" Diffuse="White" Position="500,500,500"/>
                        <lighting:OmniLight Name="Light2" Diffuse="Gray" Position="-500,500,-500"/>
                        <geometries:Box x:Name="Cube" Dimension="50,50,50" Position="0,0,-25" Opacity="1" Color="Red" InteractionEnabled="false" />
                        <execution:Game.Camera>
                            <view:Camera x:Name="Camera" Position="0,0,200" Target="0,0,0"/>
                        </execution:Game.Camera>
                        <execution:Game.RenderTransform>
                            <ScaleTransform ScaleX="1" ScaleY="1" />
                        </execution:Game.RenderTransform>
                    </execution:Game>
                </Grid>
            </StackPanel>
            <TextBlock Name="Txt" Text="Camera Position" Width="120" Style="{StaticResource TextBlockStyle}" />
            <StackPanel Orientation="Vertical" HorizontalAlignment="Center" VerticalAlignment="Center">
                <StackPanel Orientation="Horizontal">
                    <Slider Name="Position_X" Minimum="-200" Maximum="200" Value="0" ValueChanged="Position_X_ValueChanged" Style="{StaticResource SliderStyle}"/>
                    <TextBlock Name="Display_Position_X" Text="Pos X" Style="{StaticResource TextBlockStyle}"/>
                </StackPanel>
                <StackPanel Orientation="Horizontal">
                    <Slider Name="Position_Y" Minimum="-200" Maximum="200" Value="0" ValueChanged="Position_Y_ValueChanged" Style="{StaticResource SliderStyle}"/>
                    <TextBlock Name="Display_Position_Y" Text="Pos Y" Style="{StaticResource TextBlockStyle}"/>
                </StackPanel>
                <StackPanel Orientation="Horizontal">
                    <Slider Name="Position_Z" Minimum="-200" Maximum="200" Value="200" ValueChanged="Position_Z_ValueChanged" Style="{StaticResource SliderStyle}"/>
                    <TextBlock Name="Display_Position_Z" Text="Pos Z" Style="{StaticResource TextBlockStyle}"/>
                </StackPanel>
            </StackPanel>
        </StackPanel>
    </Grid>
</UserControl>

<Application xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             x:Class="BalderCameraDemo.App"
             >
    <Application.Resources>
        <Style x:Key="TextBlockStyle" TargetType="TextBlock">
            <Setter Property="Foreground" Value="#FF14517B"/>
            <Setter Property="Margin" Value="10,3,0,0"/>
            <Setter Property="TextAlignment" Value="Left"/>
            <Setter Property="VerticalAlignment" Value="Center"/>
            <Setter Property="Width" Value="100"/>
        </Style>
        <Style x:Key="SliderStyle" TargetType="Slider">
            <Setter Property="Width" Value="400"/>
            <Setter Property="Height" Value="15"/>
            <Setter Property="HorizontalAlignment" Value="Left"/>
        </Style>
    </Application.Resources>
</Application>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace BalderCameraDemo
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
        }

        private void Initialize()
        {
            try
            {
                // Balder designer bug workarounds
                Game.Viewport.Width = (int)this.ViewportContainer.Width;
                Game.Viewport.Height = (int)this.ViewportContainer.Height;

                Game.Background = new SolidColorBrush(Color.FromArgb(0, 0, 0, 0));

                // Init Light
                Light1.Range = Light2.Range = 800;
                Light1.Strength = Light2.Strength = 1.0f;
                Light1.Specular = Light2.Specular = Balder.Color.FromArgb(255, 255, 255, 255);

                Camera.Far = 1000;
                Camera.Near = 0;
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error during initialization: " + ex.ToString());
                throw;
            }
        }

        private void Position_X_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            if (Position_X != null)
            {
                int v = (int)Math.Floor(this.Position_X.Value);
                this.Camera.Position.X = v;
                Display_Position_X.Text = String.Format("Pos X={0:###0}", v);
            }
        }

        private void Position_Y_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            if (Position_Y != null)
            {
                int v = (int)Math.Floor(this.Position_Y.Value);
                this.Camera.Position.Y = v;
                Display_Position_Y.Text = String.Format("Pos Y={0:###0}", v);
            }
        }

        private void Position_Z_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            if (Position_Z != null)
            {
                int v = (int)Math.Floor(this.Position_Z.Value);
                this.Camera.Position.Z = v;
                Display_Position_Z.Text = String.Format("Pos Z={0:###0}", v);
            }
        }
    }
}

To see the problem, move the three sliders and watch the view closely. At certain positions the view blinks. Moving the slider slowly back and forth around the position that causes the blink you will find a position where the image disappears or changes suddenly. For example, it disappears at (-116, 49, 67) and jumps at (-120, -200, -120).

Coordinator
Dec 30, 2010 at 10:04 PM
Hi, Thanks for posting the entire source with great repro description. I'll take a lok at it as soon as I get a chance and will get back to you.