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 FPCamera::Reset(float eyeX, float eyeY, float eyeZ, float centerX, float centerY, float centerZ, float upX, float upY, float upZ) { glm::vec3 eyePt(eyeX, eyeY, eyeZ); glm::vec3 centerPt(centerX, centerY, centerZ); glm::vec3 upVec(upX, upY, upZ); Reset(eyePt, centerPt, upVec); }
//-------------------------------------------------------------- // render system in far mode void SolarSystem::renderFar( const mgPoint3& moonCenter) { mgPoint3 eyePt(m_eyePt.x / SYSTEM_FAR_SCALE, m_eyePt.y / SYSTEM_FAR_SCALE, m_eyePt.z / SYSTEM_FAR_SCALE); mgDisplay->setEyePt(eyePt); // mgDisplay->setZEnable(false); // get distance from eye to moon center mgPoint3 pt(moonCenter); pt.subtract(m_eyePt); double moonDist = pt.length(); // planet origin is 0,0,0, so eye pt is distance double planetDist = m_eyePt.length(); // draw farthest object first if (moonDist > planetDist) { renderMoonAndRingFar(moonCenter); renderPlanetAndBeltFar(); } else { renderPlanetAndBeltFar(); renderMoonAndRingFar(moonCenter); } // mgDisplay->setZEnable(true); mgDisplay->clearBuffer(MG_DEPTH_BUFFER); }
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(); }
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) ; // Set view matrix D3DXVECTOR3 eyePt(0, 1.0f, -5.0f) ; D3DXVECTOR3 lookAt(0, 0.0f, 0) ; D3DXVECTOR3 upVec(0, 1.0f, 0) ; g_pCamera->SetViewParams(eyePt, lookAt, upVec) ; // Set projection matrix g_pCamera->SetProjParams(D3DX_PI / 4, 1.0f, 1.0f, 1000.0f) ; g_pd3dDevice->SetSamplerState( 0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR ); g_pd3dDevice->SetSamplerState( 0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR ); g_pd3dDevice->SetSamplerState( 0, D3DSAMP_MIPFILTER, D3DTEXF_LINEAR ); g_pd3dDevice->SetSamplerState( 0, D3DSAMP_ADDRESSU, D3DTADDRESS_WRAP ); g_pd3dDevice->SetSamplerState( 0, D3DSAMP_ADDRESSV, D3DTADDRESS_WRAP ); // Create teapot D3DXCreateTeapot(g_pd3dDevice, &g_pTeapotMesh, NULL) ; return S_OK; }
VOID SetupMatrix() { // Set view matrix D3DXVECTOR3 eyePt(0, 1.0f, -15.0f) ; D3DXVECTOR3 lookAt(0, 0.0f, 0) ; D3DXVECTOR3 upVec(0, 1.0f, 0) ; D3DXMATRIX view ; D3DXMatrixLookAtLH(&view, &eyePt, &lookAt, &upVec) ; g_pd3dDevice->SetTransform(D3DTS_VIEW, &view) ; // Set projection matrix D3DXMATRIX proj ; D3DXMatrixPerspectiveFovLH(&proj, D3DX_PI / 4, 1.0f, 1.0f, 1000.0f) ; g_pd3dDevice->SetTransform(D3DTS_PROJECTION, &proj) ; }
void SetupMatrix() { g_pd3dDevice->SetTransform(D3DTS_WORLD, &gWorldMatrix) ; // 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 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 upVec(0.0f, 1.0f, 0.0f) ; D3DXVECTOR3 lookCenter(0.0f, 0.0f, 0.0f) ; D3DXVECTOR3 eyePt(0.0f, 0.0f, -20.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; }
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 teapot D3DXCreateTeapot(g_pd3dDevice, &g_pTeapotMesh, NULL) ; // Initialize terrain g_pTerrain->BuildGridsBuffer(g_pd3dDevice) ; D3DXMatrixIdentity(&g_matBillboardWorld) ; // Set view matrix D3DXVECTOR3 eyePt(0, 0.0f, -20.0f) ; D3DXVECTOR3 lookAt(0, 1.0f, 0) ; D3DXVECTOR3 upVec(0, 1.0f, 0) ; g_pCamera->SetViewParams(eyePt, lookAt, upVec) ; // Set projection matrix g_pCamera->SetProjParams(D3DX_PI / 4, 1.0f, 1.0f, 1000.0f) ; // Create texture HRESULT hr = D3DXCreateTextureFromFile(g_pd3dDevice, "../Common/Media/autumn.jpg", &g_pTexture) ; if (FAILED(hr)) { DXTRACE_ERR_MSGBOX(DXGetErrorString(hr), hr) ; } // Set texture sampler state g_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLOROP, D3DTOP_MODULATE ); g_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE ); g_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG2, D3DTA_DIFFUSE ); g_pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1 ); g_pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE ); g_pd3dDevice->SetSamplerState( 0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR ); g_pd3dDevice->SetSamplerState( 0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR ); g_pd3dDevice->SetSamplerState( 0, D3DSAMP_MIPFILTER, D3DTEXF_LINEAR ); g_pd3dDevice->SetSamplerState( 0, D3DSAMP_ADDRESSU, D3DTADDRESS_WRAP ); g_pd3dDevice->SetSamplerState( 0, D3DSAMP_ADDRESSV, D3DTADDRESS_WRAP ); // Set material g_Material.Diffuse = D3DXCOLOR(1.0f, 0.0f, 0.0f, 1.0f) ; // white g_Material.Ambient = D3DXCOLOR(1.0f, 0.0f, 0.0f, 1.0f) ; // white g_Material.Specular = D3DXCOLOR(1.0f, 0.0f, 0.0f, 1.0f) ; // white // Set light g_Light.Type = D3DLIGHT_DIRECTIONAL ; g_Light.Direction = D3DXVECTOR3(1.0f, 0, 0) ; g_Light.Diffuse = D3DXCOLOR(1.0f, 1.0f, 1.0f, 1.0f) ; // white g_Light.Ambient = D3DXCOLOR(1.0f, 1.0f, 1.0f, 1.0f) ; // white g_Light.Specular = D3DXCOLOR(1.0f, 1.0f, 1.0f, 1.0f) ; // white g_Light.Range = 1000.0f ; return S_OK; }