beginning direct3d gameprogramming04_3dfundamentals_20160414_jintaeks

24
Beginning Direct3D Game Programming: 4. 3D Fundamentals [email protected] Division of Digital Contents, DongSeo University. April 2016

Upload: jintaek-seo

Post on 13-Feb-2017

35 views

Category:

Engineering


0 download

TRANSCRIPT

Page 1: Beginning direct3d gameprogramming04_3dfundamentals_20160414_jintaeks

Beginning Direct3D Game Programming:4. 3D Fundamentals

[email protected] of Digital Contents, DongSeo University.

April 2016

Page 2: Beginning direct3d gameprogramming04_3dfundamentals_20160414_jintaeks

Presentation outline Work with vertices and orientation Work with faces and polygons Use Gouraud shading Put into practice the basics of texture mapping

2

Page 3: Beginning direct3d gameprogramming04_3dfundamentals_20160414_jintaeks

3D Fundamentals

The Direct3D engine uses a left-handed coordinate system by default.

The OpenGL coordinate system uses a positive z-axis that points out of the screen. This is called a right-handed coordinate system.

3

Page 4: Beginning direct3d gameprogramming04_3dfundamentals_20160414_jintaeks

vertex

The square uses four vertices, v1 through v4. A vertex defines the point of intersection of one or

more lines; Direct3D needs a way to place these primitives in a

scene.

4

Page 5: Beginning direct3d gameprogramming04_3dfundamentals_20160414_jintaeks

To define a vertex in 3D space, you must specify three coordinate values: x, y, and z.v1 (0, 0, 0)v2 (0, 2, 0)v3 (0, 2, 2)v4 (0, 0, 2)

You can define the location of a vertex by specifying a vector from the origin to another point in 3D space.

5

Page 6: Beginning direct3d gameprogramming04_3dfundamentals_20160414_jintaeks

vector They are mathematical entities that describe a direc-

tion and a magnitude (which can be used for velocity, for example).

Vectors are usually denoted by a bold-faced letter, such as a.

Thus you could say the vector v = (1, 0, 1).

Vectors are not only used to define the position of the vertices, they are also used to define a direction.

6

Page 7: Beginning direct3d gameprogramming04_3dfundamentals_20160414_jintaeks

D3DXVECTORtypedef struct D3DXVECTOR3 { FLOAT x; FLOAT y; FLOAT z;} D3DXVECTOR3, *LPD3DXVECTOR3;

#ifdef __cplusplustypedef struct D3DXVECTOR3 : public D3DVECTOR{public: D3DXVECTOR3() {}; D3DXVECTOR3( CONST D3DVECTOR& ); D3DXVECTOR3( FLOAT x, FLOAT y, FLOAT z );

// assignment operators D3DXVECTOR3& operator += ( CONST D3DXVECTOR3& ); D3DXVECTOR3& operator -= ( CONST D3DXVECTOR3& ); D3DXVECTOR3& operator *= ( FLOAT );

7

Page 8: Beginning direct3d gameprogramming04_3dfundamentals_20160414_jintaeks

Understanding Vertices Vector is often used to describe the position of a vertex.

Nevertheless, a vertex consists of much more data than the position.struct Vertex{

D3DVECTOR3 vPosition;DWORD dwDiffuse; // diffuse vertex colorD3DVECTOR3 vNormal; // vertex normalFLOAT u, v; // texture coordinates of vertex

}

All vertices are stored in a vertex buffer.

8

Page 9: Beginning direct3d gameprogramming04_3dfundamentals_20160414_jintaeks

Working with Orientation You might describe the orientation of an object by using

at least four vectors—LOOK, UP, RIGHT, and POSITION.

9

Page 10: Beginning direct3d gameprogramming04_3dfundamentals_20160414_jintaeks

Understanding Faces Any 3D object consists of faces, as shown in Figure 4.6.

10

Page 11: Beginning direct3d gameprogramming04_3dfundamentals_20160414_jintaeks

Every model and 3D world is broken down into points, lines, and triangles, the only primitives Direct3D is able to render.

If you design your shapes using polygons with four or more vertices to reduce the number of faces, Direct3D will divide every face into triangles.

11

Page 12: Beginning direct3d gameprogramming04_3dfundamentals_20160414_jintaeks

Backface Culling Direct3D can cull back faces.

Backface Culling uses the cross-product of two sides of a triangle to calculate a vector that is perpendicular to the plane that is formed by the two sides.

12

Page 13: Beginning direct3d gameprogramming04_3dfundamentals_20160414_jintaeks

D3DCULLtypedef enum D3DCULL { D3DCULL_NONE = 1, D3DCULL_CW = 2, D3DCULL_CCW = 3, D3DCULL_FORCE_DWORD = 0x7fffffff } D3DCULL, *LPD3DCULL;

13

Page 14: Beginning direct3d gameprogramming04_3dfundamentals_20160414_jintaeks

Understanding Polygons A polygon can be defined in either 2D or 3D space. We will refer to polygons as n-sided closed figures de-

fined by at least three vertices. The simplest polygon is a triangle. Direct3D uses triangles to compose most polygons be-

cause all three vertices in a triangle are guaranteed to be co-planar.

14

Page 15: Beginning direct3d gameprogramming04_3dfundamentals_20160414_jintaeks

Understanding Normals Normals are perpendicular vectors that can be used to

define the direction a face is pointing or the visible side of a face.

A vertex normal is often perpendicular to a face, and it usually sits upon the vertex as one unit long.

The other type of normal is a face normal. It is usually perpendicular to a face and sits in the middle of the face.15

Page 16: Beginning direct3d gameprogramming04_3dfundamentals_20160414_jintaeks

Understanding Normals and Gouraud Shading

Shading is the process of performing lighting computa-tions and determining pixel colors from them.

16

Page 17: Beginning direct3d gameprogramming04_3dfundamentals_20160414_jintaeks

Gouraud Shading

The vertex normal vector is used in Gouraud shading mode (the default shading mode since the advent of Di-rect3D 6.0) to control lighting and to make some textur-ing effects.

17

Page 18: Beginning direct3d gameprogramming04_3dfundamentals_20160414_jintaeks

Comparison of Shading Models

18

Page 19: Beginning direct3d gameprogramming04_3dfundamentals_20160414_jintaeks

Texture-Mapping Basics Texture refers to a special kind of image that you can

map onto a face or multiple faces.

19

Page 20: Beginning direct3d gameprogramming04_3dfundamentals_20160414_jintaeks

Texture Coordinates in Direct3D

It uses a generic addressing scheme in which all texel addresses are in the range of 0.0 to 1.0, inclusive.

20

Page 21: Beginning direct3d gameprogramming04_3dfundamentals_20160414_jintaeks

• Illustration of pixel (a square of color) that is mapped into object space

21

Page 22: Beginning direct3d gameprogramming04_3dfundamentals_20160414_jintaeks

You can assign parts of a texture to a primitive by using texture coordinates.

If you want to texture a triangle, it could work something like Figure 4.13.

cvVertices [0] = {0.0f, 0.0f, 0.5f, 1.0f, 0.0f, 0.0f}; // x, y, z, rhw, tu, tvcvVertices [1] = {1.0f, 0.0f, 0.5f, 1.0f, 1.0f, 0.0f};cvVertices [2] = {1.0f, 1.0f, 0.5f, 1.0f, 1.0f, 1.0f};cvVertices [3] = {0.0f, 1.0f, 0.5f, 1.0f, 0.0f, 1.0f};

22

Page 23: Beginning direct3d gameprogramming04_3dfundamentals_20160414_jintaeks

References

23

Page 24: Beginning direct3d gameprogramming04_3dfundamentals_20160414_jintaeks