diff --git a/Examples/GLModel Demo/GLModelExample/ViewController.m b/Examples/GLModel Demo/GLModelExample/ViewController.m index af61563..cc66cd5 100644 --- a/Examples/GLModel Demo/GLModelExample/ViewController.m +++ b/Examples/GLModel Demo/GLModelExample/ViewController.m @@ -20,6 +20,9 @@ - (void)viewDidUnload - (void)setModel:(NSInteger)index { + [self.modelView.models removeAllObjects]; + [self.modelView.textures removeAllObjects]; + switch (index) { case 0: @@ -28,9 +31,9 @@ - (void)setModel:(NSInteger)index self.navBar.topItem.title = @"demon.model"; //set model - self.modelView.texture = [GLImage imageNamed:@"demon.png"]; + [self.modelView addTexture:[GLImage imageNamed:@"demon.png"]]; self.modelView.blendColor = nil; - self.modelView.model = [GLModel modelWithContentsOfFile:@"demon.model"]; + [self.modelView addModel:[GLModel modelWithContentsOfFile:@"demon.model"]]; //set default transform CATransform3D transform = CATransform3DMakeTranslation(0.0f, 0.0f, -2.0f); @@ -46,9 +49,8 @@ - (void)setModel:(NSInteger)index self.navBar.topItem.title = @"quad"; //set model - self.modelView.texture = nil; self.modelView.blendColor = [UIColor redColor]; - self.modelView.model = [GLModel modelWithContentsOfFile:@"quad.obj"]; + [self.modelView addModel:[GLModel modelWithContentsOfFile:@"quad.obj"]]; //set default transform self.modelView.modelTransform = CATransform3DMakeTranslation(0.0f, 0.0f, -2.0f); @@ -61,9 +63,9 @@ - (void)setModel:(NSInteger)index self.navBar.topItem.title = @"chair.obj"; //set model - self.modelView.texture = [GLImage imageNamed:@"chair.tga"]; + [self.modelView addTexture:[GLImage imageNamed:@"chair.tga"]]; self.modelView.blendColor = nil; - self.modelView.model = [GLModel modelWithContentsOfFile:@"chair.obj"]; + [self.modelView addModel:[GLModel modelWithContentsOfFile:@"chair.obj"]]; //set default transform CATransform3D transform = CATransform3DMakeTranslation(0.0f, 0.0f, -2.0f); @@ -79,9 +81,8 @@ - (void)setModel:(NSInteger)index self.navBar.topItem.title = @"diamond.obj"; //set model - self.modelView.texture = nil; self.modelView.blendColor = [UIColor greenColor]; - self.modelView.model = [GLModel modelWithContentsOfFile:@"diamond.obj"]; + [self.modelView addModel:[GLModel modelWithContentsOfFile:@"diamond.obj"]]; //set default transform CATransform3D transform = CATransform3DMakeTranslation(0.0f, 0.0f, -1.0f); @@ -97,9 +98,8 @@ - (void)setModel:(NSInteger)index self.navBar.topItem.title = @"cube.obj"; //set model - self.modelView.texture = nil; self.modelView.blendColor = [UIColor whiteColor]; - self.modelView.model = [GLModel modelWithContentsOfFile:@"cube.obj"]; + [self.modelView addModel:[GLModel modelWithContentsOfFile:@"cube.obj"]]; //set default transform CATransform3D transform = CATransform3DMakeTranslation(0.0f, 0.0f, -1.0f); @@ -114,9 +114,8 @@ - (void)setModel:(NSInteger)index self.navBar.topItem.title = @"ship.obj"; //set model - self.modelView.texture = nil; self.modelView.blendColor = [UIColor grayColor]; - self.modelView.model = [GLModel modelWithContentsOfFile:@"ship.obj"]; + [self.modelView addModel:[GLModel modelWithContentsOfFile:@"ship.obj"]]; //set default transform CATransform3D transform = CATransform3DMakeTranslation(0.0f, 0.0f, -15.0f); diff --git a/GLView.podspec b/GLView.podspec index d0869c3..7ba5d6d 100644 --- a/GLView.podspec +++ b/GLView.podspec @@ -1,7 +1,7 @@ Pod::Spec.new do |s| s.name = "GLView" - s.version = "1.6.1" + s.version = "1.6.3" s.license = 'zlib' s.summary = "Simple library for displaying OpenGL cimages and models on iOS." s.description = <<-DESC diff --git a/GLView/Models/GLModel.m b/GLView/Models/GLModel.m index 2303440..7822eec 100644 --- a/GLView/Models/GLModel.m +++ b/GLView/Models/GLModel.m @@ -283,6 +283,10 @@ - (BOOL)loadObjModel:(NSData *)data indexStrings[indexString] = @(fIndex); GLuint vIndex = [parts[0] intValue]; + + NSUInteger currLoc = (vIndex - 1) * sizeof(GLfloat) * 3; + if (currLoc >= tempVertexData.length) break; + [vertexData appendBytes:tempVertexData.bytes + (vIndex - 1) * sizeof(GLfloat) * 3 length:sizeof(GLfloat) * 3]; if ([parts count] > 1) diff --git a/GLView/Models/GLModelView.h b/GLView/Models/GLModelView.h index 8940fae..f6a1595 100644 --- a/GLView/Models/GLModelView.h +++ b/GLView/Models/GLModelView.h @@ -45,13 +45,16 @@ @interface GLModelView : GLView -@property (nonatomic, strong) GLModel *model; -@property (nonatomic, strong) GLImage *texture; +//@property (nonatomic, strong) GLModel *model; +@property (nonatomic, strong) NSMutableArray *textures; @property (nonatomic, strong) UIColor *blendColor; @property (nonatomic, assign) CATransform3D modelTransform; @property (nonatomic, copy) NSArray *lights; +@property (nonatomic, strong) NSMutableArray *models; -@end +-(void) addTexture:(GLImage *)texture; +-(void) addModel:(GLModel *)model; +@end #pragma GCC diagnostic pop diff --git a/GLView/Models/GLModelView.m b/GLView/Models/GLModelView.m index 1897458..cee5db9 100644 --- a/GLView/Models/GLModelView.m +++ b/GLView/Models/GLModelView.m @@ -52,6 +52,9 @@ - (void)setUp self.lights = @[light]; _modelTransform = CATransform3DIdentity; + + self.models = [[NSMutableArray alloc] init]; + self.textures = [[NSMutableArray alloc] init]; } - (void)setLights:(NSArray *)lights @@ -63,11 +66,11 @@ - (void)setLights:(NSArray *)lights } } -- (void)setModel:(GLModel *)model +- (void)addModel:(GLModel *)model { - if (_model != model) + if (model != nil) { - _model = model; + [self.models addObject:model]; [self setNeedsDisplay]; } } @@ -81,11 +84,11 @@ - (void)setBlendColor:(UIColor *)blendColor } } -- (void)setTexture:(GLImage *)texture +- (void)addTexture:(GLImage *)texture { - if (_texture != texture) + if (texture != nil) { - _texture = texture; + [self.textures addObject:texture]; [self setNeedsDisplay]; } } @@ -121,24 +124,30 @@ - (void)drawRect:(__unused CGRect)rect glDisable(GL_LIGHTING); } - //apply model transform - GLLoadCATransform3D(self.modelTransform); - - //set texture - [self.blendColor ?: [UIColor whiteColor] bindGLColor]; - if (self.texture) - { - [self.texture bindTexture]; - } - else - { - glDisable(GL_TEXTURE_2D); - glEnable(GL_BLEND); - glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + //render the model + for (NSUInteger i = 0; i < self.models.count; i++) { + //apply model transform + GLLoadCATransform3D(self.modelTransform); + + //set texture + [self.blendColor ?: [UIColor whiteColor] bindGLColor]; + + GLModel *model = [self.models objectAtIndex:i]; + + if (self.textures.count > i) + { + GLImage *texture = [self.textures objectAtIndex:i]; + [texture bindTexture]; + } + else + { + glDisable(GL_TEXTURE_2D); + glEnable(GL_BLEND); + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + } + [model draw]; } - //render the model - [self.model draw]; } @end