void cleanup() // it's a dirty job.. but some function has to do it... { if (myRect1) delete myRect1; if (myRect2) delete myRect2; if (myRect3) delete myRect3; if (myRect4) delete myRect4; if (myRect5) delete myRect5; if( lpD3DDevice8 != NULL ) lpD3DDevice8->Release(); if( lpD3D8 != NULL ) lpD3D8->Release(); if( lpD3DXFont != NULL ) lpD3DXFont->Release(); if( lpD3DTex1 != NULL ) lpD3DTex1->Release(); }
//----------------------------------------------------------------------------- // Name: ApplyEnvironmentMap() // Desc: Performs a calculation on each of the vertices' normals to determine // what the texture coordinates should be for the environment map (in this // case the bump map). //----------------------------------------------------------------------------- VOID CMyD3DApplication::ApplyEnvironmentMap() { EMBOSSVERTEX* pv; DWORD dwNumVertices; dwNumVertices = m_pObject->GetLocalMesh()->GetNumVertices(); LPDIRECT3DVERTEXBUFFER8 pVB; m_pObject->GetLocalMesh()->GetVertexBuffer( &pVB ); pVB->Lock( 0, 0, (BYTE**)&pv, 0 ); // Get the World matrix D3DXMATRIX WV,InvWV; m_pd3dDevice->GetTransform( D3DTS_WORLD, &WV ); D3DXMatrixInverse( &InvWV, NULL, &WV ); // Get the current light position in object space D3DXVECTOR4 vTransformed; D3DXVec3Transform( &vTransformed, (D3DXVECTOR3*)&m_Light.Position, &InvWV ); m_vBumpLightPos.x = vTransformed.x; m_vBumpLightPos.y = vTransformed.y; m_vBumpLightPos.z = vTransformed.z; // Dimensions of texture needed for shifting tex coords D3DSURFACE_DESC d3dsd; m_pEmbossTexture->GetLevelDesc( 0, &d3dsd ); // Loop through the vertices, transforming each one and calculating // the correct texture coordinates. for( WORD i = 0; i < dwNumVertices; i++ ) { // Find light vector in tangent space D3DXVECTOR3 vLightToVertex; D3DXVec3Normalize( &vLightToVertex, &(m_vBumpLightPos - pv[i].p) ); // Create rotation matrix (rotate into tangent space) FLOAT r = D3DXVec3Dot( &vLightToVertex, &pv[i].n ); if( r < 0.f ) { // Don't shift coordinates when light below surface pv[i].tu2 = pv[i].tu; pv[i].tv2 = pv[i].tv; } else { // Shift coordinates for the emboss effect D3DXVECTOR2 vEmbossShift; vEmbossShift.x = D3DXVec3Dot( &vLightToVertex, &m_pTangents[i] ); vEmbossShift.y = D3DXVec3Dot( &vLightToVertex, &m_pBinormals[i] ); D3DXVec2Normalize( &vEmbossShift, &vEmbossShift ); pv[i].tu2 = pv[i].tu + vEmbossShift.x/d3dsd.Width; pv[i].tv2 = pv[i].tv - vEmbossShift.y/d3dsd.Height; } } pVB->Unlock(); pVB->Release(); }
//----------------------------------------------------------------------------- // Name: CreateBumpmap() // Desc: Create a bump map texture and fill its content to BUMPDUDV format //----------------------------------------------------------------------------- HRESULT CMyD3DApplication::CreateBumpMap( UINT iWidth, UINT iHeight ) { // Create the bumpmap's surface and texture objects if( FAILED( m_pd3dDevice->CreateTexture( iWidth, iHeight, 1, 0, D3DFMT_V8U8, D3DPOOL_MANAGED, &m_pBumpMapTexture ) ) ) { return E_FAIL; } // Fill the bumpmap texels to simulate a lens D3DLOCKED_RECT d3dlr; m_pBumpMapTexture->LockRect( 0, &d3dlr, 0, 0 ); DWORD dwDstPitch = (DWORD)d3dlr.Pitch; BYTE* pDst = (BYTE*)d3dlr.pBits; UINT mid = iWidth/2; for( DWORD y0 = 0; y0 < iHeight; y0++ ) { CHAR* pDst = (CHAR*)d3dlr.pBits + y0*d3dlr.Pitch; for( DWORD x0 = 0; x0 < iWidth; x0++ ) { DWORD x1 = ( (x0==iWidth-1) ? x0 : x0+1 ); DWORD y1 = ( (x0==iHeight-1) ? y0 : y0+1 ); FLOAT fDistSq00 = (FLOAT)( (x0-mid)*(x0-mid) + (y0-mid)*(y0-mid) ); FLOAT fDistSq01 = (FLOAT)( (x1-mid)*(x1-mid) + (y0-mid)*(y0-mid) ); FLOAT fDistSq10 = (FLOAT)( (x0-mid)*(x0-mid) + (y1-mid)*(y1-mid) ); FLOAT v00 = ( fDistSq00 > (mid*mid) ) ? 0.0f : sqrtf( (mid*mid) - fDistSq00 ); FLOAT v01 = ( fDistSq01 > (mid*mid) ) ? 0.0f : sqrtf( (mid*mid) - fDistSq01 ); FLOAT v10 = ( fDistSq10 > (mid*mid) ) ? 0.0f : sqrtf( (mid*mid) - fDistSq10 ); FLOAT iDu = (128/D3DX_PI)*atanf(v00-v01); // The delta-u bump value FLOAT iDv = (128/D3DX_PI)*atanf(v00-v10); // The delta-v bump value *pDst++ = (CHAR)(iDu); *pDst++ = (CHAR)(iDv); } } m_pBumpMapTexture->UnlockRect(0); return S_OK; }
// Loads a font texture and sets it as current font to display BOOL D3D_LoadFontTextureByFile ( char *file_path ) { D3DSURFACE_DESC desc; // Free our old texture if (m_pFontTexture) m_pFontTexture->Release(); // Try to load it if ( !(D3D_OK == D3DXCreateTextureFromFile ( m_pOrigDDevice, file_path, &m_pFontTexture )) ) return FALSE; m_pFontTexture->GetLevelDesc( 0, &desc ); m_uiFontWidth = desc.Width; m_uiFontHeight = desc.Height; return TRUE; }
// XBMC tells us to stop the screensaver // we should free any memory and release // any resources we have created. extern "C" void Stop() { delete world.waterField; world.waterField = NULL; for (int i = 0; effects[i] != NULL; i++) delete effects[i]; if (g_Texture != NULL) { g_Texture->Release(); g_Texture = NULL; } return; }
bool CN3Texture::Convert(D3DFORMAT Format, int nWidth, int nHeight, BOOL bGenerateMipMap) { if(m_lpTexture == NULL) return false; D3DSURFACE_DESC dsd; m_lpTexture->GetLevelDesc(0, &dsd); if(0 >= nWidth || 0 >= nHeight) { nWidth = dsd.Width; nHeight = dsd.Height; } LPDIRECT3DTEXTURE8 lpTexOld = m_lpTexture; m_lpTexture = NULL; if(this->Create(nWidth, nHeight, Format, bGenerateMipMap) == false) return false; if(bGenerateMipMap) { LPDIRECT3DSURFACE8 lpTSOld; lpTexOld->GetSurfaceLevel(0, &lpTSOld); this->GenerateMipMap(lpTSOld); // MipMap 생성 lpTSOld->Release(); } else { LPDIRECT3DSURFACE8 lpTSNew; LPDIRECT3DSURFACE8 lpTSOld; m_lpTexture->GetSurfaceLevel(0, &lpTSNew); lpTexOld->GetSurfaceLevel(0, &lpTSOld); D3DXLoadSurfaceFromSurface(lpTSNew, NULL, NULL, lpTSOld, NULL, NULL, D3DX_FILTER_NONE, 0); // 첫번재 레벨 서피스 복사. lpTSOld->Release(); lpTSNew->Release(); } lpTexOld->Release(); lpTexOld = NULL; return true; }
//----------------------------------------------------------------------------- // Name: Cleanup() // Desc: Releases all previously initialized objects //----------------------------------------------------------------------------- VOID Cleanup() { if( g_pTexture != NULL ) g_pTexture->Release(); if( g_pVB != NULL ) g_pVB->Release(); if( g_pd3dDevice != NULL ) g_pd3dDevice->Release(); if( g_pD3D != NULL ) g_pD3D->Release(); }
void CTextureMap::Dump() const { if (IsEmpty()) return ; CStdString strLog; strLog.Format(" texure:%s has %i frames\n", m_strTextureName.c_str(), m_vecTexures.size()); OutputDebugString(strLog.c_str()); for (int i = 0; i < (int)m_vecTexures.size(); ++i) { const CTexture* pTexture = m_vecTexures[i]; strLog.Format(" item:%i ", i); OutputDebugString(strLog.c_str()); pTexture->Dump(); } }
void LoadTexture() { // Setup our texture //long hFile; int numTextures = 0; static char szSearchPath[512]; static char szPath[512]; static char foundTexture[1024]; strcpy(szPath,world.szTextureSearchPath); if (world.szTextureSearchPath[strlen(world.szTextureSearchPath) - 1] != '\\') strcat(szPath,"\\"); strcpy(szSearchPath,szPath); strcat(szSearchPath,"*"); WIN32_FIND_DATA fd; HANDLE hFind = FindFirstFile( szSearchPath, &fd); if(hFind != INVALID_HANDLE_VALUE) { do { int len = (int)strlen(fd.cFileName); if (len < 4 || (strcmpi(fd.cFileName + len - 4, ".txt") == 0)) continue; if (rand() % (numTextures+1) == 0) // after n textures each has 1/n prob { strcpy(foundTexture,szPath); strcat(foundTexture,fd.cFileName); } numTextures++; } while( FindNextFile( hFind, &fd)); FindClose( hFind); } if (g_Texture != NULL && numTextures > 0) { g_Texture->Release(); g_Texture = NULL; } if (numTextures > 0) D3DXCreateTextureFromFileA(g_pd3dDevice, foundTexture, &g_Texture); }
int CGUITextureManager::Load(const CStdString& strTextureName, DWORD dwColorKey, bool checkBundleOnly /*= false */) { CStdString strPath; int bundle = -1; int size = 0; if (!HasTexture(strTextureName, &strPath, &bundle, &size)) return 0; if (size) // we found the texture return size; // See if texture is being overridden. CStdString strTextureFile = strTextureName; CStdString strTextureOverridePath1; CStdString strTextureOverridePath2; CStdString strExt = CUtil::GetExtension(strTextureFile); CUtil::RemoveExtension(strTextureFile); // Check original format and JPEG. strTextureOverridePath1.Format("%s/media/%s%s", _P("Q:"), strTextureFile.c_str(), strExt.c_str()); strTextureOverridePath2.Format("%s/media/%s.jpg", _P("Q:"), strTextureFile.c_str(), strExt.c_str()); if (checkBundleOnly && bundle == -1) return 0; if (XFILE::CFile::Exists(strTextureOverridePath1)) { strPath = strTextureOverridePath1; bundle = -1; } else if (XFILE::CFile::Exists(strTextureOverridePath2)) { strPath = strTextureOverridePath2; bundle = -1; } else if (bundle == -1) strPath = GetTexturePath(strTextureName); else strPath = strTextureName; //Lock here, we will do stuff that could break rendering CSingleLock lock(g_graphicsContext); #ifndef HAS_SDL LPDIRECT3DTEXTURE8 pTexture; LPDIRECT3DPALETTE8 pPal = 0; #else SDL_Surface* pTexture; SDL_Palette* pPal = NULL; #endif #ifdef _DEBUG LARGE_INTEGER start; QueryPerformanceCounter(&start); #endif D3DXIMAGE_INFO info; if (strPath.Right(4).ToLower() == ".gif") { CTextureMap* pMap; if (bundle >= 0) { #ifndef HAS_SDL LPDIRECT3DTEXTURE8* pTextures; #else SDL_Surface** pTextures; #endif int nLoops = 0; int* Delay; #ifndef HAS_SDL int nImages = m_TexBundle[bundle].LoadAnim(g_graphicsContext.Get3DDevice(), strTextureName, &info, &pTextures, &pPal, nLoops, &Delay); #else int nImages = m_TexBundle[bundle].LoadAnim(strTextureName, &info, &pTextures, &pPal, nLoops, &Delay); #endif if (!nImages) { CLog::Log(LOGERROR, "Texture manager unable to load bundled file: %s", strTextureName.c_str()); return 0; } pMap = new CTextureMap(strTextureName); for (int iImage = 0; iImage < nImages; ++iImage) { CTexture* pclsTexture = new CTexture(pTextures[iImage], info.Width, info.Height, true, 100, pPal); pclsTexture->SetDelay(Delay[iImage]); pclsTexture->SetLoops(nLoops); pMap->Add(pclsTexture); #ifndef HAS_SDL delete pTextures[iImage]; #else SDL_FreeSurface(pTextures[iImage]); #endif } delete [] pTextures; delete [] Delay; } else { CAnimatedGifSet AnimatedGifSet; int iImages = AnimatedGifSet.LoadGIF(strPath.c_str()); if (iImages == 0) { if (!strnicmp(strPath.c_str(), "q:\\skin", 7)) CLog::Log(LOGERROR, "Texture manager unable to load file: %s", strPath.c_str()); return 0; } int iWidth = AnimatedGifSet.FrameWidth; int iHeight = AnimatedGifSet.FrameHeight; int iPaletteSize = (1 << AnimatedGifSet.m_vecimg[0]->BPP); pMap = new CTextureMap(strTextureName); for (int iImage = 0; iImage < iImages; iImage++) { int w = iWidth; int h = iHeight; #if defined(HAS_SDL) pTexture = SDL_CreateRGBSurface(SDL_HWSURFACE, w, h, 32, 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000); if (pTexture) #else if (D3DXCreateTexture(g_graphicsContext.Get3DDevice(), w, h, 1, 0, D3DFMT_LIN_A8R8G8B8, D3DPOOL_MANAGED, &pTexture) == D3D_OK) #endif { CAnimatedGif* pImage = AnimatedGifSet.m_vecimg[iImage]; #ifndef HAS_SDL D3DLOCKED_RECT lr; RECT rc = { 0, 0, pImage->Width, pImage->Height }; if ( D3D_OK == pTexture->LockRect( 0, &lr, &rc, 0 )) #else if (SDL_LockSurface(pTexture) != -1) #endif { COLOR *palette = AnimatedGifSet.m_vecimg[0]->Palette; // set the alpha values to fully opaque for (int i = 0; i < iPaletteSize; i++) palette[i].x = 0xff; // and set the transparent colour if (AnimatedGifSet.m_vecimg[0]->Transparency && AnimatedGifSet.m_vecimg[0]->Transparent >= 0) palette[AnimatedGifSet.m_vecimg[0]->Transparent].x = 0; #ifdef HAS_SDL // Allocate memory for the actual pixels in the surface and set the surface BYTE* pixels = (BYTE*) malloc(w * h * 4); pTexture->pixels = pixels; #endif for (int y = 0; y < pImage->Height; y++) { #ifndef HAS_SDL BYTE *dest = (BYTE *)lr.pBits + y * lr.Pitch; #else BYTE *dest = (BYTE *)pixels + (y * w * 4); #endif BYTE *source = (BYTE *)pImage->Raster + y * pImage->BytesPerRow; for (int x = 0; x < pImage->Width; x++) { COLOR col = palette[*source++]; *dest++ = col.b; *dest++ = col.g; *dest++ = col.r; *dest++ = col.x; } } #ifndef HAS_SDL pTexture->UnlockRect( 0 ); #else SDL_UnlockSurface(pTexture); #endif CTexture* pclsTexture = new CTexture(pTexture, iWidth, iHeight, false, 100, pPal); pclsTexture->SetDelay(pImage->Delay); pclsTexture->SetLoops(AnimatedGifSet.nLoops); #ifdef HAS_SDL free(pixels); #endif #ifdef HAS_SDL_2D SDL_FreeSurface(pTexture); #endif pMap->Add(pclsTexture); } } } // of for (int iImage=0; iImage < iImages; iImage++) } #ifdef _DEBUG LARGE_INTEGER end, freq; QueryPerformanceCounter(&end); QueryPerformanceFrequency(&freq); char temp[200]; sprintf(temp, "Load %s: %.1fms%s\n", strPath.c_str(), 1000.f * (end.QuadPart - start.QuadPart) / freq.QuadPart, (bundle >= 0) ? " (bundled)" : ""); OutputDebugString(temp); #endif m_vecTextures.push_back(pMap); return pMap->size(); } // of if (strPath.Right(4).ToLower()==".gif") if (bundle >= 0) { #ifndef HAS_SDL if (FAILED(m_TexBundle[bundle].LoadTexture(g_graphicsContext.Get3DDevice(), strTextureName, &info, &pTexture, &pPal))) #else if (FAILED(m_TexBundle[bundle].LoadTexture(strTextureName, &info, &pTexture, &pPal))) #endif { CLog::Log(LOGERROR, "Texture manager unable to load bundled file: %s", strTextureName.c_str()); return 0; } } else { // normal picture // convert from utf8 CStdString texturePath; g_charsetConverter.utf8ToStringCharset(_P(strPath), texturePath); #ifndef HAS_SDL if ( D3DXCreateTextureFromFileEx(g_graphicsContext.Get3DDevice(), texturePath.c_str(), D3DX_DEFAULT, D3DX_DEFAULT, 1, 0, D3DFMT_LIN_A8R8G8B8, D3DPOOL_MANAGED, D3DX_FILTER_NONE , D3DX_FILTER_NONE, dwColorKey, &info, NULL, &pTexture) != D3D_OK) { if (!strnicmp(strPath.c_str(), "q:\\skin", 7)) CLog::Log(LOGERROR, "Texture manager unable to load file: %s", strPath.c_str()); return 0; } #else SDL_Surface *original = IMG_Load(texturePath.c_str()); CPicture pic; if (!original && !(original = pic.Load(texturePath, MAX_PICTURE_WIDTH, MAX_PICTURE_HEIGHT))) { CLog::Log(LOGERROR, "Texture manager unable to load file: %s", strPath.c_str()); return 0; } // make sure the texture format is correct SDL_PixelFormat format; format.palette = 0; format.colorkey = 0; format.alpha = 0; format.BitsPerPixel = 32; format.BytesPerPixel = 4; format.Amask = 0xff000000; format.Ashift = 24; format.Rmask = 0x00ff0000; format.Rshift = 16; format.Gmask = 0x0000ff00; format.Gshift = 8; format.Bmask = 0x000000ff; format.Bshift = 0; #ifdef HAS_SDL_OPENGL pTexture = SDL_ConvertSurface(original, &format, SDL_SWSURFACE); #else pTexture = SDL_ConvertSurface(original, &format, SDL_HWSURFACE); #endif SDL_FreeSurface(original); if (!pTexture) { CLog::Log(LOGERROR, "Texture manager unable to load file: %s", strPath.c_str()); return 0; } info.Width = pTexture->w; info.Height = pTexture->h; #endif } CTextureMap* pMap = new CTextureMap(strTextureName); CTexture* pclsTexture = new CTexture(pTexture, info.Width, info.Height, bundle >= 0, 100, pPal); pMap->Add(pclsTexture); m_vecTextures.push_back(pMap); #ifdef HAS_SDL_OPENGL SDL_FreeSurface(pTexture); #endif #ifdef _DEBUG LARGE_INTEGER end, freq; QueryPerformanceCounter(&end); QueryPerformanceFrequency(&freq); char temp[200]; sprintf(temp, "Load %s: %.1fms%s\n", strPath.c_str(), 1000.f * (end.QuadPart - start.QuadPart) / freq.QuadPart, (bundle >= 0) ? " (bundled)" : ""); OutputDebugString(temp); #endif return 1; }
bool CN3Texture::GenerateMipMap(LPDIRECT3DSURFACE8 lpSurfSrc) { if(m_lpTexture == NULL) return false; // MipMap 이 몇개 필요한지 계산.. int nMMC = m_lpTexture->GetLevelCount(); int nMMC2 = 0; for(int nW = m_Header.nWidth, nH = m_Header.nHeight; nW >=4 && nH >= 4; nW /=2, nH /= 2) nMMC2++; bool bNeedReleaseSurf = false; if(NULL == lpSurfSrc) { bNeedReleaseSurf = true; if(D3D_OK != m_lpTexture->GetSurfaceLevel(0, &lpSurfSrc)) return false; } HRESULT rval = D3D_OK; if(nMMC < nMMC2) // 적으면 새로 생성.. { LPDIRECT3DTEXTURE8 lpTexOld = m_lpTexture; m_lpTexture = NULL; rval = this->CreateFromSurface(lpSurfSrc, m_Header.Format, TRUE); if(bNeedReleaseSurf) { lpSurfSrc->Release(); lpSurfSrc = NULL; } lpTexOld->Release(); lpTexOld = NULL; if(D3D_OK == rval) { m_Header.bMipMap = TRUE; return true; } else { m_Header.bMipMap = FALSE; return FALSE; } } else // MipMap 이 있으면 그냥 표면만 복사 { if(false == bNeedReleaseSurf) // 다른 서피스에서 복사해야 되는 거면 0 레벨도 복사.. { LPDIRECT3DSURFACE8 lpSurfDest; m_lpTexture->GetSurfaceLevel(0, &lpSurfDest); DWORD dwFilter = D3DX_FILTER_TRIANGLE; // 기본 필터는 없다.. HRESULT rval = D3DXLoadSurfaceFromSurface(lpSurfDest, NULL, NULL, lpSurfSrc, NULL, NULL, dwFilter, 0); // 작은 맵 체인에 서피스 이미지 축소 복사 lpSurfDest->Release(); lpSurfDest = NULL; } for(int i = 1; i < nMMC2; i++) { LPDIRECT3DSURFACE8 lpSurfDest, lpSurfUp; m_lpTexture->GetSurfaceLevel(i-1, &lpSurfUp); m_lpTexture->GetSurfaceLevel(i, &lpSurfDest); DWORD dwFilter = D3DX_FILTER_TRIANGLE; // 기본 필터는 없다.. HRESULT rval = D3DXLoadSurfaceFromSurface(lpSurfDest, NULL, NULL, lpSurfUp, NULL, NULL, dwFilter, 0); // 작은 맵 체인에 서피스 이미지 축소 복사 lpSurfDest->Release(); lpSurfUp->Release(); } if(bNeedReleaseSurf) { lpSurfSrc->Release(); lpSurfSrc = NULL; } if(D3D_OK == rval) { m_Header.bMipMap = TRUE; return true; } else { m_Header.bMipMap = FALSE; return FALSE; } } }
void KRipper8::Dump_TextureDesc2Log( IDirect3DBaseTexture8* pTexture ){ if( !pTexture ) return; D3DRESOURCETYPE Type = pTexture->GetType(); g_pLog->Write( L"-----Texture desc-----\n" ); g_pLog->Write( L"Level count: %u\n", pTexture->GetLevelCount() ); g_pLog->Write( L"Type: %s\n", D3DRESOURCETYPE_2Str( pTexture->GetType() ) ); g_pLog->Write( L"----------------------\n" ); HRESULT hr; if( Type == D3DRTYPE_TEXTURE ) { LPDIRECT3DTEXTURE8 pTex = static_cast <LPDIRECT3DTEXTURE8>( pTexture ); D3DSURFACE_DESC desc; ZeroMemory( &desc, sizeof( desc ) ); hr = pTex->GetLevelDesc( 0, &desc ); if( SUCCEEDED( hr ) ) { g_pLog->Write( L"Format: %s\n", D3DFORMAT_2Str( desc.Format ) ); g_pLog->Write( L"Type : %s\n", D3DRESOURCETYPE_2Str( desc.Type ) ); g_pLog->Write( L"Usage : %s\n", D3DUSAGE_2Str( desc.Usage ) ); g_pLog->Write( L"Pool : %s\n", D3DPOOL_2Str( desc.Pool ) ); g_pLog->Write( L"Size : %u\n", desc.Size ); g_pLog->Write( L"MultiSampleType: %s\n", D3DMULTISAMPLE_2Str( desc.MultiSampleType ) ); g_pLog->Write( L"Width : %d\n", desc.Width ); g_pLog->Write( L"Height: %d\n", desc.Height ); } else { g_pLog->Write( L"IDirect3DTexture8::GetLevelDesc() error: 0x%08X\n", hr ); } } else if( Type == D3DRTYPE_VOLUMETEXTURE ) {//VOLUME LPDIRECT3DVOLUMETEXTURE8 pTex = static_cast <LPDIRECT3DVOLUMETEXTURE8>( pTexture ); D3DVOLUME_DESC desc; ZeroMemory( &desc, sizeof( desc ) ); hr = pTex->GetLevelDesc( 0, &desc ); if( SUCCEEDED( hr ) ){ g_pLog->Write( L"Format: %s\n", D3DFORMAT_2Str( desc.Format ) ); g_pLog->Write( L"Type : %s\n", D3DRESOURCETYPE_2Str( desc.Type ) ); g_pLog->Write( L"Usage : %s\n", D3DUSAGE_2Str( desc.Usage ) ); g_pLog->Write( L"Pool : %s\n", D3DPOOL_2Str( desc.Pool ) ); g_pLog->Write( L"Size : %u\n", desc.Size ); g_pLog->Write( L"Width : %d\n", desc.Width ); g_pLog->Write( L"Height: %d\n", desc.Height ); g_pLog->Write( L"Depth : %d\n", desc.Depth ); } else{ g_pLog->Write( L"IDirect3DVolumeTexture8::GetLevelDesc() error: 0x%08X\n", hr ); } } else if( Type == D3DRTYPE_CUBETEXTURE ) {//CUBE LPDIRECT3DCUBETEXTURE8 pTex = static_cast <LPDIRECT3DCUBETEXTURE8>( pTexture ); D3DSURFACE_DESC desc; ZeroMemory( &desc, sizeof( desc ) ); hr = pTex->GetLevelDesc( 0, &desc ); if( SUCCEEDED( hr ) ) { g_pLog->Write( L"Format: %s\n", D3DFORMAT_2Str( desc.Format ) ); g_pLog->Write( L"Type : %s\n", D3DRESOURCETYPE_2Str( desc.Type ) ); g_pLog->Write( L"Usage : %s\n", D3DUSAGE_2Str( desc.Usage ) ); g_pLog->Write( L"Pool : %s\n", D3DPOOL_2Str( desc.Pool ) ); g_pLog->Write( L"Size : %u\n", desc.Size ); g_pLog->Write( L"MultiSampleType: %s\n", D3DMULTISAMPLE_2Str( desc.MultiSampleType ) ); g_pLog->Write( L"Width : %d\n", desc.Width ); g_pLog->Write( L"Height: %d\n", desc.Height ); } else { g_pLog->Write( L"IDirect3DCubeTexture8::GetLevelDesc() error: 0x%08X\n", hr ); } } g_pLog->Write( L"----------------------\n" ); }
Filename_notice *prepare_filename_notice(LPDIRECT3DDEVICE8 pd3dDevice, char const *filename) { HRESULT result; Filename_notice *notice = NULL; HDC hDC = NULL; HFONT hFont = NULL; SIZE size; int width, height; LPDIRECT3DTEXTURE8 pTexture = NULL; LPDIRECT3DVERTEXBUFFER8 pVB = NULL; DWORD *pBitmapBits = NULL; BITMAPINFO bmi; HBITMAP hbmBitmap = NULL; D3DLOCKED_RECT d3dlr; BYTE *pDstRow; int x, y; hDC = CreateCompatibleDC(NULL); SetMapMode(hDC, MM_TEXT); hFont = CreateFont( FONT_HEIGHT, // height 0, // width (0 = closest) 0, // escapement (0 = none) 0, // orientation (0 = none) FW_NORMAL, // bold FALSE, // italic FALSE, // underline FALSE, // strikeout DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, // TrueType (OUT_TT_PRECIS) doesn't help CLIP_DEFAULT_PRECIS, ANTIALIASED_QUALITY, VARIABLE_PITCH, FONT_NAME); if (hFont == NULL) { goto done; } // Set text properties SelectObject(hDC, hFont); SetTextColor(hDC, RGB(255,255,255)); SetBkColor(hDC, 0x00000000); SetTextAlign(hDC, TA_TOP); GetTextExtentPoint32(hDC, filename, strlen(filename), &size); width = size.cx; height = size.cy; // Create a new texture for the font result = pd3dDevice->CreateTexture(width, height, 1, 0, D3DFMT_A4R4G4B4, D3DPOOL_MANAGED, &pTexture); if (FAILED(result)) { goto done; } // Prepare to create a bitmap ZeroMemory(&bmi.bmiHeader, sizeof(BITMAPINFOHEADER)); bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER); bmi.bmiHeader.biWidth = width; bmi.bmiHeader.biHeight = -height; // negative means top-down bmi.bmiHeader.biPlanes = 1; bmi.bmiHeader.biCompression = BI_RGB; bmi.bmiHeader.biBitCount = 32; hbmBitmap = CreateDIBSection(hDC, &bmi, DIB_RGB_COLORS, (VOID**)&pBitmapBits, NULL, 0); SelectObject(hDC, hbmBitmap); ExtTextOut(hDC, 0, 0, ETO_OPAQUE, NULL, filename, strlen(filename), NULL); // Lock the surface and write the alpha values for the set pixels pTexture->LockRect(0, &d3dlr, 0, 0); pDstRow = (BYTE*)d3dlr.pBits; for (y = 0; y < height; y++) { WORD *pDst16 = (WORD *)pDstRow; for (x = 0; x < width; x++) { BYTE bAlpha = (BYTE)((pBitmapBits[width*y + x] & 0xff) >> 4); if (bAlpha > 0) { *pDst16++ = (WORD)((bAlpha << 12) | 0x0fff); } else { *pDst16++ = (WORD)(0x0000); } } pDstRow += d3dlr.Pitch; } // Done updating texture pTexture->UnlockRect(0); // Create vertices result = pd3dDevice->CreateVertexBuffer(4*sizeof(FONT2DVERTEX), D3DUSAGE_WRITEONLY | D3DUSAGE_DYNAMIC, 0, D3DPOOL_DEFAULT, &pVB); if (FAILED(result)) { goto done; } notice = new Filename_notice; notice->width = width; notice->height = height; notice->pd3dDevice = pd3dDevice; notice->pTexture = pTexture; pTexture = NULL; notice->pVB = pVB; pVB = NULL; pd3dDevice->BeginStateBlock(); apply_render_state(pd3dDevice); pd3dDevice->SetTexture(0, notice->pTexture); pd3dDevice->EndStateBlock(¬ice->dwSavedStateBlock); pd3dDevice->BeginStateBlock(); apply_render_state(pd3dDevice); pd3dDevice->SetTexture(0, notice->pTexture); pd3dDevice->EndStateBlock(¬ice->dwDrawTextStateBlock); done: if (pVB != NULL) { pVB->Release(); } if (pTexture != NULL) { pTexture->Release(); } if (hbmBitmap != NULL) { DeleteObject(hbmBitmap); } if (hFont != NULL) { DeleteObject(hFont); } if (hDC != NULL) { DeleteDC(hDC); } return notice; }
//----------------------------------------------------------------------------- // Name: RestoreDeviceObjects // Desc: //----------------------------------------------------------------------------- HRESULT CMyD3DApplication::RestoreDeviceObjects() { HRESULT hr; // Restore the font m_pFont->RestoreDeviceObjects(); m_pFontSmall->RestoreDeviceObjects(); // Create light D3DLIGHT8 light; ZeroMemory(&light, sizeof(light)); light.Type = D3DLIGHT_DIRECTIONAL; light.Diffuse.r = m_colorLight.r; light.Diffuse.g = m_colorLight.g; light.Diffuse.b = m_colorLight.b; light.Diffuse.a = m_colorLight.a; light.Specular.r = 1.0f; light.Specular.g = 1.0f; light.Specular.b = 1.0f; light.Specular.a = 0.0f; light.Direction.x = m_vecLight.x; light.Direction.y = m_vecLight.y; light.Direction.z = m_vecLight.z; m_pd3dDevice->SetLight(0, &light); m_pd3dDevice->LightEnable(0, TRUE); // Create material D3DMATERIAL8 material; ZeroMemory(&material, sizeof(material)); material.Diffuse.a = 1.0f; material.Specular.r = 0.5f; material.Specular.g = 0.5f; material.Specular.b = 0.5f; material.Power = 20.0f; m_pd3dDevice->SetMaterial(&material); // Setup render states m_pd3dDevice->SetVertexShader(D3DFVF_XYZ); m_pd3dDevice->SetTransform(D3DTS_VIEW, &m_matView); m_pd3dDevice->SetTransform(D3DTS_WORLD, &m_matIdentity); m_pd3dDevice->SetRenderState(D3DRS_LIGHTING, FALSE); m_pd3dDevice->SetRenderState(D3DRS_SPECULARENABLE, FALSE); m_pd3dDevice->SetRenderState(D3DRS_ZFUNC, D3DCMP_LESSEQUAL); m_pd3dDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_CW); m_pd3dDevice->SetRenderState(D3DRS_SHADEMODE, D3DSHADE_GOURAUD); m_pd3dDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_ONE); m_pd3dDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ONE); m_pd3dDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_DISABLE); m_pd3dDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE); m_pd3dDevice->SetTextureStageState(0, D3DTSS_COLORARG2, D3DTA_CURRENT); m_pd3dDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_DISABLE); m_pd3dDevice->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE); m_pd3dDevice->SetTextureStageState(0, D3DTSS_ALPHAARG2, D3DTA_CURRENT); m_pd3dDevice->SetTextureStageState(0, D3DTSS_MINFILTER, D3DTEXF_LINEAR); m_pd3dDevice->SetTextureStageState(0, D3DTSS_MAGFILTER, D3DTEXF_LINEAR); m_pd3dDevice->SetTextureStageState(0, D3DTSS_MIPFILTER, D3DTEXF_POINT); m_pd3dDevice->SetTextureStageState(0, D3DTSS_TEXCOORDINDEX, 0); m_pd3dDevice->SetTextureStageState(1, D3DTSS_COLOROP, D3DTOP_DISABLE); m_pd3dDevice->SetTextureStageState(1, D3DTSS_COLORARG1, D3DTA_TEXTURE); m_pd3dDevice->SetTextureStageState(1, D3DTSS_COLORARG2, D3DTA_CURRENT); m_pd3dDevice->SetTextureStageState(1, D3DTSS_ALPHAOP, D3DTOP_DISABLE); m_pd3dDevice->SetTextureStageState(1, D3DTSS_ALPHAARG1, D3DTA_TEXTURE); m_pd3dDevice->SetTextureStageState(1, D3DTSS_ALPHAARG2, D3DTA_CURRENT); m_pd3dDevice->SetTextureStageState(1, D3DTSS_MINFILTER, D3DTEXF_LINEAR); m_pd3dDevice->SetTextureStageState(1, D3DTSS_MAGFILTER, D3DTEXF_LINEAR); m_pd3dDevice->SetTextureStageState(1, D3DTSS_MIPFILTER, D3DTEXF_POINT); m_pd3dDevice->SetTextureStageState(1, D3DTSS_TEXCOORDINDEX, 0); // Create caustic texture D3DDISPLAYMODE mode; m_pd3dDevice->GetDisplayMode(&mode); if(FAILED(hr = D3DXCreateTexture(m_pd3dDevice, WATER_CAUSTICS_SIZE, WATER_CAUSTICS_SIZE, 1, D3DUSAGE_RENDERTARGET, mode.Format, D3DPOOL_DEFAULT, &m_pCausticTex)) && FAILED(hr = D3DXCreateTexture(m_pd3dDevice, WATER_CAUSTICS_SIZE, WATER_CAUSTICS_SIZE, 1, 0, mode.Format, D3DPOOL_DEFAULT, &m_pCausticTex))) { return hr; } D3DSURFACE_DESC desc; m_pCausticTex->GetSurfaceLevel(0, &m_pCausticSurf); m_pCausticSurf->GetDesc(&desc); if(FAILED(hr = D3DXCreateRenderToSurface(m_pd3dDevice, desc.Width, desc.Height, desc.Format, FALSE, D3DFMT_UNKNOWN, &m_pRenderToSurface))) { return hr; } // Shader TCHAR sz[512]; DXUtil_FindMediaFile(sz, _T("water.sha")); if(FAILED(hr = D3DXCreateEffectFromFile(m_pd3dDevice, sz, &m_pEffect, NULL))) return hr; m_pEffect->SetMatrix("mID", &m_matIdentity); m_pEffect->SetMatrix("mENV", &m_matIdentity); m_pEffect->SetTexture("tFLR", m_pFloorTex); m_pEffect->SetTexture("tCAU", m_pCausticTex); m_pEffect->SetTexture("tENV", m_pSkyCubeTex); if(FAILED(hr = GetNextTechnique(0, FALSE))) return hr; // Set surfaces if(FAILED(hr = m_Environment.SetSurfaces( m_pSkyTex[D3DCUBEMAP_FACE_NEGATIVE_X], m_pSkyTex[D3DCUBEMAP_FACE_POSITIVE_X], m_pSkyTex[D3DCUBEMAP_FACE_NEGATIVE_Y], m_pSkyTex[D3DCUBEMAP_FACE_POSITIVE_Y], m_pSkyTex[D3DCUBEMAP_FACE_POSITIVE_Z], m_pSkyTex[D3DCUBEMAP_FACE_NEGATIVE_Z]))) { return hr; } // OnResetDevice if(FAILED(hr = m_Water.OnResetDevice())) return hr; if(FAILED(hr = m_Environment.OnResetDevice())) return hr; return S_OK; }