3D Graphics in WPF
Download
Report
Transcript 3D Graphics in WPF
Rujchai Ung-arunyawee
Department of Computer Engineering
Khon Kaen University
3D Graphics Basics: The Scene
A three dimensional model of an object
A camera that takes a picture of the object
A viewport (projection surface) for a picture , which
is a projection of the object to a planar surface
The 3D rendering engine determines the color for
every pixel by calculating the amount of light that is
reflected by any light sources
All surfaces of objects have a material (brush). The
material defines how much light is reflected for a
specific angle and the brush defines the color.
A brush can either be a simple color or a gradient or
even an image called texture
3D WPF is a world of triangles
A surface of a 3D object is called a mesh
A mesh is defined by a number of 3D points called
Vertices (Vertex)
The vertices are joined together by a winding pattern
to define the triangles
Every triangle has a front and a back side. Only the
front side is rendered
WPF uses a counter clockwise winding pattern
WPF is a right handed coordinate
system
WPF Elements of a 3D scene
Viewport3D
The viewport is the control that builds the gate between the
2D and the 3D world.
Camera
Every 3D scene has excactly one camera. The camera defines
the Position and the LookDirection and the UpDirection of
the viewer. WPF supports orthographical and perspective
cameras.
3D Models
A 3D model defines an object in the scene. It has a Geometry
that is the mesh and a Material that can be a diffuse, specular
or emmisive material. The material itself has a brush.
Lights
Without any lights you see nothing. So we need to place at
least one light in our scene to illuminate our models
A Point in Space
In C# code, we create and initialize a Point3D
object:
In XAML, you represent a Point3D objects as a text
string with spaces and/or single commas
separating the numbers:
or
Points in Space
In C# code, use the Point3DCollection class
In XAML, just by listing them in a string:
Vectors
A vector encapsulates a magnitude and a direction
Points and Vectors Relationship
For vector (xv, yv, zv), the direction of the vector is
the direction from the origin (0, 0, 0) to the point
(xv, yv, zv).
The magnitude of the vector (M) is
The normalized vectors or unit vectors will have
magnitude of 1
point ± point vector
point ± vector point
vector * D vector
vector / D vector
Define the 3D object
Define the simplest light
A 3D scene usually requires some light.
If you don't have any light in a 3D scene, you'll still
be able to see the figures you've created, but they
won't have any color
Different types of light are available in WPF 3D
The simplest light is a class named AmbientLight
which has a white color by default.
or
Define the simplest camera
The camera must be located in a specific position
in 3D space by setting the Position property
You also need to specify a direction in which the
camera is pointed by setting the LookDirection
property.
The simplest camera
Sits right on the positive Z axis, which means that
the X and Y coordinates are set to zero
Points in a direction parallel to the negative Z axis,
which is the vector (0, 0, –1)
<PerspectiveCamera Position=“0 0 5"
LookDirection="0 0 –1" />
Camera In the scene
The Viewport3D and Its Constituents
We generally define a whole 3D scene inside a
Viewport3D element
It can be a part of a larger layout of elements, and
it can receive mouse, keyboard, and stylus input
Viewport3D defines a ModelVisual3D and Camera
properties.
ModelVisual3D has a property named Content
Each Content might contains GeometryModel3D
or Light
GeometryModel3D has a Geometry, Material, and
BackMaterial properties.
Ggeneral structure of a simple
Viewport3D
Put everything together
Model3DGroup
Combine the geometry and light into one
Light sources
AmbientLight
Setting a Color property
DirectionalLight
Setting Color and Direction properties.
PointLight
SpotLight
DirectionalLight
PointLight
SpotLight
Camera
Perspective Camera
Orthographic Camera
PerspectiveCamera
OrthographicCamera
LookDirection property
A vector that points from the Position property to the
target point (x, y, z).
If the Position property is (–2, 0, 5) and the target is
the origin (0, 0, 0), the calculation is
(0, 0, 0) – (–2, 0, 5) = (2, 0, –5)
It's also possible to calculate LookDirection so that the
center of the triangle appears in the center of the
display
(0, 0.5, –1) – (–2, 0, 5) = (2, 0.5, –6)
Field Of View (FOV)