11package main.obj
22
3- import com.sun.org.apache.xpath.internal.operations.Mod
43import main.*
5- import main.AiVector3D
64import java.io.File
7- import java.io.RandomAccessFile
8- import java.nio.ByteOrder
9- import java.nio.channels.FileChannel
105import java.nio.file.FileSystemException
116import java.util.*
7+ import kotlin.collections.ArrayList
128
139/* *
1410 * Created by elect on 21/11/2016.
@@ -51,14 +47,8 @@ class ObjFileImporter : BaseImporter() {
5147
5248 if (fileSize < ObjMinSize ) throw Error (" OBJ-file is too small." )
5349
54- // Allocate buffer and read file into it
55- val streamedBuffer = file.readLines()
56-
57- // Get the model name
58- val modelName = file.name
59-
6050 // parse the file into a temporary representation
61- val parser = ObjFileParser (streamedBuffer, modelName )
51+ val parser = ObjFileParser (file )
6252
6353 // And create the proper return structures out of it
6454 createDataFromImport(parser.m_pModel, pScene)
@@ -188,26 +178,22 @@ class ObjFileImporter : BaseImporter() {
188178
189179 // Copy all data from all stored meshes
190180 pObjMesh.m_Faces.forEach {
191- val face = AiFace ()
181+ val face = ArrayList < Int > ()
192182 when (it.m_PrimitiveType) {
193183 AiPrimitiveType .LINE -> for (i in 0 until it.m_vertices.size - 1 ) {
194- face.mNumIndices = 2
195- uiIdxCount + = face.mNumIndices
196- face.mIndices = IntArray (2 ).toMutableList()
197- continue
184+ val mNumIndices = 2
185+ uiIdxCount + = mNumIndices
186+ repeat(mNumIndices, { face.add(0 ) })
198187 }
199188 AiPrimitiveType .POINT -> for (i in 0 until it.m_vertices.size) {
200- face.mNumIndices = 1
201- uiIdxCount + = face.mNumIndices
202- face.mIndices = IntArray (1 ).toMutableList()
203- continue
189+ val mNumIndices = 1
190+ uiIdxCount + = mNumIndices
191+ repeat(mNumIndices, { face.add(0 ) })
204192 }
205193 else -> {
206194 val uiNumIndices = it.m_vertices.size
207- face.mNumIndices = uiNumIndices
208195 uiIdxCount + = uiNumIndices
209- if (face.mNumIndices > 0 )
210- face.mIndices = IntArray (uiNumIndices).toMutableList()
196+ repeat(uiNumIndices, { face.add(0 ) })
211197 }
212198 }
213199 faces.add(face)
@@ -239,15 +225,15 @@ class ObjFileImporter : BaseImporter() {
239225 else if (pMesh.mNumVertices > AI_MAX_ALLOC (main.AiVector3D .SIZE ))
240226 throw Error (" OBJ: Too many vertices, would run out of memory" )
241227
242- pMesh.mVertices = Array < AiVector3D > (pMesh.mNumVertices, { AiVector3D () }).toMutableList()
228+ pMesh.mVertices = Array (pMesh.mNumVertices, { AiVector3D () }).toMutableList()
243229
244230 // Allocate buffer for normal vectors
245231 if (pModel.m_Normals.isNotEmpty() && pObjMesh.m_hasNormals)
246- pMesh.mNormals = ArrayList (pMesh.mNumVertices)
232+ pMesh.mNormals = Array (pMesh.mNumVertices, { AiVector3D () }).toMutableList( )
247233
248234 // Allocate buffer for vertex-color vectors
249235 if (pModel.m_VertexColors.isNotEmpty())
250- pMesh.mColors[0 ] = Array (pMesh.mNumVertices, { AiColor4D () })
236+ pMesh.mColors[0 ] = Array (pMesh.mNumVertices, { AiColor4D () }).toMutableList()
251237
252238 // Allocate buffer for texture coordinates
253239 if (pModel.m_TextureCoord.isNotEmpty() && pObjMesh.m_uiUVCoordinates[0 ] != 0 ) {
@@ -270,7 +256,7 @@ class ObjFileImporter : BaseImporter() {
270256 pMesh.mVertices[newIndex] = pModel.m_Vertices[vertex]
271257
272258 // Copy all normals
273- if (pModel.m_Normals.isNotEmpty() && vertexIndex in it.m_normals) {
259+ if (pModel.m_Normals.isNotEmpty() && vertexIndex in it.m_normals.indices ) {
274260 val normal = it.m_normals[vertexIndex]
275261 if (normal >= pModel.m_Normals.size)
276262 throw Error (" OBJ: vertex normal index out of range" )
@@ -298,7 +284,7 @@ class ObjFileImporter : BaseImporter() {
298284
299285 val last = (vertexIndex == it.m_vertices.size - 1 )
300286 if (it.m_PrimitiveType != AiPrimitiveType .LINE || ! last) {
301- pDestFace.mIndices [outVertexIndex] = newIndex
287+ pDestFace[outVertexIndex] = newIndex
302288 outVertexIndex++
303289 }
304290
@@ -384,10 +370,11 @@ class ObjFileImporter : BaseImporter() {
384370 mat.refracti = pCurrentMaterial.ior
385371
386372 // Adding textures
387- if ( pCurrentMaterial.texture.isNotEmpty()) {
373+ pCurrentMaterial.textures.firstOrNull { it.type == Material . Texture . Type .diffuse }?. let {
388374
389- mat.textures!! .add(AiMaterialTexture (path = pCurrentMaterial.texture ))
375+ mat.textures!! .add(AiMaterialTexture (type = AiTextureType .diffuse ))
390376 }
377+
391378 // TODO
392379
393380 // Store material property info in material array in scene
0 commit comments