beginning direct3d gameprogramming04_3dfundamentals_20160414_jintaeks

Post on 13-Feb-2017

35 Views

Category:

Engineering

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Beginning Direct3D Game Programming:4. 3D Fundamentals

jintaeks@gmail.comDivision of Digital Contents, DongSeo University.

April 2016

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

2

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

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

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

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

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

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

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

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

9

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

10

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

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

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

13

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

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

Understanding Normals and Gouraud Shading

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

16

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

Comparison of Shading Models

18

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

map onto a face or multiple faces.

19

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

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

21

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

References

23

top related