Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #import "ViewController.h"
- #define isIpad (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad? YES : NO)
- #define ratioSpeed (isIpad? 250 : 100)
- typedef struct {
- float Position[3];
- float Color[4];
- } Vertex;
- const Vertex Vertices[] = {
- {{0, 1, 0}, {1, 0, 0, 1}},
- {{-1, -1, 1}, {1, 0, 0, 1}},
- {{1, -1, 1}, {1, 0, 0, 1}},
- {{0, 1, 0}, {0, 1, 0, 1}},
- {{1, -1, 1}, {0, 1, 0, 1}},
- {{0, -1, -1}, {0, 1, 0, 1}},
- {{0, 1, 0}, {0, 0, 1, 1}},
- {{-1, -1, 1}, {0, 0, 1, 1}},
- {{0, -1, -1}, {0, 0, 1, 1}},
- {{-1, -1, 1}, {1, 1, 0, 1}},
- {{0,-1, -1}, {1, 1, 0, 1}},
- {{1, -1, 1}, {1, 1, 0, 1}}
- };
- const GLubyte Indices[] = {
- 0, 1, 2,
- 3, 4, 5,
- 6, 7, 8,
- 9, 10, 11
- };
- @interface ViewController ()
- {
- GLuint _vertexBuffer;
- GLuint _indexBuffer;
- float previousScale;
- float rotateX;
- float rotateY;
- float rotateZ;
- float rotation;
- CGFloat scale;
- CGFloat lastScale;
- }
- @property (strong, nonatomic) EAGLContext *context;
- @property (strong, nonatomic) GLKBaseEffect *effect;
- @end
- @implementation ViewController
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- self.preferredFramesPerSecond = 60;
- self.context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
- if (!self.context) {
- NSLog(@"Failed to create ES context");
- }
- scale = 1.;
- lastScale = 1.;
- GLKView *view = (GLKView *)self.view;
- view.context = self.context;
- view.drawableDepthFormat = GLKViewDrawableDepthFormat24;
- [EAGLContext setCurrentContext:self.context];
- glGenBuffers(1, &_vertexBuffer);
- glBindBuffer(GL_ARRAY_BUFFER, _vertexBuffer);
- glBufferData(GL_ARRAY_BUFFER, sizeof(Vertices), Vertices, GL_STATIC_DRAW);
- glGenBuffers(1, &_indexBuffer);
- glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _indexBuffer);
- glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(Indices), Indices, GL_STATIC_DRAW);
- glEnableVertexAttribArray(GLKVertexAttribPosition);
- glVertexAttribPointer(GLKVertexAttribPosition, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (const GLvoid *) offsetof(Vertex, Position));
- glEnableVertexAttribArray(GLKVertexAttribColor);
- glVertexAttribPointer(GLKVertexAttribColor, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex), (const GLvoid *) offsetof(Vertex, Color));
- self.effect = [[GLKBaseEffect alloc] init];
- float aspect = fabsf( self.view.bounds.size.width/self.view.bounds.size.height);
- GLKMatrix4 projectionMatrix = GLKMatrix4MakePerspective(GLKMathDegreesToRadians(20), aspect, 1, 10);
- self.effect.transform.projectionMatrix = projectionMatrix;
- UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinch:)];
- [self.view addGestureRecognizer:pinchGesture];
- }
- - (void) pinch:(UIPinchGestureRecognizer *) gesture
- {
- if([gesture state] == UIGestureRecognizerStateEnded)
- {
- lastScale = 1.0;
- }
- else
- {
- scale = scale - (lastScale - gesture.scale);
- lastScale = gesture.scale;
- }
- }
- - (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)tearDownGL
- {
- [EAGLContext setCurrentContext:self.context];
- glDeleteBuffers(1, &_vertexBuffer);
- glDeleteBuffers(1, &_indexBuffer);
- self.effect = nil;
- }
- -(void)update
- {
- GLKMatrix4 modelMatrix = GLKMatrix4MakeTranslation(0, 0, -6);
- modelMatrix = GLKMatrix4Rotate(modelMatrix,rotation,1.0f,1.0f,0.7f);
- modelMatrix = GLKMatrix4Rotate(modelMatrix, GLKMathDegreesToRadians(rotateX), 1.0, 0.0, 0.0);
- modelMatrix = GLKMatrix4Rotate(modelMatrix, GLKMathDegreesToRadians(rotateY), 0.0, 1.0, 0.0);
- modelMatrix = GLKMatrix4Rotate(modelMatrix, GLKMathDegreesToRadians(rotateZ), 0.0, 0.0, 1.0);
- modelMatrix = GLKMatrix4Scale(modelMatrix, 0.33 * scale, 0.33 * scale, 0.33 * scale);
- self.effect.transform.modelviewMatrix = modelMatrix;
- // rotation += self.timeSinceLastUpdate * 1.0f;
- }
- #pragma mark - GLKView and GLKViewController delegate methods
- - (void)glkView:(GLKView *)view drawInRect:(CGRect)rect
- {
- glClearColor(0.6f, 0.6f, 0.65f, 1.0f);
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- [self.effect prepareToDraw];
- glDrawElements(GL_TRIANGLES, sizeof(Indices)/sizeof(Indices[0]), GL_UNSIGNED_BYTE, 0);
- }
- #pragma mark - touches
- -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
- {
- UITouch* t = [touches anyObject];
- rotateY += ([t locationInView:self.view].x - [t previousLocationInView:self.view].x);
- rotateX += ([t locationInView:self.view].y - [t previousLocationInView:self.view].y);
- if (rotateX > 360)
- {
- rotateX = 0;
- }
- else if (rotateX < 0)
- {
- rotateX = 360;
- }
- if (rotateY > 360)
- {
- rotateY = 0;
- }
- else if (rotateY < 0)
- {
- rotateY = 360;
- }
- }
- @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement