-
Notifications
You must be signed in to change notification settings - Fork 6
/
GLLItem+OBJExport.m
63 lines (50 loc) · 1.88 KB
/
GLLItem+OBJExport.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
//
// GLLItem+OBJExport.m
// GLLara
//
// Created by Torsten Kammer on 19.09.12.
// Copyright (c) 2012 Torsten Kammer. All rights reserved.
//
#import "GLLItem+OBJExport.h"
#import "NSArray+Map.h"
#import "GLLItemBone.h"
#import "GLLara-Swift.h"
@implementation GLLItem (OBJExport)
- (BOOL)willLoseDataWhenConvertedToOBJ
{
return [self.meshes firstObjectMatching:^(id mesh) {
return [mesh willLoseDataWhenConvertedToOBJ];
}] != nil;
}
- (BOOL)writeOBJToLocation:(NSURL *)location withTransform:(BOOL)transform withColor:(BOOL)color error:(NSError *__autoreleasing*)error;
{
NSMutableString *obj = [NSMutableString string];
NSString *materialLibraryName = [[location.lastPathComponent stringByDeletingPathExtension] stringByAppendingPathExtension:@"mtl"];
[obj appendFormat:@"mtllib %@\n", materialLibraryName];
mat_float16 *transforms = malloc(sizeof(mat_float16) * self.bones.count);
NSUInteger boneIndex = 0;
for (GLLItemBone *bone in self.bones)
{
if (transform)
transforms[boneIndex] = bone.globalTransform;
else
transforms[boneIndex] = bone.bone.positionMatrix;
boneIndex += 1;
}
uint32_t indexOffset = 0;
for (GLLItemMesh *mesh in self.meshes)
{
[obj appendString:[mesh writeOBJWithTransformations:transforms baseIndex:indexOffset includeColors:color]];
indexOffset += mesh.mesh.countOfVertices;
}
free(transforms);
return [obj writeToURL:location atomically:YES encoding:NSUTF8StringEncoding error:error];
}
- (BOOL)writeMTLToLocation:(NSURL *)location error:(NSError *__autoreleasing*)error;
{
NSString *mtl = [[self.meshes map:^(GLLItemMesh *mesh) {
return [mesh writeMTLWithBaseURL:location];
}] componentsJoinedByString:@"\n"];
return [mtl writeToURL:location atomically:YES encoding:NSUTF8StringEncoding error:error];
}
@end