public H1ModelContext AddModel()
        {
            H1ModelContext model = new H1ModelContext();

            m_Models.Add(model);

            return(model);
        }
        public H1StaticMesh(H1ModelContext context)
        {
            m_StaticMeshData = new H1StaticMeshData(context);

            // @TODO - temporary one unified material
            H1MaterialInterface material = H1Material.DefaultMaterial;

            m_Materials.Add(material);
        }
        public H1StaticMeshData(H1ModelContext context)
        {
            // looping the mesh context in H1ModelContext
            foreach (H1MeshContext meshContext in context.Meshes)
            {
                // @TODO - need to change this after applying quad splitting
                if (meshContext.Indices.Count == 0)
                {
                    continue;
                }

                H1StaticMeshLODResource meshLOD = new H1StaticMeshLODResource(meshContext);
                m_LODResources.Add(meshLOD);
            }
        }
Example #4
0
        public void Initialize(IntPtr windowHandle, int width, int height)
        {
            m_WindowHandle = windowHandle;

            //@TODO - re-ordering this correctly!

            // renderer
            m_Renderer = H1Global <H1ManagedRenderer> .Instance;
            m_Renderer.Initialize(width, height, windowHandle);

            // asset importer (Assimp)
            m_AssimpImporter = H1Global <H1AssimpImporter> .Instance;
            m_AssimpImporter.Initialize();

            // world system
            m_World = H1Global <H1World> .Instance;
            Int32 lvIndex = m_World.AddLevel(new H1Level());
            // set persistent level
            H1Level persistentLevel = m_World.GetLevel(lvIndex);

            m_World.PersistentLevel = persistentLevel;

            H1AssetContext AssetContext = H1Global <H1AssimpImporter> .Instance.asset;

            if (AssetContext != null)
            {
                H1ModelContext ModelContext = H1Global <H1AssimpImporter> .Instance.asset.GetModel(0);

                // create temporary actor
                H1Actor testActor = new H1Actor();
                H1StaticMeshComponent staticMeshComponent = new H1StaticMeshComponent();
                staticMeshComponent.StaticMesh = new H1StaticMesh(ModelContext);
                testActor.AddActorComponent <H1StaticMeshComponent>(staticMeshComponent);

                // create temporary skeletal mesh component
                H1SkeletalMeshComponent skeletalMeshComponent = new H1SkeletalMeshComponent();
                skeletalMeshComponent.SkeletalMesh = new H1SkeletalMesh();
                H1StaticLODModel staticLODModelRef = skeletalMeshComponent.SkeletalMesh.PrepareProcessAssetContext(ModelContext.SkeletalContexts[0]);
                skeletalMeshComponent.SkeletalMesh.ProcessAssetContext(staticLODModelRef, ModelContext.Meshes.ToArray(), ModelContext.SkeletalContexts[0]);
                skeletalMeshComponent.AnimScriptInstance.ProcessAnimationContext(ModelContext.AnimationContext);

                // generate skeletalmeshobject
                skeletalMeshComponent.GenerateSkeleltalMeshObjectGpuSkin();

                testActor.AddActorComponent <H1SkeletalMeshComponent>(skeletalMeshComponent);

                // add the actor to the world
                m_World.PersistentLevel.AddActor(testActor);

                //@TODO - temporary force to order to working
                // after load assets
                m_Renderer.LoadAssets();
            }

            // @TODO - make the global accessor to access the current running app class!
            m_WPFInputManager = H1Global <H1InputManagerWpf> .Instance;

            H1InteractionContext <Window> context = new H1InteractionContext <Window>(Application.Current.MainWindow);

            m_WPFInputManager.Initialize(context);

            // initialize the camera
            m_Camera = new H1Camera();

            // set the camera properties
            Vector3 eye         = new Vector3(0.0f, 0.0f, -10.0f);
            Vector3 lookAtPoint = new Vector3(0.0f, 0.0f, 0.0f);
            Vector3 upVector    = new Vector3(0.0f, 1.0f, 0.0f);
            float   fov         = Convert.ToSingle((Math.PI / 180.0) * 45.0);
            float   nearZ       = 1.0f;
            float   farZ        = 10000.0f;
            float   focusRadius = 1.0f;
            float   aspectRatio = m_Renderer.Width / m_Renderer.Height;

            // set the camera state
            m_Camera.SetState(H1ViewTypes.Perspective, eye, lookAtPoint, upVector, fov, aspectRatio, nearZ, farZ, focusRadius);

            // camera controller
            m_CameraController        = new H1CameraController();
            m_CameraController.Camera = m_Camera;

            m_VisualDebugger = H1Global <H1VisualDebugger> .Instance;

            // task scheduler (fiber-based) c++
            SGDManagedEngineWrapper.H1ManagedTaskSchedulerLayerWrapper.InitializeTaskScheduler();
            SGDManagedEngineWrapper.H1ManagedTaskSchedulerLayerWrapper.StartTaskScheduler();
        }
Example #5
0
        public bool Load(String fileName)
        {
            // @TODO - change the file path for asset folder in the future currently, set as executable path
            String path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "Assets\\");

            m_FileName = path + fileName;

            // setting assimp context
            AssimpContext importer = new AssimpContext();

            importer.SetConfig(new NormalSmoothingAngleConfig(66.0f));

            // loading scene
            Scene scene = null;

            try
            {
                scene = importer.ImportFile(m_FileName, PostProcessPreset.TargetRealTimeMaximumQuality | PostProcessSteps.Triangulate | PostProcessSteps.FlipUVs | PostProcessSteps.FlipWindingOrder);
            }
            catch (Exception e) // handling assimp exception
            {
                if (e.Source != null)
                {
                    Console.WriteLine("Error: {0}", e.Message);
                }
            }

            asset = new H1AssetContext();
            H1ModelContext convertedModel = asset.AddModel();

            // extract the meshes
            if (scene.HasMeshes)
            {
                Int32 meshCount = scene.Meshes.Count;
                List <H1MeshContext> meshContexts = new List <H1MeshContext>();

                ExtractMeshData(scene, ref meshContexts);

                foreach (H1MeshContext meshContext in meshContexts)
                {
                    convertedModel.Meshes.Add(meshContext);
                }
            }

            // extract skeletal data

            // what I learned from this failed algorithm
            // 1. root bone nodes can be multiple
            // 2. for example, blabla_Hub(containing null meshes, only space infos) have null information for weighted vertices, but it should not be considered as leaf node!
            #region Disabled Extracting skeletal mesh data methods

            /*if (scene.HasMeshes)
             * {
             *  // collect all bones from meshes
             *  List<Bone> bones = new List<Bone>();
             *  foreach (Mesh mesh in scene.Meshes)
             *  {
             *      if (mesh.HasBones)
             *      {
             *          bones.AddRange(mesh.Bones);
             *      }
             *  }
             *
             *  // find root node for all meshes
             *  Node boneRootNode = FindRootBone(bones, scene.RootNode);
             *
             *  H1SkeletalContext skeletalContext = null;
             *  skeletalContext = new H1SkeletalContext();
             *  //@TODO - need to optimize for deep copies
             *  ExtractSkeletalMeshData(boneRootNode, bones, ref skeletalContext);
             *
             *  convertedModel.SkeletalContexts.Add(skeletalContext);
             * }*/
            #endregion

            // what I learned from this failed algorithm
            // 1. the counts of bones and bone nodes (joint nodes) can be different
            // 2. bone node can be used for just transforming spaces
            // 3. bone node could have empty meshes (just includes space information)
            // 4. bones are subsidiary for bone nodes
            // 5. don't consider aiBone as bone node in hierarchy rather regards node(aiNode) as bone node in hierarchy
            // 6. consider aiBone as node bone data containing all necessary data
            #region Failed Algorithm
            // new algorithms to handle multiple root node (like pelvis and head) more efficient way

            /*if (scene.HasMeshes)
             * {
             *  // 1. extracted all bones for all meshes in the scene
             *  // NOTICE - multiple meshes in file are considered in same body but chunked several parts
             *  Dictionary<String, Bone> extractedBones = new Dictionary<String, Bone>();
             *  Dictionary<String, H1SkeletalContext.JointNode> jointNodes = new Dictionary<string, H1SkeletalContext.JointNode>();
             *  foreach (Mesh mesh in scene.Meshes)
             *  {
             *      if (mesh.HasBones)
             *      {
             *          foreach (Bone bone in mesh.Bones)
             *          {
             *              String boneName = bone.Name;
             *              if (!extractedBones.ContainsKey(boneName))
             *              {
             *                  extractedBones.Add(boneName, bone);
             *
             *                  H1SkeletalContext.JointNode newJointNode = new H1SkeletalContext.JointNode();
             *                  newJointNode.JointData.JointName = boneName;
             *                  jointNodes.Add(boneName, newJointNode);
             *              }
             *          }
             *      }
             *  }
             *
             *  // 2. process extracted bones
             *  // @TODO - naive algorithm need to optimize by searching tree and set the bones parent
             *  foreach (KeyValuePair<String, Bone> extractedBone in extractedBones)
             *  {
             *      Node node = scene.RootNode.FindNode(extractedBone.Key);
             *      H1SkeletalContext.JointNode jointNode = jointNodes[node.Name];
             *      H1SkeletalContext.JointNode parentJointNode = jointNodes[node.Parent.Name];
             *
             *      // set proper properties
             *      jointNode.Parent = parentJointNode;
             *      foreach (Node child in node.Children)
             *      {
             *          H1SkeletalContext.JointNode childJointNode = null;
             *          if (jointNodes.TryGetValue(child.Name, out childJointNode))
             *          {
             *              jointNode.Children.Add(childJointNode);
             *          }
             *      }
             *      ExtractBone(extractedBone.Value, ref jointNode.JointData);
             *  }
             *
             *  // 3. find root node(s) and set them at the front
             * }*/
            #endregion

            if (scene.HasMeshes)
            {
                H1SkeletalContext skeletalContext = null;
                skeletalContext = new H1SkeletalContext();

                // prepare data for BFS search
                List <H1SkeletalContext.JointNode> jointNodes = skeletalContext.JointNodes;
                Dictionary <String, Int32>         jointNameToJointNodeIndex = new Dictionary <String, Int32>();

                // extractedBones could have same bone name and multiple bone data
                Dictionary <String, List <Bone> > extractedBones = new Dictionary <String, List <Bone> >();
                // boneToMeshContexIndex should be mirrored same as extractedBones (index of list should be same)
                Dictionary <String, List <Int32> > boneToMeshContextIndex = new Dictionary <String, List <Int32> >();

                #region Debug Validation Weight Vertex Counts
#if DEBUG
                List <Boolean> taggedVertexIndex       = new List <Boolean>(); // verification to tag all needed vertices
                List <float>   taggedVertexWeights     = new List <float>();   // verification to tag all vertex weight for sum of each vertex
                List <Int32>   taggedVertexIndexOffset = new List <Int32>();

                // pre-test for validation
                foreach (Mesh mesh in scene.Meshes)
                {
                    if (mesh.HasVertices)
                    {
                        foreach (Vector3D vertex in mesh.Vertices)
                        {
                            taggedVertexIndex.Add(false);
                        }
                    }

                    if (mesh.HasBones)
                    {
                        foreach (Bone bone in mesh.Bones)
                        {
                            if (bone.HasVertexWeights)
                            {
                                foreach (VertexWeight vertexWeight in bone.VertexWeights)
                                {
                                    taggedVertexIndex[vertexWeight.VertexID] = true;
                                }
                            }
                        }
                    }

                    List <Int32> notTaggedVertexIndex0 = new List <Int32>();
                    Int32        currVertexIndex0      = 0;
                    foreach (Boolean isTagged in taggedVertexIndex)
                    {
                        if (isTagged == false)
                        {
                            notTaggedVertexIndex0.Add(currVertexIndex0);
                        }
                        currVertexIndex0++;
                    }

                    if (notTaggedVertexIndex0.Count > 0)
                    {
                        return(false);
                    }

                    taggedVertexIndex.Clear();
                }
#endif
                #endregion

                Int32 validMeshContextIndex = 0;
                foreach (Mesh mesh in scene.Meshes)
                {
                    if (mesh.HasVertices &&
                        mesh.HasNormals &&
                        mesh.HasTangentBasis &&
                        mesh.TextureCoordinateChannels[0].Count > 0 &&
                        mesh.HasFaces) // process vertices in the mesh
                    {
                        if (mesh.HasBones)
                        {
                            foreach (Bone bone in mesh.Bones)
                            {
                                String boneName = bone.Name;
                                if (!extractedBones.ContainsKey(boneName))
                                {
                                    // add new list of bone list
                                    extractedBones.Add(boneName, new List <Bone>());
                                    extractedBones[boneName].Add(bone);
                                    // add new list of mesh context index
                                    boneToMeshContextIndex.Add(boneName, new List <Int32>());
                                    boneToMeshContextIndex[boneName].Add(validMeshContextIndex);
                                }
                                else // same bone name, but different bone data exists
                                {
                                    // no need to create list of data, just add new item
                                    // bone data could have different set of weighted vertices, but with same bone name
                                    extractedBones[boneName].Add(bone);
                                    boneToMeshContextIndex[boneName].Add(validMeshContextIndex);
                                }
                            }

                            validMeshContextIndex++;

                            #region Debug Validation Weight Vertex Count
#if DEBUG
                            taggedVertexIndexOffset.Add(taggedVertexIndex.Count);
                            for (Int32 i = 0; i < mesh.VertexCount; ++i)
                            {
                                taggedVertexIndex.Add(false);
                                taggedVertexWeights.Add(0.0f);
                            }
#endif
                            #endregion
                        }
                    }
                }

                // BFS search to construct bone data
                Stack <Node> nodes = new Stack <Node>();
                nodes.Push(scene.RootNode);

                while (nodes.Count != 0)
                {
                    Node currNode = nodes.Pop();
                    H1SkeletalContext.JointNode jointNode = new H1SkeletalContext.JointNode();

                    H1SkeletalContext.JointNode parentJointNode = jointNodes.Find(x => (x.JointName == currNode.Parent.Name));
                    jointNode.Parent = parentJointNode;
                    ExtractNodeSpace(currNode, ref jointNode);

                    List <Bone>  boneDataList         = null;
                    List <Int32> meshContextIndexList = null;
                    if (extractedBones.TryGetValue(jointNode.JointName, out boneDataList))
                    {
                        meshContextIndexList = boneToMeshContextIndex[jointNode.JointName];

                        // looping bone data list, extract bone data
                        Int32 currBoneIndex = 0;
                        foreach (Bone bone in boneDataList)
                        {
                            H1SkeletalContext.Joint newJointData = new H1SkeletalContext.Joint();
                            ExtractBone(bone, ref newJointData);

                            newJointData.MeshContextIndex = meshContextIndexList[currBoneIndex];
                            currBoneIndex++;

                            // store mesh context local-to-global H1Transform for later transformation of offsetMatrix for animation
                            newJointData.MeshContextLocalToGlobal = convertedModel.Meshes[newJointData.MeshContextIndex].LocalToGlobalTransform;

                            // add new joint data
                            jointNode.JointDataList.Add(newJointData);
                        }

                        // mark this node is bone-space
                        jointNode.MarkedAsBoneSpace = true;

                        // tag its parent until it reaches the state that 'MarkedAsBoneSpace' is true
                        H1SkeletalContext.JointNode markNode = jointNode.Parent;
                        while (markNode != null && markNode.MarkedAsBoneSpace != true)
                        {
                            markNode.MarkedAsBoneSpace = true;
                            markNode = markNode.Parent;
                        }
                    }
                    else
                    {
                        // for debugging
                    }

                    foreach (Node child in currNode.Children)
                    {
                        nodes.Push(child);
                        // tag child names to process child nodes after BFS search
                        jointNode.ChildNodeNames.Add(child.Name);
                    }

                    // add new joint node
                    Int32 newJointNodeIndex = jointNodes.Count;
                    jointNodes.Add(jointNode);
                    jointNameToJointNodeIndex.Add(jointNode.JointName, newJointNodeIndex);
                }

                // process tagged child nodes
                foreach (H1SkeletalContext.JointNode node in jointNodes)
                {
                    foreach (String childName in node.ChildNodeNames)
                    {
                        node.Children.Add(jointNodes[jointNameToJointNodeIndex[childName]]);
                    }
                }

                #region Debug Validation for total weight value of weighted vertices & vertex counts
#if DEBUG
                foreach (H1SkeletalContext.JointNode node in jointNodes)
                {
                    foreach (H1SkeletalContext.Joint jointData in node.JointDataList)
                    {
                        foreach (H1SkeletalContext.WeightedVertex weightedVertex in jointData.WeightedVertices)
                        {
                            // confirm all vertices in Mesh are tagged by weighted vertices
                            taggedVertexIndex[taggedVertexIndexOffset[jointData.MeshContextIndex] + weightedVertex.VertexIndex] = true;
                            // add vertex weight to verify
                            taggedVertexWeights[taggedVertexIndexOffset[jointData.MeshContextIndex] + weightedVertex.VertexIndex] += weightedVertex.Weight;
                        }
                    }
                }

                // verification code to extract not tagged vertex index
                List <Int32> notTaggedVertexIndex = new List <Int32>();
                Int32        currVertexIndex      = 0;
                foreach (Boolean isTagged in taggedVertexIndex)
                {
                    if (isTagged == false)
                    {
                        notTaggedVertexIndex.Add(currVertexIndex);
                    }
                    currVertexIndex++;
                }
                if (notTaggedVertexIndex.Count > 0)
                {
                    return(false);
                }

                // verification code to extract vertex which has invalid vertex weight value
                List <Int32> invalidVertexWeightVertexIndex = new List <Int32>();
                currVertexIndex = 0;
                foreach (float currVertexWeights in taggedVertexWeights)
                {
                    if (currVertexWeights < 0.99f)
                    {
                        invalidVertexWeightVertexIndex.Add(currVertexIndex);
                    }
                }
                if (invalidVertexWeightVertexIndex.Count > 0)
                {
                    return(false);
                }
#endif
                #endregion

                convertedModel.SkeletalContexts.Add(skeletalContext);
            }

            // extract the animations
            if (scene.HasAnimations)
            {
                H1AnimationContext newAnimContext = new H1AnimationContext();
                for (Int32 animIndex = 0; animIndex < scene.AnimationCount; ++animIndex)
                {
                    Animation currAnimation = scene.Animations[animIndex];
                    if (currAnimation.HasNodeAnimations) // we only handle node animations (not vertex animation)
                    {
                        // create new animation sequence
                        H1AnimationContext.AnimSequence newAnimSeq = new H1AnimationContext.AnimSequence();
                        newAnimSeq.AnimSeqName    = currAnimation.Name;
                        newAnimSeq.Duration       = currAnimation.DurationInTicks;
                        newAnimSeq.TicksPerSecond = currAnimation.TicksPerSecond;

                        foreach (NodeAnimationChannel animChannel in currAnimation.NodeAnimationChannels)
                        {
                            H1AnimationContext.JointAnimation newJointAnim = new H1AnimationContext.JointAnimation();
                            newJointAnim.BoneName = animChannel.NodeName;

                            // calculate maximum number of keys to fill up the special case (only holding one key)
                            Int32 maxNumKeys = Math.Max(Math.Max(animChannel.ScalingKeyCount, animChannel.RotationKeyCount), animChannel.PositionKeyCount);

                            if (animChannel.HasPositionKeys)
                            {
                                if (animChannel.PositionKeyCount == 1) // special handling (only holding one key)
                                {
                                    VectorKey positionKey = animChannel.PositionKeys[0];
                                    for (Int32 numKey = 0; numKey < maxNumKeys; ++numKey)
                                    {
                                        H1AnimationContext.PositionKey newPosKey = new H1AnimationContext.PositionKey();
                                        newPosKey.Time  = positionKey.Time;
                                        newPosKey.Value = new Vector3(positionKey.Value.X, positionKey.Value.Y, positionKey.Value.Z);

                                        newJointAnim.PosKeys.Add(newPosKey);
                                    }
                                }

                                else
                                {
                                    foreach (VectorKey positionKey in animChannel.PositionKeys)
                                    {
                                        H1AnimationContext.PositionKey newPosKey = new H1AnimationContext.PositionKey();
                                        newPosKey.Time  = positionKey.Time;
                                        newPosKey.Value = new Vector3(positionKey.Value.X, positionKey.Value.Y, positionKey.Value.Z);

                                        newJointAnim.PosKeys.Add(newPosKey);
                                    }
                                }
                            }

                            if (animChannel.HasRotationKeys)
                            {
                                if (animChannel.RotationKeyCount == 1) // special handling (only holding one key)
                                {
                                    QuaternionKey quatKey = animChannel.RotationKeys[0];
                                    for (Int32 numKey = 0; numKey < maxNumKeys; ++numKey)
                                    {
                                        H1AnimationContext.QuaternionKey newQuatKey = new H1AnimationContext.QuaternionKey();
                                        newQuatKey.Time  = quatKey.Time;
                                        newQuatKey.Value = new SharpDX.Quaternion(quatKey.Value.X, quatKey.Value.Y, quatKey.Value.Z, quatKey.Value.W);

                                        newJointAnim.RotKeys.Add(newQuatKey);
                                    }
                                }

                                else
                                {
                                    foreach (QuaternionKey quatKey in animChannel.RotationKeys)
                                    {
                                        H1AnimationContext.QuaternionKey newQuatKey = new H1AnimationContext.QuaternionKey();
                                        newQuatKey.Time  = quatKey.Time;
                                        newQuatKey.Value = new SharpDX.Quaternion(quatKey.Value.X, quatKey.Value.Y, quatKey.Value.Z, quatKey.Value.W);

                                        newJointAnim.RotKeys.Add(newQuatKey);
                                    }
                                }
                            }

                            if (animChannel.HasScalingKeys)
                            {
                                if (animChannel.ScalingKeyCount == 1) // special handling (only holding one key)
                                {
                                    VectorKey scalingKey = animChannel.ScalingKeys[0];
                                    for (Int32 numKey = 0; numKey < maxNumKeys; ++numKey)
                                    {
                                        H1AnimationContext.ScalingKey newScalingKey = new H1AnimationContext.ScalingKey();
                                        newScalingKey.Time  = scalingKey.Time;
                                        newScalingKey.Value = new Vector3(scalingKey.Value.X, scalingKey.Value.Y, scalingKey.Value.Z);

                                        newJointAnim.ScaleKeys.Add(newScalingKey);
                                    }
                                }

                                else
                                {
                                    foreach (VectorKey scalingKey in animChannel.ScalingKeys)
                                    {
                                        H1AnimationContext.ScalingKey newScalingKey = new H1AnimationContext.ScalingKey();
                                        newScalingKey.Time  = scalingKey.Time;
                                        newScalingKey.Value = new Vector3(scalingKey.Value.X, scalingKey.Value.Y, scalingKey.Value.Z);

                                        newJointAnim.ScaleKeys.Add(newScalingKey);
                                    }
                                }
                            }

                            newAnimSeq.BoneAnimations.Add(newJointAnim);
                        }

                        newAnimContext.AnimSequences.Add(newAnimSeq);
                    }
                }

                // set the animation context
                convertedModel.AnimationContext = newAnimContext;
            }

            return(true);
        }