Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for multiple models and textures per model for one GLModelView #9

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 11 additions & 12 deletions Examples/GLModel Demo/GLModelExample/ViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ - (void)viewDidUnload

- (void)setModel:(NSInteger)index
{
[self.modelView.models removeAllObjects];
[self.modelView.textures removeAllObjects];

switch (index)
{
case 0:
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion GLView.podspec
Original file line number Diff line number Diff line change
@@ -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
Expand Down
4 changes: 4 additions & 0 deletions GLView/Models/GLModel.m
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
9 changes: 6 additions & 3 deletions GLView/Models/GLModelView.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
53 changes: 31 additions & 22 deletions GLView/Models/GLModelView.m
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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];
}
}
Expand All @@ -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];
}
}
Expand Down Expand Up @@ -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