Guest User

Untitled

a guest
Sep 20th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.25 KB | None | 0 0
  1.  
  2. #import <QuartzCore/QuartzCore.h>
  3. #import <OpenGLES/ES1/glext.h>
  4. #import "GLView2D.h"
  5. #import "GLAppDelegate.h"
  6. #import "Util.h"
  7. #include <sys/sysctl.h>
  8.  
  9.  
  10. @interface GLView2D ()
  11. - (BOOL) _createContext;
  12. - (BOOL) _createSurface;
  13. - (void) _destroySurface;
  14. @end
  15.  
  16.  
  17. @implementation GLView2D
  18.  
  19. @synthesize size = _size;
  20. @synthesize orientation = _orientation;
  21. @synthesize autoRotation = _autoRotation;
  22. @synthesize isSlowDevice = _isSlowDevice;
  23.  
  24. + (Class) layerClass {
  25. return [CAEAGLLayer class];
  26. }
  27.  
  28. - (id) init {
  29. if (!(self = [super initWithFrame:CGRectMake(0, 0, 320, 480)])) return nil;
  30.  
  31. size_t size;
  32. sysctlbyname("hw.machine", NULL, &size, NULL, 0);
  33. char *machine = malloc(size);
  34. sysctlbyname("hw.machine", machine, &size, NULL, 0);
  35. NSString *platform = [NSString stringWithCString:machine encoding: NSUTF8StringEncoding];
  36. free(machine);
  37. _isSlowDevice =
  38. [platform isEqualToString:@"iPhone1,1"] || // iPhone 1G
  39. [platform isEqualToString:@"iPhone1,2"] || // iPhone 3G
  40. [platform isEqualToString:@"iPpod1,1"] || // iPod Touch 1G
  41. [platform isEqualToString:@"iPod2,1"]; // iPod Touch 2G
  42.  
  43. _orientation = UIDeviceOrientationPortrait;
  44.  
  45. if (![self _createContext]) {
  46. NSLog(@"Error creating OpenGL context.");
  47. [self release];
  48. return nil;
  49. }
  50.  
  51. if ([[[UIDevice currentDevice] systemVersion] compare:@"3.1" options:NSNumericSearch] != NSOrderedAscending) {
  52. _displayLink = [NSClassFromString(@"CADisplayLink") displayLinkWithTarget:self selector:@selector(tick)];
  53. [_displayLink setFrameInterval:_isSlowDevice ? 2 : 1];
  54. [_displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
  55. [_displayLink setPaused:YES];
  56. } else {
  57. NSLog(@"WARNING: Running on pre-3.1 OS.");
  58. _timer = [[Timer alloc] initWithTicksPerSecond:_isSlowDevice ? 30 : 60 listener:self];
  59. }
  60. return self;
  61. }
  62.  
  63. - (BOOL) _createContext {
  64. CAEAGLLayer *eaglLayer = (CAEAGLLayer*)[self layer];
  65. [eaglLayer setDrawableProperties:
  66. [NSDictionary dictionaryWithObjectsAndKeys:
  67. [NSNumber numberWithBool:NO], kEAGLDrawablePropertyRetainedBacking,
  68. kEAGLColorFormatRGB565, kEAGLDrawablePropertyColorFormat, // kEAGLColorFormatRGBA8?
  69. nil
  70. ]
  71. ];
  72.  
  73. _context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES1];
  74. if (!_context) return NO;
  75.  
  76. if (![self _createSurface]) return NO;
  77.  
  78. glEnableClientState(GL_VERTEX_ARRAY);
  79. glEnable(GL_SCISSOR_TEST);
  80. glEnable(GL_TEXTURE_2D);
  81. glEnableClientState(GL_TEXTURE_COORD_ARRAY);
  82.  
  83. glDisable(GL_FOG);
  84. glDisable(GL_DEPTH_TEST);
  85.  
  86. glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
  87. glEnable(GL_BLEND);
  88.  
  89. glClearColor(0, 0, 0, 1);
  90. glClear(GL_COLOR_BUFFER_BIT);
  91.  
  92. return YES;
  93. }
  94.  
  95. - (BOOL) _createSurface {
  96. if (![EAGLContext setCurrentContext:_context]) return NO;
  97.  
  98. GLuint oldRenderbuffer, oldFramebuffer;
  99. glGetIntegerv(GL_RENDERBUFFER_BINDING_OES, (GLint *)&oldRenderbuffer);
  100. glGetIntegerv(GL_FRAMEBUFFER_BINDING_OES, (GLint *)&oldFramebuffer);
  101.  
  102. glGenRenderbuffersOES(1, &_renderBuffer);
  103. glBindRenderbufferOES(GL_RENDERBUFFER_OES, _renderBuffer);
  104.  
  105. CAEAGLLayer *eaglLayer = (CAEAGLLayer*)[self layer];
  106. if (![_context renderbufferStorage:GL_RENDERBUFFER_OES fromDrawable:eaglLayer]) {
  107. glDeleteRenderbuffersOES(1, &_renderBuffer);
  108. glBindRenderbufferOES(GL_RENDERBUFFER_BINDING_OES, oldRenderbuffer);
  109. return NO;
  110. }
  111.  
  112. glGenFramebuffersOES(1, &_frameBuffer);
  113. glBindFramebufferOES(GL_FRAMEBUFFER_OES, _frameBuffer);
  114. glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, _renderBuffer);
  115.  
  116. glBindRenderbufferOES(GL_RENDERBUFFER_OES, oldRenderbuffer);
  117.  
  118. [self setupSurface];
  119.  
  120. return YES;
  121. }
  122.  
  123. - (void) setupSurface {
  124. _size = [self bounds].size;
  125. _size.width = roundf(_size.width);
  126. _size.height = roundf(_size.height);
  127.  
  128. glDisable(GL_DITHER);
  129. glDisable(GL_DEPTH_TEST);
  130. glShadeModel(GL_FLAT);
  131. glViewport(0, 0, _size.width, _size.height);
  132. glScissor(0, 0, _size.width, _size.height);
  133.  
  134. [self orthographic];
  135. }
  136.  
  137. - (void) _destroySurface {
  138. [EAGLContext setCurrentContext:_context];
  139.  
  140. glDeleteRenderbuffersOES(1, &_renderBuffer);
  141. _renderBuffer = 0;
  142.  
  143. glDeleteFramebuffersOES(1, &_frameBuffer);
  144. _frameBuffer = 0;
  145. }
  146.  
  147. // Called when the view is resized.
  148. - (void) layoutSubviews {
  149. CGRect bounds = [self bounds];
  150. if (roundf(bounds.size.width) == _size.width) return;
  151. if (roundf(bounds.size.height) == _size.height) return;
  152. [self _destroySurface];
  153. [self _createSurface];
  154. [self setupSurface];
  155. }
  156.  
  157. - (void) dealloc {
  158. [_timer release];
  159. [fpsFont release];
  160. [self _destroySurface];
  161. [EAGLContext setCurrentContext:nil];
  162. [_context release]; _context = nil;
  163. [super dealloc];
  164. }
  165.  
  166. - (GLAppDelegate *) app {
  167. return [[UIApplication sharedApplication] delegate];
  168. }
  169.  
  170. - (void) setShowFPS:(BOOL)showFPS font:(Font*)font {
  171. if (showFPS == _showFPS) return;
  172. _showFPS = showFPS;
  173. [fpsFont release];
  174. fpsFont = nil;
  175. if (_showFPS) {
  176. fpsFont = [font retain];
  177. if (!fpsFont) _showFPS = NO;
  178. }
  179. }
  180.  
  181. @end
  182.  
  183.  
  184. @implementation GLView2D (Update)
  185.  
  186. - (void) _setOrientationToDevice {
  187. UIDeviceOrientation deviceOrientation = [[UIDevice currentDevice] orientation];
  188. if (deviceOrientation == _orientation) return;
  189. if (
  190. deviceOrientation != UIDeviceOrientationPortrait &&
  191. deviceOrientation != UIDeviceOrientationPortraitUpsideDown &&
  192. deviceOrientation != UIDeviceOrientationLandscapeLeft &&
  193. deviceOrientation != UIDeviceOrientationLandscapeRight
  194. ) return;
  195. _orientation = deviceOrientation;
  196. [self setupSurface];
  197. }
  198.  
  199. - (void) setOrientation:(UIDeviceOrientation)orientation {
  200. _orientation = orientation;
  201. [self setupSurface];
  202. }
  203.  
  204. - (void) tick {
  205. double timestamp = [_displayLink timestamp];
  206. [self tick:timestamp - _lastTimestamp];
  207. _lastTimestamp = timestamp;
  208. }
  209.  
  210. - (void) tick:(double)delta {
  211. //if (_autoRotation) [self _setOrientationToDevice];
  212.  
  213. glPushMatrix();
  214. [self update:delta];
  215. glPopMatrix();
  216.  
  217. if (_showFPS) {
  218. fpsTime -= delta;
  219. if (fpsTime <= 0) {
  220. fpsTime = 1;
  221. fps = frameCount;
  222. frameCount = 0;
  223. NSLog(@"fps %d", fps);
  224. } else
  225. frameCount++;
  226. glColor4ub(255, 255, 255, 255);
  227. if (fps && NO)
  228. [fpsFont drawTextAtPoint:CGPointMake(10, 10) text:[NSString stringWithFormat:@"%d", fps]];
  229. }
  230.  
  231. [self swapBuffers];
  232. }
  233.  
  234. - (void) update:(double)delta {
  235. }
  236.  
  237. - (void) start {
  238. [_displayLink setPaused:NO];
  239. [_timer start];
  240. }
  241.  
  242. - (void) stop {
  243. [_displayLink setPaused:YES];
  244. [_timer stop];
  245. }
  246.  
  247. - (void) shutdown {
  248. [_displayLink invalidate];
  249. _displayLink = nil;
  250. [_timer stop];
  251. }
  252.  
  253. - (void) setAutoRotation:(BOOL)autoRotation {
  254. if (autoRotation == _autoRotation) return;
  255. _autoRotation = autoRotation;
  256. if (_autoRotation) {
  257. [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
  258. [self _setOrientationToDevice];
  259. } else
  260. [[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];
  261. }
  262.  
  263.  
  264. - (BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
  265. {
  266. return (interfaceOrientation==UIInterfaceOrientationLandscapeLeft ||
  267. interfaceOrientation==UIInterfaceOrientationLandscapeRight) ? YES : NO;
  268. }
  269.  
  270. @end
  271.  
  272.  
  273. @implementation GLView2D (Drawing)
  274.  
  275. - (void) swapBuffers {
  276. [EAGLContext setCurrentContext:_context];
  277. glBindRenderbufferOES(GL_RENDERBUFFER_OES, _renderBuffer);
  278. if (![_context presentRenderbuffer:GL_RENDERBUFFER_OES])
  279. ERROR("Failed to swap render buffer.");
  280. }
  281.  
  282. - (void) clear {
  283. glClear(GL_COLOR_BUFFER_BIT);
  284. }
  285.  
  286. - (void) orthographic {
  287. glMatrixMode(GL_PROJECTION);
  288. glLoadIdentity();
  289.  
  290. if (_orientation == UIDeviceOrientationPortraitUpsideDown) {
  291. glRotatef(180, 0, 0, 1);
  292. } else if (_orientation == UIDeviceOrientationLandscapeLeft) {
  293. CGFloat temp = _size.width;
  294. _size.width = _size.height;
  295. _size.height = temp;
  296. glRotatef(90, 0, 0, 1);
  297. } else if (_orientation == UIDeviceOrientationLandscapeRight) {
  298. CGFloat temp = _size.width;
  299. _size.width = _size.height;
  300. _size.height = temp;
  301. glRotatef(-90, 0, 0, 1);
  302. }
  303. glOrthof(0, _size.width, _size.height, 0, -1, 1);
  304.  
  305. glMatrixMode(GL_MODELVIEW);
  306. glLoadIdentity();
  307. }
  308.  
  309. - (void) perspectiveWithFOV:(double)fovy aspect:(double)aspect zNear:(double)zNear zFar:(double)zFar {
  310.  
  311. if (_orientation == UIDeviceOrientationPortraitUpsideDown) {
  312. glRotatef(180, 0, 0, 1);
  313. } else if (_orientation == UIDeviceOrientationLandscapeLeft) {
  314. glRotatef(90, 0, 0, 1);
  315. } else if (_orientation == UIDeviceOrientationLandscapeRight) {
  316. glRotatef(-90, 0, 0, 1);
  317. }
  318.  
  319. double ymax = zNear * tan(fovy * M_PI / 360.0);
  320. double ymin = -ymax;
  321. double xmin = ymin * aspect;
  322. double xmax = ymax * aspect;
  323. glFrustumf(xmin, xmax, ymin, ymax, zNear, zFar);
  324. }
  325.  
  326. - (void) lookAtEyex:(GLfloat)eyex eyey:(GLfloat)eyey eyez:(GLfloat)eyez
  327. centerx:(GLfloat)centerx centery:(GLfloat)centery centerz:(GLfloat)centerz
  328. upx:(GLfloat)upx upy:(GLfloat)upy upz:(GLfloat)upz
  329. {
  330. GLfloat m[16];
  331. GLfloat x[3], y[3], z[3];
  332. GLfloat mag;
  333.  
  334. // Make rotation matrix.
  335.  
  336. // Z vector
  337. z[0] = eyex - centerx;
  338. z[1] = eyey - centery;
  339. z[2] = eyez - centerz;
  340. mag = sqrtf(z[0] * z[0] + z[1] * z[1] + z[2] * z[2]);
  341. if (mag) { // mpichler, 19950515
  342. z[0] /= mag;
  343. z[1] /= mag;
  344. z[2] /= mag;
  345. }
  346.  
  347. // Y vector
  348. y[0] = upx;
  349. y[1] = upy;
  350. y[2] = upz;
  351.  
  352. // X vector = Y cross Z
  353. x[0] = y[1] * z[2] - y[2] * z[1];
  354. x[1] = -y[0] * z[2] + y[2] * z[0];
  355. x[2] = y[0] * z[1] - y[1] * z[0];
  356.  
  357. // Recompute Y = Z cross X
  358. y[0] = z[1] * x[2] - z[2] * x[1];
  359. y[1] = -z[0] * x[2] + z[2] * x[0];
  360. y[2] = z[0] * x[1] - z[1] * x[0];
  361.  
  362. // mpichler, 19950515
  363. // cross product gives area of parallelogram, which is < 1.0 for
  364. // non-perpendicular unit-length vectors; so normalize x, y here
  365.  
  366. mag = sqrt(x[0] * x[0] + x[1] * x[1] + x[2] * x[2]);
  367. if (mag) {
  368. x[0] /= mag;
  369. x[1] /= mag;
  370. x[2] /= mag;
  371. }
  372. mag = sqrt(y[0] * y[0] + y[1] * y[1] + y[2] * y[2]);
  373. if (mag) {
  374. y[0] /= mag;
  375. y[1] /= mag;
  376. y[2] /= mag;
  377. }
  378.  
  379. #define M(row,col) m[col*4+row]
  380. M(0, 0) = x[0];
  381. M(0, 1) = x[1];
  382. M(0, 2) = x[2];
  383. M(0, 3) = 0.0;
  384. M(1, 0) = y[0];
  385. M(1, 1) = y[1];
  386. M(1, 2) = y[2];
  387. M(1, 3) = 0.0;
  388. M(2, 0) = z[0];
  389. M(2, 1) = z[1];
  390. M(2, 2) = z[2];
  391. M(2, 3) = 0.0;
  392. M(3, 0) = 0.0;
  393. M(3, 1) = 0.0;
  394. M(3, 2) = 0.0;
  395. M(3, 3) = 1.0;
  396. #undef M
  397. glMultMatrixf(m);
  398.  
  399. // Translate eye to origin.
  400. glTranslatef(-eyex, -eyey, -eyez);
  401. }
  402.  
  403. @end
Add Comment
Please, Sign In to add comment