Skip to content

Commit

Permalink
Draw glTF files (without materials)
Browse files Browse the repository at this point in the history
Initial support for rendering glTF 1.0 files! Very, very limited so far, but that doesn't have to remain the case. In particular:

- No materials whatsoever, even the default material is not implemented correctly.
- Only triangles, no other types of primitive
- No binary file (*.glb) support
- No glTF 2.0 support whatsoever, trying to load those files will probably fail in funny ways
- No support for files without indices
- Probably many more

This is the first major step for issue #122.
  • Loading branch information
cochrane committed Apr 8, 2021
1 parent 3b55dbb commit 762f5e2
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 8 deletions.
4 changes: 4 additions & 0 deletions GLLara.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,7 @@
52D8DDAB261D01BD0006F0E5 /* GLLVertexAttribAccessor.m in Sources */ = {isa = PBXBuildFile; fileRef = 52D8DDAA261D01BD0006F0E5 /* GLLVertexAttribAccessor.m */; };
52D8DDB0261D9FD30006F0E5 /* GLLVertexAttribAccessorSet.m in Sources */ = {isa = PBXBuildFile; fileRef = 52D8DDAF261D9FD30006F0E5 /* GLLVertexAttribAccessorSet.m */; };
52D8DDBE261E06F40006F0E5 /* GLLModelGltf.swift in Sources */ = {isa = PBXBuildFile; fileRef = 52D8DDBD261E06F40006F0E5 /* GLLModelGltf.swift */; };
52D8DDD6261F79E70006F0E5 /* gltfFile.modelparams.plist in Resources */ = {isa = PBXBuildFile; fileRef = 52D8DDD5261F79E70006F0E5 /* gltfFile.modelparams.plist */; };
52DD80F3163B464200534475 /* GLLRenderingTest.m in Sources */ = {isa = PBXBuildFile; fileRef = 52DD80F2163B464200534475 /* GLLRenderingTest.m */; };
52ED9E1E168399AE0082EABB /* GLLSelection.m in Sources */ = {isa = PBXBuildFile; fileRef = 52ED9E1D168399AE0082EABB /* GLLSelection.m */; };
52ED9E26169332540082EABB /* GLLMeshController.m in Sources */ = {isa = PBXBuildFile; fileRef = 52ED9E25169332540082EABB /* GLLMeshController.m */; };
Expand Down Expand Up @@ -873,6 +874,7 @@
52D8DDAF261D9FD30006F0E5 /* GLLVertexAttribAccessorSet.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = GLLVertexAttribAccessorSet.m; sourceTree = "<group>"; };
52D8DDBC261E06F30006F0E5 /* GLLara-Bridging-Header.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "GLLara-Bridging-Header.h"; sourceTree = "<group>"; };
52D8DDBD261E06F40006F0E5 /* GLLModelGltf.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GLLModelGltf.swift; sourceTree = "<group>"; };
52D8DDD5261F79E70006F0E5 /* gltfFile.modelparams.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = gltfFile.modelparams.plist; sourceTree = "<group>"; };
52DD80F1163B464200534475 /* GLLRenderingTest.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = GLLRenderingTest.h; sourceTree = "<group>"; };
52DD80F2163B464200534475 /* GLLRenderingTest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GLLRenderingTest.m; sourceTree = "<group>"; };
52DD80F6163B58E400534475 /* GLLExportASCIITest.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = GLLExportASCIITest.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1369,6 +1371,7 @@
523DE1121604E04300BB9F61 /* yeti_thrall.modelparams.plist */,
523DE1111604E04200BB9F61 /* yeti_thrall_nochains.modelparams.plist */,
523DE10D1604E00700BB9F61 /* zip.modelparams.plist */,
52D8DDD5261F79E70006F0E5 /* gltfFile.modelparams.plist */,
);
path = "Model Parameters";
sourceTree = "<group>";
Expand Down Expand Up @@ -1851,6 +1854,7 @@
52423B2F1604EBFE009F5753 /* lara_snow_light.modelparams.plist in Resources */,
52423B301604EBFE009F5753 /* lara_wetsuit_nogear.modelparams.plist in Resources */,
52423B311604EBFE009F5753 /* lara_wetsuit.modelparams.plist in Resources */,
52D8DDD6261F79E70006F0E5 /* gltfFile.modelparams.plist in Resources */,
52423B471604EC5B009F5753 /* gol_brute_minion.modelparams.plist in Resources */,
52423B481604EC5B009F5753 /* gol_chompy.modelparams.plist in Resources */,
52423B491604EC5B009F5753 /* gol_doppelganger.modelparams.plist in Resources */,
Expand Down
10 changes: 8 additions & 2 deletions GLLara/GLLModelGltf.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ struct Buffer: Codable {
}

struct Material: Codable {

var name: String?
}

struct Mesh: Codable {
Expand Down Expand Up @@ -209,6 +209,8 @@ class GLLModelGltf: GLLModel {

super.init()

self.parameters = try GLLModelParams.parameters(forName: "gltfFile")

self.baseURL = baseUrl
self.bones = []
self.meshes = []
Expand Down Expand Up @@ -305,12 +307,13 @@ class GLLModelGltf: GLLModel {
mesh.name = keyAndMesh.key
mesh.displayName = mesh.name
mesh.textures = []
mesh.shader = self.parameters.shaderNamed("DefaultMaterial")
mesh.countOfVertices = UInt(countOfVertices)
mesh.countOfUVLayers = UInt(uvLayers.count)
mesh.vertexDataAccessors = GLLVertexAttribAccessorSet(accessors: accessors)

let elements = try loadData.getUnboundAccessor(for: primitive.indices ?? "this mesh does not have indices we should probably add support for that ya know")
mesh.elementData = elements.view.buffer.data
mesh.elementData = elements.view.buffer.data.subdata(in: elements.view.range)
if ![5120, 5121, 5122, 5123, 5124, 5125, 5126].contains(elements.accessor.componentType) {
throw NSError()
}
Expand All @@ -323,6 +326,9 @@ class GLLModelGltf: GLLModel {
}
}
}

// Set up the one and only bone we have for now
self.bones = [GLLModelBone(model: self)]
}

}
1 change: 1 addition & 0 deletions GLLara/GLLModelMesh.m
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ - (id)initAsPartOfModel:(GLLModel *)model;

_model = model;
_elementComponentType = GllVertexAttribComponentTypeUnsignedInt;
_initiallyVisible = YES;

return self;
}
Expand Down
8 changes: 4 additions & 4 deletions GLLara/GLLModelParams.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@
*/
@interface GLLModelParams : NSObject

+ (id)parametersForModel:(GLLModel *)model error:(NSError *__autoreleasing*)error;
+ (id)parametersForName:(NSString *)name error:(NSError *__autoreleasing*)error;
+ (GLLModelParams *)parametersForModel:(GLLModel *)model error:(NSError *__autoreleasing*)error;
+ (GLLModelParams *)parametersForName:(NSString *)name error:(NSError *__autoreleasing*)error;

- (id)initWithPlist:(NSDictionary *)propertyList error:(NSError *__autoreleasing*)error;
- (instancetype)initWithPlist:(NSDictionary *)propertyList error:(NSError *__autoreleasing*)error;

// Generic item format
- (id)initWithModel:(GLLModel *)aModel error:(NSError *__autoreleasing*)error;
- (instancetype)initWithModel:(GLLModel *)aModel error:(NSError *__autoreleasing*)error;

@property (nonatomic, copy, readonly) NSString *modelName;
@property (nonatomic, copy, readonly) GLLModelParams *base;
Expand Down
4 changes: 2 additions & 2 deletions GLLara/GLLModelParams.m
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ + (void)initialize
parameterCache = [[NSCache alloc] init];
}

+ (id)parametersForModel:(GLLModel *)model error:(NSError *__autoreleasing *)error;
+ (GLLModelParams *)parametersForModel:(GLLModel *)model error:(NSError *__autoreleasing *)error;
{
// Check whether it is an XPS file
NSArray<NSString *> *typeIdentifiersForFile = (__bridge_transfer NSArray<NSString *> *)UTTypeCreateAllIdentifiersForTag(kUTTagClassFilenameExtension, (__bridge CFStringRef)model.baseURL.pathExtension, NULL);
Expand All @@ -129,7 +129,7 @@ + (id)parametersForModel:(GLLModel *)model error:(NSError *__autoreleasing *)err
return [self parametersForName:name error:error];
}

+ (id)parametersForName:(NSString *)name error:(NSError *__autoreleasing *)error;
+ (GLLModelParams *)parametersForName:(NSString *)name error:(NSError *__autoreleasing *)error;
{
id result = [parameterCache objectForKey:name];
if (!result)
Expand Down
2 changes: 2 additions & 0 deletions GLLara/GLLara-Bridging-Header.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
#import "GLLModel.h"
#import "GLLModelBone.h"
#import "GLLModelMesh.h"
#import "GLLModelParams.h"
#import "GLLVertexAttrib.h"
#import "GLLVertexAttribAccessor.h"
#import "GLLVertexAttribAccessorSet.h"
#import "GLLVertexFormat.h"
#import "GLLShaderDescription.h"
21 changes: 21 additions & 0 deletions GLLara/Model Parameters/gltfFile.modelparams.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>shaders</key>
<dict>
<key>DefaultMaterial</key>
<dict>
<key>vertex</key>
<string>Combined.vs</string>
<key>fragment</key>
<string>TexturelessOBJ.fs</string>
<key>defines</key>
<dict>
<key>CALCULATE_NORMAL_WORLD</key>
<string>1</string>
</dict>
</dict>
</dict>
</dict>
</plist>

0 comments on commit 762f5e2

Please sign in to comment.