Practical WPF Graphics Programming
About this Book
The book "Practical WPF Graphics Programming - Advanced .NET Graphics Development with the Windows Presentation Foundation " is available now. You can order the eBook version of this book from this Web Site. The paperback version can be purchased at Amazon, Barnes & Noble, and other booksellers worldwide. Click here to order the book now.
This book follows the same style of my previous book "Practical C# Charts and Graphics". I emphasize the practicality of WPF to your real-world graphics applications and try to explain WPF graphics concepts based on code examples. This in-depth book on graphics programming in the Windows Presentation Foundation (WPF) contains over 120 ready-to-run code examples, which provide you with everything you need to add advanced graphics to your applications. From basic 2D shapes to complex interactive 3D models, this book uses code examples to clearly explain every step involved in building a variety of WPF graphics applications. You will learn how to use WPF to create impressive graphic effects and high-fidelity user interfaces.
This book contains:
- An overview of WPF graphics capabilities and the mathematical basics of computer graphics.
- Step-by-step procedures for creating a variety of 2D and 3D custom geometries and shapes with complete ready-to-run XAML and C# code for each application.
- Powerful 2D chart applications and user controls that can be directly used in your WPF applications or can be easily modified to create your own sophisticated chart packages.
- Detailed procedures on how to create various 3D surfaces in WPF using rectangular meshes.
- An introduction to building physics-based models games and fractals.
- Advanced color, lighting, and shading effects for 3D graphics objects.
- Instructions on adding direct interaction, such as animation, transformations, hit-testing, and mouse events, to graphics models.
Table of Contents
Introduction
Overview
What this Book includes
Is This Book for You
What Do You Need to Use This Book
How This Book Is Organized
Using Code Examples
Customer Support
Chapter 1: Overview of WPF Programming
New Features in WPF
XAML Basics
Why is XAML Needed?
Creating XAML Files
Code-Behind Files
Your First WPF Program
Properties in XAML
Event Handlers in Code-Behind Files
Code-Only Example
XAML-Only Example
Chapter 2: WPF Graphics Basics in 2D
2D Coordinate Systems in WPF
Default Coordinates
Custom Coordinates
Custom Coordinates for 2D Charts
2D Viewport
Zooming and Panning
Basic 2D Graphics Shapes
Lines
Rectangles and Ellipses
Polylines
Polygons
Chapter 3: 2D Transformations
Basics of Matrices and Transforms
Vectors and Points
Scaling
Reflection
Rotation
Translation
Homogeneous Coordinates
Translation in Homogeneous Coordinates
Scaling in Homegeneous Coordinates
Rotation in Homegeneous Coordinates
Combining Transforms
Vector and Matrix in WPF
Vector Structure
Matrix Structure
Matrix Operations
Matrix Transforms
Creating Perpendicular Lines
Object Transforms in WPF
MatrixTransform Class
ScaleTransform Class
TranslateTransform Class
RotateTransform Class
SkewTransform Class
Combining Transforms in WPF
Chapter 4: Geometry and 2D Drawing
Path and Geometry Classes
Line, Rectangle, and Ellipse Geometries
GeometryGroup Class
CombinedGeometry Class
PathGeometry Class
Lines and Polylines
Arcs
Bezier Curves
Geometry and Mini-Language
Interactive 2D Drawing
Mouse Events
Creating and Combining Shapes
Dragging and Moving Shapes
Hit Testing
Custom Shapes
Star Shape
Arrowline
Testing Custom Shapes
Chapter 5: Colors and Brushes
Colors
System Colors
Color Picker
Brushes
SolidColorBrush
LinearGradientBrush
Custom Colormap Brush
RadialGradientBrush
Custom Radial Colormap Brush
ImageBrush
DrawingBrush
VisualBrush
Bitmap Effects
Opacit Masks
Brush Transforms
LinearGradientBrush Transform
RadialGradientBrush Transform
ImageBrush Transform
Drawing and Visual Brush Transform
Chapter 6: Animation
WPF Animation Basics
Property-Based Animation
A Simple Animation in Code
Animation and Storyboard
Storyboard and Even Trigger
Storyboard Animation in Code
Animation and Timing Behavior
Duration
RepeatBehavior
AutoReverse
BeginTime
Speed Controls
Interactive Control
Animation and Transform
Animating Translation
Rolling Balls
Combining Transform Animation
Path Animation
Frame-Based Animation
Key-Frame Animation
Spline Key-Frame Animation
Custom Animation
Custom Animation Class
Per-Frame Animation
Animation Using Timer
Chapter 7: Physics and Games in WPF
Ordinary Differiential Equations
Fourth-Order Runge-Kutta Method
Higher-Order ODEs
ODE Solver
Pendulum
Equation of Motion
Pendulum Simulator
Couple-Spring System
Equations of Motion
Coupled-Spring Simulator
Projectiles
Aerodynamic Drag Force
Projectile Equations of Motion
Golf Game
Collission
Bouncing Ball Physics
Bouncing Ball Simulator
Fractals
Binary Tree
Snowflake
Mandelbrot Set
Julia Set
Chapter 8: Charts in WPF
Simple Line Charts
Creating Simple Line Chart
How It Works?
Line Charts with Data Collection
Chart Style
Data Collection
Data Series
Creating Charts
Gridlines and Labels
XAML Layout
ChartStyleGridlines Class
Testing Project
Legend
Legend Class
Testing Project
Chart User Control
Creating Chart Control
Defining Dependency Propeties
Chart Style for Chart Control
Data Collection and Data Series
Using Chart Control
Creating Simple Chart
Creating Multiple Charts
Chapter 9: 3D Transformations
3D Matrices and Vectors in WPF
3D Points and Wectors
Matrix3D Structure
Matrix3D Operations
Matrix3D Transforms
Rotation and Quaternion
Projections
Orthographic Projections
Multi-View Projections
Axonometric Projections
Perspective Projections
One-Point Perspective Projections
Two-Point Perspective Projections
Three-Point Perspective Projections
Perspective Projection Matrix
Views and Projections in WPF
View Transform
Perspective Projecion
View Frustum
Perspective Transform Matrix
Implementing Perspective Transforms
Testing Perspectice Projections
Orthographic Projecion
Orthographic Transform Matrix
Implementing Orthographic Transforms
Testing Orthograohic projections
Object Transformsin WPF
ScaleTransform3D class
TranslateTransform3D class
Rotate Transform3D class
MatrixTransform3D class
Combining Transforms
Chapter 10: WPF Graphics Basics in 3D
3D Graphics Basics
Viewpoint3D
3D Objects in WPF
Geometry and Mesh
GeometryModel3D and Surfaces
Illuminating the Scene
3Camera Positions
Simple Triangle in 3D
Basic 3D Shapes
Coordinate Axes and Wireframe
Creating Cube
Creating Sphere
Creating Cylinder
Creating Cone
Creating Torus
Chapter 11: Custom 3D Geometries
Cube Geometry
CubeGeometry Class
Testing CubeGeometry
Ellipsoid Geometry
EllipsoidGeometry Class
Testing EllipsoidGeometry
Cylindrical Geometry
CylinderGeometry Class
Testing CylinderGeometry
Cone Geometry
ConeGeometry Class
Testing ConeGeometry
Torus Geometry
TorusGeometry Class
Testing TorusGeometry
Icosahedron Geometry
IcosahedronGeometry Class
Testing IcosahedronGeometry
Dodecahedron Geometry
DodecahedronGeometry Class
Testing DodecahedronGeometry
Soccer Ball Geometry
SoccerGeometry Class
Testing SoccerGeometry
Chapter 12: Custom 3D Shapes
Deriving from UIElement3D
Cube Shape
Cube Shape Class
Testing Cube Shape
Ellipsoid Shape
Ellipsoid Shape Class
Testing Ellipsoid Shape
Cylinder Shape
Cylinder Shape Class
Testing Cylinder Shape
Cone Shape
Cone Shape Class
Testing Cone Shape
Torus Shape
Torus Shape Class
Testing Torus Shape
Icosahedron Shape
Icosahedron Shape Class
Testing Icosahedron Shape
Dodecahedron Shape
Dodecahedron Shape Class
Testing Dodecahedron Shape
Soccer Shape
Soccer Shape Class
Testing Soccer Shape
Combining Shape
Combining Shape Class
Testing Combining Shape
Chapter 13: 3D Surfaces
Simple Surfaces
Rectangular Meshes
SimpleSurface Class
Creating Simple Surfaces
Parametric Surfaces
ParametricSurface Class
Creating Parametric Surfaces
Helicoid Surface
Sphere Surface
Torus Surface
Quadric Surface
Extruded Surfaces
ExtrudedSurface Class
Creating Extruded Surfaces
Surfaces of Revolution
RotatedSurface Class
Creating Surfaces of Revolution
Surface Shading
SurfaceShading Class
Creating Shaded Surfaces
Chapter 14: 3D Model Manipulation
Lighting and Shading
Light Sources
Testing Light Sources
Shading
Materials
Diffuse Materials
Specular and Emissive Materials
Texture Mapping
Using LinearGradientBrush
Using RadialGradientBrush
Uisng Image and Tile Brushes
2D Elements on 3D Surfaces
Viewport2DVisual3D Class
Using Viewport2DVisual3D
Interacting with 3D Models
Hit-Testing for 3D Geometries
Hit-Testing for 3D Shapes
Rotating 3D Models with Mouse
Index
Introduction
The Windows Presentation Foundation (WPF) is a next generation graphics platform that is part of .NET 3.0 and .NET 3.5. It allows you to build advanced user interfaces that incorporate documents, media, 2D and 3D graphics, animations, and web-like characteristics. Practical WPF Graphics Programming provides all the tools you need to develop professional graphics applications using WPF and C# based on the .NET framework. I hope this book would be useful for WPF and C# programmers of all skill levels.
The book is written with the intention of providing you with a complete and comprehensive explanation of the WPF graphics capability, and pays special attention to the code implementation details, which will be useful when you create your own real-world WPF graphics Applications. It includes over 120 code examples, which cover a broad array of topics on WPF graphics programming. The book shows you how to create a variety of graphics, ranging from simple 2D shapes to complex 3D surfaces and interactive 3D models. You can learn from this book how to create a full range of 2D and 3D graphics applications and how to implement custom 3D geometries and shapes that can be reused in your WPF projects.
What this Book Includes
This book and its sample code listings provide you with:
- A complete, in-depth instruction on practical WPF graphics programming. After reading this book and running the example programs, you will be able to add various sophisticated graphics to your WPF applications.
- Over 120 ready-to-run example programs that allow you to explore the graphics techniques described in the book. These examples can be used to better understand how graphics algorithms work. You can modify the code examples or add new features to them to form the basis of your own projects. Some of the example code listings provided in this book are already sophisticated graphics packages that can be used directly in your own real-world WPF applications.
- Many classes in the sample code listings that you will find useful in your WPF graphics programming. These classes contain matrix manipulation, coordinate transformation, color maps, chart controls, and the other useful utility classes. You can extract these classes and plug them into your own applications.
Is this Book for You?
You do not have to be an experienced WPF developer or an expert to use this book. I designed this book to be useful to people of all levels of WPF programming experience. In fact, I believe that if you have some experience with the programming language C#, Windows Forms, HTML, and the .NET framework, you will be able to sit down in front of your computer, start up Microsoft Visual Studio 2008 and .NET 3.5, follow the examples provided in this book, and quickly become familiar with WPF graphics programming. For those of you who are already experienced WPF developers, I believe this book has much to offer as well. There is a great deal of information in this book about graphics programming not available in other WPF tutorial and reference books. In addition, most of the example programs in this book can be used directly in your own real-world application development. This book will provide you with a level of detail, explanation, instruction, and sample program code that will enable you to do just about anything WPF graphics-related.
The majority of the example programs in this book can be used routinely by WPF developers and technical professionals. Throughout the book, I will emphasize the usefulness of WPF graphics programming to real-world applications. If you follow the instructions presented in this book closely, you will be able to easily develop various practical WPF graphics applications, from 2D graphics and charts to a sophisticated 3D model libraries. At the same time, I will not spend too much time discussing programming style, execution speed, and code optimization, because there is a plethora of books out there that already deal with such topics. Most of the example programs you will find in this book omit error handlings. This makes the code easier to understand by focusing only on the key concepts and practical applications.
How this Book Is Organized
This book is organized into fourteen chapters, each of which covers a different topic about WPF graphics programming. The following summaries of each chapter should give you an overview of the book?s content:
Chapter 1, Overview of WPF Programming.
This chapter introduces the basics of WPF and reviews some of
the general aspects of WPF programming, including XAML files used to define user interfaces.
Chapter 2, WPF Graphics Basics in 2D.
This chapter reviews some fundamental concepts of the 2D graphics
and the 2D drawing model in WPF. It introduces coordinate systems and basic 2D shapes.
Chapter 3, 2D Transformations.
This chapter covers the mathematical basics for 2D graphics programming.
2D vectors, matrices, and transformations in the homogeneous coordinate system, including translation,
scaling, reflection, and rotation, are discussed. These 2D matrices and transformations allow WPF applications
to perform a wide variety of graphical operations on graphics objects in a simple and consistent manner.
Chapter 4, Geometry and 2D Drawing.
This chapter introduces WPF?s Geometry classes and demonstrates why
you need them to create complex 2D graphics objects. It also shows you how to create interactive 2D drawing
programs and custom shapes.
Chapter 5, Colors and Brushes.
This chapter covers the color system and brushes that WPF uses to paint
graphics objects. It introduces a variety of brushes and their transformations. You?ll learn how to create
exotic visual effects using different brushes, including the gradient, tile, and image brushes.
Chapter 6, Animation.
This chapter describes WPF animation facilities, which allow most of the
properties and transformations of the graphics objects (such as position, size, translation, rotation, etc.)
to be animated. It also describes how to create a custom animation class that can be used in physics-based
animation.
Chapter 7, Physics and Games in WPF.
This chapter covers topics related to real-world WPF applications.
You?ll learn how to create and simulate physics models by solving ordinary differential equations with the
Runge-Kutta method, and how to incorporate physics models into real-world games in WPF. This chapter discusses
several physics models and games, including a pendulum, a coupled spring system, a golf ball (projectiles),
ball collision, and fractals.
Chapter 8, Charts in WPF.
This chapter contains instructions on creating 2D line charts in WPF.
It introduces basic chart elements including the chart canvas, text canvas, axes, title, labels, ticks, and
legend. From this chapter, you?ll also learn how to put a 2D chart application into a custom user control
and how to reuse this control in your WPF applications.
Chapter 9, 3D Transformations.
This chapter extends the concepts described in Chapter 3 into the
third dimension. It explains how to define 3D graphics objects and how to translate, scale, reflect, and
rotate these 3D objects. It also describes transformation matrices that represent projection and transformations,
which allow you to view 3D graphics objects on a 2D screen. You?ll also learn how WPF defines 3D vectors,
matrices, and projections.
Chapter 10, WPF Graphics Basics in 3D.
This chapter explores the basics of 3D models in WPF. It
introduces Viewport3D, the 3D geometry and the mesh model, lighting, camera, etc. You?ll also learn how
to create basic 3D shapes directly in WPF.
Chapter 11, Custom 3D Geometries.
This chapter explains how to create custom geometries for various 3D
shapes. These custom geometry classes can be used as resources in XAML files, and these resources can be
used in your markup with a data binding.
Chapter 12, Custom 3D Shapes.
This chapter shows how to implement custom 3D shape classes. Unlike
custom 3D geometry classes, which can only be used as shareable resources, these custom 3D shape classes can be
used directly in your XAML files in the same way as the 2D shapes, such as the Line, Rectangle, and Ellipse.
You can use these custom 3D shape classes to create a powerful 3D model library.
Chapter 13, 3D Surfaces.
This chapter explains how to create various 3D surfaces, from simple surfaces
to complex surfaces, using rectangular meshes and different techniques, including parametric, extrusion, and
revolution approaches. It also describes how to add lighting and shading effects to these surfaces.
Chapter 14, 3D Model Manipulation.
This chapter covers broad array of topics on manipulating 3D models
in WPF. It describes how to create various 3D special effects using different materials, different light sources,
and texture maps. It also explains how to place interactive 2D elements on 3D surfaces and how to rotate 3D
graphics objects with the mouse using the virtual trackball method.