Click here to Skip to main content
15,885,953 members
Articles / Desktop Programming / WPF

Anaglyph ShaderEffect in WPF

Rate me:
Please Sign up or sign in to vote.
4.96/5 (62 votes)
8 Nov 2009CPOL5 min read 110.9K   3.1K   108  
This article shows how to use a WPF ShaderEffect for anaglyph blending (for red/cyan glasses). The effect can be used for both 2D and 3D elements.
<Window x:Class="AnaglyphWebcam.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:webcam="clr-namespace:CatenaLogic.Windows.Presentation.WebcamPlayer"
    xmlns:local="clr-namespace:AnaglyphWebcam"
    xmlns:me="clr-namespace:MyEffects"
    Title="Anaglyph webcam" Height="540" Width="656">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="24"/>
            <RowDefinition Height="24"/>
            <RowDefinition Height="480"/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="100"/>
            <ColumnDefinition Width="540"/>
        </Grid.ColumnDefinitions>
        
        <!-- Select webcams -->
        <Label Grid.Row="0" Grid.Column="0" Content="Left cam:"/>
        <ComboBox Grid.Row="0" Grid.Column="1" x:Name="webcam1ComboBox" ItemsSource="{x:Static webcam:CapDevice.DeviceMonikers}"
                  DisplayMemberPath="Name" SelectedValuePath="MonikerString"
                  SelectedValue="{Binding RelativeSource={RelativeSource AncestorType={x:Type local:MainWindow}}, Path=SelectedWebcam1MonikerString}"
                  />

        <Label Grid.Row="1" Grid.Column="0" Content="Right cam:"/>
        <ComboBox Grid.Row="1" Grid.Column="1" x:Name="webcam2ComboBox" ItemsSource="{x:Static webcam:CapDevice.DeviceMonikers}"
                  DisplayMemberPath="Name" SelectedValuePath="MonikerString"
                  SelectedValue="{Binding RelativeSource={RelativeSource AncestorType={x:Type local:MainWindow}}, Path=SelectedWebcam2MonikerString}"
                  />
        
        <webcam:CapPlayer Grid.Row="2" Grid.ColumnSpan="2" x:Name="webcamPlayer1" Height="480" 
            Device="{Binding RelativeSource={RelativeSource AncestorType={x:Type local:MainWindow}}, Path=SelectedWebcam1}">
        </webcam:CapPlayer>
        <webcam:CapPlayer Grid.Row="2" Grid.ColumnSpan="2" x:Name="webcamPlayer2" Height="480" 
            Device="{Binding RelativeSource={RelativeSource AncestorType={x:Type local:MainWindow}}, Path=SelectedWebcam2}">
            <webcam:CapPlayer.Effect>
                <me:AnaglyphEffect x:Name="Effect1">
                    <me:AnaglyphEffect.LeftInput>
                        <VisualBrush Visual="{Binding ElementName=webcamPlayer1}"/>
                    </me:AnaglyphEffect.LeftInput>
                </me:AnaglyphEffect>
            </webcam:CapPlayer.Effect>
            <webcam:CapPlayer.LayoutTransform>
                <ScaleTransform ScaleX="1" ScaleY="-1"/>
            </webcam:CapPlayer.LayoutTransform>
        </webcam:CapPlayer>

    </Grid>
</Window>

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Norway Norway
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions