Esempio n. 1
0
        /// <summary>
        /// Retrieves either an attribute table for a mesh, or the number of entries stored in an attribute table for a mesh.
        /// </summary>
        /// <remarks>
        /// An attribute table is used to identify areas of the mesh that need to be drawn with different textures, render states, materials, and so on. In addition, the application can use the attribute table to hide portions of a mesh by not drawing a given attribute identifier when drawing the frame.
        /// </remarks>
        /// <returns>Returns an array of <see cref="MeshAttributeRange"/> structures, representing the entries in the mesh's attribute table. </returns>
        /// <unmanaged>HRESULT ID3DX10Mesh::GetAttributeTable([Out, Buffer, Optional] D3DX10_ATTRIBUTE_RANGE* pAttribTable,[None] int* pAttribTableSize)</unmanaged>
        public MeshAttributeRange[] GetAttributeTable()
        {
            int sizeAttributeTable = 0;

            GetAttributeTable(null, ref sizeAttributeTable);
            var temp = new MeshAttributeRange[sizeAttributeTable];

            GetAttributeTable(temp, ref sizeAttributeTable);
            return(temp);
        }
        public static void RenderMesh2(Model m, Vector3 position, string effectName, string technique, Matrix mWorld, int stride)
        {
            if (m == null || m.MeshObj == null)
            {
                return;
            }
            Shader e     = WorldData.GetObject(effectName) as Shader;
            Matrix world = Matrix.Identity + Matrix.Translation(position);

            if (mWorld != null)
            {
                world = mWorld;
            }
            ShaderHelper.UpdateCommonEffectVars(e, world);

            EffectTechnique t = e.EffectObj.GetTechniqueByName(technique);

            D3D10.Buffer indexBuffer   = m.MeshObj.GetDeviceIndexBuffer();
            D3D10.Buffer vertextBuffer = m.MeshObj.GetDeviceVertexBuffer(0);

            Game.Device.InputAssembler.SetPrimitiveTopology(PrimitiveTopology.TriangleList);
            Game.Device.InputAssembler.SetIndexBuffer(indexBuffer, Format.R16_UInt, 0);
            Game.Device.InputAssembler.SetVertexBuffers(0, new VertexBufferBinding(vertextBuffer, stride, 0));

            for (int p = 0; p < t.Description.PassCount; p++)
            {
                EffectPass  pass = t.GetPassByIndex(p);
                InputLayout l    = ShaderHelper.ConstructInputLayout(m.Mesh3d.inputElements, pass.Description.Signature);
                Game.Device.InputAssembler.SetInputLayout(l);

                for (int subset = 0; subset < m.Mesh3d.NumAttributes; subset++)
                {
                    EffectResourceVariable diffTex = e.GetVar(ShaderHelper.DiffTex).AsResource();
                    if (diffTex != null)
                    {
                        diffTex.SetResource(m.Mesh3d.textureViews[subset]);
                    }
                    pass.Apply();
                    MeshAttributeRange r = m.Mesh3d.attrTable[subset];
                    // * 2 cause adj data is twice as much data
                    //Game.Device.DrawIndexed((r.FaceCount * 3) * 2, (r.FaceStart * 3) * 2, 0);

                    Game.Device.DrawIndexed((r.FaceCount * 3), (r.FaceStart * 3), 0);
                }
            }

            indexBuffer.Dispose();
            vertextBuffer.Dispose();
        }
Esempio n. 3
0
        internal static Mesh3D LoadFromFileSUB(Device device, Assimp.Scene pScene, InputElement[] ieLayout, List <SceneObject> meshArr)
        {
            string sourceTextures = ConfigurationSettings.AppSettings["SourceTextures"];

            int iVBufferSize = 0;
            int iIBufferSize = 0;

            foreach (SceneObject so in meshArr)
            {
                iVBufferSize += (int)so.pMesh.VertexCount;
                iIBufferSize += (int)so.pMesh.FaceCount * 3;
            }


            MeshInputElements10.sNormalMesh[] tVertex = new MeshInputElements10.sNormalMesh[iVBufferSize];
            //short[] tIndex = new short[iIBufferSize];
            uint[] tIndex = new uint[iIBufferSize];
            MeshAttributeRange[] tAttibutes = new MeshAttributeRange[meshArr.Count];
            MeshNode[]           nodes      = new MeshNode[meshArr.Count];
            string[]             tTextures  = new string[meshArr.Count];

            // Monitor global poisition in the vertex, index and attribute buffers.
            int iAttribute    = 0;
            int iBVertex      = 0;
            int iBIndex       = 0;
            int prevVertCount = 0;

            foreach (SceneObject so in meshArr)
            {
                MeshNode n = new MeshNode();
                n.attributeId     = iAttribute;
                n.nodeName        = so.nodeName;
                n.parentNodeName  = so.parentNodeName;
                n.transform       = toDXMat(so.transform);
                nodes[iAttribute] = n;


                MeshAttributeRange pAttrib = new MeshAttributeRange();
                pAttrib.Id          = iAttribute;
                pAttrib.VertexStart = iBVertex;
                pAttrib.FaceStart   = iBIndex / 3;

                Assimp.Material mat = pScene.Materials[so.pMesh.MaterialIndex];
                if (mat.GetTextureCount(Assimp.TextureType.Diffuse) > 0)
                {
                    string inputFilePath = mat.GetTexture(Assimp.TextureType.Diffuse, 0).FilePath.Replace("\\\\", "\\");
                    string inputFileName = Path.GetFileName(inputFilePath);
                    string ext           = Path.GetExtension(inputFileName);
                    string subFolder     = inputFilePath.Replace(sourceTextures, "").Split('\\')[1];
                    string diffOutFile   = subFolder + "\\" + inputFileName.Replace(ext, ".dds");
                    tTextures[iAttribute] = diffOutFile;
                }

                // Copy verticies.
                int iMeshVerts = (int)so.pMesh.VertexCount;
                for (int iVertex = 0; iVertex < iMeshVerts; ++iVertex)
                {
                    tVertex[iVertex + iBVertex] = getVert(so, iVertex);
                }

                // Increment the vertex count by the number of verticies we just looped over.
                iBVertex += iMeshVerts;

                // Copy indicies.
                int iMeshFaces = (int)so.pMesh.FaceCount;
                for (int iFace = 0; iFace < iMeshFaces; ++iFace)
                {
                    uint[] tIndices = so.pMesh.Faces[iFace].Indices;
                    //tIndex[iBIndex++] = Convert.ToInt16(tIndices[0] + (iAttribute == 0 ? 0 : prevVertCount));
                    //tIndex[iBIndex++] = Convert.ToInt16(tIndices[1] + (iAttribute == 0 ? 0 : prevVertCount));
                    //tIndex[iBIndex++] = Convert.ToInt16(tIndices[2] + (iAttribute == 0 ? 0 : prevVertCount));
                    tIndex[iBIndex++] = Convert.ToUInt32(tIndices[0] + (iAttribute == 0 ? 0 : prevVertCount));
                    tIndex[iBIndex++] = Convert.ToUInt32(tIndices[1] + (iAttribute == 0 ? 0 : prevVertCount));
                    tIndex[iBIndex++] = Convert.ToUInt32(tIndices[2] + (iAttribute == 0 ? 0 : prevVertCount));
                }

                // Increment the face count by the number of faces we just looped over.
                prevVertCount += iMeshVerts;


                pAttrib.FaceCount      = iMeshFaces;
                pAttrib.VertexCount    = iMeshVerts;
                tAttibutes[iAttribute] = pAttrib;
                iAttribute++;
            }

            Mesh3D gm     = new Mesh3D();
            Mesh   output = new Mesh(device, ieLayout, ieLayout[0].SemanticName, iVBufferSize, iIBufferSize / 3, MeshFlags.Has32BitIndices);

            output.SetAttributeTable(tAttibutes);

            DataStream verts = new DataStream(iVBufferSize * Marshal.SizeOf(typeof(MeshInputElements10.sNormalMesh)), true, true);
            //DataStream indicies = new DataStream(iIBufferSize * Marshal.SizeOf(typeof(short)), true, true);
            DataStream indicies = new DataStream(iIBufferSize * Marshal.SizeOf(typeof(uint)), true, true);

            verts.WriteRange(tVertex);
            indicies.WriteRange(tIndex);


            verts.Position    = 0;
            indicies.Position = 0;

            output.SetVertexData(0, verts);
            output.SetIndexData(indicies, iIBufferSize);
            output.Commit();
            verts.Dispose();
            indicies.Dispose();

            gm.attrTable     = tAttibutes;
            gm.meshObj       = output;
            gm.NumAttributes = meshArr.Count;
            gm.materials     = tTextures;
            gm.nodes         = nodes;

            if (pScene.HasAnimations)
            {
                gm.animations = new List <Mesh3DAnimation>();
                foreach (Assimp.Animation a in pScene.Animations)
                {
                    Mesh3DAnimation anim = new Mesh3DAnimation();
                    anim.animName = a.Name;
                    anim.duration = a.DurationInTicks;
                    anim.channels = new AnimationChannel[a.NodeAnimationChannelCount];

                    int x = 0;
                    foreach (Assimp.NodeAnimationChannel c in a.NodeAnimationChannels)
                    {
                        if (c == null)
                        {
                            continue;
                        }

                        AnimationChannel ac = new AnimationChannel();
                        ac.nodeName = c.NodeName;
                        ac.animKeys = new AnimationKey[c.RotationKeyCount];
                        for (int i = 0; i < c.PositionKeys.Length; i++)
                        {
                            double      time = 0;
                            SceneObject sObj = meshArr[0];
                            foreach (SceneObject so in meshArr)
                            {
                                if (so.nodeName == c.NodeName)
                                {
                                    sObj = so;
                                    break;
                                }
                            }

                            time = c.PositionKeys[i].Time;
                            Matrix  mat            = toDXMat(sObj.transform);
                            Vector3 rotationCenter = new Vector3(mat.M41, mat.M42, mat.M43);
                            mat.Invert();
                            Vector3 pos = Vector3.TransformCoordinate(toDX(c.PositionKeys[i].Value), mat);


                            Matrix matAnim = Matrix.Transformation(Vector3.Zero, Quaternion.Identity, new Vector3(1, 1, 1), rotationCenter, toDX(c.RotationKeys[i].Value), pos);


                            AnimationKey ak = new AnimationKey();
                            ak.animMat     = matAnim;
                            ak.time        = time;
                            ac.animKeys[i] = ak;
                        }

                        anim.channels[x++] = ac;
                    }

                    gm.animations.Add(anim);
                }
            }



            return(gm);
        }