void DevState::blit(unsigned int x, unsigned int y, unsigned int w, unsigned int h) { // Blit is called often. Thus, we do not want to always log here. #ifdef EXTENDED_OVERLAY_DEBUGOUTPUT ods("D3D9: Blit %d %d %d %d", x, y, w, h); #endif if (! texTexture || !a_ucTexture) return; D3DLOCKED_RECT lr; if ((x == 0) && (y == 0) && (w == uiWidth) && (h == uiHeight)) { if (texTexture->LockRect(0, &lr, NULL, D3DLOCK_DISCARD) != D3D_OK) return; } else { RECT r; r.left = x; r.top = y; r.right = x + w; r.bottom = y + h; if (texTexture->LockRect(0, &lr, &r, 0) != D3D_OK) return; } for (unsigned int r=0;r < h;++r) { unsigned char *dptr = reinterpret_cast<unsigned char *>(lr.pBits) + r * lr.Pitch; unsigned char *sptr = a_ucTexture + 4 * ((y + r) * uiWidth + x); memcpy(dptr, sptr, w * 4); } texTexture->UnlockRect(0); }
void DevState::blit(unsigned int x, unsigned int y, unsigned int w, unsigned int h) { ods("D3D9: Blit %d %d %d %d", x, y, w, h); if (! texTexture || !a_ucTexture) return; D3DLOCKED_RECT lr; if ((x == 0) && (y == 0) && (w == uiWidth) && (h == uiHeight)) { if (texTexture->LockRect(0, &lr, NULL, D3DLOCK_DISCARD) != D3D_OK) return; } else { RECT r; r.left = x; r.top = y; r.right = x + w; r.bottom = y + h; if (texTexture->LockRect(0, &lr, &r, 0) != D3D_OK) return; } for (unsigned int r=0;r < h;++r) { unsigned char *dptr = reinterpret_cast<unsigned char *>(lr.pBits) + r * lr.Pitch; unsigned char *sptr = a_ucTexture + 4 * ((y + r) * uiWidth + x); memcpy(dptr, sptr, w * 4); } texTexture->UnlockRect(0); }
IDirect3DTexture9 *CreatTex(LPSTR dat) { LPDIRECT3DTEXTURE9 pTex = NULL; UINT xx,yy; xx = *(UINT*)(dat+4); yy = *(UINT*)(dat+8); D3DLOCKED_RECT rc; HRESULT hr; if (*(DWORD*)(dat+0x28) == 'DXT3') { hr = g_pD3DDevice->CreateTexture(xx,yy,0,0 ,D3DFMT_DXT3,D3DPOOL_MANAGED,&pTex,NULL); if (hr != D3D_OK) return NULL; hr = pTex->LockRect(0,&rc,NULL,0); if (hr == D3D_OK) { CopyMemory(rc.pBits,dat+0x28+12,(xx/4) * (yy/4) * 16 ); pTex->UnlockRect(0); } return pTex; } if (*(DWORD*)(dat+0x28) == 'DXT1') { hr = g_pD3DDevice->CreateTexture(xx,yy,0,0,D3DFMT_DXT1,D3DPOOL_MANAGED,&pTex,NULL); if (hr != D3D_OK) return NULL; hr = pTex->LockRect(0,&rc,NULL,0); if (hr == D3D_OK) { CopyMemory(rc.pBits,dat+0x28+12,(xx/4) * (yy/4) * 8 ); pTex->UnlockRect(0); } return pTex; } hr = g_pD3DDevice->CreateTexture(xx,yy,0,0,D3DFMT_A8R8G8B8,D3DPOOL_MANAGED,&pTex,NULL); if (hr != D3D_OK) return NULL; hr = pTex->LockRect(0,&rc,NULL,0); if (hr == D3D_OK) { for (DWORD jy = 0; jy < yy; ++jy) { for (DWORD jx = 0; jx < xx; ++jx) { DWORD *pp = (DWORD *)rc.pBits; BYTE *idx = (BYTE *)(dat+0x28+0x400); DWORD *pal = (DWORD *)(dat+0x28); pp[(yy-jy-1)*xx+jx] = pal[idx[jy*xx+jx]]; } } pTex->UnlockRect(0); } return pTex; }
bool AlphaMap::init(bool pAlphaCheck, LPDIRECT3DTEXTURE9 pTexture) { d_solid = true; if (!pAlphaCheck) return false; D3DSURFACE_DESC surfaceDesc; pTexture->GetLevelDesc(0, &surfaceDesc); d_width = surfaceDesc.Width; d_height = surfaceDesc.Height; d_mask.resize(d_width * d_height); D3DLOCKED_RECT lockedRect; if (SUCCEEDED(pTexture->LockRect(0, &lockedRect, NULL, D3DLOCK_DISCARD))) { unsigned char* bits = (unsigned char*)lockedRect.pBits; for (int y = 0; y < d_height; ++y) { for (int x = 0; x < d_width; ++x) { bool solid = (bits[x * 4 + y * lockedRect.Pitch + 3] != 0); d_mask[x + y * d_width] = solid; if (!solid) d_solid = false; } } pTexture->UnlockRect(0); } return true; }
lpta::LptaTexture::DATA LptaD3DTextureManager::GenerateDefaultData(void) { LPDIRECT3DTEXTURE9 textureData = nullptr; D3DLOCKED_RECT d3dLockedRect; HRESULT result = d3ddev->CreateTexture( DEFAULT_TEXTURE_WIDTH, DEFAULT_TEXTURE_HEIGHT, 1, 0, D3DFMT_A8R8G8B8, D3DPOOL_MANAGED, &textureData, nullptr ); if (FAILED(result)) { throw TextureD3DFailure("could not obtain texture object for default"); } result = textureData->LockRect(0, &d3dLockedRect, nullptr, 0); if (FAILED(result)) { textureData->Release(); throw TextureD3DFailure("failed to lock rectangle to set default texture color"); } unsigned long *rawData = (unsigned long *)d3dLockedRect.pBits; for (unsigned long *pixel = rawData; pixel < rawData + (DEFAULT_TEXTURE_WIDTH * DEFAULT_TEXTURE_HEIGHT); ++pixel) { *pixel = DEFAULT_TEXTURE_COLOR; } textureData->UnlockRect(0); return (lpta::LptaTexture::DATA)textureData; }
/* Copy bitmap->memory to texture memory */ static void d3d_sync_bitmap_texture(ALLEGRO_BITMAP *bitmap, int x, int y, int width, int height) { D3DLOCKED_RECT locked_rect; RECT rect; ALLEGRO_BITMAP_D3D *d3d_bmp = (ALLEGRO_BITMAP_D3D *)bitmap; LPDIRECT3DTEXTURE9 texture; rect.left = x; rect.top = y; rect.right = x + width; rect.bottom = y + height; if (_al_d3d_render_to_texture_supported()) texture = d3d_bmp->system_texture; else texture = d3d_bmp->video_texture; if (texture->LockRect(0, &locked_rect, &rect, 0) == D3D_OK) { _al_convert_bitmap_data(bitmap->memory, bitmap->format, bitmap->w*al_get_pixel_size(bitmap->format), locked_rect.pBits, bitmap->format, locked_rect.Pitch, x, y, 0, 0, width, height); /* Copy an extra row and column so the texture ends nicely */ if (rect.bottom > bitmap->h) { _al_convert_bitmap_data( bitmap->memory, bitmap->format, bitmap->w*al_get_pixel_size(bitmap->format), locked_rect.pBits, bitmap->format, locked_rect.Pitch, 0, bitmap->h-1, 0, height, width, 1); } if (rect.right > bitmap->w) { _al_convert_bitmap_data( bitmap->memory, bitmap->format, bitmap->w*al_get_pixel_size(bitmap->format), locked_rect.pBits, bitmap->format, locked_rect.Pitch, bitmap->w-1, 0, width, 0, 1, height); } if (rect.bottom > bitmap->h && rect.right > bitmap->w) { _al_convert_bitmap_data( bitmap->memory, bitmap->format, bitmap->w*al_get_pixel_size(bitmap->format), locked_rect.pBits, bitmap->format, locked_rect.Pitch, bitmap->w-1, bitmap->h-1, width, height, 1, 1); } texture->UnlockRect(0); } else { ALLEGRO_ERROR("d3d_sync_bitmap_texture: Couldn't lock texture to upload.\n"); } }
int l_Image_Load( lua_State* L ) { D3DXIMAGE_INFO imgInfo; const char* fileName = lua_tostring( L, 1 ); bool dxtCompression = false; D3DFORMAT d3dfmt = D3DFMT_UNKNOWN; //check for dds format { if( _strcmpi( "dds", fileName + strlen( fileName ) - 3 ) == 0 ) { DDSHEAD header; FILE* f; if(fopen_s( &f, fileName, "rb" ) == 0) { if( fread( &header, sizeof(header), 1, f ) == 1 ) { if( strncmp( (const char*)header.Signature, "DDS ", 4 ) == 0 && ( header.FourCC == D3DFMT_DXT1 || header.FourCC == D3DFMT_DXT3 || header.FourCC == D3DFMT_DXT5 ) ) { d3dfmt = D3DFMT_A8R8G8B8; } } fclose(f); } } } LPDIRECT3DTEXTURE9 texture; HRESULT hr = D3DXCreateTextureFromFileEx( g_pd3dDevice, fileName, D3DX_DEFAULT_NONPOW2, D3DX_DEFAULT_NONPOW2, D3DX_FROM_FILE, 0, d3dfmt, D3DPOOL_SCRATCH, D3DX_DEFAULT, D3DX_DEFAULT, 0, &imgInfo, 0, &texture ); if( FAILED( hr ) ) { return 0; } ImageData* imgData = createImage(); imgData->width = imgInfo.Width; imgData->height = imgInfo.Height; imgData->format = getFormatByD3D( imgInfo.Format ); imgData->imgData = malloc( imgData->width * imgData->height * imgData->format->bytesPerPixel ); D3DLOCKED_RECT rect; texture->LockRect(0, &rect, 0, 0 ); memcpy( imgData->imgData, rect.pBits, imgData->width * imgData->height * imgData->format->bytesPerPixel ); texture->UnlockRect( 0 ); texture->Release(); //lua_pushlightuserdata( L, imgData ); ImageData** luaData = lua_pushimage( L ); *luaData = imgData; return 1; }
LPDIRECT3DTEXTURE9 D3D9::CreateTexture(int texWidth, int texHeight, D3DCOLOR color) { LPDIRECT3DTEXTURE9 pTexture; HRESULT hr = D3DXCreateTexture(d3ddevice_, texWidth, texHeight, 0, 0, D3DFMT_A8R8G8B8, // 4 bytes for a pixel D3DPOOL_MANAGED, &pTexture); if (FAILED(hr)) { MessageBox(NULL, L"Create texture failed", L"Error", 0); } // Lock the texture and fill in color D3DLOCKED_RECT lockedRect; hr = pTexture->LockRect(0, &lockedRect, NULL, 0); if (FAILED(hr)) { MessageBox(NULL, L"Lock texture failed!", L"Error", 0); } DWORD sideColor = 0xff000000; // the side line color int side_width = 10; // Calculate number of rows in the locked Rect int rowCount = (texWidth * texHeight * 4 ) / lockedRect.Pitch; for (int i = 0; i < texWidth; ++i) { for (int j = 0; j < texHeight; ++j) { int index = i * rowCount + j; int* pData = (int*)lockedRect.pBits; if (i <= side_width || i >= texWidth - side_width || j <= side_width || j >= texHeight - side_width) { memcpy(&pData[index], &sideColor, 4); } else { memcpy(&pData[index], &color, 4); } } } pTexture->UnlockRect(0); return pTexture; }
// シーンの描画 void DrawScene(void* data) { #if 1 struct vertex { FLOAT x, y, z, rhw; FLOAT tu, tv; }; vertex v[4] = { {0, 0, 0.0f, 1.0f, 0.0f, 0.0f}, {SCREEN_WIDTH, 0, 0.0f, 1.0f, 1.0f, 0.0f}, {0, SCREEN_HEIGHT, 0.0f, 1.0f, 0.0f, 1.0f}, {SCREEN_WIDTH, SCREEN_HEIGHT, 0.0f, 1.0f, 1.0f, 1.0f}, }; D3DLOCKED_RECT pRect; DWORD time,lasttime; lasttime = timeGetTime(); time = lasttime; while(true) { time = timeGetTime(); if(time-lasttime >= 1000*10) { g_tex->LockRect(0, &pRect, NULL, D3DLOCK_DISCARD); for(int y = 0; y<SCREEN_HEIGHT; y++) { for(int x = 0; x<SCREEN_WIDTH; x++) { DWORD color = 0x00ff00ff; color = (edupt::to_int(image[y*SCREEN_WIDTH+x].x_)<<16) + (edupt::to_int(image[y*SCREEN_WIDTH+x].y_)<<8) + edupt::to_int(image[y*SCREEN_WIDTH+x].z_); memcpy((BYTE*)pRect.pBits+pRect.Pitch*y + 4*x, &color, sizeof(DWORD)); } } g_tex->UnlockRect(0); lasttime = time; } g_pD3DDev->BeginScene(); g_pD3DDev->Clear(0,NULL,D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER,D3DCOLOR_RGBA(128,128,128,0),1.0f,0); g_pD3DDev->SetFVF(D3DFVF_XYZRHW | D3DFVF_TEX1); g_pD3DDev->SetTexture(0, g_tex); g_pD3DDev->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, v, sizeof(vertex)); g_pD3DDev->EndScene(); g_pD3DDev->Present(NULL, NULL, NULL, NULL); } #endif }
static void copy_texture_bits( LPTEXTURE srcT, LPTEXTURE dstT ) { HRESULT hr; D3DLOCKED_RECT srcRect; D3DLOCKED_RECT dstRect; unsigned int y, x; BYTE* srcBits; BYTE* dstBits; D3DSURFACE_DESC srcDesc; LPDIRECT3DTEXTURE9 srcTexture = (LPDIRECT3DTEXTURE9) srcT; LPDIRECT3DTEXTURE9 dstTexture = (LPDIRECT3DTEXTURE9) dstT; srcTexture->GetLevelDesc(0,&srcDesc); hr = srcTexture->LockRect(0,&srcRect,NULL,D3DLOCK_READONLY); if(FAILED(hr)) return; hr = dstTexture->LockRect(0,&dstRect,NULL,D3DLOCK_DISCARD); if(FAILED(hr)) return; srcBits = (BYTE*)srcRect.pBits; dstBits = (BYTE*)dstRect.pBits; for (y = 0; y < srcDesc.Height; y++) { for (x = 0; x < srcDesc.Width; x++) { // move to the correct off set in the table // pitch is the width of a row // (x*size) is the length of each pixel data DWORD srcIndex = (x*4)+(y*srcRect.Pitch); DWORD dstIndex = (x*4)+(y*dstRect.Pitch); // D3DFMT_A8R8G8B8 data will be accessible backwards: bgra dstBits[dstIndex] = (BYTE)gamma_table[srcBits[srcIndex]]; // Blue dstBits[dstIndex+1] = (BYTE)gamma_table[srcBits[srcIndex+1]]; // Green dstBits[dstIndex+2] = (BYTE)gamma_table[srcBits[srcIndex+2]]; // Red dstBits[dstIndex+3] = (BYTE)gamma_table[srcBits[srcIndex+3]]; // Alpha } } dstTexture->UnlockRect(0); srcTexture->UnlockRect(0); }
bool Atlas::createTexure_( Logger & _logger, const MAGIC_CHANGE_ATLAS & c, LPDIRECT3DTEXTURE9 * _texture ) { uint32_t image_width; uint32_t image_height; unsigned char * pixels = nullptr; if( c.data == nullptr ) { pixels = imageLoadFromFile( c.file, image_width, image_height ); } else { pixels = imageLoadFromMemory( c.data, c.length, image_width, image_height ); } unsigned char * correct_pixels = pixels; UINT texture_width = getTexturePOW2( image_width ); UINT texture_height = getTexturePOW2( image_height ); LPDIRECT3DTEXTURE9 texture = NULL; if( m_pDevice->CreateTexture( texture_width, texture_height, 1, 0, D3DFMT_A8R8G8B8, D3DPOOL_MANAGED, &texture, NULL ) != D3D_OK ) { return false; } D3DLOCKED_RECT lockedRect; DXCALL texture->LockRect( 0, &lockedRect, NULL, D3DLOCK_DISCARD ); if( lockedRect.pBits == NULL || lockedRect.Pitch == 0 ) { return false; } for( size_t i = 0; i != image_height; ++i ) { unsigned char * image = correct_pixels + i * image_width * 4; unsigned char * bits = static_cast<unsigned char *>(lockedRect.pBits) + lockedRect.Pitch * i; std::copy( image, image + image_width * 4, bits ); } DXCALL texture->UnlockRect( 0 ); imageFree( pixels ); *_texture = texture; return true; }
/**----------------------------------------------------------------------------- * 정점버퍼를 생성하고 정점값을 채워넣는다. * HeightMap정보도 여기서 초기화한다. *------------------------------------------------------------------------------ */ HRESULT InitVB() { D3DSURFACE_DESC ddsd; D3DLOCKED_RECT d3drc; g_pTexHeight->GetLevelDesc( 0, &ddsd ); // 텍스처의 정보 g_cxHeight = ddsd.Width; // 텍스처의 가로크기 g_czHeight = ddsd.Height; // 텍스처의 세로크기 g_pLog->Log( "Texture Size:[%d,%d]", g_cxHeight, g_czHeight ); g_pvHeightMap = new D3DXVECTOR3[g_cxHeight * g_czHeight]; // 높이맵배열 생성 if ( FAILED( g_pd3dDevice->CreateVertexBuffer( ddsd.Width*ddsd.Height*sizeof( CUSTOMVERTEX ), 0, D3DFVF_CUSTOMVERTEX, D3DPOOL_DEFAULT, &g_pVB, NULL ) ) ) { return E_FAIL; } // 텍스처 메모리 락! g_pTexHeight->LockRect( 0, &d3drc, NULL, D3DLOCK_READONLY ); VOID* pVertices; // 정점버퍼 락! if ( FAILED( g_pVB->Lock( 0, g_cxHeight*g_czHeight*sizeof( CUSTOMVERTEX ), (void**)&pVertices, 0 ) ) ) { return E_FAIL; } CUSTOMVERTEX v; CUSTOMVERTEX* pV = (CUSTOMVERTEX*)pVertices; for ( DWORD z = 0; z < g_czHeight; z++ ) { for ( DWORD x = 0; x < g_cxHeight; x++ ) { v.p.x = (float)x - g_cxHeight / 2.0f; // 정점의 x좌표(메시를 원점에 정렬) v.p.z = -( (float)z - g_czHeight / 2.0f ); // 정점의 z좌표(메시를 원점에 정렬), z축이 모니터안쪽이므로 -를 곱한다. v.p.y = ( (float)( *( (LPDWORD)d3drc.pBits + x + z*( d3drc.Pitch / 4 ) ) & 0x000000ff ) ) / 10.0f; /// DWORD이므로 pitch/4 v.n.x = v.p.x; v.n.y = v.p.y; v.n.z = v.p.z; D3DXVec3Normalize( &v.n, &v.n ); v.t.x = (float)x / ( g_cxHeight - 1 ); v.t.y = (float)z / ( g_czHeight - 1 ); *pV++ = v; // 정점버퍼에 정점 저장 g_pvHeightMap[z * g_cxHeight + x] = v.p; // 높이맵에 정점 저장 } } g_pVB->Unlock(); g_pTexHeight->UnlockRect( 0 ); return S_OK; }
void AmjuGLDX9::SetTexture( AmjuGL::TextureHandle* th, AmjuGL::TextureType tt, AmjuGL::TextureDepth td, int width, int height, uint8* data) { AMJU_CALL_STACK; HRESULT res = D3DXCreateTexture( dd, //LPDIRECT3DDEVICE9 pDevice, width, //UINT Width, height, //UINT Height, 0, // Mip Levels: 0 means create all D3DUSAGE_DYNAMIC, //DWORD Usage, (td == AmjuGL::AMJU_RGB ? D3DFMT_R8G8B8 : D3DFMT_A8R8G8B8), //D3DFORMAT Format, D3DPOOL_DEFAULT, // Pool, reinterpret_cast<LPDIRECT3DTEXTURE9*>(th) //LPDIRECT3DTEXTURE9 * ppTexture ); LPDIRECT3DTEXTURE9 pTex = reinterpret_cast<LPDIRECT3DTEXTURE9>(*th); D3DLOCKED_RECT lockedRect; pTex->LockRect(0, &lockedRect, NULL, 0); switch (td) { case AmjuGL::AMJU_RGB: CopyRGBTexture((uint8*)lockedRect.pBits, lockedRect.Pitch, data, width, height); break; case AmjuGL::AMJU_RGBA: CopyRGBATexture((uint8*)lockedRect.pBits, lockedRect.Pitch, data, width, height); break; } pTex->UnlockRect(0); // TODO Create data for each mipmap level. // NB We want the same functionality as screenshot shrinking. IDirect3DSurface9 * pSurfaceLevel; for (unsigned int iLevel = 0; iLevel < pTex->GetLevelCount(); iLevel++) { pTex->GetSurfaceLevel(iLevel, &pSurfaceLevel); // TODO Write this mip map pSurfaceLevel->Release(); } }
/* CDirect3D::BlankTexture clears a texture (fills it with zeroes) IN: texture - the texture to be blanked ----- returns true if successful, false otherwise */ bool CDirect3D::BlankTexture(LPDIRECT3DTEXTURE9 texture) { D3DLOCKED_RECT lr; HRESULT hr; if(FAILED(hr = texture->LockRect(0, &lr, NULL, 0))) { DXTRACE_ERR_MSGBOX(TEXT("Unable to lock texture"), hr); return false; } else { memset(lr.pBits, 0, lr.Pitch * quadTextureSize); texture->UnlockRect(0); return true; } }
bool CAnimationSpooler::SetColorKey(LPDIRECT3DTEXTURE9 pTexture, LPDIRECT3DSURFACE9 pSurface, int iTexSize, COLORREF clrColorKey) { ASSERT(pTexture); ASSERT(pSurface); if( clrColorKey == CLR_INVALID ) return true; // Get colorkey's red, green, and blue components // and put the colorkey in the texture's native format DWORD r = GetRValue(clrColorKey); DWORD g = GetGValue(clrColorKey); DWORD b = GetBValue(clrColorKey); DWORD dwColorKey = D3DCOLOR_ARGB(255,r,g,b); HRESULT Hr; LPDIRECT3DTEXTURE9 pTex = NULL; Hr = m_p3DDevice->CreateTexture(iTexSize, iTexSize, 1, D3DUSAGE_DYNAMIC, D3DFMT_A8R8G8B8, D3DPOOL_SYSTEMMEM, &pTex, NULL); if( FAILED(Hr) ) return false; CSafeRelease<IDirect3DTexture9> RefTex = pTex; LPDIRECT3DSURFACE9 pTexSurf = NULL; Hr = pTex->GetSurfaceLevel(0, &pTexSurf); if( FAILED(Hr) ) return false; CSafeRelease<IDirect3DSurface9> RefTexSurf = pTexSurf; Hr = m_p3DDevice->GetRenderTargetData(pSurface, pTexSurf); if( FAILED(Hr) ) return false; // Lock the texture and scan through each pixel, replacing the colorkey pixels D3DLOCKED_RECT d3dlr; Hr = pTex->LockRect(0, &d3dlr, 0, 0); if( FAILED(Hr) ) return false; DWORD* pBits = static_cast<DWORD*>(d3dlr.pBits); for( int y = 0; y < iTexSize; y++ ) { for( int x = 0; x < iTexSize; x++ ) { if( pBits[x] == dwColorKey ) pBits[x] = 0x00000000; else pBits[x] |= 0xff000000; } pBits += d3dlr.Pitch / sizeof(DWORD); } pTex->UnlockRect(0); // Copy modified data back POINT pt = { 0, 0 }; RECT rcDest = { 0, 0, iTexSize, iTexSize }; Hr = m_p3DDevice->UpdateSurface(pTexSurf, &rcDest, pSurface, &pt); return true; }
int l_Image_Save( lua_State* L ) { ImageData* imgData = lua_checkimage( L, 1 ); const char* fileName = lua_tostring( L, 2 ); if( !imgData || !fileName ) { lua_pushstring( L, "Wrong parameters to Image_Save functions" ); lua_error( L ); return 0; } LPDIRECT3DTEXTURE9 texture; D3DLOCKED_RECT rect; D3DFORMAT dxFormat = (D3DFORMAT)imgData->format->d3dformat; if( dxFormat == D3DFMT_DXT1 || dxFormat == D3DFMT_DXT3 || dxFormat == D3DFMT_DXT5 ) dxFormat = D3DFMT_A8R8G8B8; g_pd3dDevice->CreateTexture( imgData->width, imgData->height, 1, 0, dxFormat, D3DPOOL_MANAGED, &texture, 0 ); texture->LockRect(0, &rect, 0, 0 ); memcpy( rect.pBits, imgData->imgData, imgData->width * imgData->height * imgData->format->bytesPerPixel ); texture->UnlockRect( 0 ); if( imgData->format->d3dformat == D3DFMT_DXT1 || imgData->format->d3dformat == D3DFMT_DXT3 || imgData->format->d3dformat == D3DFMT_DXT5 ) { ID3DXBuffer* dxBuffer; LPDIRECT3DTEXTURE9 outTexture; D3DXSaveTextureToFileInMemory( &dxBuffer, D3DXIFF_BMP, texture, 0 ); D3DXCreateTextureFromFileInMemoryEx( g_pd3dDevice, dxBuffer->GetBufferPointer(), dxBuffer->GetBufferSize(), imgData->width, imgData->height, 0, 0, (D3DFORMAT)imgData->format->d3dformat, D3DPOOL_SCRATCH, D3DX_DEFAULT, D3DX_DEFAULT, 0, 0, 0, &outTexture ); D3DXSaveTextureToFile( fileName, extToFormat(fileName), outTexture, 0 ); outTexture->Release(); }else { D3DXSaveTextureToFile( fileName, extToFormat(fileName), texture, 0 ); } texture->Release(); return 0; }
inline void LoadDataToRect(LPDIRECT3DTEXTURE9 pTexture, const u8* buffer, const int width, const int height, const int pitch, D3DFORMAT fmt, const bool swap_r_b, int level) { D3DLOCKED_RECT Lock; pTexture->LockRect(level, &Lock, NULL, 0); s32 pixelsize = 0; switch (fmt) { case D3DFMT_L8: case D3DFMT_A8: case D3DFMT_A4L4: pixelsize = 1; break; case D3DFMT_R5G6B5: pixelsize = 2; break; case D3DFMT_A8P8: TextureUtil::ExpandI8Data((u8*)Lock.pBits, buffer, width, height, pitch, Lock.Pitch); break; case D3DFMT_A8L8: pixelsize = 2; break; case D3DFMT_A8R8G8B8: if (!swap_r_b) { pixelsize = 4; } else { TextureUtil::ConvertRGBA_BGRA((u32 *)Lock.pBits, Lock.Pitch, (u32*)buffer, width, height, pitch); } break; case D3DFMT_DXT1: case D3DFMT_DXT3: case D3DFMT_DXT5: TextureUtil::CopyCompressedTextureData((u8*)Lock.pBits, buffer, width, height, pitch, fmt == D3DFMT_DXT1 ? 8 : 16, Lock.Pitch); break; default: PanicAlert("D3D: Invalid texture format %i", fmt); } if (pixelsize > 0) { TextureUtil::CopyTextureData((u8*)Lock.pBits, buffer, width, height, pitch * pixelsize, Lock.Pitch, pixelsize); } pTexture->UnlockRect(level); }
//----------------------------------------------------------------------------- // Name: Render() // Desc: Draws the scene //----------------------------------------------------------------------------- VOID Render() { // Clear the backbuffer and the zbuffer g_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB( 0, 0, 255 ), 1.0f, 0 ); D3DLOCKED_RECT locked; if(g_pTexture->LockRect(0, &locked, NULL, /*D3DLOCK_DISCARD*/0)==D3D_OK) { memcpy(locked.pBits, videoFrame, TEXTURE_WIDTH*TEXTURE_HEIGHT*3); g_pTexture->UnlockRect(0); } // Begin the scene if( SUCCEEDED( g_pd3dDevice->BeginScene() ) ) { // Setup the world, view, and projection matrices SetupMatrices(); // Setup our texture. Using textures introduces the texture stage states, // which govern how textures get blended together (in the case of multiple // textures) and lighting information. In this case, we are modulating // (blending) our texture with the diffuse color of the vertices. g_pd3dDevice->SetTexture( 0, g_pTexture ); 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_DISABLE ); // Render the vertex buffer contents g_pd3dDevice->SetStreamSource( 0, g_pVB, 0, sizeof( CUSTOMVERTEX ) ); g_pd3dDevice->SetFVF( D3DFVF_CUSTOMVERTEX ); // Draws two triangles (makes a quad that will support our drone video picture) g_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP, 0, 2 ); // End the scene g_pd3dDevice->EndScene(); } // Present the backbuffer contents to the display g_pd3dDevice->Present( NULL, NULL, NULL, NULL ); }
/* Copy texture memory to bitmap->memory */ static void d3d_sync_bitmap_memory(ALLEGRO_BITMAP *bitmap) { D3DLOCKED_RECT locked_rect; ALLEGRO_BITMAP_D3D *d3d_bmp = (ALLEGRO_BITMAP_D3D *)bitmap; LPDIRECT3DTEXTURE9 texture; if (_al_d3d_render_to_texture_supported()) texture = d3d_bmp->system_texture; else texture = d3d_bmp->video_texture; if (texture->LockRect(0, &locked_rect, NULL, 0) == D3D_OK) { _al_convert_bitmap_data(locked_rect.pBits, bitmap->format, locked_rect.Pitch, bitmap->memory, bitmap->format, al_get_pixel_size(bitmap->format)*bitmap->w, 0, 0, 0, 0, bitmap->w, bitmap->h); texture->UnlockRect(0); } else { ALLEGRO_ERROR("d3d_sync_bitmap_memory: Couldn't lock texture.\n"); } }
VOID Render() { D3DLOCKED_RECT lr; if(SUCCEEDED(g_pTexture->LockRect(0,&lr,NULL,0))) { g_ogg.video_read((char*)lr.pBits,lr.Pitch); g_pTexture->UnlockRect(0); } if( SUCCEEDED( g_pd3dDevice->BeginScene() ) ) { g_pd3dDevice->SetTexture(0,g_pTexture); g_pd3dDevice->SetStreamSource( 0, g_pVB, 0, sizeof( CUSTOMVERTEX ) ); g_pd3dDevice->SetFVF( D3DFVF_CUSTOMVERTEX ); g_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLEFAN, 0, 2 ); g_pd3dDevice->EndScene(); } g_pd3dDevice->Present( NULL, NULL, NULL, NULL ); }
BOOL dx9vid_update() { // Update from the graphics // lets draw a random pixel D3DLOCKED_RECT pLocked; DWORD ret; ret = g_screenTex->LockRect(0, &pLocked, 0, 0); if ( ret != D3D_OK ) { return true; } // Draw some pixels DWORD* pDst = (DWORD*)pLocked.pBits; //draw the screen to our sprite //for (int count=0; count < NGPC_SIZEY*NGPC_SIZEX; count++) //{ for(int y = 0; y < NGPC_SIZEY; y++) { for(int x = 0; x < NGPC_SIZEX; x++) { unsigned short color = drawBuffer[y*NGPC_SIZEX+x]; unsigned short b = ((color&0x003E)>>1)*8; unsigned short g = ((color&0x07C0)>>6)*8; unsigned short r = ((color&0xF800)>>11)*8; pDst[y*256+x] = (r<<16) | (g<<8) | b; //b (4-bit) //pDst[y*256+x] |= g<<4; //g (4-bit) //pDst[y*256+x] |= r<<8; //b (4-bit)1 //pDst[y*256+x] |= 0<<12; //x } } g_screenTex->UnlockRect(0); return TRUE; }
LPDIRECT3DTEXTURE9 LoadBLP(char* blpdata, DWORD blpsize) { LPDIRECT3DTEXTURE9 pTexture = NULL; DWORD width,height,type,subtype,nSizeBuffer = ConvertBLP(blpdata, NULL, &width,&height,&type,&subtype); char *timage = new char[nSizeBuffer]; ConvertBLP(blpdata, timage, &width,&height,&type,&subtype); LPDIRECT3DDEVICE9 pd3dDevice = CGlobals::GetRenderDevice(); if(SUCCEEDED(D3DXCreateTexture(pd3dDevice, width, height, 0, 0, D3DFMT_A8R8G8B8, D3DPOOL_MANAGED, &pTexture))) { D3DLOCKED_RECT lr; if(SUCCEEDED(pTexture->LockRect(0, &lr, NULL, 0))) { char *pp = (char*)lr.pBits; unsigned long index = 0, x = 0, y = 0; for (y=0; y < height; y++) { for (x=0; x < width; x++) { pp[index++] = timage[(y*4*width)+4*x]; pp[index++] = timage[(y*4*width)+4*x+1]; pp[index++] = timage[(y*4*width)+4*x+2]; pp[index++] = timage[(y*4*width)+4*x+3]; } index += lr.Pitch - (width*4); } pTexture->UnlockRect(0); } } delete []timage; return pTexture; }
static bool ImGui_ImplDX9_CreateFontsTexture() { // Build texture atlas ImGuiIO& io = ImGui::GetIO(); unsigned char* pixels; int width, height, bytes_per_pixel; io.Fonts->GetTexDataAsRGBA32(&pixels, &width, &height, &bytes_per_pixel); // Upload texture to graphics system g_FontTexture = NULL; if (g_pd3dDevice->CreateTexture(width, height, 1, D3DUSAGE_DYNAMIC, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &g_FontTexture, NULL) < 0) return false; D3DLOCKED_RECT tex_locked_rect; if (g_FontTexture->LockRect(0, &tex_locked_rect, NULL, 0) != D3D_OK) return false; for (int y = 0; y < height; y++) memcpy((unsigned char *)tex_locked_rect.pBits + tex_locked_rect.Pitch * y, pixels + (width * bytes_per_pixel) * y, (width * bytes_per_pixel)); g_FontTexture->UnlockRect(0); // Store our identifier io.Fonts->TexID = (void *)g_FontTexture; return true; }
bool Model::load( unsigned char * _buffer, size_t _size ) { size_t read = 0; stdex::memory_reader mr( _buffer, _size, read ); mr << m_frameCount; mr << m_vertexCount; mr << m_indicesCount; mr << m_frameDelay; mr << m_cameraFOV; mr << m_cameraWidth; mr << m_cameraHeight; m_model3DFrames = new Model3DFrame[ m_frameCount ]; for( uint32_t i = 0; i != m_frameCount; ++i ) { Model3DFrame & frame = m_model3DFrames[ i ]; mr << frame.cameraRight; mr << frame.cameraUp; mr << frame.cameraDir; mr << frame.cameraPos; frame.pos = new mt::vec3f[m_vertexCount]; frame.uv = new mt::vec2f[m_vertexCount]; frame.indecies = new uint16_t[m_indicesCount]; mr.readPODs( frame.pos, m_vertexCount ); mr.readPODs( frame.uv, m_vertexCount ); mr.readPODs( frame.indecies, m_indicesCount ); } uint16_t width; uint16_t height; mr << width; mr << height; if( width != 0 && height != 0 ) { if( isTexturePOW2( width ) == false || isTexturePOW2( height ) == false ) { //_logger.message( "Model::load invalid texture POW2 %d:%d please set texture %d:%d" // , width // , height // , getTexturePOW2( width ) // , getTexturePOW2( height ) // ); return false; } LPDIRECT3DTEXTURE9 texture = NULL; if( m_pDevice->CreateTexture( width, height, 1, 0, D3DFMT_A8R8G8B8, D3DPOOL_MANAGED, &texture, NULL ) != D3D_OK ) { //_logger.message( "Model::load invalid create texture %d:%d" // , width // , height // ); return false; } D3DLOCKED_RECT lockedRect; DXCALL texture->LockRect( 0, &lockedRect, NULL, 0 ); if( lockedRect.pBits == NULL || lockedRect.Pitch == 0 ) { //_logger.message( "Model::load invalid lock texture %d:%d" // , width // , height // ); return false; } for( uint16_t i = 0; i != height; ++i ) { unsigned char * bits = static_cast<unsigned char *>(lockedRect.pBits) + lockedRect.Pitch * i; mr.readPODs( bits, width * 4 ); } DXCALL texture->UnlockRect( 0 ); m_pTexture = texture; } else { m_pTexture = NULL; } return true; }
void WaterEffect::UpdateTexture( Real FrameTime, Screen3D& Screen ) { //Disperse the water /*for(int y=1; y<WATERDIM-1; y++) { for(int x=1; x<WATERDIM-1; x++) { if(NewWater[x+y*WATERDIM] == -1) continue; NewWater[x+y * WATERDIM] = ((OldWater[x+ ((y-1)*WATERDIM)] + OldWater[x+ ((y+1)*WATERDIM)] + OldWater[(x+1)+ (y*WATERDIM)] + OldWater[(x-1)+ (y*WATERDIM)] )*.5f) - NewWater[x+y * WATERDIM]; NewWater[x+y * WATERDIM] -= (NewWater[x+y * WATERDIM]*0.0625) ; if(NewWater[x+y * WATERDIM] < 0) NewWater[x+y * WATERDIM] =0; if(NewWater[x+y * WATERDIM] > 250) NewWater[x+y * WATERDIM] = 250; } }*/ for(int i=0; i<GRID_DIM*GRID_DIM; i++) { if(JumpTable[i]) { for(int y=JumpTable[i]->StartY; y<JumpTable[i]->EndY; y++) { for(int x=JumpTable[i]->StartX; x<JumpTable[i]->EndX; x++) { if(NewWater[x+y*WATERDIM] == -1) continue; NewWater[x+y * WATERDIM] = ((OldWater[x+ ((y-1)*WATERDIM)] + OldWater[x+ ((y+1)*WATERDIM)] + OldWater[(x+1)+ (y*WATERDIM)] + OldWater[(x-1)+ (y*WATERDIM)] )*.5f) - NewWater[x+y * WATERDIM]; NewWater[x+y * WATERDIM] -= (NewWater[x+y * WATERDIM]*0.0625) * FrameTime; if(NewWater[x+y * WATERDIM] < 0) NewWater[x+y * WATERDIM] =0; if(NewWater[x+y * WATERDIM] > 250) NewWater[x+y * WATERDIM] = 250; } } } } //Update the texture LPDIRECT3DTEXTURE9 TextureSurface; D3DSURFACE_DESC SurfDesc; D3DLOCKED_RECT LockedArea; BYTE* Surface; Texture* T = Screen.TM.GetTexture(RippleTexture); TextureSurface = T->GetTextureSurface(); //Figure out how many "levels" (mip-maps) there are //in the texture int nLevels = TextureSurface->GetLevelCount(); TextureSurface->GetLevelDesc(0, &SurfDesc); Real DivFactor=1; //loop through each level for(int l=0; l<nLevels; l++) { //Get the surface's attributes TextureSurface->GetLevelDesc(l, &SurfDesc); //lock down a pointer TextureSurface->LockRect(l, &LockedArea, NULL, NULL); //draw the pixel to the surface Surface = (BYTE*)LockedArea.pBits; if(SurfDesc.Format == D3DFMT_A8R8G8B8) { for(int y=0; y<WATERDIM/DivFactor; y++) { for(int x=0; x<WATERDIM/DivFactor; x++) { int rx=x*DivFactor; int ry=y*DivFactor; Surface[(x*4)+0+y*(SurfDesc.Width*4)] = 128+NewWater[rx+ry * WATERDIM]; Surface[(x*4)+1+y*(SurfDesc.Width*4)] = 128+NewWater[rx+ry * WATERDIM]; Surface[(x*4)+2+y*(SurfDesc.Width*4)] = 128+NewWater[rx+ry * WATERDIM]; } } } if(SurfDesc.Format == D3DFMT_L8) { for(int y=0; y<WATERDIM/DivFactor; y++) { for(int x=0; x<WATERDIM/DivFactor; x++) { int rx=x*DivFactor; int ry=y*DivFactor; Surface[(x+y*SurfDesc.Width)] = 128+NewWater[rx+ry * WATERDIM]; } } } DivFactor *=2; //unlock the surface TextureSurface->UnlockRect(l); } //Swap the buffers Real* Temp = NewWater; NewWater = OldWater; OldWater = Temp; }
bool SegmentImage(LPDIRECT3DTEXTURE9 texture, int subimage_width, int subimage_height, int rows, int columns, int selected_index, LPDIRECT3DTEXTURE9* segment) { D3DSURFACE_DESC desc; HRESULT hr = texture->GetLevelDesc(0, &desc); CONFIRM(SUCCEEDED(hr)) else return false; // Locate the source rectangle UINT sii = selected_index; // The sub-image index (sii) UINT sixc = sii % columns; // sub-image x-coordinate UINT siyc = sii / columns; // sub-image y-coordinate RECT sourceRect = { (sixc+0) * subimage_width, (siyc+0) * subimage_height, (sixc+1) * subimage_width, (siyc+1) * subimage_height }; // Create a texture for the sub-image LPDIRECT3DTEXTURE9 subimageTexture = NULL; { LPDIRECT3DDEVICE9 d3d_device; texture->GetDevice(&d3d_device); hr = D3DXCreateTexture(d3d_device, subimage_width, subimage_height, -1, 0, desc.Format, desc.Pool, &subimageTexture); SAFE_RELEASE(d3d_device); // If we can't make this texture, something is wrong, but we can't do much about it if (APP_WARNING(FAILED(hr))("Unable to create sub-image texture")) { return false; } //D3DSURFACE_DESC desc; //subimageTexture->GetLevelDesc(0, &desc); //DEBUG_INFO("creating image: %lu, %lu; actual size: %lu, %lu", // subimage_width, // subimage_height, // desc.Width, // desc.Height); } DWORD format_byte_size = 4; switch (desc.Format) { default: DEBUG_ERROR("Unknown image format"); return false; case D3DFMT_A8R8G8B8: case D3DFMT_X8R8G8B8: format_byte_size = sizeof(D3DCOLOR); break; case D3DFMT_A8P8: case D3DFMT_R5G6B5: case D3DFMT_X1R5G5B5: case D3DFMT_X4R4G4B4: format_byte_size = sizeof(WORD); break; } // Copy the texture D3DLOCKED_RECT sourceLR, destLR; texture->LockRect(0, &sourceLR, &sourceRect, D3DLOCK_READONLY); subimageTexture->LockRect(0, &destLR, NULL, 0); BYTE* sourceBits = (BYTE*)sourceLR.pBits; BYTE* destBits = (BYTE*)destLR.pBits; // Copy each row for (long y = 0; y < subimage_height; ++y) { memcpy(&destBits[destLR.Pitch*y], &sourceBits[sourceLR.Pitch*y], subimage_width*format_byte_size); } // Unlock the source and destination texture->UnlockRect(0); subimageTexture->UnlockRect(0); // Generate mipmaps, if necessary // TODO: this generates an error...why?! //if (mipmap) // subimageTexture->GenerateMipSubLevels(); // Save the sub-image as the output texture *segment = subimageTexture; return true; }
static ALLEGRO_LOCKED_REGION *d3d_lock_region(ALLEGRO_BITMAP *bitmap, int x, int y, int w, int h, int format, int flags) { ALLEGRO_BITMAP_D3D *d3d_bmp = (ALLEGRO_BITMAP_D3D *)bitmap; if (d3d_bmp->display->device_lost) return NULL; RECT rect; DWORD Flags = flags & ALLEGRO_LOCK_READONLY ? D3DLOCK_READONLY : 0; int f = _al_get_real_pixel_format(al_get_current_display(), format); if (f < 0) { return NULL; } rect.left = x; rect.right = x + w; rect.top = y; rect.bottom = y + h; if (d3d_bmp->is_backbuffer) { ALLEGRO_DISPLAY_D3D *d3d_disp = (ALLEGRO_DISPLAY_D3D *)bitmap->display; if (d3d_disp->render_target->LockRect(&d3d_bmp->locked_rect, &rect, Flags) != D3D_OK) { ALLEGRO_ERROR("LockRect failed in d3d_lock_region.\n"); return NULL; } } else { LPDIRECT3DTEXTURE9 texture; if (_al_d3d_render_to_texture_supported()) { /* * Sync bitmap->memory with texture */ bitmap->locked = false; _al_d3d_sync_bitmap(bitmap); bitmap->locked = true; texture = d3d_bmp->system_texture; } else { texture = d3d_bmp->video_texture; } if (texture->LockRect(0, &d3d_bmp->locked_rect, &rect, Flags) != D3D_OK) { ALLEGRO_ERROR("LockRect failed in d3d_lock_region.\n"); return NULL; } } if (format == ALLEGRO_PIXEL_FORMAT_ANY || bitmap->format == format || f == bitmap->format) { bitmap->locked_region.data = d3d_bmp->locked_rect.pBits; bitmap->locked_region.format = bitmap->format; bitmap->locked_region.pitch = d3d_bmp->locked_rect.Pitch; bitmap->locked_region.pixel_size = al_get_pixel_size(bitmap->format); } else { bitmap->locked_region.pitch = al_get_pixel_size(f) * w; bitmap->locked_region.data = al_malloc(bitmap->locked_region.pitch*h); bitmap->locked_region.format = f; bitmap->locked_region.pixel_size = al_get_pixel_size(bitmap->format); if (!(bitmap->lock_flags & ALLEGRO_LOCK_WRITEONLY)) { _al_convert_bitmap_data( d3d_bmp->locked_rect.pBits, bitmap->format, d3d_bmp->locked_rect.Pitch, bitmap->locked_region.data, f, bitmap->locked_region.pitch, 0, 0, 0, 0, w, h); } } return &bitmap->locked_region; }
LPDIRECT3DTEXTURE9 Create_texture( LPDIRECT3DDEVICE9 d3d_device, D3DFORMAT d3d_surface_type, u32 pitch, u32 pixel_size, u32 width, u32 height ) { LPDIRECT3DTEXTURE9 texture = NULL; // // try to create a dynamic texture first // /* if ( SUCCEEDED( d3d_device->CreateTexture(width, height, 1, D3DUSAGE_DYNAMIC, d3d_surface_type, D3DPOOL_DEFAULT, &texture, 0 ) ) ) { goto gotit; } */ // // if that fails, create a regular texture // if ( SUCCEEDED( d3d_device->CreateTexture(width, height, 1, 0, d3d_surface_type, D3DPOOL_MANAGED, &texture, 0 ) ) ) { D3DLOCKED_RECT locked_rect; // gotit: // // Clear the texture to black. // if ( SUCCEEDED( texture->LockRect( 0, &locked_rect, NULL, 0 ) ) ) { // // Clear the pixels. // u8* dest = ( u8* ) locked_rect.pBits; u32 bytes = width * pixel_size; for ( u32 y = 0 ; y < height ; y++ ) { memset( dest, 0, bytes ); dest += locked_rect.Pitch; } // // Unlock the DirectX texture. // texture->UnlockRect( 0 ); } } return texture; }
//---------------------------------------------------------------------------- SEImage* SEImageConverter::CreateImageFromFile(const char* acFilename, const char* acImageName, bool bInsert) { if( !acFilename ) { return 0; } HRESULT hResult; LPDIRECT3DTEXTURE9 pDXTex; SEImage* pImage; hResult = D3DXCreateTextureFromFile(m_pDXDevice, acFilename, &pDXTex); if( !SUCCEEDED(hResult) ) { return 0; } D3DSURFACE_DESC tempDesc; pDXTex->GetLevelDesc(0, &tempDesc); int iWidth = tempDesc.Width; int iHeight = tempDesc.Height; int eFormat = D3DFMT_UNKNOWN; int iBytesPerPixel = 0; switch( tempDesc.Format ) { case D3DFMT_R8G8B8: { eFormat = SEImage::IT_RGB888; iBytesPerPixel = 3; break; } case D3DFMT_A8R8G8B8: case D3DFMT_X8R8G8B8: { eFormat = SEImage::IT_RGBA8888; iBytesPerPixel = 4; break; } case D3DFMT_L8: { eFormat = SEImage::IT_L8; iBytesPerPixel = 1; break; } case D3DFMT_L16: { eFormat = SEImage::IT_L16; iBytesPerPixel = 2; break; } default: { // 尚未支持的情况. SE_ASSERT( 0 ); break; } } int iCount = iWidth * iHeight; int iByteCount = iCount * iBytesPerPixel; unsigned char* aucDst = SE_NEW unsigned char[iByteCount]; unsigned char* pDst, * pSrc; D3DLOCKED_RECT tempLockRect; hResult = pDXTex->LockRect(0, &tempLockRect, 0, 0); SE_ASSERT( SUCCEEDED(hResult) ); pSrc = (unsigned char*)tempLockRect.pBits; pDst = aucDst; int i, iBase = 0; switch( eFormat ) { case SEImage::IT_RGB888: { for( i = 0; i < iCount; i++, iBase += 3 ) { pDst[iBase ] = pSrc[iBase + 2]; pDst[iBase + 1] = pSrc[iBase + 1]; pDst[iBase + 2] = pSrc[iBase ]; } break; } case SEImage::IT_RGBA8888: { for( i = 0; i < iCount; i++, iBase += 4 ) { pDst[iBase ] = pSrc[iBase + 2]; pDst[iBase + 1] = pSrc[iBase + 1]; pDst[iBase + 2] = pSrc[iBase ]; pDst[iBase + 3] = pSrc[iBase + 3]; } break; } case SEImage::IT_L8: { for( i = 0; i < iCount; i++, iBase += 1 ) { pDst[iBase] = pSrc[iBase]; } break; } case SEImage::IT_L16: { for( i = 0; i < iCount; i++, iBase += 2 ) { pDst[iBase ] = pSrc[iBase ]; pDst[iBase + 1] = pSrc[iBase + 1]; } break; } default: { // 尚未支持的情况. SE_ASSERT( 0 ); break; } } hResult = pDXTex->UnlockRect(0); SE_ASSERT( SUCCEEDED(hResult) ); pDXTex->Release(); if( acImageName ) { pImage = SE_NEW SEImage((SEImage::FormatMode)eFormat, iWidth, iHeight, aucDst, acImageName, bInsert); } else { pImage = SE_NEW SEImage((SEImage::FormatMode)eFormat, iWidth, iHeight, aucDst, acFilename, bInsert); } SE_ASSERT( pImage ); return pImage; }
HRESULT KModelWater::InitBumpMap() { //创建水面bump纹理 HRESULT hr; LPDIRECT3DTEXTURE9 psBumpSrc; D3DSURFACE_DESC d3dsd; D3DLOCKED_RECT d3dlr; TCHAR Name[256]; wsprintf(Name,"%s\\%s",g_Def_AppDirectory,"Textures\\Water.bmp"); hr = D3DXCreateTextureFromFile(g_pd3dDevice, Name, &psBumpSrc); if (FAILED(hr)) return hr; psBumpSrc->GetLevelDesc( 0, &d3dsd ); // Create the bumpmap's surface and texture objects if( FAILED( g_pd3dDevice->CreateTexture( d3dsd.Width, d3dsd.Height, 1, 0, D3DFMT_V8U8, D3DPOOL_MANAGED, &m_pBumpMapTexture,NULL ) ) ) { return E_FAIL; } // Fill the bits of the new texture surface with bits from // a private format. psBumpSrc->LockRect( 0, &d3dlr, 0, 0 ); DWORD dwSrcPitch = (DWORD)d3dlr.Pitch; BYTE* pSrcTopRow = (BYTE*)d3dlr.pBits; BYTE* pSrcCurRow = pSrcTopRow; BYTE* pSrcBotRow = pSrcTopRow + (dwSrcPitch * (d3dsd.Height - 1) ); m_pBumpMapTexture->LockRect( 0, &d3dlr, 0, 0 ); DWORD dwDstPitch = (DWORD)d3dlr.Pitch; BYTE* pDstTopRow = (BYTE*)d3dlr.pBits; BYTE* pDstCurRow = pDstTopRow; for( DWORD y=0; y<d3dsd.Height; y++ ) { BYTE* pSrcB0; // addr of current pixel BYTE* pSrcB1; // addr of pixel below current pixel, wrapping to top if necessary BYTE* pSrcB2; // addr of pixel above current pixel, wrapping to bottom if necessary BYTE* pDstT; // addr of dest pixel; pSrcB0 = pSrcCurRow; if( y == d3dsd.Height - 1) pSrcB1 = pSrcTopRow; else pSrcB1 = pSrcCurRow + dwSrcPitch; if( y == 0 ) pSrcB2 = pSrcBotRow; else pSrcB2 = pSrcCurRow - dwSrcPitch; pDstT = pDstCurRow; for( DWORD x=0; x<d3dsd.Width; x++ ) { LONG v00; // Current pixel LONG v01; // Pixel to the right of current pixel, wrapping to left edge if necessary LONG vM1; // Pixel to the left of current pixel, wrapping to right edge if necessary LONG v10; // Pixel one line below. LONG v1M; // Pixel one line above. v00 = *(pSrcB0+0); if( x == d3dsd.Width - 1 ) v01 = *(pSrcCurRow); else v01 = *(pSrcB0+4); if( x == 0 ) vM1 = *(pSrcCurRow + (4 * (d3dsd.Width - 1))); else vM1 = *(pSrcB0-4); v10 = *(pSrcB1+0); v1M = *(pSrcB2+0); LONG iDu = (vM1-v01); // The delta-u bump value LONG iDv = (v1M-v10); // The delta-v bump value *pDstT++ = (BYTE)(iDu / 2); *pDstT++ = (BYTE)(iDv / 2); // Move one pixel to the right (src is 32-bpp) pSrcB0+=4; pSrcB1+=4; pSrcB2+=4; } // Move to the next line pSrcCurRow += dwSrcPitch; pDstCurRow += dwDstPitch; } m_pBumpMapTexture->UnlockRect(0); psBumpSrc->UnlockRect(0); return S_OK; }