Guest User

Untitled

a guest
Sep 18th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.58 KB | None | 0 0
  1. iOS OpenGL 2D rotating images (not screen)
  2. self.effect.transform.modelviewMatrix = GLKMatrix4Rotate(self.effect.transform.modelviewMatrix, radians(10), 0, 0, -1);
  3.  
  4. //
  5. // SGGViewController.m
  6. // SimpleGLKitGame
  7. //
  8. // Created by Ray Wenderlich on 1/30/12.
  9. // Copyright (c) 2012 __MyCompanyName__. All rights reserved.
  10. //
  11.  
  12. #import "SGGViewController.h"
  13. #import "SGGSprite.h"
  14.  
  15. @interface SGGViewController ()
  16. @property (strong, nonatomic) EAGLContext *context;
  17. @property (strong) GLKBaseEffect * effect;
  18. @property (strong) SGGSprite * player;
  19. @property (strong) NSMutableArray * children;
  20. @property (assign) float timeSinceLastSpawn;
  21. @end
  22.  
  23. @implementation SGGViewController
  24. @synthesize effect = _effect;
  25. @synthesize context = _context;
  26. @synthesize player = _player;
  27. @synthesize children = _children;
  28. @synthesize timeSinceLastSpawn = _timeSinceLastSpawn;
  29.  
  30. - (void)viewDidLoad
  31. {
  32. [super viewDidLoad];
  33.  
  34. self.context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
  35.  
  36.  
  37. if (!self.context) {
  38. NSLog(@"Failed to create ES context");
  39. }
  40.  
  41. GLKView *view = (GLKView *)self.view;
  42. view.context = self.context;
  43. [EAGLContext setCurrentContext:self.context];
  44.  
  45. self.effect = [[GLKBaseEffect alloc] init];
  46.  
  47. GLKMatrix4 projectionMatrix = GLKMatrix4MakeOrtho(0, 1024, 0, 768, -1, 1);
  48. self.effect.transform.projectionMatrix = projectionMatrix;
  49.  
  50. self.player = [[SGGSprite alloc] initWithFile:@"2.png" effect:self.effect];
  51. self.player.position = GLKVector2Make(self.player.contentSize.width/2, 160);
  52.  
  53. self.children = [NSMutableArray array];
  54. [self.children addObject:self.player];
  55.  
  56.  
  57. }
  58.  
  59. - (void)addTarget {
  60. SGGSprite * target = [[SGGSprite alloc] initWithFile:@"Target.png" effect:self.effect];
  61. [self.children addObject:target];
  62.  
  63. int minY = target.contentSize.height/2;
  64. int maxY = 320 - target.contentSize.height/2;
  65. int rangeY = maxY - minY;
  66. int actualY = (arc4random() % rangeY) + minY;
  67.  
  68. target.position = GLKVector2Make(480 + (target.contentSize.width/2), actualY);
  69.  
  70. int minVelocity = 480.0/4.0;
  71. int maxVelocity = 480.0/2.0;
  72. int rangeVelocity = maxVelocity - minVelocity;
  73. int actualVelocity = (arc4random() % rangeVelocity) + minVelocity;
  74.  
  75. target.moveVelocity = GLKVector2Make(-actualVelocity, 0);
  76. }
  77.  
  78. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
  79. {
  80. return UIInterfaceOrientationIsLandscape(interfaceOrientation);
  81. }
  82.  
  83. #pragma mark - GLKViewDelegate
  84.  
  85.  
  86. static inline double radians (double degrees) {return degrees * M_PI/180;}
  87.  
  88.  
  89. - (void)glkView:(GLKView *)view drawInRect:(CGRect)rect {
  90.  
  91.  
  92.  
  93. glClearColor(1, 1, 1, 1);
  94. glClear(GL_COLOR_BUFFER_BIT);
  95. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  96. glEnable(GL_BLEND);
  97.  
  98. for (SGGSprite * sprite in self.children) {
  99. [sprite render];
  100. }
  101. }
  102.  
  103. - (void)update {
  104.  
  105. self.timeSinceLastSpawn += self.timeSinceLastUpdate;
  106. if (self.timeSinceLastSpawn > 1.0) {
  107. self.timeSinceLastSpawn = 0;
  108. [self addTarget];
  109. }
  110.  
  111. for (SGGSprite * sprite in self.children) {
  112. [sprite update:self.timeSinceLastUpdate];
  113. }
  114. }
  115.  
  116. @end
  117.  
  118. //
  119. // SGGSprite.m
  120. // SimpleGLKitGame
  121. //
  122. // Created by Ray Wenderlich on 1/30/12.
  123. // Copyright (c) 2012 __MyCompanyName__. All rights reserved.
  124. //
  125.  
  126. #import "SGGSprite.h"
  127.  
  128. typedef struct {
  129. CGPoint geometryVertex;
  130. CGPoint textureVertex;
  131. } TexturedVertex;
  132.  
  133. typedef struct {
  134. TexturedVertex bl;
  135. TexturedVertex br;
  136. TexturedVertex tl;
  137. TexturedVertex tr;
  138. } TexturedQuad;
  139.  
  140. @interface SGGSprite()
  141.  
  142. @property (strong) GLKBaseEffect * effect;
  143. @property (assign) TexturedQuad quad;
  144. @property (strong) GLKTextureInfo * textureInfo;
  145.  
  146. @end
  147.  
  148. @implementation SGGSprite
  149. @synthesize position = _position;
  150. @synthesize contentSize = _contentSize;
  151. @synthesize effect = _effect;
  152. @synthesize quad = _quad;
  153. @synthesize textureInfo = _textureInfo;
  154. @synthesize moveVelocity = _moveVelocity;
  155.  
  156. - (id)initWithFile:(NSString *)fileName effect:(GLKBaseEffect *)effect {
  157. if ((self = [super init])) {
  158. self.effect = effect;
  159.  
  160. NSDictionary * options = [NSDictionary dictionaryWithObjectsAndKeys:
  161. [NSNumber numberWithBool:YES],
  162. GLKTextureLoaderOriginBottomLeft,
  163. nil];
  164.  
  165. NSError * error;
  166. NSString *path = [[NSBundle mainBundle] pathForResource:fileName ofType:nil];
  167. self.textureInfo = [GLKTextureLoader textureWithContentsOfFile:path options:options error:&error];
  168. if (self.textureInfo == nil) {
  169. NSLog(@"Error loading file: %@", [error localizedDescription]);
  170. return nil;
  171. }
  172.  
  173. self.contentSize = CGSizeMake(self.textureInfo.width, self.textureInfo.height);
  174.  
  175. TexturedQuad newQuad;
  176. newQuad.bl.geometryVertex = CGPointMake(0, 0);
  177. newQuad.br.geometryVertex = CGPointMake(self.textureInfo.width, 0);
  178. newQuad.tl.geometryVertex = CGPointMake(0, self.textureInfo.height);
  179. newQuad.tr.geometryVertex = CGPointMake(self.textureInfo.width, self.textureInfo.height);
  180.  
  181. newQuad.bl.textureVertex = CGPointMake(0, 0);
  182. newQuad.br.textureVertex = CGPointMake(1, 0);
  183. newQuad.tl.textureVertex = CGPointMake(0, 1);
  184. newQuad.tr.textureVertex = CGPointMake(1, 1);
  185. self.quad = newQuad;
  186.  
  187. }
  188. return self;
  189. }
  190.  
  191. - (GLKMatrix4) modelMatrix {
  192.  
  193. GLKMatrix4 modelMatrix = GLKMatrix4Identity;
  194. modelMatrix = GLKMatrix4Translate(modelMatrix, self.position.x, self.position.y, 0);
  195. modelMatrix = GLKMatrix4Translate(modelMatrix, -self.contentSize.width/2, -self.contentSize.height/2, 0);
  196. return modelMatrix;
  197.  
  198. }
  199.  
  200.  
  201. static inline double radians (double degrees) {return degrees * M_PI/180;}
  202.  
  203. - (void)render {
  204.  
  205. self.effect.texture2d0.name = self.textureInfo.name;
  206. self.effect.texture2d0.enabled = YES;
  207. self.effect.transform.modelviewMatrix = self.modelMatrix;
  208.  
  209. [self.effect prepareToDraw];
  210. long offset = (long)&_quad;
  211.  
  212.  
  213. glEnableVertexAttribArray(GLKVertexAttribPosition);
  214. glEnableVertexAttribArray(GLKVertexAttribTexCoord0);
  215.  
  216. glVertexAttribPointer(GLKVertexAttribPosition, 2, GL_FLOAT, GL_FALSE, sizeof(TexturedVertex), (void *) (offset + offsetof(TexturedVertex, geometryVertex)));
  217. glVertexAttribPointer(GLKVertexAttribTexCoord0, 2, GL_FLOAT, GL_FALSE, sizeof(TexturedVertex), (void *) (offset + offsetof(TexturedVertex, textureVertex)));
  218.  
  219. glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
  220.  
  221. }
  222.  
  223. - (void)update:(float)dt {
  224.  
  225. GLKVector2 curMove = GLKVector2MultiplyScalar(self.moveVelocity, dt);
  226.  
  227.  
  228.  
  229. self.position = GLKVector2Add(self.position, curMove);
  230.  
  231. }
  232.  
  233. @end
Add Comment
Please, Sign In to add comment