Upload
cmp2012
View
222
Download
0
Embed Size (px)
Citation preview
8/14/2019 Section 4 - Drawing Colors
1/60
Computer GraphicsComputer Graphics
Using Direct 3DUsing Direct 3D
Drawing & ColorsDrawing & Colors
8/14/2019 Section 4 - Drawing Colors
2/60
2
Outline Vertex Buffers Drawing Primitives
Indexed Primitives & Index Buffers Color Representation
Shading
Rendering State
Moving to 3D
8/14/2019 Section 4 - Drawing Colors
3/60
3
Outline Vertex Buffers Drawing Primitives
Indexed Primitives & Index Buffers
Color Representation
Shading
Rendering State
Moving to 3D
8/14/2019 Section 4 - Drawing Colors
4/60
4
Vertex Buffer
As mentioned before, the basicdrawing elements are vertices and
primitives. Vertices hold information such as
coordinates, colors and texture
coordinates while primitives are basicelements composed of thesevertices.
8/14/2019 Section 4 - Drawing Colors
5/60
5
Vertex Buffer (Cont.)
Vertex information is placed in avertex buffer.
What we need is to Create a vertex buffer
Fill it Use it to draw primitives
8/14/2019 Section 4 - Drawing Colors
6/60
6
Vertex Buffer (Cont.)
To create a vertex buffer we mainlyneed to specify
The size of the buffer. What kind of information each vertex will
hold.
8/14/2019 Section 4 - Drawing Colors
7/60
7
Vertex Buffer (Cont.)
8/14/2019 Section 4 - Drawing Colors
8/60
8
Vertex Buffer (Cont.)
FVF (Flexible Vertex Format) code isan OR-Combination of flags that
indicates The information stored in each vertex How the vertex will be processed by the
fixed pipeline
8/14/2019 Section 4 - Drawing Colors
9/60
9
Vertex Buffer (Cont.)
Example FVF Flags D3DFVF_DIFFUSE : Indicates the presence of a diffuse color
component
D3DFVF_XYZ : Indicates the presence of untransformedcoordinates
D3DFVF_XYZRHW : Indicates the presence of transformedcoordinates
D3DFVF_NORMAL : Indicates the presence of normal vector
component, which means the vertex is unlit D3DFVF_TEX1 : Indicates the presence of texture coordinates
for 1 texture
8/14/2019 Section 4 - Drawing Colors
10/60
10
Vertex Buffer (Cont.)
The Usageparameter indicates how the bufferwill be used. For example: D3DUSAGE_POINTS flag indicates that the buffer
will be used to store point primitives. D3DUSAGE_WRITEONLY indicates that the
application will only write to the buffer. D3D tries toplace the buffer in the bast memory for writing.
We will set this parameter to 0 in most cases.
8/14/2019 Section 4 - Drawing Colors
11/60
11
Vertex Buffer (Cont.)
The Poolparameter determines the memoryarea that will hold the buffer
D3D Resources can be placed in systemmemory, local video memory, AGP memory ...etc
Each has its pros & cons
We will use D3DPOOL_DEFAULTorD3DPOOL_MANAGED
8/14/2019 Section 4 - Drawing Colors
12/60
12
Vertex Buffer (Cont.)
To fill the vertex buffer we
Lock it (and obtain memory address)
Fill the memory
Unlock it
0 Offset & Size = Lock the whole buffer
You can set it to 0.
Other values indicate intended usage
to enhance performance
8/14/2019 Section 4 - Drawing Colors
13/60
13
Vertex Buffer (Cont.)
It is easier to deal with vertices asstructures rather than raw bytes.
This simplifies: Determining the size of the vertex usingsizeof(And hence the size of vertexbuffer)
Populating the vertex buffer
8/14/2019 Section 4 - Drawing Colors
14/60
14
Vertex Buffer (Cont.)
Example: Transformed Lit Verticesstruct TransformedLitVertex
{
FLOAT x, y, z;
FLOAT rhw;
DWORD color;
};
#define FVF_TL (D3DFVF_XYZRHW | D3DFVF_DIFFUSE)
8/14/2019 Section 4 - Drawing Colors
15/60
15
Vertex Buffer (Cont.)
Example: Transformed Lit Vertices//Creating a vertex buffer
IDirect3DVertexBuffer9* VB = NULL;
if( FAILED(Device->CreateVertexBuffer( 3*sizeof(TransformedLitVertex), 0 /*Usage*/,FVF_TL, D3DPOOL_DEFAULT, &VB, NULL ) ) )
{
//Handle failure}
8/14/2019 Section 4 - Drawing Colors
16/60
16
Vertex Buffer (Cont.)
Example: Transformed Lit Vertices//Filling the vertex buffer
TransformedLitVertex vertices[] = { ... };
VOID* pVertices;//1. Lock and obtain address
if( FAILED( VB->Lock( 0, 0, (void**)&pVertices, 0 ) ) ) { /*HandleFailure*/ }
//2. Write
memcpy( pVertices, vertices, sizeof(vertices) );//3. Unlock
VB->Unlock();
8/14/2019 Section 4 - Drawing Colors
17/60
17
Outline Vertex Buffers Drawing Primitives
Indexed Primitives & Index Buffers
Color Representation
Shading
Rendering State
Moving to 3D
8/14/2019 Section 4 - Drawing Colors
18/60
18
Drawing Primitives
Having prepared the vertex buffer itis time to draw objects using the
vertices in that buffer. Before drawing we have to
Set the vertex buffer to read from
Set the FVF to assume
8/14/2019 Section 4 - Drawing Colors
19/60
19
Drawing Primitives (Cont.)
To draw a primitive we useDrawPrimitive method
8/14/2019 Section 4 - Drawing Colors
20/60
20
Drawing Primitives (Cont.)
Some primitive types are D3DPT_POINTLIST
D3DPT_LINELIST D3DPT_LINESTRIP
D3DPT_TRIANGLELIST
D3DPT_TRIANGLESTRIP D3DPT_TRIANGLEFAN
8/14/2019 Section 4 - Drawing Colors
21/60
21
Drawing Primitives (Cont.)
Some primitive types are D3DPT_POINTLIST
D3DPT_LINELIST D3DPT_LINESTRIP
D3DPT_TRIANGLELIST
D3DPT_TRIANGLESTRIP D3DPT_TRIANGLEFAN
Image from MSDN
8/14/2019 Section 4 - Drawing Colors
22/60
22
Drawing Primitives (Cont.)
Some primitive types are D3DPT_POINTLIST
D3DPT_LINELIST D3DPT_LINESTRIP
D3DPT_TRIANGLELIST
D3DPT_TRIANGLESTRIP D3DPT_TRIANGLEFAN
Image from MSDN
8/14/2019 Section 4 - Drawing Colors
23/60
23
Drawing Primitives (Cont.)
Some primitive types are D3DPT_POINTLIST
D3DPT_LINELIST D3DPT_LINESTRIP
D3DPT_TRIANGLELIST
D3DPT_TRIANGLESTRIP D3DPT_TRIANGLEFAN Image from MSDN
8/14/2019 Section 4 - Drawing Colors
24/60
24
Drawing Primitives (Cont.)
Drawing commands are issued betweenIDirect3DDevice9::BeginScene andIDirect3DDevice9::EndScene methods
ExampleDevice->Clear(0, 0, D3DCLEAR_TARGET , 0x0, 0, 0);Device->BeginScene();
Device->SetStreamSource( 0, VB, 0, sizeof(CUSTOMVERTEX) );
Device->SetFVF( D3DFVF_CUSTOMVERTEX );
Device->DrawPrimitive( D3DPT_TRIANGLELIST, 0, 2 );Device->EndScene();
Device->Present(NULL,NULL,NULL,NULL);
8/14/2019 Section 4 - Drawing Colors
25/60
25
Summing it up
Setup Initialize D3D
Create & Fill Vertex Buffer
Render Clear Buffers
Begin Scene
Set Stream Source
Set FVF
Drawing Commands
End Scene
Present
8/14/2019 Section 4 - Drawing Colors
26/60
26
Think about it
Can you draw a cube using one drawingcommand without specifying a vertex
in the buffer twice ?
8/14/2019 Section 4 - Drawing Colors
27/60
27
Think about it
Remember, redundant vertices =wasted memory BW + redundant
vertex processing
8/14/2019 Section 4 - Drawing Colors
28/60
28
Outline Vertex Buffers Drawing Primitives
Indexed Primitives & Index Buffers
Color Representation
Shading
Rendering State
Moving to 3D
8/14/2019 Section 4 - Drawing Colors
29/60
29
Indexed Primitives
Indexed primitives are the solution to the problem ofredundant vertices
Instead of accessing the vertices in the vertex buffer instrict sequential order, provide an index array (called index
buffer) that specifies the order in which vertices in thevertex buffer are accessed. The point is that a vertex can bereferenced more than once in that array.
Image from alt.pluralsight.com
8/14/2019 Section 4 - Drawing Colors
30/60
30
Indexed Primitives (Cont.)
Index buffers are almost identical increation and usage to vertex buffers
D3DFMT_INDEX16
or D3DFMT_INDEX32 (Check device caps)
N * sizeof(WORD)
or N * sizeof(DWORD)
Depending on Format
8/14/2019 Section 4 - Drawing Colors
31/60
31
Indexed Primitives (Cont.)
Index buffers are almost identical increation and usage to vertex buffers
8/14/2019 Section 4 - Drawing Colors
32/60
32
Indexed Primitives (Cont.)
Just as we callIDirect3DDevice9::SetStreamSourceto the set the vertex buffer to be used, we
also callIdirect3DDevice9::SetIndices to setthe index buffer to be used
8/14/2019 Section 4 - Drawing Colors
33/60
33
Indexed Primitives (Cont.)
To draw a primitive using a vertex buffer and anindex buffer, we useIDirect3DDevice9::DrawIndexedPrimitive
8/14/2019 Section 4 - Drawing Colors
34/60
34
Summing it up again
Setup Initialize D3D
Create & Fill Vertex Buffer
Create & Fill Index Buffer
Render Clear Buffers
Begin Scene
Set Stream Source Set Indices
Set FVF
Drawing Commands
End Scene
Present
8/14/2019 Section 4 - Drawing Colors
35/60
35
Outline Vertex Buffers Drawing Primitives
Indexed Primitives & Index Buffers
Color Representation
Shading
Rendering State
Moving to 3D
8/14/2019 Section 4 - Drawing Colors
36/60
36
Color Representation
A color is represented by an RGBtriplet. Each component occupies a
byte (0~255)
8/14/2019 Section 4 - Drawing Colors
37/60
37
Color Representation(Cont.)
Direct3D9 provides D3DCOLOR type
An alias for DWORD D3DCOLOR_ARGB(a, r, g, b) macro
Accepts color components and returns thecorresponding D3DColor e.g.
D3DCOLOR red = D3DCOLOR_ARGB(255, 255, 0, 0)
8/14/2019 Section 4 - Drawing Colors
38/60
38
Color Representation(Cont.)
Direct3D9 provides D3DCOLORVALUE
A 128-bit color structure where eachcomponent is represented by a floating point(0 = no intensity, 1 = max intensity)
8/14/2019 Section 4 - Drawing Colors
39/60
39
Color Representation(Cont.)
Direct3D9 provides D3DXCOLOR
Similar to D3DCOLORVALUE but providesuseful constructors and overloaded operatorsfor e.g. (color addition and component-wisemultiplication)
8/14/2019 Section 4 - Drawing Colors
40/60
40
Color Representation(Cont.)
The color of a primitive by the colorof the vertices that constitute it.
To add color to vertices we add aD3DCOLOR (or DWORD) componentthe vertex structure and set theD3DFVF_DIFFUSE flag in the FVFcode.
8/14/2019 Section 4 - Drawing Colors
41/60
41
Color Representation(Cont.)
Note that you must use 32-bit colorrepresentation.
To use 128-bit color representationyou must use a custom vertex shader
(and of course there must be HWsupport).
8/14/2019 Section 4 - Drawing Colors
42/60
42
Outline Vertex Buffers Drawing Primitives
Indexed Primitives & Index Buffers
Color Representation
Shading
Rendering State
Moving to 3D
8/14/2019 Section 4 - Drawing Colors
43/60
43
Shading
Shading is the process of determiningthe colors of the pixels in a primitive
given the colors of vertices thatconstitute that primitive.
8/14/2019 Section 4 - Drawing Colors
44/60
44
Shading (Cont.)
The simplest shading technique is flatshading. Simply, the pixels in a
primitive get the color of the firstvertex in that primitive.
To use flat shading callDevice->SetRenderState(D3DRS_SHADEMODE, D3DSHADE_FLAT);
8/14/2019 Section 4 - Drawing Colors
45/60
45
Shading (Cont.)
The more elegant shading technique isGouraoud shading (a.k.a smooth
shading), where the color of a pixel islinearly interpolated using the colorsof all vertices in the primitive.
8/14/2019 Section 4 - Drawing Colors
46/60
46
Shading (Cont.)
Gouraud shading is the defaultshading technique in D3D.
If you enabled flat shading and wantto switch back to Gouraud shading,
callDevice->SetRenderState(D3DRS_SHADEMODE,D3DSHADE_GOURAUD);
8/14/2019 Section 4 - Drawing Colors
47/60
47
Shading (Cont.)
The following figure shows flat vs.Gouraud shading
8/14/2019 Section 4 - Drawing Colors
48/60
48
Outline Vertex Buffers Drawing Primitives
Indexed Primitives & Index Buffers
Color Representation Shading
Rendering State
Moving to 3D
8/14/2019 Section 4 - Drawing Colors
49/60
49
Rendering State
IDirect3DDevice9::SetRenderState is used to seta number of state variables that determine how D3Dwill render the scene.
We saw the shading technique as an example.
In general
Device->SetRenderState(StateVariableName,NewValue);
State variables have default values so you set them onlywhen needed. When set, they retain their new valuesuntil set again.
8/14/2019 Section 4 - Drawing Colors
50/60
50
Outline Vertex Buffers Drawing Primitives
Indexed Primitives & Index Buffers
Color Representation Shading
Rendering State
Moving to 3D
8/14/2019 Section 4 - Drawing Colors
51/60
51
Moving to 3D
Given the current D3D programstructure we developed. What do
we need to draw 3D scenes ??
8/14/2019 Section 4 - Drawing Colors
52/60
52
Moving to 3D
We need to Enable depth buffer.
Tell D3D that from now on, the verticesare untransformed.
Tell D3D how to map 3D vertexcoordinates to 2D screen coordinates
(specify transformation matrices).
8/14/2019 Section 4 - Drawing Colors
53/60
53
Moving to 3D (Cont.)
To enable the depth buffer, set the presentationparameters during initialization
d3dpp.EnableAutoDepthStencil = true;
d3dpp.AutoDepthStencilFormat = D3DFMT_D24S8; You have also to clear the depth buffer before
rendering a new frame
Device->Clear(0, 0, D3DCLEAR_TARGET |
D3DCLEAR_ZBUFFER, 0xffffffff, 1.0f, 0);
8/14/2019 Section 4 - Drawing Colors
54/60
54
Moving to 3D (Cont.)
To indicate untransformed vertices Remove rhw component from vertex structure.structure ColoredVertex
{FLOAT x, y, z; D3DCOLOR color;
};
Use D3DFVF_XYZ flag instead of
D3DFVF_XYZRHW in the FVF code.#define FVF_UTL (D3DFVF_XYZ | D3DFVF_DIFFUSE)
8/14/2019 Section 4 - Drawing Colors
55/60
55
Moving to 3D (Cont.)
To set a transformation we callDevice->SetTransformation(TransformationType,
TransformationMatrix)
The transformation matrix is apointer to a D3DMATRIX object
8/14/2019 Section 4 - Drawing Colors
56/60
56
Moving to 3D (Cont.)
Recall that we have three kinds oftransformations
World Transformation
Camera Transformation
Projection Transformation
So, TransformationType can beD3DTS_WORLD,D3DTS_VIEW or D3DTS_PROJECTION
8/14/2019 Section 4 - Drawing Colors
57/60
57
Moving to 3D (Cont.)
We do not need to fill the matrices by hand.Direct3D provides utility functions that creatematrices given the information we care about.
Examples of such functions are: D3DXMatrixTranslation, D3DXMatrixRotationX and
D3DXMatrixScaling for world transformation
D3DXLookAtRH for view transformation
D3DXMatrixPerspectiveLH for projectiontransformation
Details will be given in the lab
8/14/2019 Section 4 - Drawing Colors
58/60
58
Moving to 3D (Cont.)
Recall that transformations can be combinedby multiplying the corresponding matrices.
8/14/2019 Section 4 - Drawing Colors
59/60
59
Summing it up again
Setup Initialize D3D
Create & Fill Vertex Buffer
Create & Fill Index Buffer
Set Fixed Transformations
Render Clear Buffers
Begin Scene
Set Stream Source
Set Indices Set FVF
Set Frame-level/Object-level Transformations
Drawing Commands
End Scene
Present
8/14/2019 Section 4 - Drawing Colors
60/60
60
Any Questions ??