
法線ベクトルを設定したOpneGLのキューブを表示するiPhoneアプリのサンプルコードを描いてみます。
#import <GLKit/GLKit.h>
@interface ViewController : GLKViewController
@end
#import “ViewController.h”
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
};
@interface ViewController () {
GLuint v;
}
@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];
[self setupGL];
[self createButtons];
}
– (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_DEPTH_TEST);
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);
}
– (void)update
{
int x = ((UIButton *)self.buttons[0]).selected ? 1 : 0;
int y = ((UIButton *)self.buttons[1]).selected ? 1 : 0;
int z = ((UIButton *)self.buttons[2]).selected ? 1 : 0;
if (x || y || z) {
GLKMatrix4 modelViewMatrix = self.baseEffect.transform.modelviewMatrix;
modelViewMatrix = GLKMatrix4Rotate(modelViewMatrix, GLKMathDegreesToRadians(1), x, y, z);
self.baseEffect.transform.modelviewMatrix = modelViewMatrix;
}
}
– (void)createButtons
{
self.buttons = [NSMutableArray array];
NSArray *xyz = @[@”x”, @”y”, @”z”];
for (int i=0; i<3; i++) {
UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
btn.titleLabel.font = [UIFont boldSystemFontOfSize:60];
[btn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
[btn setTitleColor:[UIColor greenColor] forState:UIControlStateSelected];
[btn setTitle:xyz[i] forState:UIControlStateNormal];
[btn sizeToFit];
btn.center = CGPointMake(100 * i + 60, CGRectGetMaxY(self.view.bounds) – 150);
[self.view addSubview:btn];
[btn addTarget:self action:@selector(tap:) forControlEvents:UIControlEventTouchUpInside];
[self.buttons addObject:btn];
}
}
– (void)tap:(UIButton *)sender
{
sender.selected = !sender.selected;
}
– (void)glkView:(GLKView *)view drawInRect:(CGRect)rect
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
[self.baseEffect prepareToDraw];
glDrawArrays(GL_TRIANGLES, 0, 36);
const GLfloat aspectRatio = (GLfloat)view.drawableWidth / (GLfloat)view.drawableHeight;
self.baseEffect.transform.projectionMatrix = GLKMatrix4MakeScale(0.3, 0.3 * aspectRatio, 0.1);
}
@end