-
Notifications
You must be signed in to change notification settings - Fork 4
/
EAGLView.m
205 lines (167 loc) · 4.52 KB
/
EAGLView.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
//
// EAGLView.m
// EarthSimulation
//
// Modified by Donald Ness on 12/18/10.
// Created by David Jacobs on 3/8/10.
//
#import "EAGLView.h"
#import "EarthRenderer.h"
@implementation EAGLView
@synthesize animating;
@dynamic animationFrameInterval;
// You must implement this method
+ (Class) layerClass
{
return [CAEAGLLayer class];
}
//The GL view is stored in the nib file. When it's unarchived it's sent -initWithCoder:
- (id) initWithCoder:(NSCoder*)coder
{
if ((self = [super initWithCoder:coder]))
{
CAEAGLLayer *eaglLayer = (CAEAGLLayer *)self.layer;
eaglLayer.opaque = TRUE;
eaglLayer.drawableProperties = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:FALSE], kEAGLDrawablePropertyRetainedBacking, kEAGLColorFormatRGBA8, kEAGLDrawablePropertyColorFormat, nil];
renderer = [[EarthRenderer alloc] init];
if (!renderer)
{
[self release];
return nil;
}
animating = FALSE;
displayLinkSupported = FALSE;
animationFrameInterval = 1;
displayLink = nil;
animationTimer = nil;
// A system version of 3.1 or greater is required to use CADisplayLink. The NSTimer
// class is used as fallback when it isn't available.
NSString *reqSysVer = @"3.1";
NSString *currSysVer = [[UIDevice currentDevice] systemVersion];
if ([currSysVer compare:reqSysVer options:NSNumericSearch] != NSOrderedAscending)
{
displayLinkSupported = TRUE;
}
}
return self;
}
- (void) drawView:(id)sender
{
[renderer render];
}
- (void) layoutSubviews
{
[renderer resizeFromLayer:(CAEAGLLayer*)self.layer];
[self drawView:nil];
}
- (NSInteger) animationFrameInterval
{
return animationFrameInterval;
}
- (void) setAnimationFrameInterval:(NSInteger)frameInterval
{
if (frameInterval >= 1)
{
animationFrameInterval = frameInterval;
if (animating)
{
[self stopAnimation];
[self startAnimation];
}
}
}
- (void) startAnimation
{
if (!animating)
{
if (displayLinkSupported)
{
displayLink = [NSClassFromString(@"CADisplayLink") displayLinkWithTarget:self selector:@selector(drawView:)];
[displayLink setFrameInterval:animationFrameInterval];
[displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
}
else
{
animationTimer = [NSTimer scheduledTimerWithTimeInterval:(NSTimeInterval)((1.0 / 60.0) * animationFrameInterval) target:self selector:@selector(drawView:) userInfo:nil repeats:TRUE];
}
animating = TRUE;
}
}
- (void) stopAnimation
{
if (animating)
{
if (displayLinkSupported)
{
[displayLink invalidate];
displayLink = nil;
}
else
{
[animationTimer invalidate];
animationTimer = nil;
}
animating = FALSE;
}
}
- (float) distanceFromPoint:(CGPoint)pointA toPoint:(CGPoint)pointB
{
float xD = fabs(pointA.x - pointB.x);
float yD = fabs(pointA.y - pointB.y);
return sqrt(xD*xD + yD*yD);
}
- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touchA, *touchB;
CGPoint pointA, pointB;
if ([touches count] == 1)
{
touchA = [[touches allObjects] objectAtIndex:0];
pointA = [touchA locationInView:self];
pointB = [touchA previousLocationInView:self];
float distanceY = pointA.x - pointB.x;
float distanceX = pointA.y - pointB.y;
GLfloat rotX = [renderer rotX];
GLfloat rotY = [renderer rotY];
[renderer setRotX:(rotX + 0.5 * distanceX)];
[renderer setRotY:(rotY + 0.5 * distanceY)];
[renderer setAutoRotX:(0.25 * distanceX)];
[renderer setAutoRotY:(0.25 * distanceY)];
[self drawView:nil];
}
else if ([touches count] == 2)
{
touchA = [[touches allObjects] objectAtIndex:0];
touchB = [[touches allObjects] objectAtIndex:1];
pointA = [touchA locationInView:self];
pointB = [touchB locationInView:self];
float currDistance = [self distanceFromPoint:pointA toPoint:pointB];
pointA = [touchA previousLocationInView:self];
pointB = [touchB previousLocationInView:self];
float prevDistance = [self distanceFromPoint:pointA toPoint:pointB];
GLfloat scale = [renderer scale];
[renderer setScale:(scale + 0.005 * (currDistance - prevDistance))];
[self drawView:nil];
}
}
- (BOOL) canBecomeFirstResponder
{
return YES;
}
- (void) motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
if (event.subtype == UIEventSubtypeMotionShake)
{
[renderer reset];
}
if ([super respondsToSelector:@selector(motionEnded:withEvent:)])
{
[super motionEnded:motion withEvent:event];
}
}
- (void) dealloc
{
[renderer release];
[super dealloc];
}
@end