Skip to content

Commit ff35e26

Browse files
author
lorenzo
committed
Semicone Experiment lomanf#1
1 parent df9f4ab commit ff35e26

File tree

9 files changed

+13403
-9563
lines changed

9 files changed

+13403
-9563
lines changed

PaperStackDemo.xcodeproj/project.xcworkspace/xcuserdata/lorenzo.xcuserdatad/UserInterfaceState.xcuserstate

Lines changed: 13284 additions & 9509 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

PaperStackDemo/PaperStack/CCCommon.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@
1010
*
1111
*/
1212

13-
#define USE_TRIANGLE_STRIPS 1
14-
#define PAGE_COLUMNS 20
15-
#define PAGE_ROWS 25
13+
#define USE_TRIANGLE_STRIPS 1
14+
#define SHOW_DEBUG_LINES 1
15+
#define PAGE_COLUMNS 20
16+
#define PAGE_ROWS 25
1617

1718
#define DEGREES_TO_RADIANS(__ANGLE__) ((__ANGLE__) / 180.0f * M_PI)
1819
#define RAD (180.0f / M_PI)

PaperStackDemo/PaperStack/CCPage.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
u_short *frontStrip_; // Index triangle strip for the front side.
5858
u_short *backStrip_; // Index triangle strip for the back side.
5959
u_short stripLength_;
60+
6061
}
6162

6263
@property (nonatomic) CGFloat width; // We use standard data types to decouple our model class from the implementation details of its view.
@@ -70,6 +71,8 @@
7071
@property (nonatomic) CGFloat Ax;
7172
@property (nonatomic) CGFloat Ay;
7273

74+
@property (nonatomic) CGPoint P;
75+
7376
- (const Vertex2f *) textureArray;
7477
- (const Vertex3f *) vertices; // Deformed page mesh as a vertex array.
7578
- (const u_short *) frontFaces; // Triangle array tesselated from vertices in counter-clockwise order to represent the front face.

PaperStackDemo/PaperStack/CCPage.m

Lines changed: 77 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99

1010
#import "CCPage.h"
11+
#import <OpenGLES/ES1/gl.h>
12+
#import <OpenGLES/ES1/glext.h>
1113

1214
@interface CCPage ()
1315
// Empty category for "private" methods
@@ -20,7 +22,7 @@ @implementation CCPage
2022

2123
@synthesize width, height, columns, rows;
2224
@synthesize currentFrame, framesPerCycle;
23-
@synthesize rho, theta, Ax, Ay;
25+
@synthesize rho, theta, Ax, Ay, P;
2426

2527

2628
- (id)init
@@ -54,7 +56,6 @@ - (void)dealloc
5456
free(frontStrip_);
5557
if (backStrip_ != NULL)
5658
free(backStrip_);
57-
5859
[super dealloc];
5960
}
6061

@@ -126,7 +127,7 @@ - (void)createMesh
126127
for (iiX = 0; iiX < vCountX; iiX++)
127128
{
128129
px = (CGFloat)iiX * width / columns;
129-
py = (CGFloat)iiY * height / rows;
130+
py = (CGFloat)iiY * height / rows - (height*0.5);
130131
inputMesh_[vi].x = px;
131132
inputMesh_[vi].y = py;
132133
textureArray_[vi].x = (CGFloat)iiX / columns;
@@ -191,7 +192,7 @@ - (void)deformForTime:(CGFloat)t
191192

192193
}
193194

194-
- (void)deform
195+
- (void)deform2
195196
{
196197
// This method must be called after any values of rho, theta, or A have been changed in order to update the output geometry.
197198

@@ -203,6 +204,7 @@ - (void)deform
203204
Vertex3f v1; // First stage of the deformation, with only theta and A applied. This results in a curl, but no rotation.
204205
Vertex3f *vo; // Pointer to the finished vertex in the output mesh, after applying rho to v1 with a basic rotation transform.
205206

207+
Ay = -0.5 + fminf( 0.0, Ay );
206208
// Iterate over the input mesh to deform each vertex.
207209
CGFloat R, r, beta, ttheta;
208210
for (u_short ii = 0; ii < numVertices_; ii++)
@@ -215,15 +217,16 @@ - (void)deform
215217
ttheta = M_PI*0.5;
216218
}
217219
r = R * sin(ttheta); // From R, calculate the radius of the cone cross section intersected by our vertex in 3D space.
218-
beta = asin((vi.x-Ax) / R) / sin(ttheta); // Angle SCT, the angle of the cone cross section subtended by the arc |ST|.
220+
221+
if ( vi.x < Ax )
222+
beta = 0.0;
223+
else
224+
beta = asin((vi.x-Ax) / R) / sin(ttheta); // Angle SCT, the angle of the cone cross section subtended by the arc |ST|.
225+
219226
v1.x = r * sin(beta) + Ax;
220-
if ( vi.x <= Ax ) {
221-
v1.x = v1.x;
222-
}
227+
if (vi.x < Ax) v1.x = vi.x;
223228
v1.y = R + Ay - r * (1.0f - cos(beta)) * sin(ttheta); // *** MAGIC!!! ***
224-
if ( vi.x <= Ax ) {
225-
v1.y = v1.y;
226-
}
229+
if (vi.x < Ax) v1.y = vi.y;
227230
v1.z = r * (1.0f - cos(beta)) * cos(ttheta);
228231

229232
// Apply a basic rotation transform around the y axis to rotate the curled page. These two steps could be combined
@@ -235,6 +238,69 @@ - (void)deform
235238
}
236239
}
237240

241+
- (void)deform
242+
{
243+
CGFloat RB1, RB2, b1, b2, ipo, R, Rc, beta, ttheta;
244+
245+
// NSLog(@"Px: %f", P.x );
246+
247+
P.y = P.y + 0.01;
248+
RB2 = ( 1 + P.y ) * 0.5;
249+
RB1 = 1 - RB2;
250+
RB1 *= ( 1.0 - fabs( P.x ) ) * 0.5;
251+
RB2 *= ( 1.0 - fabs( P.x ) ) * 0.5;
252+
253+
b1 = P.x + ( RB1 * 0.5 );
254+
b2 = P.x + ( RB2 * 0.5 );
255+
ipo = sqrtf( pow( b1 - b2, 2 ) );
256+
theta = fminf( asinf( ipo ), M_PI * 0.48 );
257+
Ax = P.x * 0.5;
258+
Ay = fmaxf( 1 + fminf( RB1, RB2 ) * 0.5 * ( 1 / tanf( theta ) ), 1.1 ) * ( RB1 > RB2 ? 1.0 : -1.0 );
259+
260+
// NSLog(@"RB1: %f, RB2: %f", RB1, RB2 );
261+
// NSLog(@"Theta: %f", theta );
262+
// NSLog(@"Ay: %f", Ay );
263+
// NSLog(@"Ax: %f", Ax );
264+
265+
// deform mash
266+
267+
Vertex2f vi;
268+
Vertex3f v1;
269+
Vertex3f *vo;
270+
271+
for ( u_short ii = 0; ii < numVertices_; ii++ ) {
272+
vi = inputMesh_[ ii ];
273+
274+
ttheta = theta;
275+
if ( vi.x < Ax ) {
276+
ttheta = M_PI * 0.5;
277+
}
278+
279+
R = sqrt( pow( vi.x - Ax, 2.0f ) + pow( vi.y - Ay, 2.0f ) );
280+
Rc = R * sin( ttheta );
281+
beta = asin( ( vi.x - Ax ) / R ) / sin( ttheta );
282+
283+
// translate vertex
284+
v1.x = Rc * sin( beta ) + Ax;
285+
if ( RB1 < RB2 ) {
286+
v1.y = R + Ay - Rc * ( 1.0f - cos( beta ) ) * sin( ttheta );
287+
} else {
288+
v1.y = R - Ay - Rc * ( 1.0f - cos( beta ) ) * sin( ttheta );
289+
v1.y = -v1.y;
290+
}
291+
v1.z = Rc * ( 1.0f - cos( beta ) ) * cos( ttheta );
292+
293+
// output vertex
294+
vo = &outputMesh_[ ii ];
295+
//vo->x = ( v1.x * cos( rho ) - v1.z * sin( rho ) );
296+
//vo->y = v1.y;
297+
//vo->z = ( v1.x * sin( rho ) + v1.z * cos( rho ) );
298+
vo->x = v1.x;
299+
vo->y = v1.y;
300+
vo->z = v1.z;
301+
}
302+
}
303+
238304
#pragma mark -
239305
#pragma mark Private methods
240306

PaperStackDemo/PaperStack/EAGLView.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,6 @@
5252
- (CCPage *)activePage;
5353
- (void)drawView:(id)sender;
5454
- (void)drawViewForTime:(CGFloat)time;
55-
- (void)applyTransform;
55+
- (void)applyTransform:(CGFloat)time;
5656

5757
@end

PaperStackDemo/PaperStack/EAGLView.m

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,13 @@ - (id)initWithCoder:(NSCoder*)coder
3030
{
3131
// Get the layer
3232
CAEAGLLayer *eaglLayer = (CAEAGLLayer *)self.layer;
33-
33+
3434
eaglLayer.opaque = NO;
3535
eaglLayer.drawableProperties = [NSDictionary dictionaryWithObjectsAndKeys:
36-
[NSNumber numberWithBool:FALSE], kEAGLDrawablePropertyRetainedBacking, kEAGLColorFormatRGBA8, kEAGLDrawablePropertyColorFormat, nil];
36+
[NSNumber numberWithBool:YES], kEAGLDrawablePropertyRetainedBacking, kEAGLColorFormatRGBA8, kEAGLDrawablePropertyColorFormat, nil];
3737

3838
self.backgroundColor = [UIColor darkGrayColor];
39+
3940
if ( !renderer ) {
4041
renderer = [[ES1Renderer alloc] init];
4142
if (!renderer) {
@@ -61,7 +62,7 @@ - (id)initWithCoder:(NSCoder*)coder
6162
rightPage_ = [[CCPage alloc] init];
6263
rightPage_.currentFrame = 0;
6364
rightPage_.framesPerCycle = 120;
64-
rightPage_.width = 0.75;
65+
rightPage_.width = 1.0f;
6566
rightPage_.height = 1.0f;
6667
rightPage_.columns = PAGE_COLUMNS;
6768
rightPage_.rows = PAGE_ROWS;
@@ -88,10 +89,15 @@ - (void)drawViewForTime:(CGFloat)time
8889
[renderer renderObject:rightPage_];
8990
}
9091

91-
- (void)applyTransform
92+
- (void)applyTransform:(CGFloat)time
9293
{
9394
[rightPage_ deform];
9495
[renderer renderObject:rightPage_];
96+
97+
// CGFloat rel = ( time - 0.5 ) / 0.5;
98+
// CGFloat arel = fabs( rel );
99+
// CGFloat irel = ( 1.0 - arel ) * ceilf( rel );
100+
// CGFloat iarel = 1.0 - arel;
95101
}
96102

97103
- (void)loadTextures
@@ -108,6 +114,11 @@ - (void)loadTextures
108114
rect.size.width /= tex.size.width;
109115
rect.size.height /= tex.size.height;
110116

117+
// correct width
118+
rightPage_.width = ( self.superview.frame.size.width * 0.5 ) / ( self.frame.size.width * 0.5 ) * 0.5;
119+
rightPage_.height = self.superview.frame.size.height / self.frame.size.height;
120+
[rightPage_ createMesh];
121+
111122
NSLog( @"Rect -->: %f, %f, %f, %f", rect.origin.x, rect.origin.y, rect.size.width, rect.size.height );
112123
[rightPage_ updateTextureCoord:rect];
113124
}

PaperStackDemo/PaperStack/ES1Renderer.m

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ - (void)setupView:(CAEAGLLayer *)layer
4949
GLfloat aspectRatio = (GLfloat)backingWidth / backingHeight;
5050
glFrustumf(-size, size, -size / aspectRatio, size / aspectRatio, zNear, zFar);
5151
glViewport(0, 0, backingWidth, backingHeight);
52-
glTranslatef(0.0f, -0.5f, -90.0f);
52+
glTranslatef(0.0f, -0.0f, -61.0f);
5353

5454
glEnable(GL_DEPTH_TEST);
5555
glEnable(GL_CULL_FACE);
@@ -149,10 +149,11 @@ - (void)renderObject:(id)obj
149149
glLoadIdentity();
150150

151151
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
152-
glClear(GL_COLOR_BUFFER_BIT);
152+
glClear(GL_COLOR_BUFFER_BIT);
153153

154154
// Load our vertex and texture arrays. This needs to be done only once since the front and back pages share this data.
155155
glEnableClientState(GL_VERTEX_ARRAY);
156+
156157
glVertexPointer(3, GL_FLOAT, 0, vertices);
157158
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
158159
glTexCoordPointer(2, GL_FLOAT, 0, textures);
@@ -182,7 +183,7 @@ - (void)renderObject:(id)obj
182183
glBindTexture(GL_TEXTURE_2D, texture[1]);
183184
glDrawElements(GL_TRIANGLES, numFaces * 3, GL_UNSIGNED_SHORT, backFaces);
184185
#endif
185-
186+
186187
// This application only creates a single color renderbuffer which is already bound at this point.
187188
// This call is redundant, but needed if dealing with multiple renderbuffers.
188189
glBindRenderbufferOES(GL_RENDERBUFFER_OES, colorRenderbuffer);

PaperStackDemo/PaperStack/PSPlayerController.m

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ @interface PSPlayerController()
1818
@property (nonatomic, retain) PSPageView *pageRight;
1919

2020
// drawing elements
21-
@property (nonatomic, assign) UIView *apex;
2221

2322
- (void)deviceDidRotate;
2423
- (void)pageCurlWithPoint:(CGPoint)point;
@@ -29,7 +28,6 @@ @implementation PSPlayerController
2928

3029
@synthesize landscapeMode, pageTarget, pageLeft, pageRight;
3130
@synthesize glView;
32-
@synthesize apex;
3331

3432
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
3533
{
@@ -97,12 +95,6 @@ - (void)viewDidLoad
9795

9896
[self.view bringSubviewToFront:glView];
9997

100-
// drawing elements
101-
UIView *a = [[UIView alloc] initWithFrame:CGRectMake( 0, 0, 5, 5 )];
102-
a.backgroundColor = [UIColor redColor];
103-
[self.view addSubview:a];
104-
self.apex = a;
105-
[a release];
10698
}
10799

108100
- (void)viewDidUnload
@@ -180,24 +172,26 @@ - (CGRect)rendererGetBackTextureRect
180172
- (void)pageCurlWithPoint:(CGPoint)point
181173
{
182174
CGRect rect = self.view.frame;
183-
CGFloat half = rect.size.width * 0.5;
184-
CGFloat dx = ( point.x - half ) / half;
185-
CGFloat dy = point.y / rect.size.height;
186-
187-
CGFloat aX = dx;
188-
CGFloat aY = -dy * 1.5;
189-
190-
apex.center = CGPointMake( half + aX * half, 10 );
191-
[self.view bringSubviewToFront:apex];
192-
175+
CGFloat halfw = rect.size.width * 0.5;
176+
CGFloat halfh = rect.size.height * 0.5;
177+
CGFloat dx = ( point.x - halfw ) / halfw;
178+
CGFloat dy = ( point.y - halfh ) / halfh;
179+
180+
CGFloat pX = dx;
181+
CGFloat pY = -dy;
182+
183+
/*
193184
CCPage *page = [glView activePage];
194185
page.Ax = aX;
195186
page.Ay = aY;
196187
page.rho = 0.0;
197-
page.theta = 0.12;
198-
188+
page.theta = 0.3;
189+
*/
190+
191+
CCPage *page = [glView activePage];
192+
page.P = CGPointMake( pX, pY );
199193

200-
[glView applyTransform];
194+
[glView applyTransform:(point.x/rect.size.width)];
201195
}
202196

203197
#pragma mark -

PaperStackDemo/PaperStack/PSPlayerController.xib

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -172,17 +172,6 @@
172172
<object class="IBPartialClassDescription">
173173
<string key="className">EAGLView</string>
174174
<string key="superclassName">UIView</string>
175-
<object class="NSMutableDictionary" key="outlets">
176-
<string key="NS.key.0">animationTime</string>
177-
<string key="NS.object.0">CGFloat</string>
178-
</object>
179-
<object class="NSMutableDictionary" key="toOneOutletInfosByName">
180-
<string key="NS.key.0">animationTime</string>
181-
<object class="IBToOneOutletInfo" key="NS.object.0">
182-
<string key="name">animationTime</string>
183-
<string key="candidateClassName">CGFloat</string>
184-
</object>
185-
</object>
186175
<object class="IBClassDescriptionSource" key="sourceIdentifier">
187176
<string key="majorKey">IBProjectSource</string>
188177
<string key="minorKey">./Classes/EAGLView.h</string>

0 commit comments

Comments
 (0)