SlideShare a Scribd company logo
Beginning Direct3D Game Programming:
6. First Steps to Animation
jintaeks@gmail.com
Division of Digital Contents, DongSeo University.
April 2016
Understanding Transformations
 At the head of the pipeline, a model's vertices are declared
relative to a local coordinate system. This is a local origin
and an orientation.
2
World Transform
 The first stage of the geometry pipeline transforms a model's
vertices from their local coordinate system to a coordinate
system that is used by all the objects in a scene. The process
of reorienting the vertices is called the world transform.
3
View Transform
 In the next stage, the vertices that describe your 3D world are
oriented with respect to a camera. That is, your application
chooses a point-of-view for the scene, and world space
coordinates are relocated and rotated around the camera's
view, turning world space into camera space.
4
Projection Transform
 In this part of the pipeline, objects are usually scaled with
relation to their distance from the viewer in order to give
the illusion of depth to a scene; close objects are made to
appear larger than distant objects, and so on.
5
Clipping
 In the final part of the pipeline, any vertices that will not be
visible on the screen are removed.
6
Transform Pipeline
7
World Transform
 You move an object in a 3D world using a world
transformation.
 Figure 6.1 shows two cubes that are the same size but have
different positions and orientations.
8
 1. The cube is standing with its origin in the origin of the
world coordinate system.
 2. Rotate the cube.
 3. Move the cube to its new position.
9
 1. The cube is standing with its origin in the origin of the
world coordinate system.
 2. Move the cube to its new position.
 3. Rotate the cube.
10
 A 4×4 world matrix contains four vectors, which might
represent the orientation and position of an object.
 The u, v, and w vectors describe the orientation of the
object’s vertices compared to the world coordinate system.
 The t vector describes the position of the object’s vertex
compared to the world coordinate system.
 Every vertex of the cube gets the correct orientation and
position by being multiplied by this matrix.
11
 To describe the position of
the cube in Figure 6.4, you
must multiply the following
matrix by every vertex of the
cube.
12
D3DMATRIX mat;
mat._11 = 1.0f; mat._12 = 0.0f; mat._13 = 0.0f; mat._14 = 0.0f;
mat._21 = 0.0f; mat._22 = 1.0f; mat._23 = 0.0f; mat._24 = 0.0f;
mat._31 = 0.0f; mat._32 = 0.0f; mat._33 = 1.0f; mat._34 = 0.0f;
mat._41 = 2.0f; mat._42 = 2.0f; mat._43 = 2.0f; mat._44 = 1.0f;
D3DXMatrixTranslation()
 D3DXMatrixTranslation() might look like this:
inline VOID D3DXMatrixTranslation (D3DXMATRIX* m, FLOAT tx, FLOAT ty, FLOAT tz )
{
D3DXMatrixIdentity(m );
m._41 = tx; m._42 = ty; m._43 = tz;
}
=
1 0 0 0
0 1 0 0
0 0 1 0
tx ty tz 1
13
D3DXMatrixRotationY()
VOID D3DXMatrixRotationY( D3DXMATRIX* mat, FLOAT fRads )
{
D3DXMatrixIdentity(mat);
mat._11 = cosf( fRads );
mat._13 = -sinf( fRads );
mat._31 = sinf( fRads );
mat._33 = cosf( fRads );
}
=
cosf(fRads) 0 –sinf(fRads) 0
0 0 0 0
sinf(fRads) 0 cosf(fRads) 0
0 0 0 0
14
Concatenating Matrices
 One advantage of using matrices is that you can combine the
effects of two or more matrices by multiplying them.
 Use the D3DXMatrixMultiply function to perform matrix
multiplication.
15
D3DXMatrixMultiply()
D3DXMATRIX* D3DXMatrixMultiply(D3DXMATRIX* pOut
, CONST D3DXMATRIX* pM1
, CONST D3DMATRIX* pM2)
{
FLOAT pM[16];
ZeroMemory( pM, sizeof(D3DXMATRIX) );
for( WORD i=0; i<4; i++ )
for( WORD j=0; j<4; j++ )
for( WORD k=0; k<4; k++ )
pM[4*i+j] += pM1[4*i+k] * pM2[4*k+j];
memcpy( pOut, pM, sizeof(D3DXMATRIX) );
return (pOut);
}
16
IDirect3DDevice9::SetTransform
 After you prepare the world matrix, call the
IDirect3DDevice9::SetTransform method to set it, specifying
the D3DTS_WORLD macro for the first parameter.
// Set up the rotation matrix to generate 1 full rotation (2*PI radians)
// every 1000 ms. To avoid the loss of precision inherent in very high
// floating point numbers, the system time is modulated by the rotation
// period before conversion to a radian angle.
UINT iTime = timeGetTime() % 1000;
FLOAT fAngle = iTime * ( 2.0f * D3DX_PI ) / 1000.0f;
D3DXMatrixRotationY( &matWorld, fAngle );
g_pd3dDevice->SetTransform( D3DTS_WORLD, &matWorld );
17
View Transform
 The view transform locates the viewer in world space,
transforming vertices into camera space.
 In this formula, V is the view matrix being created, T is a
translation matrix that repositions objects in the world, and Rx
through Rz are rotation matrices that rotate objects along the
x-, y-, and z-axis.
18
Setting Up a View Matrix
 The following example creates a view matrix for left-handed
coordinates.
D3DXMATRIX mView;
D3DXVECTOR3 eye(2,3,3);
D3DXVECTOR3 at(0,0,0);
D3DXVECTOR3 up(0,1,0);
D3DXMatrixLookAtLH(&mView, &eye, &at, &up);
…
g_pd3dDevice->SetTransform( D3DTS_VIEW, &mView);
19
Projection Transform
 The projection matrix is typically a scale and perspective
projection. The projection transformation converts the
viewing frustum into a cuboid shape.
20
How to Project?
Two triangles are similar.
△ABC ∽ △ A'OC
x:x' = (z+d):d
x'(z+d) = xd
x' = xd / (z+d)
y' = yd / (z+d)
z' = 0
21
22
𝑥′
𝑦′
𝑧′
1
←
𝑥 ∙ 𝑑
𝑦 ∙ 𝑑
0
𝑧 + 𝑑
=
𝑑
0
0
0
0
𝑑
0
0
0
0
0
1
0
0
0
𝑑
𝑥
𝑦
𝑧
1
𝑥′
𝑦′
𝑧′
1
←
( )
(𝑧 + 𝑑)
Setting Up a Projection Matrix in DirectX
 In this matrix, Zn is the z-value of the near clipping plane. The
variables w, h, and Q have the following meanings. Note that
fovw and fovh represent the viewport's horizontal and vertical
fields of view, in radians.
23
Trigonometric Functions
 z’=zQ-QZn
 z’=ZnQ-QZn=0
 z’=ZfQ-QZn=Q(Zf-Zn)=Zf
24
Cot(x) graph
25
D3DXMatrixPerspectiveFovLH()
D3DXMATRIX * D3DXMatrixPerspectiveFovLH(
D3DXMATRIX * pOut,
FLOAT fovy,
FLOAT Aspect,
FLOAT zn,
FLOAT zf
);
// For the projection matrix, we set up a perspective transform (which
// transforms geometry from 3D view space to 2D viewport space, with
// a perspective divide making objects smaller in the distance). To build
// a perpsective transform, we need the field of view (1/4 pi is common),
// the aspect ratio, and the near and far clipping planes (which define at
// what distances geometry should be no longer be rendered).
D3DXMATRIXA16 matProj;
D3DXMatrixPerspectiveFovLH( &matProj, D3DX_PI / 4, 1.0f, 1.0f, 100.0f );
g_pd3dDevice->SetTransform( D3DTS_PROJECTION, &matProj );
26
Working with the Viewport
 Conceptually, a viewport is a two-dimensional (2D) rectangle
into which a 3D scene is projected.
 In Direct3D, the rectangle exists as coordinates within a
Direct3D surface that the system uses as a rendering target.
27
The Viewing Frustum
 A viewing frustum is 3D volume in a scene positioned
relative to the viewport's camera.
 The shape of the volume affects how models are projected
from camera space onto the screen.
28
 The viewing frustum is defined by fov (field of view) and by
the distances of the front and back clipping planes, specified
in z-coordinates.
29
Viewport Rectangle
 You define the viewport rectangle in C++ by using the
D3DVIEWPORT9 structure.
 The D3DVIEWPORT9 structure is used with the following
viewport manipulation methods exposed.
– IDirect3DDevice9::GetViewport
– IDirect3DDevice9::SetViewport
 Use IDirect3DDevice9::Clear to clear the viewport.
30
 You can use the following settings for the members of the
D3DVIEWPORT9 structure to achieve this in C++.
D3DVIEWPORT9 viewData = { 0, 0, width, height, 0.0f, 1.0f };
 After setting values in the D3DVIEWPORT9 structure, apply the
viewport parameters to the device by calling its
IDirect3DDevice9::SetViewport method.
HRESULT hr;
hr = pd3dDevice->SetViewport(&viewData);
if(FAILED(hr))
return hr;
31
Depth Buffering
 A depth buffer, often
called a z-buffer or a
w-buffer, is a property
of the device that
stores depth
information to be used
by Direct3D.
32
Creating a Depth Buffer
 To create a depth buffer that is managed by Direct3D, set the
appropriate members of the D3DPRESENT_PARAMETERS
structure.
D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory( &d3dpp, sizeof(d3dpp) );
d3dpp.Windowed = TRUE;
d3dpp.SwapEffect = D3DSWAPEFFECT_COPY;
d3dpp.EnableAutoDepthStencil = TRUE;
d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
 …
if( FAILED( g_pD3D->CreateDevice( D3DADAPTER_DEFAULT
, D3DDEVTYPE_HAL, hWnd
, D3DCREATE_SOFTWARE_VERTEXPROCESSING
, &d3dpp, &d3dDevice ) ) )
return E_FAIL;
33
Quaternion
 Quaternions contain a scalar component and a 3D vector
component.
 With the Direct3D quaternion class, you use the w, x, y, and z
components by adding .x, .y, .z, and .w to the variable names.
34
D3DXQUATERNION * D3DXQuaternionRotationAxis(
D3DXQUATERNION *pOut
, CONST D3DXVECTOR3 *pV
, FLOAT Angle
);
D3DXQUATERNION * D3DXQuaternionRotationMatrix(
D3DXQUATERNION * pOut,
CONST D3DXMATRIX * pM
);
D3DXMATRIX * D3DXMatrixRotationQuaternion(
D3DXMATRIX *pOut,
CONST D3DXQUATERNION *pQ
);
35
Tutorial 03: Using Matrices
 This tutorial introduces the concept of matrices and shows
how to use them.
– Step 1 - Defining the World Transformation Matrix
– Step 2 - Defining the View Transformation Matrix
– Step 3 - Defining the Projection Transformation Matrix
36
Step 1 - Defining the World Transformation Matrix
 The world transformation matrix defines how to translate,
scale, and rotate the geometry in the 3D model space.
D3DXMATRIX matWorld;
D3DXMatrixRotationY( &matWorld, timeGetTime()/150.0f );
g_pd3dDevice->SetTransform( D3DTS_WORLD, &matWorld );
37
Step 2 - Defining the View Transformation Matrix
 The view transformation matrix defines the position and
rotation of the view. The view matrix is the camera for the
scene.
D3DXVECTOR3 vEyePt ( 0.0f, 3.0f,-5.0f );
D3DXVECTOR3 vLookatPt( 0.0f, 0.0f, 0.0f );
D3DXVECTOR3 vUpVec ( 0.0f, 1.0f, 0.0f );
D3DXMATRIXA16 matView;
D3DXMatrixLookAtLH( &matView, &vEyePt, &vLookatPt, &vUpVec );
g_pd3dDevice->SetTransform( D3DTS_VIEW, &matView );
38
Step 3 - Defining the Projection Transformation Matrix
 The projection transformation matrix defines how geometry is
transformed from 3D view space to 2D viewport space.
D3DXMATRIX matProj;
D3DXMatrixPerspectiveFovLH( &matProj, D3DX_PI/4, 1.0f, 1.0f, 100.0f );
g_pd3dDevice->SetTransform( D3DTS_PROJECTION, &matProj );
39
Practice
 Achieve similar animation by setting View Matrix.
 Rotate the triangle about x-axis.
 Translate the triangle about (5,5,0), then rotate.
 Rotate the triangle, the translate (5,5,0).
– Check differences between two transforms.
 Modify the Fov of Projection matrix and check the visual
differences.
40
References
41
Beginning direct3d gameprogramming06_firststepstoanimation_20161115_jintaeks

More Related Content

PPTX
Beginning direct3d gameprogramming10_shaderdetail_20160506_jintaeks
PPT
Java3 d 1
PPT
Designing Parametric cubic Curves
PDF
[MIRU2018] Global Average Poolingの特性を用いたAttention Branch Network
PPTX
Beginning direct3d gameprogramming07_lightsandmaterials_20161117_jintaeks
PPT
CS 354 Bezier Curves
PPTX
Shaderx5 2.6normalmappingwithoutprecomputedtangents 130318 (1)
PPT
Geometry Shader-based Bump Mapping Setup
Beginning direct3d gameprogramming10_shaderdetail_20160506_jintaeks
Java3 d 1
Designing Parametric cubic Curves
[MIRU2018] Global Average Poolingの特性を用いたAttention Branch Network
Beginning direct3d gameprogramming07_lightsandmaterials_20161117_jintaeks
CS 354 Bezier Curves
Shaderx5 2.6normalmappingwithoutprecomputedtangents 130318 (1)
Geometry Shader-based Bump Mapping Setup

What's hot (20)

PPT
CS 354 Transformation, Clipping, and Culling
PDF
SwiftGirl20170904 - Apple Map
PDF
SA09 Realtime education
PDF
[shaderx4] 4.2 Eliminating Surface Acne with Gradient Shadow Mapping
PPTX
Trident International Graphics Workshop 2014 5/5
PPT
Fingerprint High Level Classification
PPTX
ARCHITECTURAL CONDITIONING FOR DISENTANGLEMENT OF OBJECT IDENTITY AND POSTURE...
PDF
[shaderx5] 4.2 Multisampling Extension for Gradient Shadow Maps
PDF
Getting Started with 3D Game Development on Nokia Series 40 Asha Phones
PPTX
Notes on image processing
PDF
Vector Distance Transform Maps for Autonomous Mobile Robot Navigation
PDF
DeepXplore: Automated Whitebox Testing of Deep Learning
PPT
Virtual Trackball
PPTX
Brief intro : Invariance and Equivariance
PPT
CS 354 Acceleration Structures
PPT
Shadow Volumes on Programmable Graphics Hardware
PPT
Lifting 1
PDF
Lec 9 05_sept [compatibility mode]
PPT
CS 354 Object Viewing and Representation
CS 354 Transformation, Clipping, and Culling
SwiftGirl20170904 - Apple Map
SA09 Realtime education
[shaderx4] 4.2 Eliminating Surface Acne with Gradient Shadow Mapping
Trident International Graphics Workshop 2014 5/5
Fingerprint High Level Classification
ARCHITECTURAL CONDITIONING FOR DISENTANGLEMENT OF OBJECT IDENTITY AND POSTURE...
[shaderx5] 4.2 Multisampling Extension for Gradient Shadow Maps
Getting Started with 3D Game Development on Nokia Series 40 Asha Phones
Notes on image processing
Vector Distance Transform Maps for Autonomous Mobile Robot Navigation
DeepXplore: Automated Whitebox Testing of Deep Learning
Virtual Trackball
Brief intro : Invariance and Equivariance
CS 354 Acceleration Structures
Shadow Volumes on Programmable Graphics Hardware
Lifting 1
Lec 9 05_sept [compatibility mode]
CS 354 Object Viewing and Representation
Ad

Viewers also liked (14)

PPTX
Beginning direct3d gameprogramming03_programmingconventions_20160414_jintaeks
PPTX
Beginning direct3d gameprogramming02_overviewofhalandcom_20160408_jintaeks
PPTX
Beginning direct3d gameprogramming05_thebasics_20160421_jintaeks
PPTX
Beginning direct3d gameprogramming01_20161102_jintaeks
PPTX
Beginning direct3d gameprogrammingmath03_vectors_20160328_jintaeks
PPTX
Beginning direct3d gameprogrammingmath04_calculus_20160324_jintaeks
PPTX
Beginning direct3d gameprogrammingmath06_transformations_20161019_jintaeks
PPTX
Beginning direct3d gameprogramming04_3dfundamentals_20160414_jintaeks
PPTX
Beginning direct3d gameprogramming01_thehistoryofdirect3dgraphics_20160407_ji...
PPTX
Beginning direct3d gameprogramming08_usingtextures_20160428_jintaeks
PPTX
Beginning direct3d gameprogrammingmath02_logarithm_20160324_jintaeks
PPTX
Beginning direct3d gameprogrammingcpp02_20160324_jintaeks
PPTX
Beginning direct3d gameprogramming09_shaderprogramming_20160505_jintaeks
PPTX
Beginning direct3d gameprogrammingmath05_matrices_20160515_jintaeks
Beginning direct3d gameprogramming03_programmingconventions_20160414_jintaeks
Beginning direct3d gameprogramming02_overviewofhalandcom_20160408_jintaeks
Beginning direct3d gameprogramming05_thebasics_20160421_jintaeks
Beginning direct3d gameprogramming01_20161102_jintaeks
Beginning direct3d gameprogrammingmath03_vectors_20160328_jintaeks
Beginning direct3d gameprogrammingmath04_calculus_20160324_jintaeks
Beginning direct3d gameprogrammingmath06_transformations_20161019_jintaeks
Beginning direct3d gameprogramming04_3dfundamentals_20160414_jintaeks
Beginning direct3d gameprogramming01_thehistoryofdirect3dgraphics_20160407_ji...
Beginning direct3d gameprogramming08_usingtextures_20160428_jintaeks
Beginning direct3d gameprogrammingmath02_logarithm_20160324_jintaeks
Beginning direct3d gameprogrammingcpp02_20160324_jintaeks
Beginning direct3d gameprogramming09_shaderprogramming_20160505_jintaeks
Beginning direct3d gameprogrammingmath05_matrices_20160515_jintaeks
Ad

Similar to Beginning direct3d gameprogramming06_firststepstoanimation_20161115_jintaeks (20)

PDF
Geometric objects and transformations
PDF
Computer Graphics - Lecture 03 - Virtual Cameras and the Transformation Pipeline
PPTX
Trident International Graphics Workshop 2014 2/5
PDF
XNA L02–Basic Matrices and Transformations
PPTX
Task 2
PDF
ShaderX³: Geometry Manipulation - Morphing between two different objects
PDF
WT-4071, GPU accelerated 3D graphics for Java, by Kevin Rushforth, Chien Yang...
PPTX
GFX Part 5 - Introduction to Object Transformations in OpenGL ES
PPTX
Geometric-Modeling-in-Graphics-and-Visual-Computing-Updated Version.pptx
PPTX
3 d graphics with opengl part 1
PDF
Useful Tools for Making Video Games - XNA (2008)
PPT
Three dimensional concepts - Computer Graphics
PDF
unit-4-computer-graphics.pdf . VERY USE FULL
PPT
Advanced Game Development with the Mobile 3D Graphics API
DOCX
Displaying 3 d polygon animations
PDF
201707 SER332 Lecture10
PPT
OpenGL Transformations
PDF
Im looking for coding help I dont really need this to be explained.pdf
PDF
OpenGL L03-Utilities
PPT
Computer Viewing
Geometric objects and transformations
Computer Graphics - Lecture 03 - Virtual Cameras and the Transformation Pipeline
Trident International Graphics Workshop 2014 2/5
XNA L02–Basic Matrices and Transformations
Task 2
ShaderX³: Geometry Manipulation - Morphing between two different objects
WT-4071, GPU accelerated 3D graphics for Java, by Kevin Rushforth, Chien Yang...
GFX Part 5 - Introduction to Object Transformations in OpenGL ES
Geometric-Modeling-in-Graphics-and-Visual-Computing-Updated Version.pptx
3 d graphics with opengl part 1
Useful Tools for Making Video Games - XNA (2008)
Three dimensional concepts - Computer Graphics
unit-4-computer-graphics.pdf . VERY USE FULL
Advanced Game Development with the Mobile 3D Graphics API
Displaying 3 d polygon animations
201707 SER332 Lecture10
OpenGL Transformations
Im looking for coding help I dont really need this to be explained.pdf
OpenGL L03-Utilities
Computer Viewing

More from JinTaek Seo (14)

PPTX
Neural network 20161210_jintaekseo
PPTX
05 heap 20161110_jintaeks
PPT
02 linked list_20160217_jintaekseo
PPTX
Hermite spline english_20161201_jintaeks
PPT
01 stack 20160908_jintaek_seo
PPTX
03 fsm how_toimplementai_state_20161006_jintaeks
PPTX
Beginning direct3d gameprogrammingmath01_primer_20160324_jintaeks
PPT
Boost pp 20091102_서진택
PPT
아직도가야할길 훈련 20090722_서진택
PPT
3ds maxscript 튜토리얼_20151206_서진택
PPT
Multithread programming 20151206_서진택
PPT
03동물 로봇그리고사람 public_20151125_서진택
PPT
20150605 홀트입양예비부모모임 서진택
PPT
Boost라이브러리의내부구조 20151111 서진택
Neural network 20161210_jintaekseo
05 heap 20161110_jintaeks
02 linked list_20160217_jintaekseo
Hermite spline english_20161201_jintaeks
01 stack 20160908_jintaek_seo
03 fsm how_toimplementai_state_20161006_jintaeks
Beginning direct3d gameprogrammingmath01_primer_20160324_jintaeks
Boost pp 20091102_서진택
아직도가야할길 훈련 20090722_서진택
3ds maxscript 튜토리얼_20151206_서진택
Multithread programming 20151206_서진택
03동물 로봇그리고사람 public_20151125_서진택
20150605 홀트입양예비부모모임 서진택
Boost라이브러리의내부구조 20151111 서진택

Recently uploaded (20)

PDF
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
PPTX
bas. eng. economics group 4 presentation 1.pptx
PPT
Mechanical Engineering MATERIALS Selection
PDF
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
PDF
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
PPTX
Internet of Things (IOT) - A guide to understanding
PPT
CRASH COURSE IN ALTERNATIVE PLUMBING CLASS
PPTX
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
PPTX
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
PPTX
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
PDF
PPT on Performance Review to get promotions
PPTX
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
PPTX
web development for engineering and engineering
PDF
Embodied AI: Ushering in the Next Era of Intelligent Systems
PDF
Automation-in-Manufacturing-Chapter-Introduction.pdf
PPTX
CYBER-CRIMES AND SECURITY A guide to understanding
PPTX
Construction Project Organization Group 2.pptx
PDF
Digital Logic Computer Design lecture notes
PDF
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
PPTX
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
bas. eng. economics group 4 presentation 1.pptx
Mechanical Engineering MATERIALS Selection
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
Internet of Things (IOT) - A guide to understanding
CRASH COURSE IN ALTERNATIVE PLUMBING CLASS
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
PPT on Performance Review to get promotions
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
web development for engineering and engineering
Embodied AI: Ushering in the Next Era of Intelligent Systems
Automation-in-Manufacturing-Chapter-Introduction.pdf
CYBER-CRIMES AND SECURITY A guide to understanding
Construction Project Organization Group 2.pptx
Digital Logic Computer Design lecture notes
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx

Beginning direct3d gameprogramming06_firststepstoanimation_20161115_jintaeks

  • 1. Beginning Direct3D Game Programming: 6. First Steps to Animation jintaeks@gmail.com Division of Digital Contents, DongSeo University. April 2016
  • 2. Understanding Transformations  At the head of the pipeline, a model's vertices are declared relative to a local coordinate system. This is a local origin and an orientation. 2
  • 3. World Transform  The first stage of the geometry pipeline transforms a model's vertices from their local coordinate system to a coordinate system that is used by all the objects in a scene. The process of reorienting the vertices is called the world transform. 3
  • 4. View Transform  In the next stage, the vertices that describe your 3D world are oriented with respect to a camera. That is, your application chooses a point-of-view for the scene, and world space coordinates are relocated and rotated around the camera's view, turning world space into camera space. 4
  • 5. Projection Transform  In this part of the pipeline, objects are usually scaled with relation to their distance from the viewer in order to give the illusion of depth to a scene; close objects are made to appear larger than distant objects, and so on. 5
  • 6. Clipping  In the final part of the pipeline, any vertices that will not be visible on the screen are removed. 6
  • 8. World Transform  You move an object in a 3D world using a world transformation.  Figure 6.1 shows two cubes that are the same size but have different positions and orientations. 8
  • 9.  1. The cube is standing with its origin in the origin of the world coordinate system.  2. Rotate the cube.  3. Move the cube to its new position. 9
  • 10.  1. The cube is standing with its origin in the origin of the world coordinate system.  2. Move the cube to its new position.  3. Rotate the cube. 10
  • 11.  A 4×4 world matrix contains four vectors, which might represent the orientation and position of an object.  The u, v, and w vectors describe the orientation of the object’s vertices compared to the world coordinate system.  The t vector describes the position of the object’s vertex compared to the world coordinate system.  Every vertex of the cube gets the correct orientation and position by being multiplied by this matrix. 11
  • 12.  To describe the position of the cube in Figure 6.4, you must multiply the following matrix by every vertex of the cube. 12 D3DMATRIX mat; mat._11 = 1.0f; mat._12 = 0.0f; mat._13 = 0.0f; mat._14 = 0.0f; mat._21 = 0.0f; mat._22 = 1.0f; mat._23 = 0.0f; mat._24 = 0.0f; mat._31 = 0.0f; mat._32 = 0.0f; mat._33 = 1.0f; mat._34 = 0.0f; mat._41 = 2.0f; mat._42 = 2.0f; mat._43 = 2.0f; mat._44 = 1.0f;
  • 13. D3DXMatrixTranslation()  D3DXMatrixTranslation() might look like this: inline VOID D3DXMatrixTranslation (D3DXMATRIX* m, FLOAT tx, FLOAT ty, FLOAT tz ) { D3DXMatrixIdentity(m ); m._41 = tx; m._42 = ty; m._43 = tz; } = 1 0 0 0 0 1 0 0 0 0 1 0 tx ty tz 1 13
  • 14. D3DXMatrixRotationY() VOID D3DXMatrixRotationY( D3DXMATRIX* mat, FLOAT fRads ) { D3DXMatrixIdentity(mat); mat._11 = cosf( fRads ); mat._13 = -sinf( fRads ); mat._31 = sinf( fRads ); mat._33 = cosf( fRads ); } = cosf(fRads) 0 –sinf(fRads) 0 0 0 0 0 sinf(fRads) 0 cosf(fRads) 0 0 0 0 0 14
  • 15. Concatenating Matrices  One advantage of using matrices is that you can combine the effects of two or more matrices by multiplying them.  Use the D3DXMatrixMultiply function to perform matrix multiplication. 15
  • 16. D3DXMatrixMultiply() D3DXMATRIX* D3DXMatrixMultiply(D3DXMATRIX* pOut , CONST D3DXMATRIX* pM1 , CONST D3DMATRIX* pM2) { FLOAT pM[16]; ZeroMemory( pM, sizeof(D3DXMATRIX) ); for( WORD i=0; i<4; i++ ) for( WORD j=0; j<4; j++ ) for( WORD k=0; k<4; k++ ) pM[4*i+j] += pM1[4*i+k] * pM2[4*k+j]; memcpy( pOut, pM, sizeof(D3DXMATRIX) ); return (pOut); } 16
  • 17. IDirect3DDevice9::SetTransform  After you prepare the world matrix, call the IDirect3DDevice9::SetTransform method to set it, specifying the D3DTS_WORLD macro for the first parameter. // Set up the rotation matrix to generate 1 full rotation (2*PI radians) // every 1000 ms. To avoid the loss of precision inherent in very high // floating point numbers, the system time is modulated by the rotation // period before conversion to a radian angle. UINT iTime = timeGetTime() % 1000; FLOAT fAngle = iTime * ( 2.0f * D3DX_PI ) / 1000.0f; D3DXMatrixRotationY( &matWorld, fAngle ); g_pd3dDevice->SetTransform( D3DTS_WORLD, &matWorld ); 17
  • 18. View Transform  The view transform locates the viewer in world space, transforming vertices into camera space.  In this formula, V is the view matrix being created, T is a translation matrix that repositions objects in the world, and Rx through Rz are rotation matrices that rotate objects along the x-, y-, and z-axis. 18
  • 19. Setting Up a View Matrix  The following example creates a view matrix for left-handed coordinates. D3DXMATRIX mView; D3DXVECTOR3 eye(2,3,3); D3DXVECTOR3 at(0,0,0); D3DXVECTOR3 up(0,1,0); D3DXMatrixLookAtLH(&mView, &eye, &at, &up); … g_pd3dDevice->SetTransform( D3DTS_VIEW, &mView); 19
  • 20. Projection Transform  The projection matrix is typically a scale and perspective projection. The projection transformation converts the viewing frustum into a cuboid shape. 20
  • 21. How to Project? Two triangles are similar. △ABC ∽ △ A'OC x:x' = (z+d):d x'(z+d) = xd x' = xd / (z+d) y' = yd / (z+d) z' = 0 21
  • 22. 22 𝑥′ 𝑦′ 𝑧′ 1 ← 𝑥 ∙ 𝑑 𝑦 ∙ 𝑑 0 𝑧 + 𝑑 = 𝑑 0 0 0 0 𝑑 0 0 0 0 0 1 0 0 0 𝑑 𝑥 𝑦 𝑧 1 𝑥′ 𝑦′ 𝑧′ 1 ← ( ) (𝑧 + 𝑑)
  • 23. Setting Up a Projection Matrix in DirectX  In this matrix, Zn is the z-value of the near clipping plane. The variables w, h, and Q have the following meanings. Note that fovw and fovh represent the viewport's horizontal and vertical fields of view, in radians. 23
  • 24. Trigonometric Functions  z’=zQ-QZn  z’=ZnQ-QZn=0  z’=ZfQ-QZn=Q(Zf-Zn)=Zf 24
  • 26. D3DXMatrixPerspectiveFovLH() D3DXMATRIX * D3DXMatrixPerspectiveFovLH( D3DXMATRIX * pOut, FLOAT fovy, FLOAT Aspect, FLOAT zn, FLOAT zf ); // For the projection matrix, we set up a perspective transform (which // transforms geometry from 3D view space to 2D viewport space, with // a perspective divide making objects smaller in the distance). To build // a perpsective transform, we need the field of view (1/4 pi is common), // the aspect ratio, and the near and far clipping planes (which define at // what distances geometry should be no longer be rendered). D3DXMATRIXA16 matProj; D3DXMatrixPerspectiveFovLH( &matProj, D3DX_PI / 4, 1.0f, 1.0f, 100.0f ); g_pd3dDevice->SetTransform( D3DTS_PROJECTION, &matProj ); 26
  • 27. Working with the Viewport  Conceptually, a viewport is a two-dimensional (2D) rectangle into which a 3D scene is projected.  In Direct3D, the rectangle exists as coordinates within a Direct3D surface that the system uses as a rendering target. 27
  • 28. The Viewing Frustum  A viewing frustum is 3D volume in a scene positioned relative to the viewport's camera.  The shape of the volume affects how models are projected from camera space onto the screen. 28
  • 29.  The viewing frustum is defined by fov (field of view) and by the distances of the front and back clipping planes, specified in z-coordinates. 29
  • 30. Viewport Rectangle  You define the viewport rectangle in C++ by using the D3DVIEWPORT9 structure.  The D3DVIEWPORT9 structure is used with the following viewport manipulation methods exposed. – IDirect3DDevice9::GetViewport – IDirect3DDevice9::SetViewport  Use IDirect3DDevice9::Clear to clear the viewport. 30
  • 31.  You can use the following settings for the members of the D3DVIEWPORT9 structure to achieve this in C++. D3DVIEWPORT9 viewData = { 0, 0, width, height, 0.0f, 1.0f };  After setting values in the D3DVIEWPORT9 structure, apply the viewport parameters to the device by calling its IDirect3DDevice9::SetViewport method. HRESULT hr; hr = pd3dDevice->SetViewport(&viewData); if(FAILED(hr)) return hr; 31
  • 32. Depth Buffering  A depth buffer, often called a z-buffer or a w-buffer, is a property of the device that stores depth information to be used by Direct3D. 32
  • 33. Creating a Depth Buffer  To create a depth buffer that is managed by Direct3D, set the appropriate members of the D3DPRESENT_PARAMETERS structure. D3DPRESENT_PARAMETERS d3dpp; ZeroMemory( &d3dpp, sizeof(d3dpp) ); d3dpp.Windowed = TRUE; d3dpp.SwapEffect = D3DSWAPEFFECT_COPY; d3dpp.EnableAutoDepthStencil = TRUE; d3dpp.AutoDepthStencilFormat = D3DFMT_D16;  … if( FAILED( g_pD3D->CreateDevice( D3DADAPTER_DEFAULT , D3DDEVTYPE_HAL, hWnd , D3DCREATE_SOFTWARE_VERTEXPROCESSING , &d3dpp, &d3dDevice ) ) ) return E_FAIL; 33
  • 34. Quaternion  Quaternions contain a scalar component and a 3D vector component.  With the Direct3D quaternion class, you use the w, x, y, and z components by adding .x, .y, .z, and .w to the variable names. 34
  • 35. D3DXQUATERNION * D3DXQuaternionRotationAxis( D3DXQUATERNION *pOut , CONST D3DXVECTOR3 *pV , FLOAT Angle ); D3DXQUATERNION * D3DXQuaternionRotationMatrix( D3DXQUATERNION * pOut, CONST D3DXMATRIX * pM ); D3DXMATRIX * D3DXMatrixRotationQuaternion( D3DXMATRIX *pOut, CONST D3DXQUATERNION *pQ ); 35
  • 36. Tutorial 03: Using Matrices  This tutorial introduces the concept of matrices and shows how to use them. – Step 1 - Defining the World Transformation Matrix – Step 2 - Defining the View Transformation Matrix – Step 3 - Defining the Projection Transformation Matrix 36
  • 37. Step 1 - Defining the World Transformation Matrix  The world transformation matrix defines how to translate, scale, and rotate the geometry in the 3D model space. D3DXMATRIX matWorld; D3DXMatrixRotationY( &matWorld, timeGetTime()/150.0f ); g_pd3dDevice->SetTransform( D3DTS_WORLD, &matWorld ); 37
  • 38. Step 2 - Defining the View Transformation Matrix  The view transformation matrix defines the position and rotation of the view. The view matrix is the camera for the scene. D3DXVECTOR3 vEyePt ( 0.0f, 3.0f,-5.0f ); D3DXVECTOR3 vLookatPt( 0.0f, 0.0f, 0.0f ); D3DXVECTOR3 vUpVec ( 0.0f, 1.0f, 0.0f ); D3DXMATRIXA16 matView; D3DXMatrixLookAtLH( &matView, &vEyePt, &vLookatPt, &vUpVec ); g_pd3dDevice->SetTransform( D3DTS_VIEW, &matView ); 38
  • 39. Step 3 - Defining the Projection Transformation Matrix  The projection transformation matrix defines how geometry is transformed from 3D view space to 2D viewport space. D3DXMATRIX matProj; D3DXMatrixPerspectiveFovLH( &matProj, D3DX_PI/4, 1.0f, 1.0f, 100.0f ); g_pd3dDevice->SetTransform( D3DTS_PROJECTION, &matProj ); 39
  • 40. Practice  Achieve similar animation by setting View Matrix.  Rotate the triangle about x-axis.  Translate the triangle about (5,5,0), then rotate.  Rotate the triangle, the translate (5,5,0). – Check differences between two transforms.  Modify the Fov of Projection matrix and check the visual differences. 40

Editor's Notes

  • #19: The translation and rotation matrices are based on the camera's logical position and orientation in world space.
  • #21: Because the near end of the viewing frustum is smaller than the far end, this has the effect of expanding objects that are near to the camera.