iPhone変なリング

3Dのリングを作ろうと思ったら変なのができた。。とiPhoneアプリのサンプルコードを描いてみます。

#import “ViewController.h”

@interface ViewController ()

{

    GLuint v;

    int counter;

}

@property (nonatomic, strong) GLKBaseEffect *baseEffect;

@property (nonatomic, strong) GLKView *gv;

@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];

}

– (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);

    self.baseEffect.light0.ambientColor = GLKVector4Make(.0f, 0.4f, 0.4f, 1.0f);

    

    glClearColor(0.2f, 0.5f, 0.6f, 1.0f);

    

    

    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);

    

    glEnable(GL_CULL_FACE);

    glEnable(GL_DEPTH_TEST);

}

– (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.05, 0.05 * aspectRatio, 0.05);

    

    int count = 30;

    float degree = 360 / count;

    for (int i=0; i<count; i++) {

        GLKMatrix4 mat = GLKMatrix4Identity;

        mat = GLKMatrix4Rotate(mat, GLKMathDegreesToRadians(degree * i), 0, 0, 1);

        mat = GLKMatrix4Translate(mat, 5, 0, 0);

        self.baseEffect.transform.modelviewMatrix = mat;

        

        

        GLKMatrix4 mModelViewMatrix = GLKMatrix4MakeRotation(GLKMathDegreesToRadians(counter), 0.0f, 1.0f, 0.0f);

        self.baseEffect.transform.modelviewMatrix = GLKMatrix4Multiply(mModelViewMatrix, self.baseEffect.transform.modelviewMatrix);

        

        [self.baseEffect prepareToDraw];

        glDrawArrays(GL_TRIANGLES, 0, 36);

    }

    

    

    counter++;

}

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