UIView Controller에서 GLKView 사용 시도
개요
XCode6.3.1에서는 GLKView를 사용하여 UIView Controller에서 3D로 그려졌기 때문에 샘플 코드를 미리 기록했습니다.
샘플 코드는 OpenGL Game 템플릿을 간단하게 사용하고 GLKit 느낌만 사용합니다.
관련 보도: 3D 프로그래밍 미경험자 Xcode-OpenGL Game 템플릿 읽기 - Qiita
AutoLayout을 사용하지 않으므로 변형을 나타냅니다.
쓴 코드
storyboard
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
사이트 축소판 그림
storyboard
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
사이트 축소판 그림
Reference
이 문제에 관하여(UIView Controller에서 GLKView 사용 시도), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/mechamogera/items/59ad6a42add2696aba18텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)