Advertisement
Guest User

Untitled

a guest
Oct 27th, 2013
416
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. #import "ViewController.h"
  3.  
  4. #define isIpad (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad? YES : NO)
  5.  
  6. #define ratioSpeed (isIpad? 250 : 100)
  7.  
  8. typedef struct {
  9.     float Position[3];
  10.     float Color[4];
  11. } Vertex;
  12.  
  13. const Vertex Vertices[] = {
  14.  
  15.    
  16. {{0, 1, 0},   {1, 0, 0, 1}},
  17. {{-1, -1, 1}, {1, 0, 0, 1}},
  18. {{1, -1, 1},  {1, 0, 0, 1}},
  19.  
  20. {{0, 1, 0},   {0, 1, 0, 1}},
  21. {{1, -1, 1},  {0, 1, 0, 1}},
  22. {{0, -1, -1}, {0, 1, 0, 1}},
  23.  
  24. {{0, 1, 0},      {0, 0, 1, 1}},
  25. {{-1, -1, 1},    {0, 0, 1, 1}},
  26. {{0, -1, -1}, {0, 0, 1, 1}},
  27.  
  28. {{-1, -1, 1},          {1, 1, 0, 1}},
  29. {{0,-1, -1},     {1, 1, 0, 1}},
  30. {{1, -1, 1},   {1, 1, 0, 1}}
  31. };
  32.  
  33. const GLubyte Indices[] = {
  34.     0, 1, 2,
  35.     3, 4, 5,
  36.     6, 7, 8,
  37.     9, 10, 11
  38. };
  39.  
  40.  
  41. @interface ViewController ()
  42. {
  43.     GLuint _vertexBuffer;
  44.     GLuint _indexBuffer;
  45.     float previousScale;
  46.    
  47.     float rotateX;
  48.     float rotateY;
  49.     float rotateZ;
  50.     float rotation;
  51.     CGFloat scale;
  52.     CGFloat lastScale;
  53. }
  54.  
  55. @property (strong, nonatomic) EAGLContext *context;
  56. @property (strong, nonatomic) GLKBaseEffect *effect;
  57.  
  58. @end
  59.  
  60. @implementation ViewController
  61.  
  62. - (void)viewDidLoad
  63. {
  64.     [super viewDidLoad];
  65.     self.preferredFramesPerSecond = 60;
  66.    
  67.     self.context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
  68.  
  69.     if (!self.context) {
  70.         NSLog(@"Failed to create ES context");
  71.     }
  72.    
  73.     scale = 1.;
  74.     lastScale = 1.;
  75.  
  76.    
  77.     GLKView *view = (GLKView *)self.view;
  78.     view.context = self.context;
  79.     view.drawableDepthFormat = GLKViewDrawableDepthFormat24;
  80.     [EAGLContext setCurrentContext:self.context];
  81.  
  82.  
  83.     glGenBuffers(1, &_vertexBuffer);
  84.     glBindBuffer(GL_ARRAY_BUFFER, _vertexBuffer);
  85.     glBufferData(GL_ARRAY_BUFFER, sizeof(Vertices), Vertices, GL_STATIC_DRAW);
  86.    
  87.     glGenBuffers(1, &_indexBuffer);
  88.     glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _indexBuffer);
  89.     glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(Indices), Indices, GL_STATIC_DRAW);
  90.    
  91.    
  92.     glEnableVertexAttribArray(GLKVertexAttribPosition);
  93.     glVertexAttribPointer(GLKVertexAttribPosition, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (const GLvoid *) offsetof(Vertex, Position));
  94.     glEnableVertexAttribArray(GLKVertexAttribColor);
  95.     glVertexAttribPointer(GLKVertexAttribColor, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex), (const GLvoid *) offsetof(Vertex, Color));
  96.  
  97.    
  98.     self.effect = [[GLKBaseEffect alloc] init];
  99.    
  100.     float aspect = fabsf( self.view.bounds.size.width/self.view.bounds.size.height);
  101.     GLKMatrix4 projectionMatrix = GLKMatrix4MakePerspective(GLKMathDegreesToRadians(20), aspect, 1, 10);
  102.     self.effect.transform.projectionMatrix = projectionMatrix;
  103.  
  104.     UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinch:)];
  105.     [self.view addGestureRecognizer:pinchGesture];
  106.    
  107.    
  108. }
  109.  
  110. - (void) pinch:(UIPinchGestureRecognizer *) gesture
  111. {
  112.     if([gesture state] == UIGestureRecognizerStateEnded)
  113.     {
  114.         lastScale = 1.0;
  115.     }
  116.     else
  117.     {
  118.        
  119.         scale = scale - (lastScale - gesture.scale);
  120.         lastScale = gesture.scale;
  121.     }
  122. }
  123.  
  124. - (void)dealloc
  125. {    
  126.     [self tearDownGL];
  127.    
  128.     if ([EAGLContext currentContext] == self.context) {
  129.         [EAGLContext setCurrentContext:nil];
  130.     }
  131. }
  132.  
  133. - (void)didReceiveMemoryWarning
  134. {
  135.     [super didReceiveMemoryWarning];
  136.  
  137.     if ([self isViewLoaded] && ([[self view] window] == nil)) {
  138.         self.view = nil;
  139.        
  140.         [self tearDownGL];
  141.        
  142.         if ([EAGLContext currentContext] == self.context) {
  143.             [EAGLContext setCurrentContext:nil];
  144.         }
  145.         self.context = nil;
  146.     }
  147. }
  148.  
  149.  
  150. - (void)tearDownGL
  151. {
  152.     [EAGLContext setCurrentContext:self.context];
  153.    
  154.     glDeleteBuffers(1, &_vertexBuffer);
  155.     glDeleteBuffers(1, &_indexBuffer);
  156.     self.effect = nil;
  157.  
  158. }
  159.  
  160. -(void)update
  161. {
  162.     GLKMatrix4 modelMatrix = GLKMatrix4MakeTranslation(0, 0, -6);
  163.    
  164.     modelMatrix =   GLKMatrix4Rotate(modelMatrix,rotation,1.0f,1.0f,0.7f);
  165.    
  166.     modelMatrix = GLKMatrix4Rotate(modelMatrix, GLKMathDegreesToRadians(rotateX), 1.0, 0.0, 0.0);
  167.     modelMatrix = GLKMatrix4Rotate(modelMatrix, GLKMathDegreesToRadians(rotateY), 0.0, 1.0, 0.0);
  168.     modelMatrix = GLKMatrix4Rotate(modelMatrix, GLKMathDegreesToRadians(rotateZ), 0.0, 0.0, 1.0);
  169.     modelMatrix = GLKMatrix4Scale(modelMatrix, 0.33 * scale, 0.33 * scale, 0.33 * scale);
  170.  
  171.     self.effect.transform.modelviewMatrix = modelMatrix;
  172. //    rotation += self.timeSinceLastUpdate * 1.0f;
  173.  
  174. }
  175.  
  176. #pragma mark - GLKView and GLKViewController delegate methods
  177.  
  178. - (void)glkView:(GLKView *)view drawInRect:(CGRect)rect
  179. {
  180.    
  181.     glClearColor(0.6f, 0.6f, 0.65f, 1.0f);
  182.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  183.    
  184.     [self.effect prepareToDraw];
  185.     glDrawElements(GL_TRIANGLES, sizeof(Indices)/sizeof(Indices[0]), GL_UNSIGNED_BYTE, 0);
  186.  
  187. }
  188.  
  189. #pragma mark - touches
  190.  
  191. -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
  192. {
  193.  
  194.     UITouch* t = [touches anyObject];
  195.    
  196.     rotateY += ([t locationInView:self.view].x - [t previousLocationInView:self.view].x);
  197.     rotateX += ([t locationInView:self.view].y - [t previousLocationInView:self.view].y);
  198.     if (rotateX > 360)
  199.     {
  200.         rotateX = 0;
  201.     }
  202.     else if (rotateX < 0)
  203.     {
  204.         rotateX = 360;
  205.     }
  206.    
  207.     if (rotateY > 360)
  208.     {
  209.         rotateY = 0;
  210.     }
  211.     else if (rotateY < 0)
  212.     {
  213.         rotateY = 360;
  214.     }
  215.    
  216. }
  217.  
  218. @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement