同じ座標情報をつかって、複数のオブジェクトを表示するサンプル
(XcodeのiOS6 iPhone Simulatorで試しています。)
概要
GLKitをつかって、OpenGL ES2.0の練習。
同じ点の集合オブジェクトを複数表示してみようと思う。
一つの立方体の座標にたいして、TranslateとRotateを使うことで、
矢印の形に配置してみる。
ポイント
DrawElementsを呼び出す前に、GLKBaseEffectのprepareToDrawを呼んでおく。
回転、移動は、GLKBaseEffectのtransform.modelviewMatrixに代入を繰り返す。
※xibもしくはstoryboardのroot viewをGLKViewに変更する必要有り。
サンプルコード
——————————-
— 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, 0, 0, 1}},
{{1, 1, 1}, {0, 1, 0, 1}},
{{-1, 1, 1}, {0, 0, 1, 1}},
{{-1, –1, 1}, {1, 0, 0, 1}},
// Back
{{1, 1, –1}, {1, 0, 0, 1}},
{{-1, –1, –1}, {0, 1, 0, 1}},
{{1, –1, –1}, {0, 0, 1, 1}},
{{-1, 1, –1}, {1, 0, 0, 1}},
// Left
{{-1, –1, 1}, {1, 0, 0, 1}},
{{-1, 1, 1}, {0, 1, 0, 1}},
{{-1, 1, –1}, {0, 0, 1, 1}},
{{-1, –1, –1}, {1, 0, 0, 1}},
// Right
{{1, –1, –1}, {1, 0, 0, 1}},
{{1, 1, –1}, {0, 1, 0, 1}},
{{1, 1, 1}, {0, 0, 1, 1}},
{{1, –1, 1}, {1, 0, 0, 1}},
// Top
{{1, 1, 1}, {1, 0, 0, 1}},
{{1, 1, –1}, {0, 1, 0, 1}},
{{-1, 1, –1}, {0, 0, 1, 1}},
{{-1, 1, 1}, {1, 0, 0, 1}},
// Bottom
{{1, –1, –1}, {1, 0, 0, 1}},
{{1, –1, 1}, {0, 1, 0, 1}},
{{-1, –1, 1}, {0, 0, 1, 1}},
{{-1, –1, –1}, {1, 0, 0, 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;
int degree;
int x;
}
@property (nonatomic, strong) GLKBaseEffect *baseEffect;
@property (nonatomic, strong) GLKView *gv;
@end
@implementation ViewController
@synthesize baseEffect, gv;
– (void)viewDidLoad
{
[super viewDidLoad];
gv = (GLKView*)self.view;
gv.drawableDepthFormat = GLKViewDrawableDepthFormat16;
gv.context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
[EAGLContext setCurrentContext:gv.context];
self.baseEffect = [[GLKBaseEffect alloc] init];
self.baseEffect.colorMaterialEnabled = GL_TRUE;
glClearColor(.2f, .2f, .2f, 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);
// glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
}
– (void)glkView:(GLKView *)view drawInRect:(CGRect)rect
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
const GLfloat aspectRatio = (GLfloat)gv.drawableWidth / (GLfloat)gv.drawableHeight;
self.baseEffect.transform.projectionMatrix = GLKMatrix4MakeScale(0.05f, 0.05 * aspectRatio, 0.05f);
GLKMatrix4 arrowMatrix[] = {
GLKMatrix4MakeTranslation(9, 0, 0),
GLKMatrix4MakeTranslation(6, 0, 0),
GLKMatrix4MakeTranslation(3, 0, 0),
GLKMatrix4MakeTranslation(0, 0, 0),
GLKMatrix4MakeTranslation(-3, 0, 0),
GLKMatrix4MakeTranslation(-6, 0, 0),
GLKMatrix4MakeTranslation(6, 3, 0),
GLKMatrix4MakeTranslation(3, 6, 0),
GLKMatrix4MakeTranslation(6, –3, 0),
GLKMatrix4MakeTranslation(3, –6, 0),
};
for (int i=0; i<sizeof(arrowMatrix)/sizeof(arrowMatrix[0]); i++) {
GLKMatrix4 modelViewMatrix = arrowMatrix[i];
modelViewMatrix = GLKMatrix4Translate(modelViewMatrix, x*0.1, 0, 0);
modelViewMatrix = GLKMatrix4Rotate(modelViewMatrix, GLKMathDegreesToRadians(degree), 1, 0, 1);
self.baseEffect.transform.modelviewMatrix = modelViewMatrix;
[self.baseEffect prepareToDraw];
glDrawElements(GL_TRIANGLES, sizeof(indices)/sizeof(indices[0]), GL_UNSIGNED_BYTE, 0);
}
x += 4;
if (x > 250) {
x = –100;
}
degree += 5;
}
– (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end