
キューブとキューブをくるくるまわす3DなiPhoneアプリのサンプルコードを描いてみます。
#import <GLKit/GLKit.h>
@interface ViewController : GLKViewController
@end
#import “ViewController.h”
@interface ViewController () {
GLuint v;
GLKMatrixStackRef _matrixStack;
GLint counter;
}
@property (nonatomic, strong) GLKBaseEffect *baseEffect;
@property (nonatomic, strong) GLKView *gv;
@property (nonatomic, strong) NSMutableArray *buttons;
@end
@implementation ViewController
– (void)loadView
{
self.view = [[GLKView alloc] initWithFrame:CGRectMake(0, 0, 320, 568) context:[[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES3]];
}
– (void)viewDidLoad
{
[super viewDidLoad];
UILabel *label = [[UILabel alloc] init];
label.text = @”Cube x Cube”;
label.font = [UIFont boldSystemFontOfSize:70];
label.textColor = [[UIColor whiteColor] colorWithAlphaComponent:0.5];
[label sizeToFit];
label.center = self.view.center;
label.transform = CGAffineTransformMakeRotation(M_PI / 4.0);
[self.view addSubview:label];
[self setupGL];
}
– (void)setupGL
{
self.gv = (GLKView*)self.view;
self.gv.drawableColorFormat = GLKViewDrawableDepthFormat24;
[EAGLContext setCurrentContext:self.gv.context];
self.baseEffect = [[GLKBaseEffect alloc] init];
self.baseEffect.light0.enabled = GL_TRUE;
self.baseEffect.light0.diffuseColor = GLKVector4Make(1.0f, 0.4f, 0.4f, 1.0f);
glClearColor(0.2f, 0.5f, 0.6f, 1.0f);
glEnable(GL_CULL_FACE);
glGenBuffers(1, &v);
glBindBuffer(GL_ARRAY_BUFFER, v);
glBufferData(GL_ARRAY_BUFFER, sizeof(cubeVertices), cubeVertices, GL_STATIC_DRAW);
glEnableVertexAttribArray(GLKVertexAttribPosition);
glVertexAttribPointer(GLKVertexAttribPosition, 3, GL_FLOAT, GL_FALSE, 24, (char *)NULL + 0);
glEnableVertexAttribArray(GLKVertexAttribNormal);
glVertexAttribPointer(GLKVertexAttribNormal, 3, GL_FLOAT, GL_FALSE, 24, (char *)NULL + 12);
_matrixStack = GLKMatrixStackCreate(NULL);
GLKMatrixStackLoadMatrix4(_matrixStack, self.baseEffect.transform.modelviewMatrix);
}
– (void)glkView:(GLKView *)view drawInRect:(CGRect)rect
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
const GLfloat aspectRatio = (GLfloat)view.drawableWidth / (GLfloat)view.drawableHeight;
self.baseEffect.transform.projectionMatrix = GLKMatrix4MakeScale(0.3, 0.3 * aspectRatio, 0.1);
GLKMatrix4 modelViewMatrix = GLKMatrixStackGetMatrix4(_matrixStack);
modelViewMatrix = GLKMatrix4Rotate(modelViewMatrix, GLKMathDegreesToRadians(counter++), 0, 1, 1);
self.baseEffect.transform.modelviewMatrix = modelViewMatrix;
[self.baseEffect prepareToDraw];
glDrawArrays(GL_TRIANGLES, 0, 36);
GLKMatrixStackPush(_matrixStack);
GLKMatrix4 baseModelViewMatrix = GLKMatrix4Identity;
baseModelViewMatrix = GLKMatrix4Rotate(baseModelViewMatrix, GLKMathDegreesToRadians(counter), 0.0f, 1.0f, 1.0f);
modelViewMatrix = GLKMatrix4MakeTranslation(2.5f, 0.0f, –0.0f);
modelViewMatrix = GLKMatrix4Multiply(baseModelViewMatrix, modelViewMatrix);
self.baseEffect.transform.modelviewMatrix = modelViewMatrix; [self.baseEffect prepareToDraw];
glDrawArrays(GL_TRIANGLES, 0, 36);
GLKMatrixStackPop(_matrixStack);
}
GLfloat cubeVertices[216] = {
//x y z nx ny nz
1.0f, –1.0f, –1.0f, 1.0f, 0.0f, 0.0f,
1.0f, 1.0f, –1.0f, 1.0f, 0.0f, 0.0f,
1.0f, –1.0f, 1.0f, 1.0f, 0.0f, 0.0f,
1.0f, –1.0f, 1.0f, 1.0f, 0.0f, 0.0f,
1.0f, 1.0f, –1.0f, 1.0f, 0.0f, 0.0f,
1.0f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f,
1.0f, 1.0f, –1.0f, 0.0f, 1.0f, 0.0f,
–1.0f, 1.0f, –1.0f, 0.0f, 1.0f, 0.0f,
1.0f, 1.0f, 1.0f, 0.0f, 1.0f, 0.0f,
1.0f, 1.0f, 1.0f, 0.0f, 1.0f, 0.0f,
–1.0f, 1.0f, –1.0f, 0.0f, 1.0f, 0.0f,
–1.0f, 1.0f, 1.0f, 0.0f, 1.0f, 0.0f,
–1.0f, 1.0f, –1.0f, –1.0f, 0.0f, 0.0f,
–1.0f, –1.0f, –1.0f, –1.0f, 0.0f, 0.0f,
–1.0f, 1.0f, 1.0f, –1.0f, 0.0f, 0.0f,
–1.0f, 1.0f, 1.0f, –1.0f, 0.0f, 0.0f,
–1.0f, –1.0f, –1.0f, –1.0f, 0.0f, 0.0f,
–1.0f, –1.0f, 1.0f, –1.0f, 0.0f, 0.0f,
–1.0f, –1.0f, –1.0f, 0.0f, –1.0f, 0.0f,
1.0f, –1.0f, –1.0f, 0.0f, –1.0f, 0.0f,
–1.0f, –1.0f, 1.0f, 0.0f, –1.0f, 0.0f,
–1.0f, –1.0f, 1.0f, 0.0f, –1.0f, 0.0f,
1.0f, –1.0f, –1.0f, 0.0f, –1.0f, 0.0f,
1.0f, –1.0f, 1.0f, 0.0f, –1.0f, 0.0f,
1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f,
–1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f,
1.0f, –1.0f, 1.0f, 0.0f, 0.0f, 1.0f,
1.0f, –1.0f, 1.0f, 0.0f, 0.0f, 1.0f,
–1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f,
–1.0f, –1.0f, 1.0f, 0.0f, 0.0f, 1.0f,
1.0f, –1.0f, –1.0f, 0.0f, 0.0f, –1.0f,
–1.0f, –1.0f, –1.0f, 0.0f, 0.0f, –1.0f,
1.0f, 1.0f, –1.0f, 0.0f, 0.0f, –1.0f,
1.0f, 1.0f, –1.0f, 0.0f, 0.0f, –1.0f,
–1.0f, –1.0f, –1.0f, 0.0f, 0.0f, –1.0f,
–1.0f, 1.0f, –1.0f, 0.0f, 0.0f, –1.0f
};
@end