Beruflich Dokumente
Kultur Dokumente
Example 1:
typedef struct CUSTOMVERTEX
{
D3DXVECTOR3 position; // The 3D position for the vertex
D3DXVECTOR3 normal; // The surface normal for the vertex
} CUSTOMVERTEX;
#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ|D3DFVF_NORMAL) // The flexible vertex format
Example 2:
typedef struct CUSTOMVERTEX
{
float x,y,z; // vertex position
float nx,ny,nz; // vertex normal
float tu,tv; // texture co‐ordinate
DWORD colour;
} CUSTOMVERTEX;
#define D3DFVF_CUSTOMVERTEX(D3DFVF_XYZ|D3DFVF_NORMAL|D3DFVF_TEX1|D3DFVF_DIFFUSE)
Notes: if using diffuse color, the light should be turn off: d3dDevice‐>SetRenderState( D3DRS_LIGHTING,
FALSE );
MAE4060 Virtual Reality Systems And Application 2010
Vertex Buffers
Vertex buffers, represented by the IDirect3DVertexBuffer9 interface, are memory buffers that
contain vertex data. Vertex buffers can contain any vertex type ‐ transformed or
untransformed, lit or unlit ‐ that can be rendered through the use of the rendering methods in
the IDirect3DDevice9 interface. You can process the vertices in a vertex buffer to perform
operations such as transformation, lighting, or generating clipping flags.
Parameter:
g_pVB‐>Unlock();
The above two steps are included in the InitGeometry(). We should run it in InitInstance().
MAE4060 Virtual Reality Systems And Application 2010
Rendering from a Vertex Buffer
We render the vertex data in Render(). Rendering vertex data from a vertex buffer
requires a few steps.
1) you need to set the stream source by calling the IDirect3DDevice9::SetStreamSource
method, as shown in the following example.
d3dDevice‐>SetStreamSource( 0, g_pVB, sizeof(CUSTOMVERTEX) );
2) to inform Direct3D which vertex shader to use by calling the
IDirect3DDevice9::SetVertexShader method. The following sample code sets an FVF
code for the vertex shader. This informs Direct3D of the types of vertices it is dealing
with.
d3dDevice‐>SetFVF( D3DFVF_CUSTOMVERTEX );
3) After setting the stream source and vertex shader, any draw methods will use the
vertex buffer. The code example below shows how to render vertices from a vertex
buffer with the IDirect3DDevice9::DrawPrimitive method.
g_pd3dDevice‐>DrawPrimitive( D3DPT_TRIANGLESTRIP, 0, 2*50‐2 );
D3DPRIMITIVETYPE:
Vertices
Clipping and
World View Projection
Viewport
Transformation Transformation Transformation
Scaling
rasterizer
World Transform
A world transform changes coordinates from model space, where vertices are defined relative
to a model’s local origin, to World Space, where vertices are defined relative to an origin
common to all the objects in a scene. In essence, the world transform places a model into the
world; hence its name. The following diagram illustrates the relationship between the world
coordinate system and a model’s local coordinate system.
The world transform can include any combination of translations, rotations and scalings.
Perform World Transformation in D3D:
1) Declare the transformation matrix
2) Set the transformation and get the transformation matrix
3) Use SetTransform interface to perform the transformation
// Set up world matrix
D3DXMATRIXA16 matWorld;
D3DXMatrixIdentity(&matWorld);
D3DXMatrixRotationX(&matWorld, timeGetTime()/500.0f);
g_pd3dDevice->SetTransform(D3DTS_WORLD, &matWorld);
Math Functions of D3D : http://msdn.microsoft.com/en-us/library/bb172972(VS.85).aspx
MAE4060 Virtual Reality Systems And Application 2010
View Transform
The view transform locates the viewer in world space, transforming vertices into camera
space. In camera space, the camera, or viewer, is at the origin, looking in the positive
z-direction. Recall that Direct3D uses a left-handed coordinate system, so z is positive
into a scene. The view matrix relocates the objects in the world around a camera's
position - the origin of camera space - and orientation.
Projection Transform
You can think of the projection transformation as controlling the camera's internals; it is
analogous to choosing a lens for the camera. This is the most complicated of the three
transformation types.
D3DXMATRIXA16 matProj;
D3DXMatrixPerspectiveFovLH( &matProj, D3DX_PI/4, 1.0f, 1.0f, 100.0f );
g_pd3dDevice->SetTransform( D3DTS_PROJECTION, &matProj );
MAE4060 Virtual Reality Systems And Application 2010
Lighting
Light Types
Point Light
Point lights have color and position within a scene, but no single direction. They give off
light equally in all directions, as shown in the following illustration.
Directional Light
Directional lights have only color and direction, not position. They emit parallel light.
This means that all light generated by directional lights travels through a scene in the same
direction. Imagine a directional light as a light source at near infinite distance, such as the sun.
Spotlight
Spotlights have color, position, and direction in which they emit light. Light emitted
from a spotlight is made up of a bright inner cone and a larger outer cone, with the light
intensity diminishing between the two, as shown in the following illustration.
MAE4060 Virtual Reality Systems And Application 2010
D3DMATERIAL9 properties:
http://msdn.microsoft.com/en‐us/library/bb147175.aspx
Diffuse Value specifying the diffuse color of the material. See D3DCOLORVALUE
Ambient Value specifying the ambient color of the material. See D3DCOLORVALUE.
Specular Value specifying the specular color of the material. See D3DCOLORVALUE.
Emissive Value specifying the emissive color of the material. See D3DCOLORVALUE.
Power Floating‐point value specifying the sharpness of specular highlights. The higher
the value, the sharper the highlight.
Example:
// Set up a material. The material here just has the diffuse and ambient
// colors set to yellow. Note that only one material can be used at a time.
D3DMATERIAL9 mtrl;
ZeroMemory( &mtrl, sizeof(D3DMATERIAL9) );
mtrl.Diffuse.r = mtrl.Ambient.r = 1.0f;
mtrl.Diffuse.g = mtrl.Ambient.g = 1.0f;
mtrl.Diffuse.b = mtrl.Ambient.b = 0.0f;
mtrl.Diffuse.a = mtrl.Ambient.a = 1.0f;
g_pd3dDevice‐>SetMaterial( &mtrl );
MAE4060 Virtual Reality Systems And Application 2010
D3DLIGHT9 Data Structure Members:
http://msdn.microsoft.com/en‐us/library/bb172566(VS.85).aspx
D3DXVECTOR3 vecDir;
D3DLIGHT9 light;
ZeroMemory( &light, sizeof(D3DLIGHT9) );
light.Type = D3DLIGHT_DIRECTIONAL;
light.Diffuse.r = 1.0f;
light.Diffuse.g = 1.0f;
light.Diffuse.b = 1.0f;
vecDir = D3DXVECTOR3(cosf(timeGetTime()/350.0f), 1.0f, sinf(timeGetTime()/350.0f) );
D3DXVec3Normalize( (D3DXVECTOR3*)&light.Direction, &vecDir );
light.Range = 1000.0f;
g_pd3dDevice‐>SetLight( 0, &light );
g_pd3dDevice‐>LightEnable( 0, TRUE );
g_pd3dDevice‐>SetRenderState( D3DRS_LIGHTING, TRUE );