Skip to content

Commit b381d5c

Browse files
author
Darcy Liu
committed
add GLGravity
1 parent 1f5cb35 commit b381d5c

21 files changed

+4822
-1
lines changed

GLGravity.zip

60.7 KB
Binary file not shown.
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
File: GLGravityAppDelegate.h
3+
Abstract: The GLGravityAppDelegate class is the app delegate that ties
4+
everything together. It updates the acceletometer values used to draw
5+
OpenGL content in the GLGravityView class.
6+
Version: 2.2
7+
8+
Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple
9+
Inc. ("Apple") in consideration of your agreement to the following
10+
terms, and your use, installation, modification or redistribution of
11+
this Apple software constitutes acceptance of these terms. If you do
12+
not agree with these terms, please do not use, install, modify or
13+
redistribute this Apple software.
14+
15+
In consideration of your agreement to abide by the following terms, and
16+
subject to these terms, Apple grants you a personal, non-exclusive
17+
license, under Apple's copyrights in this original Apple software (the
18+
"Apple Software"), to use, reproduce, modify and redistribute the Apple
19+
Software, with or without modifications, in source and/or binary forms;
20+
provided that if you redistribute the Apple Software in its entirety and
21+
without modifications, you must retain this notice and the following
22+
text and disclaimers in all such redistributions of the Apple Software.
23+
Neither the name, trademarks, service marks or logos of Apple Inc. may
24+
be used to endorse or promote products derived from the Apple Software
25+
without specific prior written permission from Apple. Except as
26+
expressly stated in this notice, no other rights or licenses, express or
27+
implied, are granted by Apple herein, including but not limited to any
28+
patent rights that may be infringed by your derivative works or by other
29+
works in which the Apple Software may be incorporated.
30+
31+
The Apple Software is provided by Apple on an "AS IS" basis. APPLE
32+
MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION
33+
THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS
34+
FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND
35+
OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS.
36+
37+
IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL
38+
OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
39+
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
40+
INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION,
41+
MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED
42+
AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE),
43+
STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE
44+
POSSIBILITY OF SUCH DAMAGE.
45+
46+
Copyright (C) 2010 Apple Inc. All Rights Reserved.
47+
48+
*/
49+
50+
#import <UIKit/UIKit.h>
51+
52+
@class GLGravityView;
53+
54+
@interface GLGravityAppDelegate : NSObject <UIApplicationDelegate, UIAccelerometerDelegate> {
55+
UIWindow *window;
56+
GLGravityView *glView;
57+
UIAccelerationValue accel[3];
58+
}
59+
60+
@property (nonatomic, retain) IBOutlet UIWindow *window;
61+
@property (nonatomic, retain) IBOutlet GLGravityView *glView;
62+
63+
@end
64+
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/*
2+
File: GLGravityAppDelegate.m
3+
Abstract: The GLGravityAppDelegate class is the app delegate that ties
4+
everything together. It updates the acceletometer values used to draw
5+
OpenGL content in the GLGravityView class.
6+
Version: 2.2
7+
8+
Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple
9+
Inc. ("Apple") in consideration of your agreement to the following
10+
terms, and your use, installation, modification or redistribution of
11+
this Apple software constitutes acceptance of these terms. If you do
12+
not agree with these terms, please do not use, install, modify or
13+
redistribute this Apple software.
14+
15+
In consideration of your agreement to abide by the following terms, and
16+
subject to these terms, Apple grants you a personal, non-exclusive
17+
license, under Apple's copyrights in this original Apple software (the
18+
"Apple Software"), to use, reproduce, modify and redistribute the Apple
19+
Software, with or without modifications, in source and/or binary forms;
20+
provided that if you redistribute the Apple Software in its entirety and
21+
without modifications, you must retain this notice and the following
22+
text and disclaimers in all such redistributions of the Apple Software.
23+
Neither the name, trademarks, service marks or logos of Apple Inc. may
24+
be used to endorse or promote products derived from the Apple Software
25+
without specific prior written permission from Apple. Except as
26+
expressly stated in this notice, no other rights or licenses, express or
27+
implied, are granted by Apple herein, including but not limited to any
28+
patent rights that may be infringed by your derivative works or by other
29+
works in which the Apple Software may be incorporated.
30+
31+
The Apple Software is provided by Apple on an "AS IS" basis. APPLE
32+
MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION
33+
THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS
34+
FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND
35+
OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS.
36+
37+
IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL
38+
OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
39+
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
40+
INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION,
41+
MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED
42+
AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE),
43+
STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE
44+
POSSIBILITY OF SUCH DAMAGE.
45+
46+
Copyright (C) 2010 Apple Inc. All Rights Reserved.
47+
48+
*/
49+
50+
#import "GLGravityAppDelegate.h"
51+
#import "GLGravityView.h"
52+
53+
// CONSTANTS
54+
#define kAccelerometerFrequency 100.0 // Hz
55+
#define kFilteringFactor 0.1
56+
57+
@implementation GLGravityAppDelegate
58+
59+
@synthesize window;
60+
@synthesize glView;
61+
62+
- (void) applicationDidFinishLaunching:(UIApplication *)application
63+
{
64+
[glView startAnimation];
65+
66+
//Configure and start accelerometer
67+
[[UIAccelerometer sharedAccelerometer] setUpdateInterval:(1.0 / kAccelerometerFrequency)];
68+
[[UIAccelerometer sharedAccelerometer] setDelegate:self];
69+
}
70+
71+
- (void) applicationWillResignActive:(UIApplication *)application
72+
{
73+
[glView stopAnimation];
74+
}
75+
76+
- (void) applicationDidBecomeActive:(UIApplication *)application
77+
{
78+
[glView startAnimation];
79+
}
80+
81+
- (void)applicationWillTerminate:(UIApplication *)application
82+
{
83+
[glView stopAnimation];
84+
}
85+
86+
- (void) dealloc
87+
{
88+
[window release];
89+
[glView release];
90+
91+
[super dealloc];
92+
}
93+
94+
- (void)accelerometer:(UIAccelerometer*)accelerometer didAccelerate:(UIAcceleration*)acceleration
95+
{
96+
//Use a basic low-pass filter to only keep the gravity in the accelerometer values
97+
accel[0] = acceleration.x * kFilteringFactor + accel[0] * (1.0 - kFilteringFactor);
98+
accel[1] = acceleration.y * kFilteringFactor + accel[1] * (1.0 - kFilteringFactor);
99+
accel[2] = acceleration.z * kFilteringFactor + accel[2] * (1.0 - kFilteringFactor);
100+
101+
//Update the accelerometer values for the view
102+
[glView setAccel:accel];
103+
}
104+
105+
@end

GLGravity/Classes/GLGravityView.h

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/*
2+
File: GLGravityView.h
3+
Abstract: This class wraps the CAEAGLLayer from CoreAnimation into a convenient
4+
UIView subclass. The view content is basically an EAGL surface you render your
5+
OpenGL scene into. Note that setting the view non-opaque will only work if the
6+
EAGL surface has an alpha channel.
7+
Version: 2.2
8+
9+
Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple
10+
Inc. ("Apple") in consideration of your agreement to the following
11+
terms, and your use, installation, modification or redistribution of
12+
this Apple software constitutes acceptance of these terms. If you do
13+
not agree with these terms, please do not use, install, modify or
14+
redistribute this Apple software.
15+
16+
In consideration of your agreement to abide by the following terms, and
17+
subject to these terms, Apple grants you a personal, non-exclusive
18+
license, under Apple's copyrights in this original Apple software (the
19+
"Apple Software"), to use, reproduce, modify and redistribute the Apple
20+
Software, with or without modifications, in source and/or binary forms;
21+
provided that if you redistribute the Apple Software in its entirety and
22+
without modifications, you must retain this notice and the following
23+
text and disclaimers in all such redistributions of the Apple Software.
24+
Neither the name, trademarks, service marks or logos of Apple Inc. may
25+
be used to endorse or promote products derived from the Apple Software
26+
without specific prior written permission from Apple. Except as
27+
expressly stated in this notice, no other rights or licenses, express or
28+
implied, are granted by Apple herein, including but not limited to any
29+
patent rights that may be infringed by your derivative works or by other
30+
works in which the Apple Software may be incorporated.
31+
32+
The Apple Software is provided by Apple on an "AS IS" basis. APPLE
33+
MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION
34+
THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS
35+
FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND
36+
OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS.
37+
38+
IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL
39+
OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
40+
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
41+
INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION,
42+
MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED
43+
AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE),
44+
STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE
45+
POSSIBILITY OF SUCH DAMAGE.
46+
47+
Copyright (C) 2010 Apple Inc. All Rights Reserved.
48+
49+
*/
50+
51+
#import <UIKit/UIKit.h>
52+
#import <OpenGLES/EAGL.h>
53+
#import <OpenGLES/ES1/gl.h>
54+
#import <OpenGLES/ES1/glext.h>
55+
56+
@interface GLGravityView : UIView
57+
{
58+
@private
59+
// The pixel dimensions of the backbuffer
60+
GLint backingWidth;
61+
GLint backingHeight;
62+
63+
EAGLContext *context;
64+
65+
// OpenGL names for the renderbuffer and framebuffers used to render to this view
66+
GLuint viewRenderbuffer, viewFramebuffer;
67+
68+
// OpenGL name for the depth buffer that is attached to viewFramebuffer, if it exists (0 if it does not exist)
69+
GLuint depthRenderbuffer;
70+
71+
BOOL animating;
72+
BOOL displayLinkSupported;
73+
NSInteger animationFrameInterval;
74+
// Use of the CADisplayLink class is the preferred method for controlling your animation timing.
75+
// CADisplayLink will link to the main display and fire every vsync when added to a given run-loop.
76+
// The NSTimer class is used only as fallback when running on a pre 3.1 device where CADisplayLink
77+
// isn't available.
78+
id displayLink;
79+
NSTimer *animationTimer;
80+
81+
UIAccelerationValue *accel;
82+
}
83+
84+
@property (readonly, nonatomic, getter=isAnimating) BOOL animating;
85+
@property (nonatomic) NSInteger animationFrameInterval;
86+
87+
@property (nonatomic) UIAccelerationValue *accel;
88+
89+
-(void)startAnimation;
90+
-(void)stopAnimation;
91+
-(void)drawView;
92+
93+
@end

0 commit comments

Comments
 (0)