protected override void AfterLoadInternal(Stack <ObjectModel> objectModels) { base.AfterLoadInternal(objectModels); PackageObjectModel package = (objectModels.Pop() as PackageObjectModel); ModelObjectModel model = (objectModels.Pop() as ModelObjectModel); }
protected override void BeforeSaveInternal(Stack <ObjectModel> objectModels) { ModelObjectModel model = (objectModels.Pop() as ModelObjectModel); PackageObjectModel package = new PackageObjectModel(); MarkupObjectModel momDocument = CreateDocument(model); File file = package.FileSystem.AddFile("model/document.xml"); file.SetObjectModel <MarkupObjectModel>(new XMLDataFormat(), momDocument); objectModels.Push(package); base.BeforeSaveInternal(objectModels); }
private static void TestOPCModel() { DateTime start = DateTime.Now; ModelObjectModel model = new ModelObjectModel(); UniversalEditor.DataFormats.Multimedia3D.Model.PolygonMovieMaker.PMDModelDataFormat pmd = new DataFormats.Multimedia3D.Model.PolygonMovieMaker.PMDModelDataFormat(); Document.Load(model, pmd, new FileAccessor("/home/beckermj/Documents/UE Tests/Open Packaging Convention/Concertroid Model Data OPC (.pmdx)/test.pmdx_source/model/kio_miku_20111121.pmd")); Document.Save(model, new OPCModelDataFormat(), new FileAccessor("/home/beckermj/Documents/UE Tests/Open Packaging Convention/Concertroid Model Data OPC (.pmdx)/kio_miku.pmdx", true, true)); DateTime end = DateTime.Now; Console.WriteLine("Took " + (end - start).ToString() + " with buffer size " + MemoryAccessor.DefaultBufferAllocationSize); }
public void DrawModel(ModelObjectModel model) { if (model == null) { return; } string basedir = Environment.CurrentDirectory; if (model.Accessor is FileAccessor) { basedir = System.IO.Path.GetDirectoryName((model.Accessor as FileAccessor).FileName); } if (RenderModels) { #region Load Materials if (!model.MaterialsLoaded && !model.MaterialsLoading) { model.MaterialsLoading = true; if (model.Accessor is FileAccessor) { Console.WriteLine("Loading materials for model: \"" + (model.Accessor as FileAccessor).FileName + "\""); } else { Console.WriteLine("Loading materials for model"); } foreach (ModelMaterial mat1 in model.Materials) { Console.Write("Loading material " + model.Materials.IndexOf(mat1).ToString() + " / " + model.Materials.Count.ToString() + "... "); foreach (ModelTexture texture in mat1.Textures) { if (texture.TexturePicture != null) { // Image has already been preloaded if (texture.TextureID == null) { // Store texture ID for this texture Texture t1 = Texture.FromPicture(texture.TexturePicture); texture.TextureID = t1.ID; } } else { if (!String.IsNullOrEmpty(texture.TextureFileName)) { if (texture.Flags != ModelTextureFlags.None) { if (texture.TextureID == null) { // Store texture ID for this texture string textureImageFullFileName = UniversalEditor.Common.Path.MakeAbsolutePath(texture.TextureFileName, basedir); if (!System.IO.File.Exists(textureImageFullFileName)) { Console.WriteLine("texture image not found: " + textureImageFullFileName); continue; } Texture t1 = CreateTextureFromFile(textureImageFullFileName); texture.TextureID = t1.ID; } } } } if ((texture.Flags & (ModelTextureFlags.Map | ModelTextureFlags.AddMap)) != ModelTextureFlags.None) { if (texture.TexturePicture != null) { // Image has already been preloaded if (texture.MapID == null) { // Store texture ID for this texture Texture t1 = Texture.FromPicture(texture.TexturePicture); texture.MapID = t1.ID; } } else { if (!String.IsNullOrEmpty(texture.MapFileName)) { if (texture.Flags != ModelTextureFlags.None) { if (texture.TextureID == null) { // Store texture ID for this texture string textureImageFullFileName = UniversalEditor.Common.Path.MakeAbsolutePath(texture.MapFileName, basedir); if (!System.IO.File.Exists(textureImageFullFileName)) { Console.WriteLine("texture image not found: " + textureImageFullFileName); continue; } Texture t1 = CreateTextureFromFile(textureImageFullFileName); texture.MapID = t1.ID; } } } } } } Console.WriteLine("done!"); } model.MaterialsLoading = false; model.MaterialsLoaded = true; } #endregion int vertexIndex = 0; if (model.Materials.Count > 0) { foreach (ModelMaterial mat in model.Materials) { // update the texture index if (mat.TextureIndex < (mat.Textures.Count - 1)) { mat.TextureIndex++; } else { mat.TextureIndex = 0; } // 輪郭・影有無で色指定方法を変える // Contour - How to specify color change with or without shadow // 半透明でなければポリゴン裏面を無効にする // To disable the reverse side must be semi-transparent polygons /* * if (mat.DiffuseColor.A >= 255) * { * CullingMode = Caltron.CullingMode.Disabled; * } * else * { * CullingMode = Caltron.CullingMode.Front; * } */ // テクスチャ・スフィアマップの処理 // Processing of the texture map sphere ModelTextureFlags fTexFlag = ModelTextureFlags.None; if (mat.TextureIndex > -1 && mat.Textures.Count > 0) { fTexFlag = mat.Textures[mat.TextureIndex].Flags; } if (((fTexFlag & ModelTextureFlags.Texture) == ModelTextureFlags.Texture) && mat.Textures[mat.TextureIndex].TextureID != null) { // テクスチャありならBindする // Bind the texture to be there Texture = Texture.FromID(mat.Textures[mat.TextureIndex].TextureID.Value); EnableTexturing = true; EnableTextureGenerationS = false; EnableTextureGenerationT = false; } else if ((((fTexFlag & ModelTextureFlags.Map) == ModelTextureFlags.Map) || ((fTexFlag & ModelTextureFlags.AddMap) == ModelTextureFlags.AddMap)) && (mat.Textures[mat.TextureIndex].MapID != null)) { // スフィアマップありならBindする // Bind sphere map, if it exists // Texture = Texture.FromID(mat.MapID.Value); Texture = Texture.FromID(mat.Textures[mat.TextureIndex].MapID.Value); EnableTexturing = false; EnableTextureGenerationS = true; EnableTextureGenerationT = true; } else { // テクスチャもスフィアマップもなし // A texture map sphere without any EnableTexturing = false; EnableTextureGenerationS = false; EnableTextureGenerationT = false; } if (!mat.AlwaysLight && (mat.EdgeFlag || model.IgnoreEdgeFlag)) { // 輪郭・影有りのときは照明を有効にする // Contour - When the shadow is there to enable the lighting SetMaterialParameter(FaceName.Both, MaterialParameterName.Diffuse, new float[] { (float)mat.DiffuseColor.R, (float)mat.DiffuseColor.G, (float)mat.DiffuseColor.B, (float)mat.DiffuseColor.A }); SetMaterialParameter(FaceName.Both, MaterialParameterName.Ambient, new float[] { (float)mat.AmbientColor.R, (float)mat.AmbientColor.G, (float)mat.AmbientColor.B, (float)mat.AmbientColor.A }); SetMaterialParameter(FaceName.Both, MaterialParameterName.Specular, new float[] { (float)mat.SpecularColor.R, (float)mat.SpecularColor.G, (float)mat.SpecularColor.B, (float)mat.SpecularColor.A }); SetMaterialParameter(FaceName.Both, MaterialParameterName.Shininess, (float)mat.Shininess); EnableLighting = true; } else { // 輪郭・影無しのときは照明を無効にする // Contour - When you disable the lighting without shadows Color = Color.FromRGBASingle((float)((mat.AmbientColor.R + mat.DiffuseColor.R)), (float)((mat.AmbientColor.G + mat.DiffuseColor.G)), (float)((mat.AmbientColor.B + mat.DiffuseColor.B)), (float)((mat.AmbientColor.A + mat.DiffuseColor.A))); EnableLighting = false; } // 頂点インデックスを指定してポリゴン描画 // Specifies the index vertex polygon drawing Begin(RenderMode.Triangles); foreach (ModelTriangle tri in mat.Triangles) { DrawTriangle(tri); } End(); } } } else { EnableLighting = false; EnableTexturing = false; Color = Color.FromRGBADouble(1, 1, 1, 1); for (int i = 0; i < model.Surfaces[0].Vertices.Count; i += 3) { DrawTriangle(new ModelTriangle(model.Surfaces[0].Vertices[i], model.Surfaces[0].Vertices[i + 1], model.Surfaces[0].Vertices[i + 2])); } } if (RenderBones) { EnableCulling = false; // Internal.OpenGL.Methods.glFrontFace(Internal.OpenGL.Constants.GLFaceOrientation.Clockwise); // Internal.OpenGL.Methods.glCullFace(Internal.OpenGL.Constants.GL_BACK); EnableTexturing = false; foreach (ModelBone bone in model.Bones) { Matrix.Push(); float[] ary = bone.Position.ToFloatArray(); Matrix.Multiply(ary); Color = Color.FromRGBADouble(1.0f, 0.0f, 1.0f, 1.0f); //glutSolidCube( 0.3f ); float fSize = 0.3f; Begin(RenderMode.Quads); DrawVertex(-fSize / 2.0f, fSize / 2.0f, -fSize / 2.0f); DrawVertex(fSize / 2.0f, fSize / 2.0f, -fSize / 2.0f); DrawVertex(fSize / 2.0f, -fSize / 2.0f, -fSize / 2.0f); DrawVertex(-fSize / 2.0f, -fSize / 2.0f, -fSize / 2.0f); DrawVertex(fSize / 2.0f, fSize / 2.0f, -fSize / 2.0f); DrawVertex(fSize / 2.0f, fSize / 2.0f, fSize / 2.0f); DrawVertex(fSize / 2.0f, -fSize / 2.0f, fSize / 2.0f); DrawVertex(fSize / 2.0f, -fSize / 2.0f, -fSize / 2.0f); DrawVertex(fSize / 2.0f, fSize / 2.0f, fSize / 2.0f); DrawVertex(-fSize / 2.0f, fSize / 2.0f, fSize / 2.0f); DrawVertex(-fSize / 2.0f, -fSize / 2.0f, fSize / 2.0f); DrawVertex(fSize / 2.0f, -fSize / 2.0f, fSize / 2.0f); DrawVertex(fSize / 2.0f, fSize / 2.0f, -fSize / 2.0f); DrawVertex(fSize / 2.0f, fSize / 2.0f, fSize / 2.0f); DrawVertex(fSize / 2.0f, -fSize / 2.0f, fSize / 2.0f); DrawVertex(fSize / 2.0f, -fSize / 2.0f, -fSize / 2.0f); End(); Matrix.Pop(); Matrix.Push(); Color = Color.FromRGBADouble(1.0f, 1.0f, 1.0f, 1.0f); if (bone.ParentBone != null) { Begin(RenderMode.Lines); DrawVertex(bone.ParentBone.Position.ToFloatArray()); DrawVertex(bone.Position.ToFloatArray()); End(); } Matrix.Pop(); } } }
private MarkupObjectModel CreateDocument(ModelObjectModel model) { MarkupObjectModel momDocument = new MarkupObjectModel(); MarkupTagElement tagModel = new MarkupTagElement(); tagModel.FullName = "cr:model"; tagModel.Attributes.Add("xmlns:cr", "urn:net.alcetech.schemas.Concertroid.OPC.Model"); MarkupTagElement tagSettings = new MarkupTagElement(); tagSettings.FullName = "cr:settings"; MarkupTagElement tagSettingIgnoreEdgeFlag = new MarkupTagElement(); tagSettingIgnoreEdgeFlag.FullName = "cr:setting"; tagSettingIgnoreEdgeFlag.Attributes.Add("name", "ignoreEdgeFlag"); tagSettingIgnoreEdgeFlag.Attributes.Add("value", model.IgnoreEdgeFlag ? "true" : "false"); tagModel.Elements.Add(tagSettings); MarkupTagElement tagBones = new MarkupTagElement(); tagBones.FullName = "cr:bones"; foreach (ModelBone bone in model.Bones) { MarkupTagElement tagBone = new MarkupTagElement(); tagBone.FullName = "cr:bone"; tagBone.Attributes.Add("id", "b" + model.Bones.IndexOf(bone).ToString()); MarkupTagElement tagAngleLimit = new MarkupTagElement(); tagAngleLimit.FullName = "cr:angleLimit"; tagAngleLimit.Attributes.Add("enabled", bone.AngleLimit.Enabled ? "true" : "false"); tagAngleLimit.Elements.Add(bone.AngleLimit.Lower.ToXML("cr:lower")); tagAngleLimit.Elements.Add(bone.AngleLimit.Upper.ToXML("cr:upper")); tagBone.Elements.Add(tagAngleLimit); tagBone.Attributes.Add("type", BoneTypeToXML(bone.BoneType)); tagBone.Attributes.Add("childId", "b" + model.Bones.IndexOf(bone.ChildBone).ToString()); tagBone.Attributes.Add("ikNumber", bone.IKNumber.ToString()); tagBone.Attributes.Add("name", bone.Name); tagBone.Attributes.Add("parentId", "b" + model.Bones.IndexOf(bone.ParentBone).ToString()); tagBone.Elements.Add(bone.Position.ToXML("cr:position")); tagBone.Elements.Add(bone.Rotation.ToXML("cr:rotation")); tagBone.Elements.Add(bone.Vector3Offset.ToXML("cr:offset")); tagBones.Elements.Add(tagBone); } tagModel.Elements.Add(tagBones); MarkupTagElement tagExpressions = new MarkupTagElement(); tagExpressions.FullName = "cr:expressions"; foreach (ushort u in model.Expressions) { MarkupTagElement tagExpression = new MarkupTagElement(); tagExpression.FullName = "cr:expression"; tagExpression.Attributes.Add("value", u.ToString()); tagExpressions.Elements.Add(tagExpression); } tagModel.Elements.Add(tagExpressions); MarkupTagElement tagJoints = new MarkupTagElement(); tagJoints.FullName = "cr:joints"; foreach (ModelJoint joint in model.Joints) { MarkupTagElement tagJoint = new MarkupTagElement(); tagJoint.FullName = "cr:joint"; MarkupTagElement tagLimits = new MarkupTagElement(); tagLimits.FullName = "cr:limits"; tagLimits.Elements.Add(joint.LimitAngleHigh.ToXML("cr:angleHigh")); tagLimits.Elements.Add(joint.LimitAngleLow.ToXML("cr:angleLow")); tagLimits.Elements.Add(joint.LimitMoveHigh.ToXML("cr:moveHigh")); tagLimits.Elements.Add(joint.LimitMoveLow.ToXML("cr:moveLow")); tagJoint.Elements.Add(tagLimits); tagJoint.Attributes.Add("name", joint.Name); tagJoint.Elements.Add(joint.Position.ToXML("cr:position")); tagJoint.Elements.Add(joint.Rotation.ToXML("cr:rotation")); MarkupTagElement tagSpringConstraint = new MarkupTagElement(); tagSpringConstraint.FullName = "cr:springConstraint"; tagSpringConstraint.Elements.Add(joint.SpringConstraintMovementStiffness.ToXML("cr:movementStiffness")); tagSpringConstraint.Elements.Add(joint.SpringConstraintRotationStiffness.ToXML("cr:rotationStiffness")); tagJoint.Elements.Add(tagSpringConstraint); tagJoints.Elements.Add(tagJoint); } tagModel.Elements.Add(tagJoints); MarkupTagElement tagIKHandles = new MarkupTagElement(); tagIKHandles.FullName = "cr:ikHandles"; foreach (ModelIK ik in model.IK) { MarkupTagElement tagIKHandle = new MarkupTagElement(); tagIKHandle.FullName = "cr:ikHandle"; tagIKHandle.Attributes.Add("effectedBoneId", model.Bones.IndexOf(ik.EffBone).ToString()); tagIKHandle.Attributes.Add("index", ik.Index.ToString()); tagIKHandle.Attributes.Add("limitOnce", ik.LimitOnce.ToString()); tagIKHandle.Attributes.Add("loopCount", ik.LoopCount.ToString()); tagIKHandle.Attributes.Add("targetBoneId", model.Bones.IndexOf(ik.TargetBone).ToString()); MarkupTagElement tagBoneList = new MarkupTagElement(); tagBoneList.FullName = "cr:boneList"; foreach (ModelBone bone in ik.BoneList) { MarkupTagElement tagBone = new MarkupTagElement(); tagBone.FullName = "cr:boneReference"; tagBone.Attributes.Add("boneId", model.Bones.IndexOf(bone).ToString()); tagBoneList.Elements.Add(tagBone); } tagIKHandle.Elements.Add(tagBoneList); } tagModel.Elements.Add(tagIKHandles); MarkupTagElement tagSurfaces = new MarkupTagElement(); tagSurfaces.FullName = "cr:surfaces"; foreach (ModelSurface surf in model.Surfaces) { MarkupTagElement tagSurface = new MarkupTagElement(); tagSurface.FullName = "cr:surface"; MarkupTagElement tagTriangles = new MarkupTagElement(); tagTriangles.FullName = "cr:triangles"; foreach (ModelTriangle tri in surf.Triangles) { MarkupTagElement tagTriangle = new MarkupTagElement(); tagTriangle.FullName = "cr:triangle"; tagTriangle.Elements.Add(tri.Vertex1.ToXML("cr:vertex")); tagTriangle.Elements.Add(tri.Vertex2.ToXML("cr:vertex")); tagTriangle.Elements.Add(tri.Vertex3.ToXML("cr:vertex")); tagTriangles.Elements.Add(tagTriangle); } tagSurface.Elements.Add(tagTriangles); MarkupTagElement tagVertices = new MarkupTagElement(); tagVertices.FullName = "cr:vertices"; foreach (ModelVertex vtx in surf.Vertices) { tagVertices.Elements.Add(vtx.ToXML("cr:vertex")); } tagSurface.Elements.Add(tagVertices); tagSurfaces.Elements.Add(tagSurface); } tagModel.Elements.Add(tagSurfaces); MarkupTagElement tagRigidBodies = new MarkupTagElement(); tagRigidBodies.FullName = "cr:rigidBodies"; foreach (ModelRigidBody rb in model.RigidBodies) { MarkupTagElement tagRigidBody = new MarkupTagElement(); tagRigidBody.FullName = "cr:rigidBody"; tagRigidBody.Attributes.Add("boneId", "b" + model.Bones.IndexOf(rb.Bone).ToString()); tagRigidBody.Elements.Add(rb.BoxSize.ToXML("cr:boxSize")); tagRigidBody.Attributes.Add("boxType", rb.BoxType.ToString()); tagRigidBody.Attributes.Add("friction", rb.Friction.ToString()); tagRigidBody.Attributes.Add("groupId", rb.GroupID.ToString()); tagRigidBody.Attributes.Add("itype", rb.IType.ToString()); tagRigidBody.Attributes.Add("mass", rb.Mass.ToString()); tagRigidBody.Attributes.Add("mode", rb.Mode.ToString()); tagRigidBody.Attributes.Add("name", rb.Name); tagRigidBody.Elements.Add(rb.Position.ToXML("cr:position")); tagRigidBody.Attributes.Add("positionDamping", rb.PositionDamping.ToString()); tagRigidBody.Attributes.Add("restitution", rb.Restitution.ToString()); tagRigidBody.Elements.Add(rb.Rotation.ToXML("cr:rotation")); tagRigidBody.Attributes.Add("rotationDamping", rb.RotationDamping.ToString()); tagRigidBodies.Elements.Add(tagRigidBody); } tagModel.Elements.Add(tagRigidBodies); momDocument.Elements.Add(tagModel); return(momDocument); }