void SetupMatrix(float timeDelta) { static float total = 0.0f; total += timeDelta; // translate model to origin D3DXMATRIX world ; D3DXMatrixTranslation(&world, 0.0f, 0.0f, 0.0f) ; D3DXMATRIX rotMatrix; D3DXMatrixRotationY(&rotMatrix, total); world *= rotMatrix; g_pd3dDevice->SetTransform(D3DTS_WORLD, &world) ; // set view D3DXVECTOR3 eyePt(0.0f, 0.0f, -10.0f) ; D3DXVECTOR3 upVec(0.0f, 1.0f, 0.0f) ; D3DXVECTOR3 lookCenter(0.0f, 0.0f, 0.0f) ; D3DXMATRIX view ; D3DXMatrixLookAtLH(&view, &eyePt, &lookCenter, &upVec) ; g_pd3dDevice->SetTransform(D3DTS_VIEW, &view) ; // set projection D3DXMATRIX proj ; D3DXMatrixPerspectiveFovLH(&proj, D3DX_PI / 4, 1.0f, 1.0f, 1000.0f) ; g_pd3dDevice->SetTransform(D3DTS_PROJECTION, &proj) ; }
void DrawCube() { // translate model to origin D3DXMATRIX world ; D3DXMatrixTranslation(&world, 0.0f, 0.0f, 0.0f) ; g_pd3dDevice->SetTransform(D3DTS_WORLD, &world) ; // set view D3DXVECTOR3 eyePt(5.0f, 5.0f, -5.0f) ; D3DXVECTOR3 upVec(0.0f, 1.0f, 0.0f) ; D3DXVECTOR3 lookCenter(0.0f, 0.0f, 0.0f) ; D3DXMATRIX view ; D3DXMatrixLookAtLH(&view, &eyePt, &lookCenter, &upVec) ; g_pd3dDevice->SetTransform(D3DTS_VIEW, &view) ; // set projection D3DXMATRIX proj ; D3DXMatrixPerspectiveFovLH(&proj, D3DX_PI / 4, 1.0f, 1.0f, 1000.0f) ; g_pd3dDevice->SetTransform(D3DTS_PROJECTION, &proj) ; D3DXMATRIX worldviewproj = world * view * proj; // Set matrix g_pEffect->SetMatrix(g_hWVP, &worldviewproj); // Set technique g_pEffect->SetTechnique(g_hTech); // Render pass UINT numPass = 0; g_pEffect->Begin(&numPass, 0); g_pEffect->BeginPass(0); // Set texture D3DXCreateTextureFromFile(g_pd3dDevice, "../Common/Media/crate.jpg", &g_pCubeTexture) ; g_pEffect->SetTexture("g_pCubeTexture", g_pCubeTexture); /*g_pd3dDevice->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR); g_pd3dDevice->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR); g_pd3dDevice->SetSamplerState(0, D3DSAMP_MIPFILTER, D3DTEXF_LINEAR);*/ // Set stream source, and index buffer g_pd3dDevice->SetStreamSource( 0, g_pVB, 0, sizeof(Vertex) ); g_pd3dDevice->SetIndices(g_pIB) ; g_pd3dDevice->SetFVF(VERTEX_FVF) ; // Totally 24 points and 12 triangles g_pd3dDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, 24, 0, 12) ; g_pEffect->EndPass(); g_pEffect->End(); }
void SetupMatrix() { // set view D3DXVECTOR3 eyePt(0.0f, 0.0f, -5.0f) ; D3DXVECTOR3 upVec(0.0f, 1.0f, 0.0f) ; D3DXVECTOR3 lookCenter(0.0f, 0.0f, 0.0f) ; D3DXMATRIX view ; D3DXMatrixLookAtLH(&view, &eyePt, &lookCenter, &upVec) ; g_pd3dDevice->SetTransform(D3DTS_VIEW, &view) ; // set projection D3DXMATRIX proj ; D3DXMatrixPerspectiveFovLH(&proj, D3DX_PI / 4, 1.0f, 1.0f, 1000.0f) ; g_pd3dDevice->SetTransform(D3DTS_PROJECTION, &proj) ; }
void SetupMatrix(float timeDelta) { static float totalTime = 0; totalTime += timeDelta; // translate model to origin D3DXMATRIX matWorld ; D3DXMatrixTranslation(&matWorld, 0.0f, 0.0f, 0.0f); D3DXMATRIX rotMatrix; D3DXMatrixRotationY(&rotMatrix, totalTime); matWorld *= rotMatrix; // Matrix in shader was stored in column-major order, we need transpose it first. // Effect->SetMatrix will transpose it automatically, but SetVertexShader not, this function set the raw data. D3DXMatrixTranspose(&matWorld, &matWorld); // Set vertex shader variable g_pd3dDevice->SetVertexShaderConstantF(0, matWorld, 4); // set view // Make sure eye point and light postion at the same side beyond the teapot. D3DXVECTOR3 eyePt(0.0f, 0.0f, -10.0f) ; D3DXVECTOR3 upVec(0.0f, 1.0f, 0.0f) ; D3DXVECTOR3 lookCenter(0.0f, 0.0f, 0.0f) ; D3DXMATRIX view ; D3DXMatrixLookAtLH(&view, &eyePt, &lookCenter, &upVec) ; // set projection D3DXMATRIX proj ; D3DXMatrixPerspectiveFovLH(&proj, D3DX_PI / 4, 1.0f, 1.0f, 1000.0f) ; D3DXMATRIX matViewProj = view * proj; D3DXMatrixTranspose(&matViewProj, &matViewProj); // Set vertex shader variable HRESULT hr = g_pd3dDevice->SetVertexShaderConstantF(4, matViewProj, 4); if (FAILED(hr)) { MessageBox(NULL, L"Error", L"Set vertex shader variable failed", 0); } }
void SetupMatrix() { // translate model to origin D3DXMATRIX world ; D3DXMatrixTranslation(&world, 0.0f, 0.0f, 0.0f) ; g_pd3dDevice->SetTransform(D3DTS_WORLD, &world) ; // set view D3DXVECTOR3 eyePt(2.0f, 2.0f, -2.0f) ; D3DXVECTOR3 upVec(0.0f, 1.0f, 0.0f) ; D3DXVECTOR3 lookCenter(0.0f, 0.0f, 0.0f) ; D3DXMATRIX view ; D3DXMatrixLookAtLH(&view, &eyePt, &lookCenter, &upVec) ; g_pd3dDevice->SetTransform(D3DTS_VIEW, &view) ; // set projection D3DXMATRIX proj ; D3DXMatrixPerspectiveFovLH(&proj, D3DX_PI / 4, 1.0f, 1.0f, 1000.0f) ; g_pd3dDevice->SetTransform(D3DTS_PROJECTION, &proj) ; }
void SetupMatrix() { // Set object position D3DXMatrixTranslation(&matWorld[0], -2.0f, 0.0f, 0.0f) ; D3DXMatrixTranslation(&matWorld[1], 2.0f, 0.0f, 0.0f) ; D3DXMatrixTranslation(&matWorld[2], 0.0f, 2.0f, 0.0f) ; D3DXMatrixTranslation(&matWorld[3], 0.0f, -2.0f, 0.0f) ; // set view D3DXVECTOR3 eyePt(0.0f, 0.0f, -10.0f) ; D3DXVECTOR3 upVec(0.0f, 1.0f, 0.0f) ; D3DXVECTOR3 lookCenter(0.0f, 0.0f, 0.0f) ; D3DXMATRIX view ; D3DXMatrixLookAtLH(&view, &eyePt, &lookCenter, &upVec) ; g_pd3dDevice->SetTransform(D3DTS_VIEW, &view) ; // set projection D3DXMATRIX proj ; D3DXMatrixPerspectiveFovLH(&proj, D3DX_PI / 4, 1.0f, 1.0f, 1000.0f) ; g_pd3dDevice->SetTransform(D3DTS_PROJECTION, &proj) ; }
void DrawTeapot() { // translate model to origin D3DXMATRIX world ; D3DXMatrixTranslation(&world, 0.0f, 0.0f, 0.0f) ; g_pd3dDevice->SetTransform(D3DTS_WORLD, &world) ; // set view D3DXVECTOR3 eyePt(0.0f, 0.0f, -10.0f) ; D3DXVECTOR3 upVec(0.0f, 1.0f, 0.0f) ; D3DXVECTOR3 lookCenter(0.0f, 0.0f, 0.0f) ; D3DXMATRIX view ; D3DXMatrixLookAtLH(&view, &eyePt, &lookCenter, &upVec) ; g_pd3dDevice->SetTransform(D3DTS_VIEW, &view) ; // set projection D3DXMATRIX proj ; D3DXMatrixPerspectiveFovLH(&proj, D3DX_PI / 4, 1.0f, 1.0f, 1000.0f) ; g_pd3dDevice->SetTransform(D3DTS_PROJECTION, &proj) ; D3DXMATRIX worldviewproj = world * view * proj; // Set matrix g_pEffect->SetMatrix(g_hWVP, &worldviewproj); // Set technique g_pEffect->SetTechnique(g_hTech); // Render pass UINT numPass = 0; g_pEffect->Begin(&numPass, 0); g_pEffect->BeginPass(0); g_pTeapotMesh->DrawSubset(0); g_pEffect->EndPass(); g_pEffect->End(); }
HRESULT InitD3D( HWND hWnd ) { // Create the D3D object, which is needed to create the D3DDevice. if( NULL == ( g_pD3D = Direct3DCreate9( D3D_SDK_VERSION ) ) ) { MessageBoxA(NULL, "Create D3D9 object failed!", "Error", 0) ; return E_FAIL; } D3DPRESENT_PARAMETERS d3dpp; ZeroMemory( &d3dpp, sizeof(d3dpp) ); d3dpp.Windowed = TRUE; // use window mode, not full screen d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; d3dpp.BackBufferFormat = D3DFMT_UNKNOWN; d3dpp.EnableAutoDepthStencil = TRUE ; d3dpp.AutoDepthStencilFormat = D3DFMT_D16 ; // Create device if( FAILED( g_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &g_pd3dDevice ) ) ) { MessageBoxA(NULL, "Create D3D9 device failed!", "Error", 0) ; return E_FAIL; } g_pd3dDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE) ; // Create texture HRESULT hr = g_pd3dDevice->CreateTexture( 256, 256, 1, D3DUSAGE_RENDERTARGET, //d3dpp.BackBufferFormat, D3DFMT_R5G6B5, D3DPOOL_DEFAULT, &g_pRenderTexture, NULL) ; if (FAILED(hr)) { MessageBox(NULL, "Create texture failed!", "Error", 0) ; return E_FAIL ; } // Get texture surface hr = g_pRenderTexture->GetSurfaceLevel(0, &g_pRenderSurface) ; if (FAILED(hr)) { MessageBox(NULL, "Get surface on texture failed!", "Error", 0) ; return E_FAIL ; } // Create teapot D3DXCreateTeapot(g_pd3dDevice, &g_pTeapotMesh, NULL) ; // Create Cube CreateCube() ; // Create camera g_ModelViewCamera = new Camera() ; // Initialize view matrix D3DXVECTOR3 eyePt(0.0f, 0.0f, -5.0f) ; D3DXVECTOR3 upVec(0.0f, 1.0f, 0.0f) ; D3DXVECTOR3 lookCenter(0.0f, 0.0f, 0.0f) ; g_ModelViewCamera->SetViewParams(&eyePt, &lookCenter, &upVec) ; g_ModelViewCamera->SetProjParams(D3DX_PI / 4, 1.0f, 1.0f, 1000.0f) ; return S_OK; }