Loading [MathJax]/extensions/MathMenu.js

2015年9月15日火曜日

iPhoneでOpenGLを使って四角を書く

iPhoneで四角だけを書くものを作りました。 OpenGL ESがわからない上にObjective-Cはしんどいです。 これ正方形を書いたつもりなんだけど長くなっているということは 解像度によって調整しないといけないということなんやろうか。。 ただこの前作った流体シミュレーションと組み合わせると面白いことができそうです。
#import "GameViewController.h"
#import <OpenGLES/ES2/glext.h>
typedef struct
{
float Position[3];
}Vertex;
typedef struct
{
float Color[4];
}Color;
const Vertex gVertices[]={
{-0.5,-0.5,0},
{0.5,-0.5,0},
{-0.5,0.5,0},
{0.5,0.5,0}
};
const GLubyte gIndices[]={
0,1,2,3
};
Color gColors[] = {
{1,0,0,1},
{0,1,0,1},
{0,0,1,1},
{1,1,1,1}
};
@interface GameViewController () {
}
@property (strong,nonatomic) EAGLContext *context;
@property (strong,nonatomic) GLKBaseEffect *effect;
@property (nonatomic) GLuint vertexBuffer;
@property (nonatomic) GLuint indexBuffer;
@property (nonatomic) GLuint colorBuffer;
@end
@implementation GameViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
if (!self.context) {
NSLog(@"Failed to create ES context");
}
GLKView *view = (GLKView *)self.view;
view.context = self.context;
view.drawableDepthFormat = GLKViewDrawableDepthFormat24;
[self setupGL];
}
- (void)dealloc
{
[self tearDownGL];
if ([EAGLContext currentContext] == self.context) {
[EAGLContext setCurrentContext:nil];
}
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
if ([self isViewLoaded] && ([[self view] window] == nil)) {
self.view = nil;
[self tearDownGL];
if ([EAGLContext currentContext] == self.context) {
[EAGLContext setCurrentContext:nil];
}
self.context = nil;
}
}
- (void)setupGL
{
[EAGLContext setCurrentContext:self.context];
self.effect =[[GLKBaseEffect alloc] init];
self.effect.colorMaterialEnabled = YES;
glEnable(GL_DEPTH_TEST);
glGenBuffers(1, &_vertexBuffer);
glBindBuffer(GL_ARRAY_BUFFER, _vertexBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(gVertices), gVertices, GL_STATIC_DRAW);
glEnableVertexAttribArray(GLKVertexAttribPosition);
glVertexAttribPointer(GLKVertexAttribPosition, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void *)NULL);
glGenBuffers(1, &_colorBuffer);
glBindBuffer(GL_ARRAY_BUFFER, _colorBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(gColors), gColors, GL_STATIC_DRAW);
glEnableVertexAttribArray(GLKVertexAttribColor);
glVertexAttribPointer(GLKVertexAttribColor, 4, GL_FLOAT, GL_FALSE, sizeof(Color), (void *)NULL);
glGenBuffers(1, &_indexBuffer);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _indexBuffer);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(gIndices), gIndices, GL_STATIC_DRAW);
}
- (void)tearDownGL
{
[EAGLContext setCurrentContext:self.context];
}
#pragma mark - GLKView and GLKViewController delegate methods
- (void)update
{
return;
}
- (void)glkView:(GLKView *)view drawInRect:(CGRect)rect
{
glClearColor(0.65f, 0.65f, 0.65f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Render the object with GLKit
[self.effect prepareToDraw];
glDrawElements(GL_TRIANGLE_STRIP, 6,GL_UNSIGNED_BYTE, 0);
}
#pragma mark - OpenGL ES 2 shader compilation
@end

0 件のコメント: