
OpenGLでボール型ワイヤーを作るiPhoneアプリのサンプルコードを描いてみます。
#import <GLKit/GLKit.h>
@interface ViewController : GLKViewController
@end
#import “ViewController.h”
@interface ViewController () {
GLuint v;
GLuint idx;
GLfloat degree;
}
@property (nonatomic, strong) GLKBaseEffect *baseEffect;
@property (nonatomic, weak) GLKView *gv;
@end
@implementation ViewController
– (void)loadView
{
self.view = [[GLKView alloc] initWithFrame:[[UIScreen mainScreen] bounds] context:[[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES3]];
}
– (void)viewDidLoad
{
[super viewDidLoad];
self.gv = (GLKView *)self.view;
self.gv.drawableColorFormat = GLKViewDrawableDepthFormat16;
self.baseEffect = [[GLKBaseEffect alloc] init];
// Let’s color the line
self.baseEffect.useConstantColor = GL_TRUE;
// Make the line a cyan color
self.baseEffect.constantColor = GLKVector4Make(0.0f, 1.0f, 1.0f, 1.0f);
}
-(void)glkView:(GLKView *)view drawInRect:(CGRect)rect
{
glClearColor(1.0f, 215.0f/255.0f, 172.0f/255.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
typedef struct
{
float Position[3];
float Color[4];
} Vertex;
Vertex vertices[101];
float dAngel = M_PI / 50.0;
for (int i=0; i<101; i++) {
float x = cos(dAngel * i);
float y = sin(dAngel * i);
Vertex vertex = {{x, y, 0}, {1.0f, 83.0f/255.0f, 78.0f/255.0f,1}};
vertices[i] = vertex;
}
glGenBuffers(1, &v);
glBindBuffer(GL_ARRAY_BUFFER, v);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, v);
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, Color));
const GLfloat aspectRatio = (GLfloat)view.drawableWidth / (GLfloat)view.drawableHeight;
self.baseEffect.transform.projectionMatrix = GLKMatrix4MakeScale(1, 1 * aspectRatio, 1);
for (int j=0; j<5; j++) {
GLKMatrix4 mat = GLKMatrix4Identity;
mat = GLKMatrix4Rotate(mat, GLKMathDegreesToRadians(degree + j*30), 1, 1, 0);
self.baseEffect.transform.modelviewMatrix = mat;
[self.baseEffect prepareToDraw];
for (int i=0; i<100; i++) {
glDrawArrays(GL_LINES, i, 2);
}
glLineWidth(10.0);
}
degree++;
}
– (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
@end