1+ package format.collada
2+
3+ import main.BaseImporter
4+ import java.net.URI
5+ import main.AiScene
6+ import main.AiMesh
7+ import main.AiMaterial
8+ import main.*
9+
10+ /* *
11+ * Created by elect on 23/01/2017.
12+ */
13+
14+ class ColladaLoader : BaseImporter () {
15+
16+ companion object {
17+
18+ val desc = AiImporterDesc (
19+ mName = " Collada Importer" ,
20+ mComments = " http://collada.org" ,
21+ mFlags = main.AiImporterFlags .SupportTextFlavour .i,
22+ mMinMajor = 1 , mMinMinor = 3 ,
23+ mMaxMajor = 1 , mMaxMinor = 5 ,
24+ mFileExtensions = " dae"
25+ )
26+ }
27+
28+ /* * Filename, for a verbose error message */
29+ lateinit var mFileName: URI
30+
31+ /* * Which mesh-material compound was stored under which mesh ID */
32+ val mMeshIndexByID = mutableMapOf<ColladaMeshIndex , Int >()
33+
34+ /* * Which material was stored under which index in the scene */
35+ val mMaterialIndexByName = mutableMapOf<String , Int >()
36+
37+ /* * Accumulated meshes for the target scene */
38+ val mMeshes = ArrayList <AiMesh >()
39+
40+ /* * Temporary material list */
41+ val newMats = ArrayList <Pair <Effect , AiMaterial >>()
42+
43+ /* * Temporary camera list */
44+ val mCameras = ArrayList <AiCamera >()
45+
46+ /* * Temporary light list */
47+ val mLights = ArrayList <AiLightSourceType >()
48+
49+ /* * Temporary texture list */
50+ val mTextures = ArrayList <AiTexture >()
51+
52+ /* * Accumulated animations for the target scene */
53+ // val mAnims = ArrayList<AiAnimation>()
54+
55+ val noSkeletonMesh = false
56+ val ignoreUpDirection = false
57+
58+ /* * Used by FindNameForNode() to generate unique node names */
59+ val mNodeNameCounter = 0
60+
61+ // ------------------------------------------------------------------------------------------------
62+ // Returns whether the class can handle the format of the given file.
63+ override fun canRead (pFile : URI , checkSig : Boolean ): Boolean {
64+
65+ // check file extension
66+ val extension = pFile.s.substring(pFile.s.lastIndexOf(' .' ) + 1 )
67+
68+ if (extension == " dae" )
69+ return true
70+
71+ // XML - too generic, we need to open the file and search for typical keywords
72+ if (extension == " xml" || extension.isEmpty() || checkSig) {
73+ // TODO
74+ }
75+ return false
76+ }
77+
78+ // ------------------------------------------------------------------------------------------------
79+ // Imports the given file into the given scene structure.
80+ override fun internReadFile (pFile : URI , pScene : AiScene ) {
81+
82+ mFileName = pFile
83+
84+ // parse the input file
85+ val parser = ColladaParser (pFile)
86+
87+ if (parser.mRootNode == null )
88+ throw Error (" Collada: File came out empty. Something is wrong here." )
89+
90+ // create the materials first, for the meshes to find
91+ buildMaterials(parser)
92+ }
93+
94+ /* * Constructs materials from the collada material definitions */
95+ protected fun buildMaterials (pParser : ColladaParser ) = pParser.mMaterialLibrary.forEach { id, material ->
96+
97+ // a material is only a reference to an effect
98+ pParser.mEffectLibrary[material.mEffect]?.let { effect ->
99+
100+ // create material
101+ val mat = AiMaterial (name = if (material.mName.isEmpty()) id else material.mName)
102+
103+ // store the material
104+ // mMaterialIndexByName[matIt->first] = newMats.size();
105+ // newMats.push_back(std::pair < Collada::Effect *, aiMaterial * >(& effect, mat) );
106+ }
107+ }
108+
109+
110+ class ColladaMeshIndex (
111+
112+ var mMeshID : String = " " ,
113+ var mSubMesh : Int = 0 ,
114+ var mMaterial : String = " " ) {
115+
116+ operator fun compareTo (p : ColladaMeshIndex ): Int =
117+ if (mMeshID == p.mMeshID)
118+ if (mSubMesh == p.mSubMesh)
119+ mMaterial.compareTo(p.mMaterial)
120+ else
121+ mSubMesh.compareTo(p.mSubMesh)
122+ else
123+ mMeshID.compareTo(p.mMeshID)
124+ }
125+ }
0 commit comments