Help us understand the problem. What is going on with this article?

UIViewControllerでGLKViewを使ってみた

More than 5 years have passed since last update.

概要

XCode 6.3.1でUIViewControllerからGLKViewを使って3D描画してみたのでサンプルコードをメモしておく。
サンプルコードはOpenGL Gameテンプレートを簡単にしてGLKitだけ使うようにした感じ。
関連記事:Xcode - OpenGL Gameテンプレートを3Dプログラミング未経験者が読む - Qiita

iOS Simulator Screen Shot 2015.05.08 16.27.16.png

AutoLayout使ってないので表示はいびつ。

書いたコード

storyboard

storyboard_glkview.png

MyViewController.h

#import <UIKit/UIKit.h>
#import <GLKit/GLKit.h>

@interface MyViewController : UIViewController <GLKViewDelegate>
@property (weak, nonatomic) IBOutlet GLKView *myGLKView;
@end

MyViewController.m

#import "MyViewController.h"
#import <OpenGLES/ES2/glext.h>

#define BUFFER_OFFSET(i) ((char *)NULL + (i))

GLfloat gMyVertexData2[18] =
{
    0.5f, 0.0f, 0.0f,       0.0f, 0.0f, 1.0f,
    0.0f, 0.5f, 0.0f,       0.0f, 0.0f, 1.0f,
    0.0f, -0.5f, 0.0f,      0.0f, 0.0f, 1.0f
};

@interface MyViewController ()
{
    float _rotation;

    GLuint _myVertexArray;
    GLuint _myVertexBuffer;
}
@property (strong, nonatomic) GLKBaseEffect *effect;

@end

@implementation MyViewController

- (void)viewDidLoad 
{
    [super viewDidLoad];

    EAGLContext *context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];

    self.myGLKView.context = context;
    self.myGLKView.delegate = self;
    [self.myGLKView setEnableSetNeedsDisplay:YES];
    [self.view addSubview:self.myGLKView];

    [EAGLContext setCurrentContext:context];

    CADisplayLink *displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(drawFrame)];
    [displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];

    [self setupGL];
}

- (void)setupGL
{
    self.effect = [[GLKBaseEffect alloc] init];
    self.effect.light0.enabled = GL_TRUE;
    self.effect.light0.diffuseColor = GLKVector4Make(1.0f, 0.4f, 0.4f, 1.0f);

    glGenVertexArraysOES(1, &_myVertexArray);
    glBindVertexArrayOES(_myVertexArray);

    glGenBuffers(1, &_myVertexBuffer);
    glBindBuffer(GL_ARRAY_BUFFER, _myVertexBuffer);
    glBufferData(GL_ARRAY_BUFFER, sizeof(gMyVertexData2), gMyVertexData2, GL_STATIC_DRAW);
    glEnableVertexAttribArray(GLKVertexAttribPosition);
    glVertexAttribPointer(GLKVertexAttribPosition, 3, GL_FLOAT, GL_FALSE, 24, BUFFER_OFFSET(0));
    glEnableVertexAttribArray(GLKVertexAttribNormal);
    glVertexAttribPointer(GLKVertexAttribNormal, 3, GL_FLOAT, GL_FALSE, 24, BUFFER_OFFSET(12));

    glBindVertexArrayOES(0);
}

- (void)drawFrame
{
    float aspect = fabs(self.view.bounds.size.width / self.view.bounds.size.height);
    GLKMatrix4 projectionMatrix = GLKMatrix4MakePerspective(GLKMathDegreesToRadians(65.0f), aspect, 0.1f, 100.0f);

    self.effect.transform.projectionMatrix = projectionMatrix;

    GLKMatrix4 modelViewMatrix = GLKMatrix4MakeTranslation(0.0f, 0.0f, -1.5f);
    modelViewMatrix = GLKMatrix4Rotate(modelViewMatrix, _rotation, 1.0f, 1.0f, 1.0f);

    self.effect.transform.modelviewMatrix = modelViewMatrix;

    _rotation += 0.2 * 0.5f;

    [self.myGLKView setNeedsDisplay];
}

- (void)glkView:(GLKView *)view drawInRect:(CGRect)rect
{
    glClearColor(1, 0, 0, 1.0f);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glBindVertexArrayOES(0);
    glBindVertexArrayOES(_myVertexArray);

    [self.effect prepareToDraw];

    glDrawArrays(GL_TRIANGLES, 0, 3);
}

@end

参照サイト

mechamogera
https://github.com/mechamogera/MyTips/wiki で技術メモを登録してましたが使いやすそうなので引っ越してみました。 保有資格:CSM、EXIN Agile Scrum Foundation、AWS認定ソリューションアーキテクトプロフェッショナル、Ruby技術者認定試験Silver/Gold、SJC-P/SJC-D、ソフトウェア開発技術者、基本情報技術者
https://github.com/mechamogera/MyTips/wiki
jrits
信頼と魅力のある先進のITをもとに、お客様のワークスタイル・イノベーションの実現を目指します。
http://www.jrits.co.jp/
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした