OpenGL ESの練習です。3DのT字ブロックを表示して、クルクルするiPhoneアプリを描いていきます。
動作イメージ
XcodeからiOS6 iPhone Simulatorで動かすとこんな感じになります。
サンプルコード
//
// ViewController.h
//
#import <GLKit/GLKit.h>
@interface ViewController : GLKViewController
@end
//
// ViewController.m
//
#import “ViewController.h”
typedef struct {
float Position[3];
float Color[4];
} Vertex;
const Vertex vertices[] = {
// Front
{{1, –1, 1}, {1, 1, 1, 1}},
{{1, 1, 1}, {1, 1, 1, 1}},
{{-1, 1, 1}, {1, 1, 1, 1}},
{{-1, –1, 1}, {1, 1, 1, 1}},
// Back
{{1, 1, –1}, {1, 1, 1, 1}},
{{-1, –1, –1}, {1, 1, 1, 1}},
{{1, –1, –1}, {1, 1, 1, 1}},
{{-1, 1, –1}, {1, 1, 1, 1}},
// Left
{{-1, –1, 1}, {1, 1, 1, 1}},
{{-1, 1, 1}, {1, 1, 1, 1}},
{{-1, 1, –1}, {1, 1, 1, 1}},
{{-1, –1, –1}, {1, 1, 1, 1}},
// Right
{{1, –1, –1}, {1, 1, 1, 1}},
{{1, 1, –1}, {1, 1, 1, 1}},
{{1, 1, 1}, {1, 1, 1, 1}},
{{1, –1, 1}, {1, 1, 1, 1}},
// Top
{{1, 1, 1}, {1, 1, 1, 1}},
{{1, 1, –1}, {1, 1, 1, 1}},
{{-1, 1, –1}, {1, 1, 1, 1}},
{{-1, 1, 1}, {1, 1, 1, 1}},
// Bottom
{{1, –1, –1}, {1, 1, 1, 1}},
{{1, –1, 1}, {1, 1, 1, 1}},
{{-1, –1, 1}, {1, 1, 1, 1}},
{{-1, –1, –1}, {1, 1, 1, 1}}
};
const GLubyte indices[] = {
0, 1, 2,
2, 3, 0,
// Back
4, 5, 6,
4, 5, 7,
// Left
8, 9, 10,
10, 11, 8,
// Right
12, 13, 14,
14, 15, 12,
// Top
16, 17, 18,
18, 19, 16,
// Bottom
20, 21, 22,
22, 23, 20
};
@interface ViewController () {
GLuint vertex;
GLuint index;
GLKMatrix4 mModelViewMatrix;
}
@property (nonatomic, strong) GLKBaseEffect *baseEffect;
@property (nonatomic, strong) GLKView *view;
@end
@implementation ViewController
– (void)loadView
{
self.view = [[GLKView alloc] initWithFrame:CGRectMake(0, 0, 320, 568) context:[[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2]];
}
– (void)viewDidLoad
{
[super viewDidLoad];
self.view.drawableDepthFormat = GLKViewDrawableDepthFormat16;
self.view.context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
[EAGLContext setCurrentContext:self.view.context];
self.baseEffect = [[GLKBaseEffect alloc] init];
self.baseEffect.colorMaterialEnabled = GL_TRUE;
glClearColor(.4f, .4f, .6f, 1.0f);
glGenBuffers(1, &vertex);
glBindBuffer(GL_ARRAY_BUFFER, vertex);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glGenBuffers(1, &index);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, index);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, vertex);
glEnableVertexAttribArray(GLKVertexAttribPosition);
glVertexAttribPointer(GLKVertexAttribPosition, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), NULL + 0);
glEnableVertexAttribArray(GLKVertexAttribColor);
glVertexAttribPointer(GLKVertexAttribColor, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex), NULL + offsetof(Vertex, Position));
glEnable(GL_DEPTH_TEST);
glEnable(GL_BLEND);
}
– (void)glkView:(GLKView*)view drawInRect:(CGRect)rect
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
const GLfloat aspectRatio = (GLfloat)self.view.drawableWidth / (GLfloat)self.view.drawableHeight;
self.baseEffect.transform.projectionMatrix = GLKMatrix4MakeScale(0.2f, 0.2 * aspectRatio, 0.2f);
GLKMatrix4 arrowMatrix[] = {
GLKMatrix4MakeTranslation(-2, 0, 0),
GLKMatrix4MakeTranslation(2, 0, 0),
GLKMatrix4MakeTranslation(0, 0, 0),
GLKMatrix4MakeTranslation(0, 2, 0),
};
for (int i=0; i<sizeof(arrowMatrix)/sizeof(arrowMatrix[0]); i++) {
GLKMatrix4 modelViewMatrix = arrowMatrix[i];
self.baseEffect.transform.modelviewMatrix = GLKMatrix4Multiply(mModelViewMatrix, modelViewMatrix);
[self.baseEffect prepareToDraw];
glDrawElements(GL_TRIANGLES, sizeof(indices)/sizeof(indices[0]), GL_UNSIGNED_BYTE, 0);
}
}
– (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *t = [touches anyObject];
CGPoint p = [t locationInView:self.view];
mModelViewMatrix = GLKMatrix4MakeRotation(GLKMathDegreesToRadians(p.x), 0.0f, 0.0f, 1.0f);
mModelViewMatrix = GLKMatrix4Rotate(mModelViewMatrix,GLKMathDegreesToRadians(p.y), 0.0f, 1.0f, 0.0f);
}
– (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end