Skip to content

Commit

Permalink
Completely Swiftify Model
Browse files Browse the repository at this point in the history
  • Loading branch information
cochrane committed May 2, 2024
1 parent 1906f82 commit 5df4711
Show file tree
Hide file tree
Showing 15 changed files with 162 additions and 237 deletions.
10 changes: 4 additions & 6 deletions GLLara.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@
525ACD5F15F0F1A700534E7D /* GLLDocument.m in Sources */ = {isa = PBXBuildFile; fileRef = 525ACD5E15F0F1A700534E7D /* GLLDocument.m */; };
525ACD6E15F0F1A700534E7D /* Cocoa.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 525ACD4B15F0F1A700534E7D /* Cocoa.framework */; };
525ACD7615F0F1A700534E7D /* InfoPlist.strings in Resources */ = {isa = PBXBuildFile; fileRef = 525ACD7415F0F1A700534E7D /* InfoPlist.strings */; };
525ACD8415F0F33700534E7D /* GLLModel.m in Sources */ = {isa = PBXBuildFile; fileRef = 525ACD8315F0F33700534E7D /* GLLModel.m */; };
525ACD8415F0F33700534E7D /* GLLModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 525ACD8315F0F33700534E7D /* GLLModel.swift */; };
525BF29B2879E95800E30D48 /* GLLAppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 525BF29A2879E95800E30D48 /* GLLAppDelegate.swift */; };
525BF29D287A0AE200E30D48 /* GLLView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 525BF29C287A0AE200E30D48 /* GLLView.swift */; };
52653D8116BAB729001D802F /* GLLPoseExporter.m in Sources */ = {isa = PBXBuildFile; fileRef = 52653D8016BAB729001D802F /* GLLPoseExporter.m */; };
Expand Down Expand Up @@ -639,8 +639,7 @@
525ACD6B15F0F1A700534E7D /* GLLaraTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = GLLaraTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
525ACD7315F0F1A700534E7D /* GLLaraTests-Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = "GLLaraTests-Info.plist"; sourceTree = "<group>"; };
525ACD7515F0F1A700534E7D /* en */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = en; path = en.lproj/InfoPlist.strings; sourceTree = "<group>"; };
525ACD8215F0F33700534E7D /* GLLModel.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = GLLModel.h; sourceTree = "<group>"; };
525ACD8315F0F33700534E7D /* GLLModel.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GLLModel.m; sourceTree = "<group>"; };
525ACD8315F0F33700534E7D /* GLLModel.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = GLLModel.swift; sourceTree = "<group>"; };
525BF29A2879E95800E30D48 /* GLLAppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GLLAppDelegate.swift; sourceTree = "<group>"; };
525BF29C287A0AE200E30D48 /* GLLView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GLLView.swift; sourceTree = "<group>"; };
5260CE9C163B16450069459D /* GLLSceneModel 2.xcdatamodel */ = {isa = PBXFileReference; lastKnownFileType = wrapper.xcdatamodel; path = "GLLSceneModel 2.xcdatamodel"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1330,8 +1329,7 @@
isa = PBXGroup;
children = (
52D8DDDD2621CFF10006F0E5 /* GLLMeshSplitter.swift */,
525ACD8215F0F33700534E7D /* GLLModel.h */,
525ACD8315F0F33700534E7D /* GLLModel.m */,
525ACD8315F0F33700534E7D /* GLLModel.swift */,
52C9F6151600022B003272E1 /* GLLModelObj.swift */,
52D8DDBD261E06F40006F0E5 /* GLLModelGltf.swift */,
52D8DDBC261E06F30006F0E5 /* GLLara-Bridging-Header.h */,
Expand Down Expand Up @@ -1869,7 +1867,7 @@
52FB1FD82879846A006ABC4F /* DepthBufferCheck.metal in Sources */,
52301E2D2087D91600B3E331 /* GLLOptionalPartController.m in Sources */,
52D8DDEC2621D56F0006F0E5 /* GLLRenderParameterDescription.swift in Sources */,
525ACD8415F0F33700534E7D /* GLLModel.m in Sources */,
525ACD8415F0F33700534E7D /* GLLModel.swift in Sources */,
5274448428031CA900E5A3FD /* GLLMeshDrawData.swift in Sources */,
529692CC15F1568200DF2FA3 /* TRInDataStream.m in Sources */,
529692CD15F1568200DF2FA3 /* TROutDataStream.m in Sources */,
Expand Down
5 changes: 2 additions & 3 deletions GLLara/GLLDocument.m
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
#import "GLLItemMeshTexture.h"
#import "GLLItem+OBJExport.h"
#import "GLLLogarithmicValueTransformer.h"
#import "GLLModel.h"
#import "GLLPoseExporter.h"
#import "GLLRenderWindowController.h"
#import "GLLSelection.h"
Expand Down Expand Up @@ -145,7 +144,7 @@ - (BOOL)configurePersistentStoreCoordinatorForURL:(NSURL *)url ofType:(NSString

- (GLLItem *)addModelAtURL:(NSURL *)url error:(NSError *__autoreleasing*)error;
{
GLLModel *model = [GLLModel cachedModelFromFile:url parent:nil error:error];
GLLModel *model = [GLLModel cachedModelFrom:url parent:nil error:error];
if (!model) return nil;

return [self addModel:model];
Expand Down Expand Up @@ -177,7 +176,7 @@ - (GLLItem *)addImagePlane:(NSURL *)url error:(NSError *__autoreleasing*)error {

// Load default model
NSURL *xyAlignedPlaneURL = [[NSBundle mainBundle] URLForResource:@"XYAlignedSquare" withExtension:@"obj" subdirectory:nil];
GLLModel *model = [GLLModel cachedModelFromFile:xyAlignedPlaneURL parent:nil error:error];
GLLModel *model = [GLLModel cachedModelFrom:xyAlignedPlaneURL parent:nil error:error];
if (!model) {
// Should never happen
return nil;
Expand Down
1 change: 0 additions & 1 deletion GLLara/GLLDocumentWindowController.m
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
#import "GLLItemViewController.h"
#import "GLLLightsListController.h"
#import "GLLMeshViewController.h"
#import "GLLModel.h"
#import "GLLOptionalPartViewController.h"
#import "GLLSelection.h"
#import "GLLSettingsListController.h"
Expand Down
3 changes: 1 addition & 2 deletions GLLara/GLLItem.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

#import "GLLItemBone.h"
#import "GLLItemMesh.h"
#import "GLLModel.h"
#import "simd_matrix.h"
#import "TRInDataStream.h"
#import "TROutDataStream.h"
Expand Down Expand Up @@ -133,7 +132,7 @@ - (void)awakeFromFetch
if (itemURL)
{
NSError *error = nil;
GLLModel *model = [GLLModel cachedModelFromFile:itemURL parent:self.parent.model error:&error];
GLLModel *model = [GLLModel cachedModelFrom:itemURL parent:self.parent.model error:&error];
if (!model) {
NSLog(@"Could not load model due to %@", error);
}
Expand Down
1 change: 0 additions & 1 deletion GLLara/GLLItemBone.m
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
#import "GLLItemBone.h"

#import "GLLItem.h"
#import "GLLModel.h"
#import "simd_matrix.h"
#import "TRInDataStream.h"
#import "TROutDataStream.h"
Expand Down
1 change: 0 additions & 1 deletion GLLara/GLLItemMesh.m
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

#import "GLLItem.h"
#import "GLLItemMeshTexture.h"
#import "GLLModel.h"
#import "GLLRenderParameter.h"
#import "NSArray+Map.h"

Expand Down
4 changes: 2 additions & 2 deletions GLLara/GLLItemViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#import "GLLItem.h"
#import "GLLItemMesh.h"
#import "GLLItemMeshTexture.h"
#import "GLLModel.h"
#import "GLLara-Swift.h"

@interface GLLItemViewController ()

Expand Down Expand Up @@ -79,7 +79,7 @@ - (IBAction)loadChildModel:(id)sender;
if (result != NSModalResponseOK) return;

NSError *error = nil;
GLLModel *model = [GLLModel cachedModelFromFile:panel.URL parent:item.model error:&error];
GLLModel *model = [GLLModel cachedModelFrom:panel.URL parent:item.model error:&error];

if (!model)
{
Expand Down
53 changes: 0 additions & 53 deletions GLLara/GLLModel.h

This file was deleted.

112 changes: 0 additions & 112 deletions GLLara/GLLModel.m

This file was deleted.

98 changes: 98 additions & 0 deletions GLLara/GLLModel.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
//
// GLLModel.m
// GLLara
//
// Created by Torsten Kammer on 31.08.12.
// Copyright (c) 2012 Torsten Kammer. All rights reserved.
//

import Foundation
import UniformTypeIdentifiers

let GLLModelLoadingErrorDomain = "GLL Model loading error domain";

enum GLLModelLoadingErrorCode: Int {
case prematureEndOfFile
case indexOutOfRange
case circularReference
case fileTypeNotSupported
case parametersNotFound
}

/**
* # A renderable object.
*
* A GLLModel corresponds to one mesh file (which actually contains many meshes; this is a bit confusing) and describes its graphics contexts. It contains some default transformations, but does not store poses and the like.
*/
@objc class GLLModel: NSObject {
@objc var bones: [GLLModelBone] = []
@objc var meshes: [GLLModelMesh] = []

static let cachedModels = NSCache<NSString, GLLModel>()

/**
* # Returns a model with a given URL, returning a cached instance if one exists.
*
* Since a model is immutable here, it can be shared as much as necessary. This method uses an internal cache to share objects. Note that a model can be evicted from this cache again, if nobody is using it.
*/
@objc static func cachedModel(from file: URL, parent: GLLModel? = nil) throws -> GLLModel {
var key = file.absoluteString
if let parent {
key += "\n\(parent.baseURL.absoluteString)"
}
if let result = cachedModels.object(forKey: key as NSString) {
return result
}

if file.pathExtension == "mesh" || file.pathExtension == "xps" {
let model = try GLLModelXNALara(binaryFromFile: file, parent: parent)
cachedModels.setObject(model, forKey: key as NSString)
return model
} else if file.lastPathComponent.hasSuffix(".mesh.ascii") {
let model = try GLLModelXNALara(ASCIIFromFile: file, parent: parent)
cachedModels.setObject(model, forKey: key as NSString)
return model
} else if file.pathExtension == "obj" {
let model = try GLLModelObj(contentsOf: file)
cachedModels.setObject(model, forKey: key as NSString)
return model
} else if file.pathExtension == ".gltf" {
let model = try GLLModelGltf(url: file, isBinary: false)
cachedModels.setObject(model, forKey: key as NSString)
return model
} else if file.pathExtension == ".glb" {
let model = try GLLModelGltf(url: file, isBinary: true)
cachedModels.setObject(model, forKey: key as NSString)
return model
} else {
// Find display name for this extension
let contentType = try file.resourceValues(forKeys: [.contentTypeKey]).contentType
let fileTypeDescription = contentType?.localizedDescription ?? file.pathExtension

throw NSError(domain: GLLModelLoadingErrorDomain, code: GLLModelLoadingErrorCode.fileTypeNotSupported.rawValue, userInfo: [
NSLocalizedDescriptionKey : String(format:NSLocalizedString("Files of type %@ are not supported.", comment: "Tried to load unsupported format"), fileTypeDescription),
NSLocalizedRecoverySuggestionErrorKey : NSLocalizedString("Only .mesh, .mesh.ascii and .obj files can be loaded.", comment: "Tried to load unsupported format")
])

}
}

@objc var baseURL: URL! = nil
@objc var parameters: GLLModelParams! = nil

@objc var hasBones: Bool {
return bones.count > 0
}

@objc var rootBones: [GLLModelBone] {
return bones.filter({ bone in bone.parent == nil } as (GLLModelBone)->Bool)
}

@objc var cameraTargetNames: [GLLCameraTargetDescription] {
return parameters.cameraTargets
}

@objc func bone(name: String) -> GLLModelBone? {
return bones.first { $0.name == name }
}
}

0 comments on commit 5df4711

Please sign in to comment.