Permalink
Browse files

Added possibility to use a texture of next view to animate to.

  • Loading branch information...
1 parent aef37d9 commit 6546b3d5562c0474119372d7dc1126d9a4f14c2f Edward Patel committed Feb 28, 2010
Showing with 181 additions and 69 deletions.
  1. +17 −7 DemoProject/Classes/MainViewController.m
  2. +3 −2 src/DemoTransition.h
  3. +34 −13 src/DemoTransition.m
  4. +30 −14 src/EPGLTransitionView.h
  5. +97 −33 src/EPGLTransitionView.m
@@ -54,15 +54,25 @@ - (IBAction)showInfo {
DemoTransition *transition = [[[DemoTransition alloc] init] autorelease];
- EPGLTransitionView *glview = [[EPGLTransitionView alloc]
- initWithWindow:self.view.window
- delegate:transition];
-
- [glview autorelease];
- [glview startAnimation];
+ EPGLTransitionView *glview = [[[EPGLTransitionView alloc]
+ initWithWindow:self.view
+ delegate:transition] autorelease];
+
+ // Get texture for the "to" view
+ [glview prepareTextureTo:controller.view];
+
+ // If you are using an "IN" animation for the "to" view set appropriate
+ // clear color (ie no alpha)
+ [glview setClearColorRed:0.3
+ green:0.3
+ blue:0.3
+ alpha:1.0];
+ [glview startTransition];
+
[self presentModalViewController:controller animated:NO];
-
+
+
[controller release];
}
@@ -33,8 +33,9 @@
// 4x6 part, 4 vertex 2 coords
GLfloat vertices[4][6][4][2];
GLfloat texcoords[4][6][4][2];
- float y[4][6];
- float dy[4][6];
+ float yOut[4][6];
+ float dyOut[4][6];
+ float yIn[4][6];
}
@end
View
@@ -31,9 +31,6 @@ - (void)setupTransition
srand(time(NULL));
// Setup matrices
- glMatrixMode(GL_TEXTURE);
- glLoadIdentity();
- glScalef(320.0/512.0, 480.0/512.0, 1.0); // Convert to screen part of the 512x512 texture
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrthof(-1, 1, -1.5, 1.5, -10, 10); // Could also use glFrustum here for a 3D look
@@ -66,8 +63,12 @@ - (void)setupTransition
texcoords[i][j][2][1] = ty;
texcoords[i][j][3][0] = tx+1.0/4.0;
texcoords[i][j][3][1] = ty;
- y[i][j] = 0;
- dy[i][j] = 0;
+ yOut[i][j] = 0;
+ dyOut[i][j] = 0;
+ if (j)
+ yIn[i][j] = yIn[i][j-1]+3.0/6.0+(rand()%200)/500.0;
+ else
+ yIn[i][j] = 3.0+3.0/6.0+(rand()%200)/500.0;
}
}
@@ -79,14 +80,15 @@ - (void)setupTransition
}
// GL context is active and screen texture bound to be used
-- (BOOL)drawTransitionFrame
+- (BOOL)drawTransitionFrameWithTextureFrom:(GLuint)textureFromView
+ textureTo:(GLuint)textureToView
{
int i, j;
for (i=0; i<4; i++) {
for (j=0; j<6; j++) {
glPushMatrix();
- glTranslatef(0, -y[i][j], 0);
+ glTranslatef(0, -yOut[i][j], 0);
glDrawArrays(GL_TRIANGLE_STRIP, (i*6+j)*4, 4);
glPopMatrix();
}
@@ -95,20 +97,20 @@ - (BOOL)drawTransitionFrame
for (j=0; j<6; j++) {
int moved = 0;
for (i=0; i<4; i++) {
- if (dy[i][j] > 0.0) {
- y[i][j] += dy[i][j];
- dy[i][j] *= 1.1;
+ if (dyOut[i][j] > 0.0) {
+ yOut[i][j] += dyOut[i][j];
+ dyOut[i][j] *= 1.1;
moved++;
}
- if (y[i][j] < 2.0)
+ if (yOut[i][j] < 0.5)
allAreGone = NO;
}
if (moved<4) {
if (rand()%100 > 50) {
while (1) { // naïve loop to find a none moving square
i = rand()%4;
- if (!(dy[i][j] > 0.0)) {
- dy[i][j] = 0.02;
+ if (!(dyOut[i][j] > 0.0)) {
+ dyOut[i][j] = 0.02;
break; // got one, leave now
}
}
@@ -117,6 +119,25 @@ - (BOOL)drawTransitionFrame
}
}
+ glBindTexture(GL_TEXTURE_2D, textureToView);
+
+ if (allAreGone) {
+ for (i=0; i<4; i++) {
+ for (j=0; j<6; j++) {
+ glPushMatrix();
+ glTranslatef(0, yIn[i][j], 0);
+ glDrawArrays(GL_TRIANGLE_STRIP, (i*6+j)*4, 4);
+ glPopMatrix();
+ yIn[i][j] -= 0.05;
+ if (yIn[i][j] < 0.0) {
+ yIn[i][j] = 0.0;
+ } else {
+ allAreGone = NO;
+ }
+ }
+ }
+ }
+
return !allAreGone;
}
@@ -30,36 +30,52 @@
// Delegate methods for you to implement your effect with
//
@protocol EPGLTransitionViewDelegate<NSObject>
+// This is done when the GL context is active, setup matrices
- (void)setupTransition;
-- (BOOL)drawTransitionFrame; // return NO to end transition
+
+// 'textureFromView' is already active (bind)
+// If no second texture was prepared 'textureTo' will not be valid
+// return NO to end transition
+- (BOOL)drawTransitionFrameWithTextureFrom:(GLuint)textureFromView
+ textureTo:(GLuint)textureToView;
@end
@interface EPGLTransitionView : UIView
{
@private
id<EPGLTransitionViewDelegate> delegate;
- BOOL animating;
- BOOL displayLinkSupported;
- NSInteger animationFrameInterval;
- id displayLink;
- NSTimer *animationTimer;
+ BOOL animating;
+ BOOL displayLinkSupported;
+ NSInteger transitionFrameInterval;
+ id displayLink;
+ NSTimer *animationTimer;
EAGLContext *context;
- GLint backingWidth;
- GLint backingHeight;
+ GLint backingWidth;
+ GLint backingHeight;
+
+ GLuint defaultFramebuffer;
+ GLuint colorRenderbuffer;
- GLuint defaultFramebuffer, colorRenderbuffer;
+ GLuint textureFromView;
+ GLuint textureToView;
- GLuint texture;
+ CGSize size;
+
+ GLfloat clearColor[4];
}
@property (readonly, nonatomic, getter=isAnimating) BOOL animating;
-@property (nonatomic) NSInteger animationFrameInterval;
+@property (nonatomic) NSInteger transitionFrameInterval;
-- (id) initWithWindow:(UIWindow*)window
+- (id) initWithWindow:(UIView*)view
delegate:(id<EPGLTransitionViewDelegate>)delegate;
-- (void) startAnimation;
-
+- (void) prepareTextureTo:(UIView*)view;
+- (void) startTransition;
+- (void) setClearColorRed:(GLfloat)red
+ green:(GLfloat)green
+ blue:(GLfloat)blue
+ alpha:(GLfloat)alpha;
@end
Oops, something went wrong.

0 comments on commit 6546b3d

Please sign in to comment.