#include "defines.h" #include "Texture.h" #include "Random.h" #include "globals.h" #include "rotmat.h" void Texture::Init() { m_iSizeX= -1; m_iSizeY= -1; m_iSizeZ= -1; m_iMipLevel= -1; m_pTexture= NULL; m_d3dlb.pBits= NULL; m_d3dlb.RowPitch= -1; m_d3dlb.SlicePitch= -1; } void Texture::DeInit() { Release(); } void Texture::Create( int iSizeX, int iSizeY, int iSizeZ, int iMipLevel ) { Release(); m_iSizeX= iSizeX; m_iSizeY= iSizeY; m_iSizeZ= iSizeZ; m_iMipLevel= iMipLevel; if(m_iSizeZ > 1) { g_d3d_device->CreateVolumeTexture( m_iSizeX, m_iSizeY, m_iSizeZ, m_iMipLevel, 0, D3DFMT_A8R8G8B8, D3DPOOL_MANAGED, (IDirect3DVolumeTexture9**)&m_pTexture, NULL ); } else { m_iSizeZ = 1; g_d3d_device->CreateTexture( m_iSizeX, m_iSizeY, m_iMipLevel, 0, D3DFMT_A8R8G8B8, D3DPOOL_MANAGED, (IDirect3DTexture9**)&m_pTexture, NULL ); } } void Texture::Release() { Unlock(); if( m_pTexture != NULL ) { m_pTexture->Release(); m_pTexture= NULL; } m_iSizeX= -1; m_iSizeY= -1; m_iSizeZ= -1; m_iMipLevel= -1; m_d3dlb.pBits= NULL; m_d3dlb.RowPitch= -1; m_d3dlb.SlicePitch= -1; } void Texture::Lock() { if(m_iSizeZ > 1) { ((IDirect3DVolumeTexture9*)m_pTexture)->LockBox( 0, &m_d3dlb, 0, 0 ); } else { D3DLOCKED_RECT d3dlr; ((IDirect3DTexture9*)m_pTexture)->LockRect( 0, &d3dlr, 0, 0 ); m_d3dlb.pBits = d3dlr.pBits; m_d3dlb.RowPitch = d3dlr.Pitch; m_d3dlb.SlicePitch = 0; } } void Texture::Unlock() { if( m_d3dlb.pBits == NULL ) { return; } if(m_iSizeZ > 1) { ((IDirect3DVolumeTexture9*)m_pTexture)->UnlockBox( 0 ); } else { ((IDirect3DTexture9*)m_pTexture)->UnlockRect( 0 ); } m_d3dlb.pBits= NULL; m_d3dlb.RowPitch= -1; m_d3dlb.SlicePitch= -1; } void Texture::PrepareRandomTexture() { RandomGenerator rg; rg.setSeed( 1024 ); for( int i= 0; i < 1024; ++i ) { rg.genFloat(); } Lock(); for( int iZ= 0; iZ < m_iSizeZ; ++iZ ) { for( int iY= 0; iY < m_iSizeY; ++iY ) { DWORD* pData= (DWORD*)((BYTE*)m_d3dlb.pBits + iY * m_d3dlb.RowPitch + iZ * m_d3dlb.SlicePitch ); for( int iX= 0; iX < m_iSizeX; ++iX ) { rg.genFloat(); *pData++ = rg.getLastTempValue(); } } } Unlock(); // Generate mip maps D3DXFilterTexture(m_pTexture, NULL, 0, D3DX_DEFAULT); } #ifdef EXTRACODE #include #endif void Texture::PrepareRotMatrixFromFile() { #ifdef EXTRACODE Release(); D3DXCreateTextureFromFile( g_d3d_device, "64k_pre/rotationalMatrix32.png", (IDirect3DTexture9**)&m_pTexture ); m_iSizeX= 32; m_iSizeY= 32; m_iSizeZ = 1; m_iMipLevel= 1; Lock(); std::ofstream ofs( "64k_pre/rotMat.txt" ); for( int iY= 0; iY < m_iSizeY; ++iY ) { DWORD* pData= (DWORD*)((BYTE*)m_d3dlb.pBits + iY * m_d3dlb.RowPitch ); for( int iX= 0; iX < m_iSizeX; ++iX ) { //rg.genFloat(); //*pData++ = rg.getLastTempValue(); DWORD dw= *pData++; ofs << dw << "," << "\n"; } } Unlock(); #endif } void Texture::PrepareRotMatrix() { Lock(); int iPos= 0; for( int iY= 0; iY < m_iSizeY; ++iY ) { DWORD* pData= (DWORD*)((BYTE*)m_d3dlb.pBits + iY * m_d3dlb.RowPitch ); for( int iX= 0; iX < m_iSizeX; ++iX ) { *pData++ = dwRotMatData[ iPos++ ]; } } Unlock(); } void Texture::PrepareSpecTexture() { g_pTextures[ TI_LightSpec1D ].Create( 256, 1, 1, 1 ); g_pTextures[ TI_LightSpec1D ].Lock(); float fVal= 255.0f; for( int i= 0; i <= 255; ++i ) { int iV= (int)fVal; ((DWORD*)(g_pTextures[ TI_LightSpec1D ].m_d3dlb.pBits))[ 255 - i ]= 0xff << 24 | ( iV & 0xff ) << 16 | ( iV & 0xff ) << 8 | ( iV & 0xff ); fVal*= 0.925f; } //unlock g_pTextures[ TI_LightSpec1D ].Unlock(); } void Texture::Prepare1DTextures() { PrepareSpecTexture(); int iTexIndex= TI_First1D; static const unsigned char pucDataTest[]= { 1, // Groesse == 8 255, 255, 255, 7, 255, 255, 255, 0, 16, // Groesse == 128 32, 32, 64, 64, 120, 120, 128, 63, 255, 251, 208, 0, 16, // Groesse == 128 255, 255, 255, 4, 192, 192, 192, 4, 145, 145, 145, 4, 108, 108, 108, 4, 81, 81, 81, 8, 45, 45, 45, 16, 14, 14, 14, 16, 4, 4, 4, 71, 0, 0, 0, 0, 16, // Groesse == 128 80, 0, 165, 16, 38, 3, 238, 18, 0, 183, 195, 12, 0, 217, 110, 18, 1, 232,0, 18, 152, 216,0, 12, 229, 200,0 , 18, 255, 62,0 , 15, 225, 18,0 , 0, 0 }; const unsigned char* pData= pucDataTest; int ColorIn[ 3 ]; int ColorOut[ 3 ]; while( *pData != 0 ) { //create Texture int iTexSize= *pData * 8; pData++; /*int iMips= 1; while( 2 << iMips <= iTexSize ) { iMips++; }*/ g_pTextures[ iTexIndex ].Create( iTexSize, 1, 1, 1 ); g_pTextures[ iTexIndex ].Lock(); int iX= 0; ColorIn[ 0 ]= *pData++; ColorIn[ 1 ]= *pData++; ColorIn[ 2 ]= *pData++; //set Texture Data while( true ) { int iLength= *pData; pData++; if( iLength == 0 ) { break; } ColorOut[ 0 ]= *pData++; ColorOut[ 1 ]= *pData++; ColorOut[ 2 ]= *pData++; //lerp colors for( int i= 0; i <= iLength; ++i ) { int iA= iLength - i; int iR= ( ColorOut[ 0 ] * i + ColorIn[ 0 ] * iA ) / iLength; int iG= ( ColorOut[ 1 ] * i + ColorIn[ 1 ] * iA ) / iLength; int iB= ( ColorOut[ 2 ] * i + ColorIn[ 2 ] * iA ) / iLength; assert( iX < g_pTextures[ iTexIndex ].m_iSizeX ); ((DWORD*)(g_pTextures[ iTexIndex ].m_d3dlb.pBits))[ iX ]= 0xff << 24 | ( iR & 0xff ) << 16 | ( iG & 0xff ) << 8 | ( iB & 0xff ); ++iX; } --iX; ColorIn[ 0 ]= ColorOut[ 0 ]; ColorIn[ 1 ]= ColorOut[ 1 ]; ColorIn[ 2 ]= ColorOut[ 2 ]; } //unlock g_pTextures[ iTexIndex ].Unlock(); iTexIndex++; } }