iPhoneスティック

棒をx,y,zで回すという感じで、iPhoneアプリのサンプルコードを描いてみます。

動かすとこんな感じです

モデル情報のヘッダファイル
stick.h

サンプルコード

#import <GLKit/GLKit.h>

@interface ViewController : GLKViewController

@end

#import “ViewController.h”

#import “stick.h”

@interface ViewController () {

    GLuint v;

    GLuint nv;

}

@property (nonatomic, strong) GLKBaseEffect *baseEffect;

@property (nonatomic, weak) GLKView *gv;

@property (nonatomic, weak) UIButton *selected;

@end

@implementation ViewController

– (void)loadView

{

    self.view = [[GLKView alloc] initWithFrame:CGRectMake(0, 0, 320, 568) context:[[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES3]];

}

– (void)viewDidLoad

{

    [super viewDidLoad];

    

    self.gv = (GLKView *)self.view;

    self.gv.drawableColorFormat = GLKViewDrawableDepthFormat16;

    [EAGLContext setCurrentContext:self.gv.context];

    

    self.baseEffect = [[GLKBaseEffect alloc] init];

    self.baseEffect.light0.enabled = GL_TRUE;

    self.baseEffect.light0.spotExponent = 10.0f;

    self.baseEffect.light0.spotCutoff = 1.0f;

    self.baseEffect.light0.position = GLKVector4Make(0, 0, 1.0f, 0);

    

    glClearColor(.2f, .4f, .2f, 1.0f);

    

    glGenBuffers(1, &v);

    glBindBuffer(GL_ARRAY_BUFFER, v);

    glBufferData(GL_ARRAY_BUFFER, sizeof(stickVerts), stickVerts, GL_STATIC_DRAW);

    

    glGenBuffers(1, &nv);

    glBindBuffer(GL_ARRAY_BUFFER, nv);

    glBufferData(GL_ARRAY_BUFFER, sizeof(stickNormals), stickNormals, GL_STATIC_DRAW);

    

    glEnable(GL_DEPTH_TEST);

    glEnable(GL_BLEND);

    glEnable(GL_CULL_FACE);

    

    

    NSArray *axis = @[@”X”, @”Y”, @”Z”];

    for (int i=0; i<3; i++) {

        UIButton *axBtn = [UIButton buttonWithType:UIButtonTypeSystem];

        axBtn.frame = CGRectMake(30 + 100 * i , 400, 50, 50);

        axBtn.titleLabel.font = [UIFont boldSystemFontOfSize:40];

        [axBtn setTitle:axis[i] forState:UIControlStateNormal];

        [self.view addSubview:axBtn];

        [axBtn addTarget:self action:@selector(axis:) forControlEvents:UIControlEventTouchUpInside];

    }

}

– (void)glkView:(GLKView *)view drawInRect:(CGRect)rect

{

    [self.baseEffect prepareToDraw];

    

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    

    glBindBuffer(GL_ARRAY_BUFFER, v);

    glEnableVertexAttribArray(GLKVertexAttribPosition);

    glVertexAttribPointer(GLKVertexAttribPosition, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), NULL + 0);

    glBindBuffer(GL_ARRAY_BUFFER, nv);

    glEnableVertexAttribArray(GLKVertexAttribNormal);

    glVertexAttribPointer(GLKVertexAttribNormal, 3, GL_FLOAT, GL_FALSE, 3*sizeof(GLfloat), NULL + 0);

    

    const GLfloat aspectRatio = (GLfloat)view.drawableWidth / (GLfloat)view.drawableHeight;

    self.baseEffect.transform.projectionMatrix = GLKMatrix4MakeScale(1, 1 * aspectRatio, 1);

    

    

    if (self.selected) {

        GLKMatrix4 mat = self.baseEffect.transform.modelviewMatrix;

        

        if ([self.selected.titleLabel.text isEqual:@”X”])

            mat = GLKMatrix4Rotate(mat, GLKMathDegreesToRadians(1), 1, 0, 0);

        else if ([self.selected.titleLabel.text isEqual:@”Y”])

            mat = GLKMatrix4Rotate(mat, GLKMathDegreesToRadians(1), 0, 1, 0);

        else if ([self.selected.titleLabel.text isEqual:@”Z”])

            mat = GLKMatrix4Rotate(mat, GLKMathDegreesToRadians(1), 0, 0, 1);

        

        self.baseEffect.transform.modelviewMatrix = mat;

    }

    glDrawArrays(GL_TRIANGLES, 0, stickNumVerts);

}

– (void)axis:(UIButton*)sender

{

    self.selected = sender;

}

– (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

@end