1- using Blacksmith . Three ;
1+ using Blacksmith . Enums ;
2+ using Blacksmith . Three ;
23using OpenTK ;
34using System ;
45using System . Collections . Generic ;
@@ -193,8 +194,6 @@ public static Model LoadFromString(string obj)
193194 texs . Add ( new Vector2 ( ) ) ;
194195 normals . Add ( new Vector3 ( ) ) ;
195196
196- //int currentindice = 0;
197-
198197 // Read file line by line
199198 foreach ( string line in lines )
200199 {
@@ -304,8 +303,6 @@ public static Model LoadFromString(string obj)
304303 success |= int . TryParse ( faceparts [ 1 ] . Split ( '/' ) [ 0 ] , out v2 ) ;
305304 success |= int . TryParse ( faceparts [ 2 ] . Split ( '/' ) [ 0 ] , out v3 ) ;
306305
307- faceIndices . Add ( new Tuple < int , int , int > ( v1 , v2 , v3 ) ) ;
308-
309306 if ( faceparts [ 0 ] . Count ( ( char c ) => c == '/' ) >= 2 )
310307 {
311308 success |= int . TryParse ( faceparts [ 0 ] . Split ( '/' ) [ 1 ] , out t1 ) ;
@@ -351,6 +348,8 @@ public static Model LoadFromString(string obj)
351348 }
352349 else
353350 {
351+ faceIndices . Add ( new Tuple < int , int , int > ( v1 , v2 , v3 ) ) ;
352+
354353 TempVertex tv1 = new TempVertex ( v1 , n1 , t1 ) ;
355354 TempVertex tv2 = new TempVertex ( v2 , n2 , t2 ) ;
356355 TempVertex tv3 = new TempVertex ( v3 , n3 , t3 ) ;
@@ -364,37 +363,33 @@ public static Model LoadFromString(string obj)
364363 }
365364 }
366365 }
367-
368- Model mdl = new Model ( ) ;
366+
369367 Mesh mesh = new Mesh ( ) ;
370- for ( int i = 0 ; i < verts . Count ; i += 3 )
368+ for ( int i = 0 ; i < faces . Count ; i += 3 )
371369 {
372370 var face = faces [ i ] ;
373- FaceVertex v1 = new FaceVertex ( verts [ face . Item1 . Vertex ] , normals [ face . Item1 . Normal ] , texs [ face . Item1 . Texcoord ] ) ;
374- FaceVertex v2 = new FaceVertex ( verts [ face . Item2 . Vertex ] , normals [ face . Item2 . Normal ] , texs [ face . Item2 . Texcoord ] ) ;
375- FaceVertex v3 = new FaceVertex ( verts [ face . Item3 . Vertex ] , normals [ face . Item3 . Normal ] , texs [ face . Item3 . Texcoord ] ) ;
376371
377372 Mesh . Vertex vert1 = new Mesh . Vertex
378373 {
379- Position = v1 . Position ,
380- Normal = v1 . Normal ,
381- TextureCoordinate = v1 . TextureCoord
374+ Position = verts [ face . Item1 . Vertex ] ,
375+ Normal = normals [ face . Item1 . Normal ] ,
376+ TextureCoordinate = texs [ face . Item1 . Texcoord ]
382377 } ;
383378 mesh . Vertices . Add ( vert1 ) ;
384379
385380 Mesh . Vertex vert2 = new Mesh . Vertex
386381 {
387- Position = v2 . Position ,
388- Normal = v2 . Normal ,
389- TextureCoordinate = v2 . TextureCoord
382+ Position = verts [ face . Item2 . Vertex ] ,
383+ Normal = normals [ face . Item2 . Normal ] ,
384+ TextureCoordinate = texs [ face . Item2 . Texcoord ]
390385 } ;
391386 mesh . Vertices . Add ( vert2 ) ;
392387
393388 Mesh . Vertex vert3 = new Mesh . Vertex
394389 {
395- Position = v3 . Position ,
396- Normal = v3 . Normal ,
397- TextureCoordinate = v3 . TextureCoord
390+ Position = verts [ face . Item3 . Vertex ] ,
391+ Normal = normals [ face . Item3 . Normal ] ,
392+ TextureCoordinate = texs [ face . Item3 . Texcoord ]
398393 } ;
399394 mesh . Vertices . Add ( vert3 ) ;
400395
@@ -416,30 +411,41 @@ public static Model LoadFromString(string obj)
416411 mesh . VertexCount = mesh . Vertices . Count ;
417412 mesh . FaceCount = mesh . Faces . Count ;
418413 mesh . IndexCount = mesh . Indices . Count ;
419- mdl . Meshes . Add ( mesh ) ;
420414
421- return mdl ;
415+ return Model . CreateFromMesh ( mesh ) ;
422416 }
423-
424- public static string Export ( Model model , bool calculateNormals = false , bool exportingIndividually = false )
417+
418+ public static string Export ( Model model , NormalExportMode normalMode , bool exportingIndividually = false )
425419 {
426420 StringBuilder obj = new StringBuilder ( ) ;
427421 for ( int i = 0 ; i < model . Meshes . Count ; i ++ )
428422 {
423+ for ( int j = 0 ; j < model . Meshes [ i ] . Vertices . Count ; j ++ )
424+ {
425+ Mesh . Vertex v = model . Meshes [ i ] . Vertices [ j ] ;
426+ obj . AppendLine ( $ "v { v . Position . X } { v . Position . Y } { v . Position . Z } ") ;
427+ }
428+
429429 for ( int j = 0 ; j < model . Meshes [ i ] . Vertices . Count ; j ++ )
430430 {
431431 Mesh . Vertex v = model . Meshes [ i ] . Vertices [ j ] ;
432432 Vector3 n = v . Normal ;
433- if ( calculateNormals )
433+
434+ if ( normalMode == NormalExportMode . CALCULATED )
434435 n = model . Meshes [ i ] . CalculatedNormals [ j ] ;
435436
436- obj . AppendLine ( $ "v { v . Position . X } { v . Position . Y } { v . Position . Z } ") ;
437- obj . AppendLine ( $ "vn { n . X } { n . Y } { n . Z } ") ;
437+ if ( normalMode != NormalExportMode . NONE )
438+ obj . AppendLine ( $ "vn { n . X } { n . Y } { n . Z } ") ;
439+ }
440+
441+ for ( int j = 0 ; j < model . Meshes [ i ] . Vertices . Count ; j ++ )
442+ {
443+ Mesh . Vertex v = model . Meshes [ i ] . Vertices [ j ] ;
438444 obj . AppendLine ( $ "vt { v . TextureCoordinate . X } { v . TextureCoordinate . Y } ") ;
439445 }
440446
441447 obj . AppendLine ( $ "g mesh{ i } ") ;
442- for ( int j = 0 ; j < model . Meshes [ i ] . FaceCount ; j ++ )
448+ for ( int j = 0 ; j < model . Meshes [ i ] . Faces . Count ; j ++ )
443449 {
444450 Mesh . Face f = model . Meshes [ i ] . Faces [ j ] ;
445451 if ( exportingIndividually )
@@ -448,9 +454,17 @@ public static string Export(Model model, bool calculateNormals = false, bool exp
448454 f . Y -= model . Meshes [ i ] . NumOfVerticesBeforeMe ;
449455 f . Z -= model . Meshes [ i ] . NumOfVerticesBeforeMe ;
450456 }
451- string x = $ "{ f . X + 1 } /{ f . X + 1 } /{ f . X + 1 } ";
452- string y = $ "{ f . Y + 1 } /{ f . Y + 1 } /{ f . Y + 1 } ";
453- string z = $ "{ f . Z + 1 } /{ f . Z + 1 } /{ f . Z + 1 } ";
457+
458+ string x = $ "{ f . X + 1 } /{ f . X + 1 } ";
459+ string y = $ "{ f . Y + 1 } /{ f . Y + 1 } ";
460+ string z = $ "{ f . Z + 1 } /{ f . Z + 1 } ";
461+ if ( normalMode != NormalExportMode . NONE )
462+ {
463+ x += f . X + 1 ;
464+ y += f . Y + 1 ;
465+ z += f . Z + 1 ;
466+ }
467+
454468 obj . AppendLine ( $ "f { x } { y } { z } ") ;
455469 }
456470 }
0 commit comments