//----------------------------------------------------------------------------- // Name: InitDeviceObjects() // Desc: Initialize scene objects. //----------------------------------------------------------------------------- HRESULT CMyD3DApplication::InitDeviceObjects() { HRESULT hr; m_pFont->InitDeviceObjects( m_pd3dDevice ); ilInit(); iluInit(); ilutInit(); ilutD3D8TexFromFile(m_pd3dDevice, __argv[1], &m_pTexture); //D3DXCreateTextureFromFile(m_pd3dDevice, __argv[1], &m_pTexture); // Create a vertex buffer { if( FAILED( hr = m_pd3dDevice->CreateVertexBuffer( 4*sizeof(VERTEX), D3DUSAGE_WRITEONLY, D3DFVF_VERTEX, D3DPOOL_MANAGED, &m_pVB ) ) ) return hr; VERTEX* pVertices; m_pVB->Lock( 0, 4*sizeof(VERTEX), (BYTE**)&pVertices, 0 ); memcpy( pVertices, g_vVertices, sizeof(VERTEX)*4 ); m_pVB->Unlock(); } return S_OK; }
//----------------------------------------------------------------------------- // Name: InitDeviceObjects() // Desc: Initialize scene objects. //----------------------------------------------------------------------------- HRESULT CMyD3DApplication::InitDeviceObjects() { // Init the font m_pFont->InitDeviceObjects( m_pd3dDevice ); // Load texture map. Note that this is a special textures, which has a // height field stored in the alpha channel if( FAILED( D3DUtil_CreateTexture( m_pd3dDevice, _T("emboss1.tga"), &m_pEmbossTexture, D3DFMT_A8R8G8B8 ) ) ) return D3DAPPERR_MEDIANOTFOUND; // Load geometry if( FAILED( m_pObject->Create( m_pd3dDevice, _T("tiger.x") ) ) ) return D3DAPPERR_MEDIANOTFOUND; // Set attributes for the geometry m_pObject->SetFVF( m_pd3dDevice, D3DFVF_EMBOSSVERTEX ); m_pObject->UseMeshMaterials( FALSE ); // Compute the object's tangents and binormals, whaich are needed for the // emboss-tecnhique's texture-coordinate shifting calculations ComputeTangentsAndBinormals(); return S_OK; }
//----------------------------------------------------------------------------- // Name: InitDeviceObjects() // Desc: Initialize scene objects. //----------------------------------------------------------------------------- HRESULT CMyD3DApplication::InitDeviceObjects() { HRESULT hr = S_OK; LPD3DXMESH pMeshSysMem = NULL; LPD3DXBUFFER pAdjacencyBuffer = NULL; // Initialize the font m_pFont->InitDeviceObjects( m_pd3dDevice ); // check current display setting CheckMenuItem( GetMenu(m_hWnd), ID_OPTIONS_DISPLAY1 + (m_cObjectsPerSide-1), MF_CHECKED ); CheckMenuItem( GetMenu(m_hWnd), IDM_SHOWNONOPTIMIZEDMESH, (!m_bShowStripReordered && !m_bShowVertexCacheOptimized) ? MF_CHECKED : MF_UNCHECKED ); CheckMenuItem( GetMenu(m_hWnd), IDM_SHOWVCACHEOPTIMIZED, m_bShowVertexCacheOptimized ? MF_CHECKED : MF_UNCHECKED ); CheckMenuItem( GetMenu(m_hWnd), IDM_SHOWSTRIPREORDERED, m_bShowStripReordered ? MF_CHECKED : MF_UNCHECKED ); CheckMenuItem( GetMenu(m_hWnd), IDM_SHOWTRILIST, (!m_bShowStrips && !m_bShowSingleStrip) ? MF_CHECKED : MF_UNCHECKED ); CheckMenuItem( GetMenu(m_hWnd), IDM_SHOWONESTRIP, m_bShowSingleStrip ? MF_CHECKED : MF_UNCHECKED ); CheckMenuItem( GetMenu(m_hWnd), IDM_SHOWMANYSTRIPS, m_bShowStrips ? MF_CHECKED : MF_UNCHECKED ); CheckMenuItem( GetMenu(m_hWnd), IDM_DYNAMICVB, (m_dwMemoryOptions == D3DXMESH_DYNAMIC) ? MF_CHECKED : MF_UNCHECKED ); CheckMenuItem( GetMenu(m_hWnd), IDM_FORCE32BYTEVERTEX, m_bForce32ByteFVF ? MF_CHECKED : MF_UNCHECKED ); hr = LoadMeshData(&pMeshSysMem, &pAdjacencyBuffer); if (FAILED(hr)) { // ignore load errors, just draw blank screen if mesh is invalid hr = S_OK; goto End; } hr = OptimizeMeshData(pMeshSysMem, pAdjacencyBuffer, D3DXMESHOPT_ATTRSORT, &m_MeshAttrSorted); if (FAILED(hr)) goto End; hr = OptimizeMeshData(pMeshSysMem, pAdjacencyBuffer, D3DXMESHOPT_STRIPREORDER, &m_MeshStripReordered); if (FAILED(hr)) goto End; hr = OptimizeMeshData(pMeshSysMem, pAdjacencyBuffer, D3DXMESHOPT_VERTEXCACHE, &m_MeshVertexCacheOptimized); if (FAILED(hr)) goto End; End: SAFE_RELEASE( pMeshSysMem ); SAFE_RELEASE( pAdjacencyBuffer ); return hr; }
//----------------------------------------------------------------------------- // Name: InitDeviceObjects() // Desc: Initialize scene objects //----------------------------------------------------------------------------- HRESULT CMyD3DApplication::InitDeviceObjects() { m_pFont->InitDeviceObjects( m_pd3dDevice ); // Load the texture for the background image if( FAILED( D3DUtil_CreateTexture( m_pd3dDevice, _T("Lake.bmp"), &m_pBackgroundTexture ) ) ) return E_FAIL; // Create the bump map texture if( FAILED( CreateBumpMap( 256, 256 ) ) ) return E_FAIL; // Create a square for rendering the background if( FAILED( m_pd3dDevice->CreateVertexBuffer( 4*sizeof(BACKGROUNDVERTEX), D3DUSAGE_WRITEONLY, D3DFVF_BACKGROUNDVERTEX, D3DPOOL_MANAGED, &m_pBackgroundVB ) ) ) return E_FAIL; // Create a square for rendering the lens if( FAILED( m_pd3dDevice->CreateVertexBuffer( 4*sizeof(BUMPVERTEX), D3DUSAGE_WRITEONLY, D3DFVF_BUMPVERTEX, D3DPOOL_MANAGED, &m_pLensVB ) ) ) return E_FAIL; BUMPVERTEX* vLens; m_pLensVB->Lock( 0, 0, (BYTE**)&vLens, 0 ); vLens[0].p = D3DXVECTOR3(-256.0f,-256.0f, 0.0f ); vLens[1].p = D3DXVECTOR3(-256.0f, 256.0f, 0.0f ); vLens[2].p = D3DXVECTOR3( 256.0f,-256.0f, 0.0f ); vLens[3].p = D3DXVECTOR3( 256.0f, 256.0f, 0.0f ); vLens[0].tu1 = 0.0f; vLens[0].tv1 = 1.0f; vLens[1].tu1 = 0.0f; vLens[1].tv1 = 0.0f; vLens[2].tu1 = 1.0f; vLens[2].tv1 = 1.0f; vLens[3].tu1 = 1.0f; vLens[3].tv1 = 0.0f; m_pLensVB->Unlock(); m_bDeviceValidationFailed = FALSE; return S_OK; }
//----------------------------------------------------------------------------- // Name: InitDeviceObjects() // Desc: Initialize scene objects. //----------------------------------------------------------------------------- HRESULT CMyD3DApplication::InitDeviceObjects() { // Load the file objects if( FAILED( m_pShinyTeapot->Create( m_pd3dDevice, _T("teapot.x") ) ) ) return D3DAPPERR_MEDIANOTFOUND; if( FAILED( m_pSkyBox->Create( m_pd3dDevice, _T("lobby_skybox.x") ) ) ) return D3DAPPERR_MEDIANOTFOUND; if( FAILED( D3DUtil_CreateTexture( m_pd3dDevice, _T("spheremap.bmp"), &m_pSphereMap ) ) ) return D3DAPPERR_MEDIANOTFOUND; // Set mesh properties m_pShinyTeapot->SetFVF( m_pd3dDevice, D3DFVF_ENVMAPVERTEX ); // Restore the device-dependent objects m_pFont->InitDeviceObjects( m_pd3dDevice ); // Create Effect object if( FAILED( D3DXCreateEffect( m_pd3dDevice, g_szEffect, g_cchEffect, &m_pEffect, NULL ) ) ) return E_FAIL; return S_OK; }
//----------------------------------------------------------------------------- // Name: InitDeviceObjects() // Desc: This creates all device-dependent managed objects, such as managed // textures and managed vertex buffers. //----------------------------------------------------------------------------- HRESULT CMyD3DApplication::InitDeviceObjects() { // Initialize the font's internal textures m_pFont->InitDeviceObjects( m_pd3dDevice ); // Create the tree textures for( DWORD i=0; i<NUMTREETEXTURES; i++ ) { if( FAILED( D3DUtil_CreateTexture( m_pd3dDevice, g_strTreeTextures[i], &m_pTreeTextures[i] ) ) ) return D3DAPPERR_MEDIANOTFOUND; } // Create a quad for rendering each tree if( FAILED( m_pd3dDevice->CreateVertexBuffer( NUM_TREES*4*sizeof(TREEVERTEX), D3DUSAGE_WRITEONLY, D3DFVF_TREEVERTEX, D3DPOOL_MANAGED, &m_pTreeVB ) ) ) { return E_FAIL; } // Copy tree mesh data into vertexbuffer TREEVERTEX* v; m_pTreeVB->Lock( 0, 0, (BYTE**)&v, 0 ); INT iTree; DWORD dwOffset = 0; for( iTree = 0; iTree < NUM_TREES; iTree++ ) { memcpy( &v[dwOffset], m_Trees[iTree].v, 4*sizeof(TREEVERTEX) ); m_Trees[iTree].dwOffset = dwOffset; dwOffset += 4; } m_pTreeVB->Unlock(); // Load the skybox if( FAILED( m_pSkyBox->Create( m_pd3dDevice, _T("SkyBox2.x") ) ) ) return D3DAPPERR_MEDIANOTFOUND; // Load the terrain if( FAILED( m_pTerrain->Create( m_pd3dDevice, _T("SeaFloor.x") ) ) ) return D3DAPPERR_MEDIANOTFOUND; // Add some "hilliness" to the terrain LPDIRECT3DVERTEXBUFFER8 pVB; if( SUCCEEDED( m_pTerrain->GetSysMemMesh()->GetVertexBuffer( &pVB ) ) ) { struct VERTEX { FLOAT x,y,z,tu,tv; }; VERTEX* pVertices; DWORD dwNumVertices = m_pTerrain->GetSysMemMesh()->GetNumVertices(); pVB->Lock( 0, 0, (BYTE**)&pVertices, 0 ); for( DWORD i=0; i<dwNumVertices; i++ ) pVertices[i].y = HeightField( pVertices[i].x, pVertices[i].z ); pVB->Unlock(); pVB->Release(); } return S_OK; }
//----------------------------------------------------------------------------- // Name: InitDeviceObjects // Desc: //----------------------------------------------------------------------------- HRESULT CMyD3DApplication::InitDeviceObjects() { HRESULT hr; TCHAR sz[512]; m_bDrawWater = TRUE; m_bDrawCaustics = TRUE; m_bDrawEnvironment = TRUE; // Initialize the font's internal textures m_pFont->InitDeviceObjects( m_pd3dDevice ); m_pFontSmall->InitDeviceObjects( m_pd3dDevice ); // Floor DXUtil_FindMediaFile(sz, _T("Water.bmp")); D3DXCreateTextureFromFileEx(m_pd3dDevice, sz, D3DX_DEFAULT, D3DX_DEFAULT, D3DX_DEFAULT, 0, D3DFMT_UNKNOWN, D3DPOOL_MANAGED, D3DX_DEFAULT, D3DX_DEFAULT, 0, NULL, NULL, &m_pFloorTex); // Sky TCHAR* szSkyTex[6] = { _T("lobbyxpos.bmp"), _T("lobbyxneg.bmp"), _T("lobbyypos.bmp"), _T("lobbyyneg.bmp"), _T("lobbyzneg.bmp"), _T("lobbyzpos.bmp") }; for(UINT i = 0; i < 6; i++) { DXUtil_FindMediaFile(sz, szSkyTex[i]); D3DXCreateTextureFromFileEx(m_pd3dDevice, sz, D3DX_DEFAULT, D3DX_DEFAULT, 1, 0, D3DFMT_UNKNOWN, D3DPOOL_MANAGED, D3DX_DEFAULT, D3DX_DEFAULT, 0, NULL, NULL, &m_pSkyTex[i]); } if(SUCCEEDED(D3DXCreateCubeTexture(m_pd3dDevice, 128, D3DX_DEFAULT, 0, D3DFMT_UNKNOWN, D3DPOOL_MANAGED, &m_pSkyCubeTex))) { for(UINT i = 0; i < 6; i++) { if(m_pSkyTex[i]) { IDirect3DSurface8 *pSrc; IDirect3DSurface8 *pDest; m_pSkyTex[i]->GetSurfaceLevel(0, &pSrc); m_pSkyCubeTex->GetCubeMapSurface((D3DCUBEMAP_FACES) i, 0, &pDest); if(pSrc && pDest) D3DXLoadSurfaceFromSurface(pDest, NULL, NULL, pSrc, NULL, NULL, D3DX_DEFAULT, 0); SAFE_RELEASE(pDest); SAFE_RELEASE(pSrc); } } D3DXFilterCubeTexture(m_pSkyCubeTex, NULL, 0, D3DX_DEFAULT); } // OnCreateDevice if(FAILED(hr = m_Water.OnCreateDevice(m_pd3dDevice))) return hr; if(FAILED(hr = m_Environment.OnCreateDevice(m_pd3dDevice))) return hr; return S_OK; }