Skip to content

Commit

Permalink
Added commands to create cubemap from a cross image
Browse files Browse the repository at this point in the history
  • Loading branch information
matyalatte committed Nov 5, 2022
1 parent 159d512 commit d085c1d
Showing 1 changed file with 128 additions and 3 deletions.
131 changes: 128 additions & 3 deletions Texassemble/texassemble.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ namespace
CMD_MERGE,
CMD_GIF,
CMD_ARRAY_STRIP,
CMD_CUBE_FROM_HC,
CMD_CUBE_FROM_VC,
CMD_MAX
};

Expand Down Expand Up @@ -143,6 +145,8 @@ namespace
{ L"merge", CMD_MERGE },
{ L"gif", CMD_GIF },
{ L"array-strip", CMD_ARRAY_STRIP },
{ L"cube-from-hc", CMD_CUBE_FROM_HC },
{ L"cube-from-vc", CMD_CUBE_FROM_VC },
{ nullptr, 0 }
};

Expand Down Expand Up @@ -744,6 +748,8 @@ namespace
L" array-strip creates a strip image from a 1D/2D array\n"
L" merge create texture from rgb image and alpha image\n"
L" gif create array from animated gif\n"
L" cube-from-hc create cubemap from a h-cross image\n"
L" cube-from-vc create cubemap from a v-cross image\n"
L"\n"
L" -r wildcard filename search is recursive\n"
L" -flist <filename> use text file with a list of input files (one per line)\n"
Expand Down Expand Up @@ -1007,10 +1013,12 @@ int __cdecl wmain(_In_ int argc, _In_z_count_(argc) wchar_t* argv[])
case CMD_MERGE:
case CMD_GIF:
case CMD_ARRAY_STRIP:
case CMD_CUBE_FROM_HC:
case CMD_CUBE_FROM_VC:
break;

default:
wprintf(L"Must use one of: cube, volume, array, cubearray,\n h-cross, v-cross, v-cross-fnz, h-tee, h-strip, v-strip,\n array-strip, merge, gif\n\n");
wprintf(L"Must use one of: cube, volume, array, cubearray,\n h-cross, v-cross, v-cross-fnz, h-tee, h-strip, v-strip,\n array-strip, merge, gif\n cube-from-hc, cube-from-vc\n\n");
return 1;
}

Expand Down Expand Up @@ -1244,10 +1252,12 @@ int __cdecl wmain(_In_ int argc, _In_z_count_(argc) wchar_t* argv[])
case CMD_ARRAY:
case CMD_CUBEARRAY:
case CMD_MERGE:
case CMD_CUBE_FROM_HC:
case CMD_CUBE_FROM_VC:
break;

default:
wprintf(L"-stripmips only applies to cube, volume, array, cubearray, or merge commands\n");
wprintf(L"-stripmips only applies to cube, volume, array, cubearray, merge, or cube-from commands\n");
return 1;
}
break;
Expand Down Expand Up @@ -1294,9 +1304,11 @@ int __cdecl wmain(_In_ int argc, _In_z_count_(argc) wchar_t* argv[])
case CMD_V_STRIP:
case CMD_GIF:
case CMD_ARRAY_STRIP:
case CMD_CUBE_FROM_HC:
case CMD_CUBE_FROM_VC:
if (conversion.size() > 1)
{
wprintf(L"ERROR: cross/strip/gif output only accepts 1 input file\n");
wprintf(L"ERROR: cross/strip/gif/cube-from output only accepts 1 input file\n");
return 1;
}
break;
Expand Down Expand Up @@ -1890,6 +1902,8 @@ int __cdecl wmain(_In_ int argc, _In_z_count_(argc) wchar_t* argv[])
case CMD_H_STRIP:
case CMD_V_STRIP:
case CMD_GIF:
case CMD_CUBE_FROM_HC:
case CMD_CUBE_FROM_VC:
break;

default:
Expand Down Expand Up @@ -2240,6 +2254,117 @@ int __cdecl wmain(_In_ int argc, _In_z_count_(argc) wchar_t* argv[])
}
break;
}

case CMD_CUBE_FROM_HC:
case CMD_CUBE_FROM_VC:
{
auto src = loadedImages.cbegin();
auto img = (*src)->GetImage(0, 0, 0);
size_t twidth = 0;
size_t theight = 0;
size_t offsetx = 0;
size_t offsety = 0;

switch (dwCommand)
{
case CMD_CUBE_FROM_HC:
twidth = width / 4;
theight = height / 3;
break;
case CMD_CUBE_FROM_VC:
twidth = width / 3;
theight = height / 4;
break;
default:
break;
}

if (twidth > maxCube || theight > maxCube)
{
wprintf(L"\nWARNING: Target size exceeds maximum cube dimensions for feature level (%u)\n", maxCube);
}

ScratchImage result;
hr = result.InitializeCube(format, twidth, theight, 1, 1);
if (FAILED(hr))
{
wprintf(L"FAILED setting up result image (%08X%ls)\n", static_cast<unsigned int>(hr), GetErrorDesc(hr));
return 1;
}
memset(result.GetPixels(), 0, result.GetPixelsSize());

for (size_t index = 0; index < 6; ++index)
{
switch (dwCommand)
{
case CMD_CUBE_FROM_HC:
{
// +Y
// -X +Z +X -Z
// -Y
static const size_t s_offsetx[6] = { 2, 0, 1, 1, 1, 3 };
static const size_t s_offsety[6] = { 1, 1, 0, 2, 1, 1 };

offsetx = s_offsetx[index] * twidth;
offsety = s_offsety[index] * theight;

break;
}

case CMD_CUBE_FROM_VC:
{
// +Y
// -X +Z +X
// -Y
// -Z
static const size_t s_offsetx[6] = { 2, 0, 1, 1, 1, 1 };
static const size_t s_offsety[6] = { 1, 1, 0, 2, 1, 3 };

offsetx = s_offsetx[index] * twidth;
offsety = s_offsety[index] * theight;

break;
}

default:
break;
}

const Rect rect(offsetx, offsety, twidth, theight);
hr = CopyRectangle(*img, rect, *result.GetImage(0, index, 0), dwFilter | dwFilterOpts, 0, 0);
}

// Write texture
wprintf(L"\nWriting %ls ", szOutputFile);
PrintInfo(result.GetMetadata());
wprintf(L"\n");
fflush(stdout);

if (dwOptions & (1 << OPT_TOLOWER))
{
std::ignore = _wcslwr_s(szOutputFile);
}

if (~dwOptions & (1 << OPT_OVERWRITE))
{
if (GetFileAttributesW(szOutputFile) != INVALID_FILE_ATTRIBUTES)
{
wprintf(L"\nERROR: Output file already exists, use -y to overwrite\n");
return 1;
}
}

hr = SaveToDDSFile(result.GetImages(), result.GetImageCount(), result.GetMetadata(),
(dwOptions & (1 << OPT_USE_DX10)) ? (DDS_FLAGS_FORCE_DX10_EXT | DDS_FLAGS_FORCE_DX10_EXT_MISC2) : DDS_FLAGS_NONE,
szOutputFile);
if (FAILED(hr))
{
wprintf(L"\nFAILED (%08X%ls)\n", static_cast<unsigned int>(hr), GetErrorDesc(hr));
return 1;
}
break;
}

default:
{
std::vector<Image> imageArray;
Expand Down

0 comments on commit d085c1d

Please sign in to comment.