istereo2
annotate src/ios/viewctl.m @ 4:d4fed8aac9a6
fixed all sideways crap in the effect code
fixed fov on all orientations
fixed inverted tangent vector in bump mapped version
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Mon, 21 Sep 2015 21:12:36 +0300 |
parents | dc735bdeeb8a |
children | 6a39b8912752 |
rev | line source |
---|---|
nuclear@2 | 1 #include <stdlib.h> |
nuclear@2 | 2 #import <OpenGLES/ES2/glext.h> |
nuclear@0 | 3 #import "viewctl.h" |
nuclear@2 | 4 #include "istereo.h" |
nuclear@0 | 5 |
nuclear@0 | 6 @interface ViewController () { |
nuclear@0 | 7 EAGLContext *ctx; |
nuclear@3 | 8 float pixel_scale; |
nuclear@1 | 9 |
nuclear@1 | 10 ADBannerView *ad; |
nuclear@1 | 11 BOOL ad_visible; |
nuclear@0 | 12 } |
nuclear@0 | 13 |
nuclear@1 | 14 |
nuclear@1 | 15 - (void)create_ad; |
nuclear@2 | 16 - (void)show_ad; |
nuclear@2 | 17 - (void)hide_ad; |
nuclear@1 | 18 |
nuclear@1 | 19 - (void)init_gl; |
nuclear@1 | 20 - (void)destroy_gl; |
nuclear@0 | 21 @end |
nuclear@0 | 22 |
nuclear@0 | 23 @implementation ViewController |
nuclear@0 | 24 |
nuclear@0 | 25 - (void)viewDidLoad |
nuclear@0 | 26 { |
nuclear@0 | 27 [super viewDidLoad]; |
nuclear@0 | 28 |
nuclear@3 | 29 |
nuclear@0 | 30 self->ctx = [[EAGLContext alloc] initWithAPI: kEAGLRenderingAPIOpenGLES2]; |
nuclear@0 | 31 if(!self->ctx) { |
nuclear@0 | 32 NSLog(@"Failed to create OpenGL ES 2.0 context"); |
nuclear@0 | 33 } |
nuclear@0 | 34 |
nuclear@0 | 35 GLKView *view = (GLKView*)self.view; |
nuclear@0 | 36 view.context = self->ctx; |
nuclear@0 | 37 view.drawableDepthFormat = GLKViewDrawableDepthFormat24; |
nuclear@0 | 38 |
nuclear@3 | 39 if([view respondsToSelector: NSSelectorFromString(@"contentScaleFactor")]) { |
nuclear@3 | 40 pixel_scale = [[UIScreen mainScreen] scale]; |
nuclear@3 | 41 view.contentScaleFactor = pixel_scale; |
nuclear@3 | 42 printf("pixel scale: %g\n", pixel_scale); |
nuclear@3 | 43 } else { |
nuclear@3 | 44 pixel_scale = 1.0f; |
nuclear@3 | 45 } |
nuclear@3 | 46 |
nuclear@1 | 47 [self create_ad]; |
nuclear@1 | 48 |
nuclear@1 | 49 [self init_gl]; |
nuclear@0 | 50 } |
nuclear@0 | 51 |
nuclear@0 | 52 - (void)dealloc |
nuclear@0 | 53 { |
nuclear@1 | 54 [self destroy_gl]; |
nuclear@0 | 55 |
nuclear@0 | 56 if([EAGLContext currentContext] == self->ctx) { |
nuclear@0 | 57 [EAGLContext setCurrentContext: nil]; |
nuclear@0 | 58 } |
nuclear@0 | 59 } |
nuclear@0 | 60 |
nuclear@0 | 61 - (void)didReceiveMemoryWarning |
nuclear@0 | 62 { |
nuclear@0 | 63 [super didReceiveMemoryWarning]; |
nuclear@0 | 64 |
nuclear@0 | 65 if([self isViewLoaded] && ([[self view] window] == nil)) { |
nuclear@0 | 66 self.view = nil; |
nuclear@0 | 67 |
nuclear@1 | 68 [self destroy_gl]; |
nuclear@0 | 69 |
nuclear@0 | 70 if([EAGLContext currentContext] == self->ctx) { |
nuclear@0 | 71 [EAGLContext setCurrentContext: nil]; |
nuclear@0 | 72 } |
nuclear@0 | 73 self->ctx = nil; |
nuclear@0 | 74 } |
nuclear@0 | 75 |
nuclear@0 | 76 // Dispose of any resources that can be recreated. |
nuclear@0 | 77 } |
nuclear@0 | 78 |
nuclear@0 | 79 - (BOOL)prefersStatusBarHidden |
nuclear@0 | 80 { |
nuclear@0 | 81 return YES; |
nuclear@0 | 82 } |
nuclear@0 | 83 |
nuclear@4 | 84 - (BOOL)shouldAutorotate |
nuclear@4 | 85 { |
nuclear@4 | 86 return YES; |
nuclear@4 | 87 } |
nuclear@4 | 88 |
nuclear@4 | 89 - (UIInterfaceOrientationMask)supportedInterfaceOrientations |
nuclear@4 | 90 { |
nuclear@4 | 91 return UIInterfaceOrientationMaskLandscape; |
nuclear@4 | 92 //return UIInterfaceOrientationMaskAll; |
nuclear@4 | 93 } |
nuclear@4 | 94 |
nuclear@1 | 95 - (void)create_ad |
nuclear@1 | 96 { |
nuclear@1 | 97 ad = [[ADBannerView alloc] initWithAdType: ADAdTypeBanner]; |
nuclear@1 | 98 [ad setAutoresizingMask: UIViewAutoresizingFlexibleWidth]; |
nuclear@2 | 99 ad_visible = YES; |
nuclear@2 | 100 [self hide_ad]; |
nuclear@1 | 101 [self.view addSubview: ad]; |
nuclear@1 | 102 ad.delegate = self; |
nuclear@1 | 103 } |
nuclear@1 | 104 |
nuclear@2 | 105 - (void)show_ad |
nuclear@1 | 106 { |
nuclear@1 | 107 if(!ad_visible) { |
nuclear@1 | 108 CGRect rect = ad.frame; |
nuclear@1 | 109 rect.origin.y = 0; |
nuclear@1 | 110 ad.frame = rect; |
nuclear@1 | 111 ad_visible = YES; |
nuclear@1 | 112 } |
nuclear@1 | 113 } |
nuclear@1 | 114 |
nuclear@2 | 115 - (void)hide_ad |
nuclear@2 | 116 { |
nuclear@2 | 117 if(ad_visible) { |
nuclear@2 | 118 ad.frame = CGRectOffset(ad.frame, 0, -ad.frame.size.height); |
nuclear@2 | 119 ad_visible = NO; |
nuclear@2 | 120 } |
nuclear@2 | 121 } |
nuclear@2 | 122 |
nuclear@2 | 123 - (void)init_gl |
nuclear@2 | 124 { |
nuclear@2 | 125 [EAGLContext setCurrentContext: self->ctx]; |
nuclear@2 | 126 |
nuclear@2 | 127 if(init() == -1) { |
nuclear@2 | 128 NSLog(@"app initialization failed"); |
nuclear@2 | 129 exit(0); |
nuclear@2 | 130 } |
nuclear@2 | 131 } |
nuclear@2 | 132 |
nuclear@2 | 133 - (void)destroy_gl |
nuclear@2 | 134 { |
nuclear@2 | 135 cleanup(); |
nuclear@2 | 136 [EAGLContext setCurrentContext: self->ctx]; |
nuclear@2 | 137 } |
nuclear@2 | 138 |
nuclear@2 | 139 |
nuclear@2 | 140 - (void)glkView: (GLKView*)view drawInRect: (CGRect)rect |
nuclear@2 | 141 { |
nuclear@2 | 142 redraw(); |
nuclear@2 | 143 } |
nuclear@2 | 144 |
nuclear@2 | 145 - (void)viewDidLayoutSubviews |
nuclear@2 | 146 { |
nuclear@2 | 147 CGRect rect = self.view.frame; |
nuclear@3 | 148 int xsz = rect.size.width * pixel_scale; |
nuclear@3 | 149 int ysz = rect.size.height * pixel_scale; |
nuclear@3 | 150 reshape(xsz, ysz); |
nuclear@3 | 151 printf("viewport %dx%d (scale: %g)\n", xsz, ysz, pixel_scale); |
nuclear@2 | 152 } |
nuclear@2 | 153 |
nuclear@2 | 154 // ADBannerDelegate functions |
nuclear@2 | 155 |
nuclear@2 | 156 - (void)bannerViewDidLoadAd: (ADBannerView*)banner |
nuclear@2 | 157 { |
nuclear@2 | 158 [self show_ad]; |
nuclear@2 | 159 } |
nuclear@2 | 160 |
nuclear@1 | 161 - (void)bannerView: (ADBannerView*)banner didFailToReceiveAdWithError: (NSError*)error |
nuclear@1 | 162 { |
nuclear@2 | 163 [self hide_ad]; |
nuclear@1 | 164 |
nuclear@1 | 165 NSLog(@"Failed to retrieve ad"); |
nuclear@1 | 166 } |
nuclear@1 | 167 |
nuclear@1 | 168 |
nuclear@0 | 169 @end /* implementation ViewController */ |