istereo2
view 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 |
line source
1 #include <stdlib.h>
2 #import <OpenGLES/ES2/glext.h>
3 #import "viewctl.h"
4 #include "istereo.h"
6 @interface ViewController () {
7 EAGLContext *ctx;
8 float pixel_scale;
10 ADBannerView *ad;
11 BOOL ad_visible;
12 }
15 - (void)create_ad;
16 - (void)show_ad;
17 - (void)hide_ad;
19 - (void)init_gl;
20 - (void)destroy_gl;
21 @end
23 @implementation ViewController
25 - (void)viewDidLoad
26 {
27 [super viewDidLoad];
30 self->ctx = [[EAGLContext alloc] initWithAPI: kEAGLRenderingAPIOpenGLES2];
31 if(!self->ctx) {
32 NSLog(@"Failed to create OpenGL ES 2.0 context");
33 }
35 GLKView *view = (GLKView*)self.view;
36 view.context = self->ctx;
37 view.drawableDepthFormat = GLKViewDrawableDepthFormat24;
39 if([view respondsToSelector: NSSelectorFromString(@"contentScaleFactor")]) {
40 pixel_scale = [[UIScreen mainScreen] scale];
41 view.contentScaleFactor = pixel_scale;
42 printf("pixel scale: %g\n", pixel_scale);
43 } else {
44 pixel_scale = 1.0f;
45 }
47 [self create_ad];
49 [self init_gl];
50 }
52 - (void)dealloc
53 {
54 [self destroy_gl];
56 if([EAGLContext currentContext] == self->ctx) {
57 [EAGLContext setCurrentContext: nil];
58 }
59 }
61 - (void)didReceiveMemoryWarning
62 {
63 [super didReceiveMemoryWarning];
65 if([self isViewLoaded] && ([[self view] window] == nil)) {
66 self.view = nil;
68 [self destroy_gl];
70 if([EAGLContext currentContext] == self->ctx) {
71 [EAGLContext setCurrentContext: nil];
72 }
73 self->ctx = nil;
74 }
76 // Dispose of any resources that can be recreated.
77 }
79 - (BOOL)prefersStatusBarHidden
80 {
81 return YES;
82 }
84 - (BOOL)shouldAutorotate
85 {
86 return YES;
87 }
89 - (UIInterfaceOrientationMask)supportedInterfaceOrientations
90 {
91 return UIInterfaceOrientationMaskLandscape;
92 //return UIInterfaceOrientationMaskAll;
93 }
95 - (void)create_ad
96 {
97 ad = [[ADBannerView alloc] initWithAdType: ADAdTypeBanner];
98 [ad setAutoresizingMask: UIViewAutoresizingFlexibleWidth];
99 ad_visible = YES;
100 [self hide_ad];
101 [self.view addSubview: ad];
102 ad.delegate = self;
103 }
105 - (void)show_ad
106 {
107 if(!ad_visible) {
108 CGRect rect = ad.frame;
109 rect.origin.y = 0;
110 ad.frame = rect;
111 ad_visible = YES;
112 }
113 }
115 - (void)hide_ad
116 {
117 if(ad_visible) {
118 ad.frame = CGRectOffset(ad.frame, 0, -ad.frame.size.height);
119 ad_visible = NO;
120 }
121 }
123 - (void)init_gl
124 {
125 [EAGLContext setCurrentContext: self->ctx];
127 if(init() == -1) {
128 NSLog(@"app initialization failed");
129 exit(0);
130 }
131 }
133 - (void)destroy_gl
134 {
135 cleanup();
136 [EAGLContext setCurrentContext: self->ctx];
137 }
140 - (void)glkView: (GLKView*)view drawInRect: (CGRect)rect
141 {
142 redraw();
143 }
145 - (void)viewDidLayoutSubviews
146 {
147 CGRect rect = self.view.frame;
148 int xsz = rect.size.width * pixel_scale;
149 int ysz = rect.size.height * pixel_scale;
150 reshape(xsz, ysz);
151 printf("viewport %dx%d (scale: %g)\n", xsz, ysz, pixel_scale);
152 }
154 // ADBannerDelegate functions
156 - (void)bannerViewDidLoadAd: (ADBannerView*)banner
157 {
158 [self show_ad];
159 }
161 - (void)bannerView: (ADBannerView*)banner didFailToReceiveAdWithError: (NSError*)error
162 {
163 [self hide_ad];
165 NSLog(@"Failed to retrieve ad");
166 }
169 @end /* implementation ViewController */