finished newly TexConv/CubemapConv codes. converted file ok, but didn't test render.

This commit is contained in:
hyzboy 2021-12-07 18:18:22 +08:00
parent 4fbbba2c64
commit 065cf74aea
9 changed files with 167 additions and 89 deletions

View File

@ -25,29 +25,36 @@ bool ConvertImage(const OSString &filename,const ImageConvertConfig *cfg)
return(false); return(false);
} }
TextureFileCreater *tex_file_creater;
const PixelFormat *fmt=cfg->pixel_fmt[channels-1]; const PixelFormat *fmt=cfg->pixel_fmt[channels-1];
uint width = image.width();
uint height = image.height();
Image2D *origin_img=nullptr; AutoDelete<TextureFileCreater> tex_file_creater=CreateTFC(fmt,channels);
if(fmt->format<ColorFormat::COMPRESS) if(!tex_file_creater->CreateTexFile(filename,TextureFileType::Tex2D))
tex_file_creater=CreateTFC[channels-1](fmt,&image);
else
tex_file_creater=CreateTextureFileCreaterCompress(fmt,&image);
if(!tex_file_creater->WriteFileHeader(filename,TextureFileType::Tex2D,miplevel))
{ {
tex_file_creater->Delete(); LOG_ERROR(OS_TEXT("Create Texture failed."));
LOG_ERROR(OS_TEXT("Write file header failed."));
return(false); return(false);
} }
// origin_img=tex_file_creater->CreateImage2D(); if(!tex_file_creater->WriteSize2D(miplevel,width,height))
{
LOG_ERROR(OS_TEXT("Write size failed."));
return(false);
}
tex_file_creater->InitFormat(); if(!tex_file_creater->WritePixelFormat())
{
LOG_ERROR(OS_TEXT("Write format failed."));
return(false);
}
if(!tex_file_creater->InitFormat(&image))
{
LOG_ERROR(OS_TEXT("Init texture format failed."));
return(false);
}
uint width=image.width();
uint height=image.height();
uint total=0; uint total=0;
uint bytes=0; uint bytes=0;
@ -63,7 +70,7 @@ bool ConvertImage(const OSString &filename,const ImageConvertConfig *cfg)
total+=bytes; total+=bytes;
if(i<miplevel) if(miplevel>1&&i<miplevel)
{ {
if(width>1)width>>=1; if(width>1)width>>=1;
if(height>1)height>>=1; if(height>1)height>>=1;
@ -76,7 +83,6 @@ bool ConvertImage(const OSString &filename,const ImageConvertConfig *cfg)
tex_file_creater->Close(); tex_file_creater->Close();
delete tex_file_creater;
return(true); return(true);
} }

View File

@ -2,6 +2,7 @@
#include<hgl/util/cmd/CmdParse.h> #include<hgl/util/cmd/CmdParse.h>
#include<hgl/filesystem/FileSystem.h> #include<hgl/filesystem/FileSystem.h>
#include<hgl/type/StringList.h> #include<hgl/type/StringList.h>
#include<hgl/log/LogInfo.h>
#include"ILImage.h" #include"ILImage.h"
#include"TextureFileCreater.h" #include"TextureFileCreater.h"
#include"ImageConvertConfig.h" #include"ImageConvertConfig.h"
@ -59,24 +60,38 @@ bool ConvertCubemap(const OSString &filename,const OSStringList &file_list,const
uint total=0; uint total=0;
uint bytes=0; uint bytes=0;
AutoDelete<TextureFileCreater> tex_file_creater=CreateTFC(fmt,channels);
if (!tex_file_creater->CreateTexFile(filename, TextureFileType::TexCubemap))
{
LOG_ERROR(OS_TEXT("Create Texture failed."));
return(false);
}
if (!tex_file_creater->WriteSize2D(miplevel, width, height))
{
LOG_ERROR(OS_TEXT("Write size failed."));
return(false);
}
if (!tex_file_creater->WritePixelFormat())
{
LOG_ERROR(OS_TEXT("Write format failed."));
return(false);
}
for(int face=0;face<6;face++) for(int face=0;face<6;face++)
{ {
TextureFileCreater *tex_file_creater; image[face].Bind();
width=image[face].width();
height=image[face].height();
if(fmt->format<ColorFormat::COMPRESS) if (!tex_file_creater->InitFormat(&image[face]))
tex_file_creater=CreateTFC[channels-1](fmt,&image[face]);
else
tex_file_creater=CreateTextureFileCreaterCompress(fmt,&image[face]);
if(!tex_file_creater->WriteFileHeader(filename,TextureFileType::TexCubemap,miplevel))
{ {
tex_file_creater->Delete(); LOG_ERROR(OS_TEXT("Init texture format failed."));
LOG_ERROR(OS_TEXT("Write file header failed."));
return(false); return(false);
} }
tex_file_creater->InitFormat();
for(int i=0;i<miplevel;i++) for(int i=0;i<miplevel;i++)
{ {
bytes=tex_file_creater->Write(); bytes=tex_file_creater->Write();
@ -89,7 +104,7 @@ bool ConvertCubemap(const OSString &filename,const OSStringList &file_list,const
total+=bytes; total+=bytes;
if(i<miplevel) if(miplevel>1&&i<miplevel)
{ {
if(width>1)width>>=1; if(width>1)width>>=1;
if(height>1)height>>=1; if(height>1)height>>=1;
@ -97,12 +112,10 @@ bool ConvertCubemap(const OSString &filename,const OSStringList &file_list,const
image[face].Resize(width,height); image[face].Resize(width,height);
} }
} }
tex_file_creater->Close();
delete tex_file_creater;
} }
tex_file_creater->Close();
LOG_INFO(OS_TEXT("pixel total length: ")+OSString::valueOf(total)+OS_TEXT(" bytes.")); LOG_INFO(OS_TEXT("pixel total length: ")+OSString::valueOf(total)+OS_TEXT(" bytes."));
return(true); return(true);
} }
@ -118,7 +131,7 @@ int os_main(int argc,os_char **argv)
if(argc<=7) if(argc<=7)
{ {
std::cout<< "Command format:\n" std::cout<< "Command format:\n"
"\tCubemapConv [/R:][/RG:][/RGB:][/RGBA:] [/mip] <output texture filename> <neg x>,<neg y>,<neg z>,<pos x>,<pos y>,<pos z>\n\n"; "\tCubemapConv [/R:][/RG:][/RGB:][/RGBA:] [/mip] <output texture filename> <pos x>,<neg x>,<pos y>,<neg y>,<pos z>,<neg z>\n\n";
PrintFormatList(); PrintFormatList();
return 0; return 0;
@ -138,20 +151,36 @@ int os_main(int argc,os_char **argv)
CMP_RegisterHostPlugins(); CMP_RegisterHostPlugins();
CMP_InitializeBCLibrary(); CMP_InitializeBCLibrary();
OSString out_filename=argv[argc-8]; OSString out_filename=argv[argc-7];
OSStringList file_list; OSStringList file_list;
for(int i=argc-7;i<argc-1;i++) for(int i=argc-6;i<argc;i++)
{ {
if(filesystem::FileCanRead(argv[i])) if(filesystem::FileCanRead(argv[i]))
file_list.Add(argv[i]); file_list.Add(argv[i]);
else
{
LOG_ERROR(OS_TEXT("Can't check file ")+OSString(argv[i]));
return(false);
}
} }
if(file_list.GetCount()==6) if(file_list.GetCount()==6)
{ {
os_out<<OS_TEXT("output: ")<<out_filename.c_str()<<std::endl; constexpr os_char face_name[6][3]=
{
OS_TEXT("+X"),
OS_TEXT("-X"),
OS_TEXT("+Y"),
OS_TEXT("-Y"),
OS_TEXT("+Z"),
OS_TEXT("-Z")
};
for(int i=0;i<6;i++) for(int i=0;i<6;i++)
os_out<<OS_TEXT("source ")<<OSString::valueOf(i).c_str()<<OS_TEXT(": ")<<file_list[i].c_str()<<std::endl; os_out<<OS_TEXT(" ")<<face_name[i]<<OS_TEXT(": ")<<file_list[i].c_str()<<std::endl;
os_out<<OS_TEXT("output: ")<<out_filename.c_str()<<std::endl;
ConvertCubemap(out_filename,file_list,&icc); ConvertCubemap(out_filename,file_list,&icc);
} }

View File

@ -61,12 +61,12 @@ bool ToILType(ILuint &type,const uint8 bits,const ColorDataType cdt)
return(type); return(type);
} }
TextureFileCreater::TextureFileCreater(const PixelFormat *pf,ILImage *img) TextureFileCreater::TextureFileCreater(const PixelFormat *pf)
{ {
pixel_format=pf; pixel_format=pf;
image=img;
dos=nullptr; dos=nullptr;
image=nullptr;
} }
TextureFileCreater::~TextureFileCreater() TextureFileCreater::~TextureFileCreater()
@ -85,7 +85,7 @@ const char texture_file_type_name[uint(TextureFileType::RANGE_SIZE)][16]=
"TexCubemapArray" "TexCubemapArray"
}; };
bool TextureFileCreater::WriteFileHeader(const OSString &old_filename,const TextureFileType &type,const uint mip_level) bool TextureFileCreater::CreateTexFile(const OSString& old_filename, const TextureFileType& type)
{ {
OSString pn,fn; OSString pn,fn;
@ -110,30 +110,43 @@ bool TextureFileCreater::WriteFileHeader(const OSString &old_filename,const Text
dos->Write(file_type_name.c_str(),file_type_name.Length()); dos->Write(file_type_name.c_str(),file_type_name.Length());
dos->WriteUint8(0x1A); dos->WriteUint8(0x1A);
dos->WriteUint8(3); //版本 dos->WriteUint8(3); //版本
dos->WriteUint8(mip_level); //mipmaps级数
dos->WriteUint32(image->width());
if(type!=TextureFileType::Tex1D return(true);
||type!=TextureFileType::Tex1DArray) }
dos->WriteUint32(image->height());
if(type==TextureFileType::Tex3D) bool TextureFileCreater::WriteSize1D(const uint mip_level,const uint length)
dos->WriteUint32(image->depth()); {
if(!dos->WriteUint8(mip_level))return(false); //mipmaps级数
if(pixel_format->format>ColorFormat::COMPRESS) if(!dos->WriteUint32(length))return(false);
return(true);
}
bool TextureFileCreater::WriteSize2D(const uint mip_level, const uint width,const uint height)
{
if(!dos->WriteUint8(mip_level))return(false); //mipmaps级数
if(!dos->WriteUint32(width))return(false);
if(!dos->WriteUint32(height))return(false);
return(true);
}
bool TextureFileCreater::WritePixelFormat()
{
if (pixel_format->format > ColorFormat::COMPRESS)
{ {
dos->WriteUint8(0); if(!dos->WriteUint8(0))return(false);
dos->WriteUint16(uint(pixel_format->format)-uint(ColorFormat::BC1RGB)); if(!dos->WriteUint16(uint(pixel_format->format)-uint(ColorFormat::BC1RGB)))return(false);
} }
else else
{ {
dos->WriteUint8(pixel_format->channels); //颜色通道数 if(!dos->WriteUint8(pixel_format->channels))return(false); //颜色通道数
dos->WriteUint8((uint8 *)pixel_format->color,4); //颜色标记 if(!dos->WriteUint8((uint8*)pixel_format->color, 4))return(false); //颜色标记
dos->WriteUint8(pixel_format->bits,4); //颜色位数 if(!dos->WriteUint8(pixel_format->bits, 4))return(false); //颜色位数
dos->WriteUint8((uint8)pixel_format->type); //数据类型 if(!dos->WriteUint8((uint8)pixel_format->type))return(false); //数据类型
} }
return(true); return(true);
} }
uint32 TextureFileCreater::Write(void *data,const uint total_bytes) uint32 TextureFileCreater::Write(void *data,const uint total_bytes)
@ -165,4 +178,32 @@ void TextureFileCreater::Delete()
Close(); Close();
filesystem::FileDelete(filename); filesystem::FileDelete(filename);
}
TextureFileCreater *CreateTextureFileCreaterR(const PixelFormat *);
TextureFileCreater *CreateTextureFileCreaterRG(const PixelFormat *);
TextureFileCreater *CreateTextureFileCreaterRGB(const PixelFormat *);
TextureFileCreater *CreateTextureFileCreaterRGBA(const PixelFormat *);
TextureFileCreater *CreateTextureFileCreaterCompress(const PixelFormat *);
TextureFileCreater *CreateTFC(const PixelFormat *fmt,const int channels)
{
if(!fmt)return(nullptr);
if(channels<1||channels>4)return(nullptr);
using CTFC_FUNC=TextureFileCreater *(*)(const PixelFormat *);
static CTFC_FUNC CreateTFC[4]=
{
CreateTextureFileCreaterR,
CreateTextureFileCreaterRG,
CreateTextureFileCreaterRGB,
CreateTextureFileCreaterRGBA
};
if(fmt->format<ColorFormat::COMPRESS)
return CreateTFC[channels-1](fmt);
else
return CreateTextureFileCreaterCompress(fmt);
} }

View File

@ -26,9 +26,8 @@ class TextureFileCreater
{ {
protected: protected:
const PixelFormat *pixel_format;
ILImage *image; ILImage *image;
const PixelFormat *pixel_format;
protected: protected:
@ -41,26 +40,19 @@ protected:
public: public:
TextureFileCreater(const PixelFormat *pf,ILImage *); TextureFileCreater(const PixelFormat *pf);
virtual ~TextureFileCreater(); virtual ~TextureFileCreater();
virtual bool WriteFileHeader(const OSString &,const TextureFileType &,const uint); virtual bool CreateTexFile(const OSString &, const TextureFileType &);
virtual bool WriteSize1D(const uint miplevel,const uint length);
virtual bool WriteSize2D(const uint miplevel, const uint width,const uint height);
virtual bool WritePixelFormat();
virtual bool InitFormat()=0; virtual bool InitFormat(ILImage *)=0;
virtual uint32 Write()=0; virtual uint32 Write()=0;
virtual void Close(); virtual void Close();
virtual void Delete(); virtual void Delete();
};//class TextureFileCreater };//class TextureFileCreater
TextureFileCreater *CreateTextureFileCreaterR(const PixelFormat *,ILImage *); TextureFileCreater *CreateTFC(const PixelFormat *,const int channels);
TextureFileCreater *CreateTextureFileCreaterRG(const PixelFormat *,ILImage *);
TextureFileCreater *CreateTextureFileCreaterRGB(const PixelFormat *,ILImage *);
TextureFileCreater *CreateTextureFileCreaterRGBA(const PixelFormat *,ILImage *);
TextureFileCreater *CreateTextureFileCreaterCompress(const PixelFormat *,ILImage *);
using CTFC_FUNC=TextureFileCreater *(*)(const PixelFormat *,ILImage *);
static CTFC_FUNC CreateTFC[4]={CreateTextureFileCreaterR,CreateTextureFileCreaterRG,CreateTextureFileCreaterRGB,CreateTextureFileCreaterRGBA};

View File

@ -27,8 +27,10 @@ public:
public: public:
bool InitFormat() override bool InitFormat(ILImage* img) override
{ {
image = img;
channels=image->channels(); channels=image->channels();
type=image->type(); type=image->type();
@ -255,7 +257,7 @@ public:
} }
};//class TextureFileCreaterCompress:public TextureFileCreater };//class TextureFileCreaterCompress:public TextureFileCreater
TextureFileCreater *CreateTextureFileCreaterCompress(const PixelFormat *pf,ILImage *image) TextureFileCreater *CreateTextureFileCreaterCompress(const PixelFormat *pf)
{ {
return(new TextureFileCreaterCompress(pf,image)); return(new TextureFileCreaterCompress(pf));
} }

View File

@ -12,8 +12,10 @@ public:
public: public:
bool InitFormat() override bool InitFormat(ILImage *img) override
{ {
image=img;
if(!ToILType(type,pixel_format->bits[0],pixel_format->type)) if(!ToILType(type,pixel_format->bits[0],pixel_format->type))
return(false); return(false);
@ -36,7 +38,7 @@ public:
} }
};//class TextureFileCreaterR:public TextureFileCreater };//class TextureFileCreaterR:public TextureFileCreater
TextureFileCreater *CreateTextureFileCreaterR(const PixelFormat *pf,ILImage *image) TextureFileCreater *CreateTextureFileCreaterR(const PixelFormat *pf)
{ {
return(new TextureFileCreaterR(pf,image)); return(new TextureFileCreaterR(pf));
} }

View File

@ -12,8 +12,10 @@ public:
public: public:
bool InitFormat() override bool InitFormat(ILImage *img) override
{ {
image=img;
if(!ToILType(type,pixel_format->bits[0],pixel_format->type)) if(!ToILType(type,pixel_format->bits[0],pixel_format->type))
return(false); return(false);
@ -34,7 +36,7 @@ public:
} }
};//class TextureFileCreaterRG:public TextureFileCreater };//class TextureFileCreaterRG:public TextureFileCreater
TextureFileCreater *CreateTextureFileCreaterRG(const PixelFormat *pf,ILImage *image) TextureFileCreater *CreateTextureFileCreaterRG(const PixelFormat *pf)
{ {
return(new TextureFileCreaterRG(pf,image)); return(new TextureFileCreaterRG(pf));
} }

View File

@ -12,8 +12,10 @@ public:
public: public:
bool InitFormat() override bool InitFormat(ILImage* img) override
{ {
image = img;
if(pixel_format->format==ColorFormat::RGB32U if(pixel_format->format==ColorFormat::RGB32U
||pixel_format->format==ColorFormat::RGB32I ||pixel_format->format==ColorFormat::RGB32I
||pixel_format->format==ColorFormat::RGB32F) ||pixel_format->format==ColorFormat::RGB32F)
@ -114,7 +116,7 @@ public:
} }
};//class TextureFileCreaterRGB:public TextureFileCreater };//class TextureFileCreaterRGB:public TextureFileCreater
TextureFileCreater *CreateTextureFileCreaterRGB(const PixelFormat *pf,ILImage *image) TextureFileCreater *CreateTextureFileCreaterRGB(const PixelFormat *pf)
{ {
return(new TextureFileCreaterRGB(pf,image)); return(new TextureFileCreaterRGB(pf));
} }

View File

@ -9,9 +9,11 @@ class TextureFileCreaterRGBA:public TextureFileCreater
public: public:
using TextureFileCreater::TextureFileCreater; using TextureFileCreater::TextureFileCreater;
bool InitFormat() override bool InitFormat(ILImage* img) override
{ {
image = img;
if(pixel_format->format==ColorFormat::RGBA8 if(pixel_format->format==ColorFormat::RGBA8
||pixel_format->format==ColorFormat::RGBA16 ||pixel_format->format==ColorFormat::RGBA16
||pixel_format->format==ColorFormat::RGBA16U ||pixel_format->format==ColorFormat::RGBA16U
@ -182,7 +184,7 @@ public:
} }
};//class TextureFileCreaterRGB:public TextureFileCreater };//class TextureFileCreaterRGB:public TextureFileCreater
TextureFileCreater *CreateTextureFileCreaterRGBA(const PixelFormat *pf,ILImage *image) TextureFileCreater *CreateTextureFileCreaterRGBA(const PixelFormat *pf)
{ {
return(new TextureFileCreaterRGBA(pf,image)); return(new TextureFileCreaterRGBA(pf));
} }