Skip to content

Instantly share code, notes, and snippets.

@Gargaj
Last active August 29, 2015 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Gargaj/64299793ee44fbdabbfb to your computer and use it in GitHub Desktop.
Save Gargaj/64299793ee44fbdabbfb to your computer and use it in GitHub Desktop.
2D-to-volume texture converter
#include <Windows.h>
#include <stdio.h>
#include <d3d9.h>
#include <d3dx9.h>
#pragma comment(lib,"d3d9.lib")
#pragma comment(lib,"d3dx9.lib")
#define RESWIDTH 320
#define RESHEIGHT 480
D3DPRESENT_PARAMETERS mPresentParam = {
RESWIDTH, RESHEIGHT, D3DFMT_A8R8G8B8, 0, // backbuffer
D3DMULTISAMPLE_NONE, 0, // multisample
D3DSWAPEFFECT_DISCARD,
NULL, // device window
TRUE, // windowed
TRUE, D3DFMT_D24S8, // depthstencil
0, // flags
0, // refresh rate
0 // presentation interval
};
void main(int argc, char* argv[])
{
printf("2D-to-volume texture converter\n");
if (argc < 2)
{
printf("Usage: %s <input-file> [output-file]\n\n",argv[0]);
return;
}
bool bMipmaps = false;
char szInput[MAX_PATH] = {0};
char szOutput[MAX_PATH] = {0};
for (int i=1; i<argc; i++)
{
if (argv[i][0] == '-')
{
if (strcmp(argv[i],"--generate-mipmaps") == 0)
bMipmaps = true;
}
else
{
if (szInput[0])
strncpy( szOutput, argv[i], MAX_PATH );
else
strncpy( szInput, argv[i], MAX_PATH );
}
}
if (!szOutput[0])
{
_snprintf( szOutput, MAX_PATH, "%s.volume.dds", szInput );
}
printf("Input file: %s\n",szInput);
printf("Output file: %s\n",szOutput);
mPresentParam.hDeviceWindow = CreateWindowExA( WS_EX_APPWINDOW,
"STATIC",
0,
WS_POPUP | WS_VISIBLE,
0, 0,
RESWIDTH, RESHEIGHT,
0, 0, 0, 0);
LPDIRECT3D9 mD3D = Direct3DCreate9(D3D_SDK_VERSION);
LPDIRECT3DDEVICE9 mDevice = NULL;
mD3D->CreateDevice( D3DADAPTER_DEFAULT,
D3DDEVTYPE_HAL,
mPresentParam.hDeviceWindow,
D3DCREATE_HARDWARE_VERTEXPROCESSING,
&mPresentParam,
&mDevice);
LPDIRECT3DTEXTURE9 tex = NULL;
D3DXIMAGE_INFO info;
HRESULT h = D3DXCreateTextureFromFileExA(
mDevice,
argv[1],
D3DX_DEFAULT,
D3DX_DEFAULT,
D3DX_DEFAULT,
NULL,
D3DFMT_FROM_FILE,
D3DPOOL_MANAGED,
D3DX_FILTER_NONE,
D3DX_DEFAULT,
NULL,
&info,
NULL,
&tex);
printf("Input: %d * %d\n",info.Width,info.Height);
LPDIRECT3DVOLUMETEXTURE9 voltex = NULL;
mDevice->CreateVolumeTexture( info.Height, info.Height, info.Height, bMipmaps ? 0 : 1, NULL, D3DFMT_X8R8G8B8, D3DPOOL_MANAGED, &voltex, NULL);
D3DLOCKED_BOX box;
D3DLOCKED_RECT rect;
HRESULT hRes = voltex->LockBox( 0, &box, NULL, NULL );
//assert(SUCCEEDED(hRes) && box.pBits);
tex->LockRect( 0, &rect, NULL, D3DLOCK_READONLY );
unsigned char * pSrc = (unsigned char *)rect.pBits;
unsigned char * pDst = (unsigned char *)box.pBits;
int nWidthInBytes = 4 * sizeof(unsigned char) * info.Height;
for (int z = 0; z < info.Height; z++)
{
unsigned char * pDstSlice = pDst;
for (int y = 0; y < info.Height; y++)
{
CopyMemory( pDstSlice, pSrc + z * nWidthInBytes + y * rect.Pitch, 4 * sizeof(unsigned char) * info.Height );
pDstSlice += box.RowPitch;
}
pDst += box.SlicePitch;
}
tex->UnlockRect( 0 );
voltex->UnlockBox( 0 );
if (bMipmaps)
{
printf("Generating mipmaps...\n");
D3DXFilterVolumeTexture( voltex, NULL, 0, D3DX_FILTER_LINEAR );
}
printf("Saving...\n");
D3DXSaveTextureToFileA( szOutput, D3DXIFF_DDS, voltex, NULL );
voltex->Release();
tex->Release();
mDevice->Release();
DestroyWindow( mPresentParam.hDeviceWindow );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment