Guest User

Untitled

a guest
Jul 13th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //
  2. //  HelloGLKitViewController.m
  3. //  HelloGLKit
  4. //
  5. //  Created by Ray Wenderlich on 9/28/11.
  6. //  Copyright (c) 2011 __MyCompanyName__. All rights reserved.
  7. //
  8.  
  9. #import "HelloGLKitViewController.h"
  10.  
  11. typedef struct {
  12.     float Position[3];
  13.     float Color[4];
  14.     float TexCoord[2];
  15. } Vertex;
  16.  
  17. const Vertex Vertices[] = {
  18.     // Front
  19.     {{1, -1, 1}, {1, 0, 0, 1}, {1, 0}},
  20.     {{1, 1, 1}, {0, 1, 0, 1}, {1, 1}},
  21.     {{-1, 1, 1}, {0, 0, 1, 1}, {0, 1}},
  22.     {{-1, -1, 1}, {0, 0, 0, 1}, {0, 0}},
  23.     // Back
  24.     {{1, 1, -1}, {1, 0, 0, 1}, {0, 1}},
  25.     {{-1, -1, -1}, {0, 1, 0, 1}, {1, 0}},
  26.     {{1, -1, -1}, {0, 0, 1, 1}, {0, 0}},
  27.     {{-1, 1, -1}, {0, 0, 0, 1}, {1, 1}},
  28.     // Left
  29.     {{-1, -1, 1}, {1, 0, 0, 1}, {1, 0}},
  30.     {{-1, 1, 1}, {0, 1, 0, 1}, {1, 1}},
  31.     {{-1, 1, -1}, {0, 0, 1, 1}, {0, 1}},
  32.     {{-1, -1, -1}, {0, 0, 0, 1}, {0, 0}},
  33.     // Right
  34.     {{1, -1, -1}, {1, 0, 0, 1}, {1, 0}},
  35.     {{1, 1, -1}, {0, 1, 0, 1}, {1, 1}},
  36.     {{1, 1, 1}, {0, 0, 1, 1}, {0, 1}},
  37.     {{1, -1, 1}, {0, 0, 0, 1}, {0, 0}},
  38.     // Top
  39.     {{1, 1, 1}, {1, 0, 0, 1}, {1, 0}},
  40.     {{1, 1, -1}, {0, 1, 0, 1}, {1, 1}},
  41.     {{-1, 1, -1}, {0, 0, 1, 1}, {0, 1}},
  42.     {{-1, 1, 1}, {0, 0, 0, 1}, {0, 0}},
  43.     // Bottom
  44.     {{1, -1, -1}, {1, 0, 0, 1}, {1, 0}},
  45.     {{1, -1, 1}, {0, 1, 0, 1}, {1, 1}},
  46.     {{-1, -1, 1}, {0, 0, 1, 1}, {0, 1}},
  47.     {{-1, -1, -1}, {0, 0, 0, 1}, {0, 0}}
  48. };
  49.  
  50. const GLubyte Indices[] = {
  51.     // Front
  52.     0, 1, 2,
  53.     2, 3, 0,
  54.     // Back
  55.     4, 6, 5,
  56.     4, 5, 7,
  57.     // Left
  58.     8, 9, 10,
  59.     10, 11, 8,
  60.     // Right
  61.     12, 13, 14,
  62.     14, 15, 12,
  63.     // Top
  64.     16, 17, 18,
  65.     18, 19, 16,
  66.     // Bottom
  67.     20, 21, 22,
  68.     22, 23, 20
  69. };
  70.  
  71. @interface HelloGLKitViewController () {
  72.     float _curRed;
  73.     BOOL _increasing;
  74.     GLuint _vertexBuffer;
  75.     GLuint _indexBuffer;  
  76.     GLuint _vertexArray;
  77.     float _rotation;
  78. }
  79. @property (strong, nonatomic) EAGLContext *context;
  80. @property (strong, nonatomic) GLKBaseEffect *effect;
  81.  
  82. @end
  83.  
  84. @implementation HelloGLKitViewController
  85. @synthesize context = _context;
  86. @synthesize effect = _effect;
  87. // Rest of file...
  88.  
  89. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
  90. {
  91.     self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
  92.     if (self) {
  93.         // Custom initialization
  94.     }
  95.     return self;
  96. }
  97.  
  98. - (void)didReceiveMemoryWarning
  99. {
  100.     // Releases the view if it doesn't have a superview.
  101.     [super didReceiveMemoryWarning];
  102.    
  103.     // Release any cached data, images, etc that aren't in use.
  104. }
  105.  
  106. #pragma mark - View lifecycle
  107.  
  108. /*
  109. // Implement loadView to create a view hierarchy programmatically, without using a nib.
  110. - (void)loadView
  111. {
  112. }
  113. */
  114.  
  115. - (void)setupGL {
  116.    
  117.     [EAGLContext setCurrentContext:self.context];
  118.     glEnable(GL_CULL_FACE);
  119.    
  120.     self.effect = [[GLKBaseEffect alloc] init];
  121.    
  122.     NSDictionary * options = [NSDictionary dictionaryWithObjectsAndKeys:
  123.                               [NSNumber numberWithBool:YES],
  124.                               GLKTextureLoaderOriginBottomLeft,
  125.                               nil];
  126.  
  127.     NSError * error;    
  128.     NSString *path = [[NSBundle mainBundle] pathForResource:@"tile_floor" ofType:@"png"];
  129.     GLKTextureInfo * info = [GLKTextureLoader textureWithContentsOfFile:path options:options error:&error];
  130.     if (info == nil) {
  131.         NSLog(@"Error loading file: %@", [error localizedDescription]);
  132.     }
  133.     self.effect.texture2d0.name = info.name;
  134.     self.effect.texture2d0.enabled = true;
  135.    
  136.     // New lines
  137.     glGenVertexArraysOES(1, &_vertexArray);
  138.     glBindVertexArrayOES(_vertexArray);
  139.    
  140.     // Old stuff
  141.     glGenBuffers(1, &_vertexBuffer);
  142.     glBindBuffer(GL_ARRAY_BUFFER, _vertexBuffer);
  143.     glBufferData(GL_ARRAY_BUFFER, sizeof(Vertices), Vertices, GL_STATIC_DRAW);
  144.    
  145.     glGenBuffers(1, &_indexBuffer);
  146.     glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _indexBuffer);
  147.     glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(Indices), Indices, GL_STATIC_DRAW);
  148.    
  149.     // New lines (were previously in draw)
  150.     glEnableVertexAttribArray(GLKVertexAttribPosition);        
  151.     glVertexAttribPointer(GLKVertexAttribPosition, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (const GLvoid *) offsetof(Vertex, Position));
  152.     glEnableVertexAttribArray(GLKVertexAttribColor);
  153.     glVertexAttribPointer(GLKVertexAttribColor, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex), (const GLvoid *) offsetof(Vertex, Color));
  154.    
  155.     glEnableVertexAttribArray(GLKVertexAttribTexCoord0);
  156.     glVertexAttribPointer(GLKVertexAttribTexCoord0, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (const GLvoid *) offsetof(Vertex, TexCoord));
  157.    
  158.     // New line
  159.     glBindVertexArrayOES(0);
  160.    
  161. }
  162.  
  163. - (void)tearDownGL {
  164.    
  165.     [EAGLContext setCurrentContext:self.context];
  166.    
  167.     glDeleteBuffers(1, &_vertexBuffer);
  168.     glDeleteBuffers(1, &_indexBuffer);
  169.     //glDeleteVertexArraysOES(1, &_vertexArray);
  170.    
  171.     self.effect = nil;    
  172.    
  173. }
  174.  
  175. - (void)viewDidLoad
  176. {
  177.     [super viewDidLoad];
  178.  
  179.     self.context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
  180.  
  181.     if (!self.context) {
  182.         NSLog(@"Failed to create ES context");
  183.     }
  184.  
  185.     GLKView *view = (GLKView *)self.view;
  186.     view.context = self.context;
  187.     view.drawableMultisample = GLKViewDrawableMultisample4X;
  188.     [self setupGL];
  189. }
  190.  
  191. - (void)viewDidUnload
  192. {
  193.     [super viewDidUnload];
  194.    
  195.     [self tearDownGL];
  196.  
  197.     if ([EAGLContext currentContext] == self.context) {
  198.         [EAGLContext setCurrentContext:nil];
  199.     }
  200.     self.context = nil;
  201. }
  202.  
  203. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
  204. {
  205.     // Return YES for supported orientations
  206.     return (interfaceOrientation == UIInterfaceOrientationPortrait);
  207. }
  208.  
  209. #pragma mark - GLKViewDelegate
  210.  
  211. - (void)glkView:(GLKView *)view drawInRect:(CGRect)rect {
  212.    
  213.     glClearColor(_curRed, 0.0, 0.0, 1.0);
  214.     glClear(GL_COLOR_BUFFER_BIT);
  215.  
  216.     [self.effect prepareToDraw];    
  217.    
  218.     glBindVertexArrayOES(_vertexArray);  
  219.     glDrawElements(GL_TRIANGLES, sizeof(Indices)/sizeof(Indices[0]), GL_UNSIGNED_BYTE, 0);
  220.    
  221. }
  222.  
  223. #pragma mark - GLKViewControllerDelegate
  224.  
  225. - (void)update {
  226.     if (_increasing) {
  227.         _curRed += 1.0 * self.timeSinceLastUpdate;
  228.     } else {
  229.         _curRed -= 1.0 * self.timeSinceLastUpdate;
  230.     }
  231.     if (_curRed >= 1.0) {
  232.         _curRed = 1.0;
  233.         _increasing = NO;
  234.     }
  235.     if (_curRed <= 0.0) {
  236.         _curRed = 0.0;
  237.         _increasing = YES;
  238.     }
  239.    
  240.     float aspect = fabsf(self.view.bounds.size.width / self.view.bounds.size.height);
  241.     GLKMatrix4 projectionMatrix = GLKMatrix4MakePerspective(GLKMathDegreesToRadians(65.0f), aspect, 4.0f, 10.0f);    
  242.     self.effect.transform.projectionMatrix = projectionMatrix;
  243.    
  244.     GLKMatrix4 modelViewMatrix = GLKMatrix4MakeTranslation(0.0f, 0.0f, -6.0f);  
  245.     _rotation += 90 * self.timeSinceLastUpdate;
  246.     modelViewMatrix = GLKMatrix4Rotate(modelViewMatrix, GLKMathDegreesToRadians(25), 1, 0, 0);
  247.     modelViewMatrix = GLKMatrix4Rotate(modelViewMatrix, GLKMathDegreesToRadians(_rotation), 0, 1, 0);    
  248.     self.effect.transform.modelviewMatrix = modelViewMatrix;
  249.    
  250. }
  251.  
  252. - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
  253.     NSLog(@"timeSinceLastUpdate: %f", self.timeSinceLastUpdate);
  254.     NSLog(@"timeSinceLastDraw: %f", self.timeSinceLastDraw);
  255.     NSLog(@"timeSinceFirstResume: %f", self.timeSinceFirstResume);
  256.     NSLog(@"timeSinceLastResume: %f", self.timeSinceLastResume);
  257.     self.paused = !self.paused;
  258. }
  259.  
  260. @end
Add Comment
Please, Sign In to add comment