Mogre Serializer mesh,Skeleton,material,texture,Particle

andyhebear1

16-11-2011 09:55:08

//blog: http://hi.baidu.com/andyhebear
//moge Serializer mesh,Skeleton,material,texture,Particle and gpuprogram
//export the loaded in memory to local disk
private static void ExportOgreSceneResFile() {
//save res
string tempsceneres = SceneTempResCreatePath;
if (System.IO.Directory.Exists(tempsceneres)) {
System.IO.Directory.Delete(tempsceneres, true);
}
System.IO.Directory.CreateDirectory(tempsceneres);
//mesh
Mogre.MeshSerializer meshsr = new Mogre.MeshSerializer();
Mogre.ResourceManager.ResourceMapIterator meshResList = Mogre.MeshManager.Singleton.GetResourceIterator();
foreach (var v in meshResList) {
if (v.IsLoaded) {
try {
string meshname = v.Name.Replace('/', '~');
meshname = meshname.Replace('/', '~');
Mogre.MeshPtr mh = (Mogre.MeshPtr)v;
meshsr.ExportMesh(mh, tempsceneres + meshname);
}
catch (Exception ex) {
int ec = 0;
}
}
}
meshsr.Dispose();
//skeleton
Mogre.SkeletonSerializer skeletonsr = new Mogre.SkeletonSerializer();
Mogre.ResourceManager.ResourceMapIterator skeletonResList = Mogre.SkeletonManager.Singleton.GetResourceIterator();
foreach (var v in skeletonResList) {
if (v.IsLoaded) {
try {
string skeletonname = v.Name.Replace('/', '~');
skeletonname = skeletonname.Replace('/', '~');
Mogre.SkeletonPtr sk = (Mogre.SkeletonPtr)v;
skeletonsr.ExportSkeleton(sk, tempsceneres + skeletonname + ".skeleton");
}
catch (Exception ex) {
int ec = 0;
}
}
}
skeletonsr.Dispose();
//material

List<Mogre.FileInfoListPtr> allmaterialfiles = new List<Mogre.FileInfoListPtr>();
foreach (var g in Mogre.ResourceGroupManager.Singleton.GetResourceGroups()) {
Mogre.FileInfoListPtr flist = Mogre.ResourceGroupManager.Singleton.FindResourceFileInfo(g, "*.material");
allmaterialfiles.Add(flist);
}
Mogre.MaterialSerializer materialsr = new Mogre.MaterialSerializer();
Mogre.ResourceManager.ResourceMapIterator materialResList = Mogre.MaterialManager.Singleton.GetResourceIterator();
foreach (var v in materialResList) {
if (v.IsLoaded) {
try {
string materialname = v.Name.Replace('/', '~');
materialname = materialname.Replace('/', '~');
//materialsr.ExportMaterial(v, tempsceneres + materialname + ".material");
bool find = false;
unsafe {
if (!string.IsNullOrEmpty(v.Origin)) {
foreach (var fl in allmaterialfiles) {
for (int i = 0; i < fl.Count; i++) {
if (fl.filename == v.Origin) {
Mogre.DataStream ds = fl.archive.Open(fl.filename);
uint size = ds.Size();
byte[] _pBuffer = new byte;
System.Runtime.InteropServices.GCHandle _handle = System.Runtime.InteropServices.GCHandle.Alloc(_pBuffer, System.Runtime.InteropServices.GCHandleType.Pinned);
void* _pUnsafeBuffer = (void*)_handle.AddrOfPinnedObject();
ds.Read(_pUnsafeBuffer, size);
//
_handle.Free();
ds.Close();
System.IO.FileStream sfs = new System.IO.FileStream(tempsceneres + v.Origin, System.IO.FileMode.Create, System.IO.FileAccess.Write);
sfs.Write(_pBuffer, 0, _pBuffer.Length);
sfs.Flush();
sfs.Close();
find = true;
goto golable;
}
}
}
golable:
find = false;
}
}
}
catch (Exception ex) {
int ec = 0;
}
}
}
materialsr.Dispose();
//Compositor不支持
//texture
Mogre.ResourceManager.ResourceMapIterator textureResList = Mogre.TextureManager.Singleton.GetResourceIterator();
foreach (var v in textureResList) {
if (v.IsLoaded) {
try {
System.Diagnostics.Debug.Assert(string.IsNullOrEmpty(v.Origin));
string texturename = v.Name.Replace('/', '~');
texturename = texturename.Replace('/', '~');
unsafe {
Mogre.TexturePtr TextureToSave = (Mogre.TexturePtr)v;
//
Mogre.HardwarePixelBufferSharedPtr readbuffer = TextureToSave.GetBuffer(0, 0);
readbuffer.Lock(Mogre.HardwareBuffer.LockOptions.HBL_NORMAL);
Mogre.PixelBox readrefpb = readbuffer.CurrentLock;
byte* readrefdata = (byte*)readrefpb.data;
Mogre.Image img = new Mogre.Image();
img = img.LoadDynamicImage(readrefdata, TextureToSave.Width,
TextureToSave.Height, TextureToSave.Format);
//foreach(var c in Mogre.ImageCodec.GetCodecIterator()){
// Console.WriteLine("type:{0} datatype:{1}", c.Type, c.DataType);
//}
img.Encode("tga");//tga,bmp,dds,gif,ico,jpe,jpeg,jpg,png,raw,tga
img.Save(tempsceneres + texturename);
//Mogre.MemoryDataStream ms = new Mogre.MemoryDataStream(readrefdata, TextureToSave.Size);
//byte[]buf=new byte[TextureToSave.Size];
//System.Runtime.InteropServices.GCHandle _gchandle = System.Runtime.InteropServices.GCHandle.Alloc(buf, System.Runtime.InteropServices.GCHandleType.Pinned);
//void* _gcPtr=(void*)_gchandle.AddrOfPinnedObject();
//ms.Read(_gcPtr, TextureToSave.Size);
//ms.Close();
//readbuffer.Unlock();
//int width = (int)TextureToSave.Width;
//int height = (int)TextureToSave.Height;
//System.Drawing.Bitmap bp = new Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
//System.Drawing.Imaging.BitmapData data = bp.LockBits(new System.Drawing.Rectangle(0, 0, width, height), System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
//System.Runtime.InteropServices.Marshal.Copy(buf, 0, data.Scan0, (width *height *4));
//bp.UnlockBits(data);
//bp.Save(tempsceneres +TextureToSave.Name);
}
}
catch (Exception ex) {
int ec = 0;
}
}
}
//font 不支持
//gpu
List<Mogre.FileInfoListPtr> allgpufiles = new List<Mogre.FileInfoListPtr>();
foreach (var g in Mogre.ResourceGroupManager.Singleton.GetResourceGroups()) {
Mogre.FileInfoListPtr flist = Mogre.ResourceGroupManager.Singleton.FindResourceFileInfo(g, "*.program");
allgpufiles.Add(flist);
}
Mogre.GpuNamedConstantsSerializer gpusr = new Mogre.GpuNamedConstantsSerializer();
Mogre.ResourceManager.ResourceMapIterator gpuResList = Mogre.GpuProgramManager.Singleton.GetResourceIterator();
foreach (var v in gpuResList) {
if (v.IsLoaded) {
if (string.IsNullOrEmpty(v.Origin)) continue;
try {
string filename = v.Name.Replace('/', '~');
filename = filename.Replace('/', '~');
unsafe {
Mogre.GpuProgramPtr gpuToSave = (Mogre.GpuProgramPtr)v;
gpusr.ExportNamedConstants(gpuToSave.NamedConstants, tempsceneres + filename + ".asm");
//gpuToSave.Target.
}
}
catch (Exception ex) {
int ec = 0;
}
}
}
gpusr.Dispose();
//
List<Mogre.FileInfoListPtr> allparticlefiles = new List<Mogre.FileInfoListPtr>();
foreach (var g in Mogre.ResourceGroupManager.Singleton.GetResourceGroups()) {
Mogre.FileInfoListPtr flist = Mogre.ResourceGroupManager.Singleton.FindResourceFileInfo(g, "*.particle");
allparticlefiles.Add(flist);
}
Mogre.ParticleSystemManager.ParticleSystemTemplateIterator particleResList = Mogre.ParticleSystemManager.Singleton.GetTemplateIterator();

unsafe {
foreach (var v in particleResList) {
Console.WriteLine(v.Name + "parent" + (v.ParentSceneNode == null ? "0" : "1"));
//if (v.CullIndividually) continue;
bool find = false;
foreach (var fl in allparticlefiles) {
for (int i = 0; i < fl.Count; i++) {
if (fl.filename == v.Origin) {
Mogre.DataStream ds = fl.archive.Open(fl.filename);
uint size = ds.Size();
byte[] _pBuffer = new byte;
System.Runtime.InteropServices.GCHandle _handle = System.Runtime.InteropServices.GCHandle.Alloc(_pBuffer, System.Runtime.InteropServices.GCHandleType.Pinned);
void* _pUnsafeBuffer = (void*)_handle.AddrOfPinnedObject();
ds.Read(_pUnsafeBuffer, size);
//
_handle.Free();
ds.Close();
System.IO.FileStream sfs = new System.IO.FileStream(tempsceneres + v.Origin, System.IO.FileMode.Create, System.IO.FileAccess.Write);
sfs.Write(_pBuffer, 0, _pBuffer.Length);
sfs.Flush();
sfs.Close();
find = true;
goto golable;
}
}
}
golable:
find = false;

}
}
}


the problem is the GpuNamedConstantsSerializer save the data is not valid

McDonte

16-11-2011 13:58:04

Can you edit your post and put the code into a code box? And just write a small text explaining what you want to say?

andyhebear1

17-11-2011 09:18:16

yes ,i will edit it again

andyhebear1

19-11-2011 01:30:00

how to use Mogre.GpuNamedConstantsSerializer save gpuprogram