280 likes | 431 Views
Lecture 7: Direct3D Basics (II). Prof. Hsien-Hsin Sean Lee School of Electrical and Computer Engineering Georgia Institute of Technology. Projection Transform. World Transform. View Transform. Direct3D Transformation. Triangle List (Vertex Pool). Transformed vertices.
E N D
Lecture 7: Direct3D Basics (II) Prof. Hsien-Hsin Sean Lee School of Electrical and Computer Engineering Georgia Institute of Technology
Projection Transform World Transform View Transform Direct3D Transformation Triangle List (Vertex Pool) Transformed vertices
D3D World Transformation Matrices • Translation D3DXMatrixTranslation(D3DXMATRIX *Out, x, y, z); • Scaling D3DXMatrixScaling(D3DXMATRIX *Out, sx, sy, sz); • Rotation D3DXMatrixRotationX(D3DXMATRIX *Out, float angle); D3DXMatrixRotationY(D3DXMATRIX *Out, float angle); D3DXMatrixRotationZ(D3DXMATRIX *Out, float angle); D3DXMatrixRotationAxis(D3DXMATRIX *Out, D3DXVECTOR3 *v, float angle); • Setup D3DTS_WORLD IDirect3DDevice9::SetTransform( D3DTRANSFORMSTATETYPE State CONST D3DMATRIX *pMatrix);
D3D World Transformation • All world transformation matrices can be simply combined gd3dDevice->BeginScene(); . . . . D3DXMatrixTranslation(&matXlat, 2.0f, 4.0f, 1.0f+inc); D3DXMatrixRotationX(&matRX, D3DXToRadian(inc_theta)); D3DXMatrixRotationY(&matRY, D3DXToRadian(inc_theta*1.5)); D3DXMatrixRotationZ(&matRZ, D3DXToRadian(inc_theta*2.2)); D3DXMatrixScaling(&matScale, 2.0f, 2.0f, 2.0f); gd3dDevice->SetTransform( D3DTS_WORLD, &(matXlat * matRZ * matRY * matRX * matScale)); . . . . gd3dDevice->DrawPrimitve(…); gd3dDevice->EndScene(); gd3dDevice->Present(0, 0, 0, 0);
y z pAt(-3, 3, 1) x Eye(4, -1, -3) D3D View Transformation • Build a Left-Handed, Look-at Matrix D3DXMatrixLookAtLH( D3DXMATRIX *Out, D3DXVECTOR3 *Eye, D3DXVECTOR3 *pAt, D3DXVECTOR3 *pUp); • Setup Usually (0, 1, 0) the world is upward IDirect3DDevice9::SetTransform( D3DTRANSFORMSTATETYPE State CONST D3DMATRIX *pMatrix); D3DTS_VIEW
D3D View Transformation gd3dDevice->BeginScene(); . . . . D3DXMatrixLookAtLH( &matView, &D3DXVECTOR3(0.0f, 0.0f, 10.0f), // camera loc &D3DXVECTOR3(0.0f, 0.0f, 0.0f), // look-at target &D3DXVECTOR3(0.0f, 1.0f, 0.0f)); gd3dDevice->SetTransform(D3DTS_VIEW, &matView); . . . . gd3dDevice->DrawPrimitve(…); gd3dDevice->EndScene(); gd3dDevice->Present(0, 0, 0, 0);
D3D Projection Transformation • Build a Left-Handed, Look-at Matrix D3DXMatrixPerspectiveFovLH( D3DXMATRIX *Out, FLOAT fovy, FLOAT Aspect, FLOAT near, FLOAT far); • Setup Width far near fovy/2 IDirect3DDevice9::SetTransform( D3DTRANSFORMSTATETYPE State CONST D3DMATRIX *pMatrix); Eye Height Aspect = width/height D3DTS_PROJECTION
D3D World Transformation gd3dDevice->BeginScene(); . . . . gd3dDevice->SetTransform(D3DTS_PROJECTION, &matProjection); . . . . gd3dDevice->DrawPrimitve(…); gd3dDevice->EndScene(); gd3dDevice->Present(0, 0, 0, 0); } buildProjMatrix() { // Every time Window is resized, aspect ratio changed float w = (float)md3dPP.BackBufferWidth; float h = (float)md3dPP.BackBufferHeight; D3DXPerspectiveProjectionFovLH( &matProjection, D3DXToRadian(45), w/h, 1.0f, 1000.0f); }
Example 1:Transformation FirstTransformDecl (See Demo in Visual Studio)
Example 2: First 3D Object Transformation FirstXformIdxTetrahedron (See Demo in Visual Studio)
(0,0) (1,0) (0,1) (1,1) Texture Mapping /* Declaration of an object class */ LPDIRECT3DTEXTURE9 texture_brick; . . . . . . /* buildVertexBuffer() */ D3DXCreateTextureFromFile(gd3dDevice, "C:\\Tech\\Texture Maps\\brick.jpg", &texture_brick); v[8]= VertexPCT(3.0f,3.0f, 3.0f, D3DCOLOR_XRGB(0, 255, 255), 8, 0); v[9]= VertexPCT(3.0f, -3.0f, 3.0f, D3DCOLOR_XRGB(0, 255, 255), 8, 8); v[10]=VertexPCT(3.0f, -3.0f,-3.0f, D3DCOLOR_XRGB(0, 255, 255), 0, 8); v[11]=VertexPCT(3.0f, 3.0f, -3.0f, D3DCOLOR_XRGB(0, 255, 255), 0, 0); . . . . . . /* drawScene() */ gd3dDevice->BeginScene(); gd3dDevice->SetTexture(0, texture_brick); gd3dDevice->DrawPrimitive(… ); gd3dDevice->EndScene(); IDirect3DDevice9::SetTexture( DWORD Stage, IDirect3DBaseTexture9*pTexture ); Stage ID (0 to 7)
Example 3: 3D Object Texturing FirstTexture See Demo in Visual Studio)
Adding Control for Object Movement • These functions simply change • “Delay” in the delay_loop() • “Translation matrix” of an object under control • E.g., z coordinate for zooming /* MainWndProc() */ case WM_KEYDOWN: if(wParam == 'S') slow(); else if( wParam == 'A') fast(); if(wParam == VK_UP) zoom_out(); else if(wParam == VK_DOWN) zoom_in(); if(wParam == VK_LEFT) move_left(); else if(wParam == VK_RIGHT) move_right();
Example 4: Textured Object Movement FirstTextureWithZ See Demo in Visual Studio)
+z +x Rotate Camera (Looking Around) Top view move /* draw_scene() */ float lookatX, lookatZ; // rotate the camera on the XZ plane lookatX = radius * sin(move); lookatZ = radius * cos(move); D3DXMatrixLookAtLH(&matView, &D3DXVECTOR3(0.0f, 30.0f, 0.0f), // camera position &D3DXVECTOR3(lookatX, 16.0f+up, lookatZ), &D3DXVECTOR3(0.0f, 1.0f, 0.0f)); gd3dDevice->SetTransform(D3DTS_VIEW, &matView);
Example 5:Moving Camera Around MovingCamera See Demo in Visual Studio)
D3DLIGHT9 /* SetLightSource */ D3DVECTOR vecPos = {90.0f, 0.0f, 100.0f}; light[0].Type = D3DLIGHT_POINT; light[0].Diffuse.r = 0.0f; light[0].Diffuse.g = 0.0f; light[0].Diffuse.b = 1.0f; light[0].Diffuse.a = 1.0f; light[0].Position = vecPos; light[0].Attenuation0 = 1.0f; light[0].Attenuation1 = 0.0f; light[0].Attenuation2 = 0.0f; light[0].Range = 300.0f; . . . . . gd3dDevice->SetLight(0, &(light[0])) POINT, DIRECTIONAL, SPOTLIGHT typedef struct D3DLIGHT9 { D3DLIGHTTYPE Type; D3DCOLORVALUE Diffuse; D3DCOLORVALUE Specular; D3DCOLORVALUE Ambient; D3DVECTOR Position; D3DVECTOR Direction; float Range; float Falloff; float Attenuation0; float Attenuation1; float Attenuation2; float Theta; float Phi; } D3DLIGHT9, *LPD3DLIGHT; Ignored by Directional Light Ignored by Point Light
Set Material Properties /* SetLightSource */ material.Diffuse.r = 1.0f; material.Diffuse.g = 1.0f; material.Diffuse.b = 1.0f; material.Ambient.r = 0.3f; material.Ambient.g = 0.3f; material.Ambient.b = 0.3f; material.Diffuse.a = 1.0f; material.Ambient.a = 1.0f; gd3dDevice->SetMaterial(&material)); // this is globally used
Enable Lighting /* SetRenderState */ gd3dDevice->SetRenderState(D3DRS_LIGHTING, TRUE); gd3dDevice->SetRenderState(D3DRS_AMBIENT, D3DCOLOR_XRGB(200, 200, 200)); // Set light ambient /* DrawScene() */ gd3dDevice->LightEnable(0, lightswitch[0]); gd3dDevice->LightEnable(1, lightswitch[1]); gd3dDevice->LightEnable(2, lightswitch[2]); gd3dDevice->LightEnable(3, lightswitch[3]);
Example 6:Ambient and Diffuse Light with Various Light Sources FirstAmbDiffLighting See Demo in Visual Studio)
D3D Mesh Objects • Pre-constructed Objects /* buildVertexBuffer() */ LPD3DXMESH meshSphere; D3DXCreateSphere(gd3dDevice, 10.0f,// radius 100, // num of slices about axis 100, // num of stacks along axis &meshSphere, NULL); /* DrawScene() */ . . . . . meshSphere->DrawSubset(0);
Example 7:Spot Light Source (Diffuse) FirstSpotLight See Demo in Visual Studio)
Adding Specular Effect gd3dDevice->SetRenderState(D3DRS_SPECULARENABLE, TRUE); /* Set Light Source */ light[4].Specular.r = 1.0f; light[4].Specular.g = 1.0f; light[4].Specular.b = 1.0f; light[4].Specular.a = 1.0f; gd3dDevice->SetLight(4, &(light[4]); . . . gd3dDevice->LightEnable(4, TRUE); /* Set Material */ material.Specular.r = 0.8f; material.Specular.g = 0.8f; material.Specular.b = 0.8f; material.Power = 20; . . . . . gd3dDevice->SetMaterial(&material);
Example 8:Spot Light Source Specular Lighting Effect FirstSpeuclarLight See Demo in Visual Studio)
Color (Alpha) Blending /* SetRenderState() */ gd3dDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE); gd3dDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA); gd3dDevice->SetRenderState(D3DRS_DESTBLEND,D3DBLEND_INVSRCALPHA); gd3dDevice->SetRenderState(D3DRS_BLENDOP, D3DBLENDOP_ADD); /* Set New Material for the Object to be transparent */ material.Diffuse.a = 0.85f; material.Ambient.a = 0.85f; material.Specular.a = 0.9f;
Example 9:Alpha Blending Translucency Effect FirstBlending See Demo in Visual Studio)
Multi-Texturing /* DrawScene() */ gd3dDevice->SetTexture(0, texture_monalisa); gd3dDevice->SetTexture(1, texture_lightmap); gd3dDevice->SetTextureStageState(0, D3DTSS_COLORARG1,D3DTA_TEXTURE); gd3dDevice->SetTextureStageState(0, D3DTSS_COLORARG2, D3DTA_DIFFUSE); gd3dDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_BLENDDIFFUSEALPHA); gd3dDevice->SetTextureStageState(1, D3DTSS_TEXCOORDINDEX, 0); gd3dDevice->SetTextureStageState(1, D3DTSS_COLORARG1, D3DTA_TEXTURE); gd3dDevice->SetTextureStageState(1, D3DTSS_COLORARG2, D3DTA_CURRENT); gd3dDevice->SetTextureStageState(1, D3DTSS_COLOROP, D3DTOP_MODULATE); gd3dDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, 60, 54, 2); gd3dDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_MODULATE); gd3dDevice->SetTextureStageState(1, D3DTSS_COLOROP, D3DTOP_MODULATE); RGBA=Arg1*Arg2
Example 10:Multi-TexturingLight Map Effect MultiTexturing See Demo in Visual Studio)