Skip to content

Commit

Permalink
New RAPageNode rendering method
Browse files Browse the repository at this point in the history
New RAPageNode rendering method separates paging in/out with page
switching, so the page generation can occur on a background thread.
Still have to work out some kinks related to updating geometries on a
separate thread.
  • Loading branch information
RossAnderson committed May 3, 2012
1 parent 69115fc commit aee1e40
Show file tree
Hide file tree
Showing 16 changed files with 275 additions and 124 deletions.
2 changes: 1 addition & 1 deletion DRAppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

#import "RASceneGraphController.h"

#define IMAGERY_DATASET 2
#define IMAGERY_DATASET 1
#define TERRAIN_DATASET 1


Expand Down
6 changes: 6 additions & 0 deletions EarthViewExample.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
/* Begin PBXBuildFile section */
9109E03C153D86100008286D /* star1.png in Resources */ = {isa = PBXBuildFile; fileRef = 9109E03B153D86100008286D /* star1.png */; };
9109E03E153D864F0008286D /* RASceneGraphController.m in Sources */ = {isa = PBXBuildFile; fileRef = 9109E03D153D864F0008286D /* RASceneGraphController.m */; };
916EEB8D1552D4E800951ACC /* RAPageNode.m in Sources */ = {isa = PBXBuildFile; fileRef = 916EEB8C1552D4E800951ACC /* RAPageNode.m */; };
91C9975C1548E1BF00B69A8A /* Icon114.png in Resources */ = {isa = PBXBuildFile; fileRef = 91C9975B1548E1BF00B69A8A /* Icon114.png */; };
91C9975E1548E1F000B69A8A /* Icon57.png in Resources */ = {isa = PBXBuildFile; fileRef = 91C9975D1548E1F000B69A8A /* Icon57.png */; };
91C997601548E21400B69A8A /* Icon144.png in Resources */ = {isa = PBXBuildFile; fileRef = 91C9975F1548E21400B69A8A /* Icon144.png */; };
Expand Down Expand Up @@ -49,6 +50,8 @@
/* Begin PBXFileReference section */
9109E03B153D86100008286D /* star1.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = star1.png; sourceTree = "<group>"; };
9109E03D153D864F0008286D /* RASceneGraphController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RASceneGraphController.m; sourceTree = "<group>"; };
916EEB8B1552D4E800951ACC /* RAPageNode.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RAPageNode.h; sourceTree = "<group>"; };
916EEB8C1552D4E800951ACC /* RAPageNode.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RAPageNode.m; sourceTree = "<group>"; };
91BBDFB1153A49A900CEF4BA /* README.md */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = README.md; sourceTree = SOURCE_ROOT; };
91C9975B1548E1BF00B69A8A /* Icon114.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = Icon114.png; sourceTree = "<group>"; };
91C9975D1548E1F000B69A8A /* Icon57.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = Icon57.png; sourceTree = "<group>"; };
Expand Down Expand Up @@ -214,6 +217,8 @@
91F77E631539341B00F8AE05 /* RANodeVisitor.m */,
91F77E641539341B00F8AE05 /* RAPage.h */,
91F77E651539341B00F8AE05 /* RAPage.m */,
916EEB8B1552D4E800951ACC /* RAPageNode.h */,
916EEB8C1552D4E800951ACC /* RAPageNode.m */,
91F77E661539341B00F8AE05 /* RARenderVisitor.h */,
91F77E671539341B00F8AE05 /* RARenderVisitor.m */,
91F77E681539341B00F8AE05 /* RASceneGraphController.h */,
Expand Down Expand Up @@ -341,6 +346,7 @@
91F77E981539C31E00F8AE05 /* RAGeographicUtils.c in Sources */,
9109E03E153D864F0008286D /* RASceneGraphController.m in Sources */,
91F22019154C7B4700A5F74E /* RAShaderProgram.m in Sources */,
916EEB8D1552D4E800951ACC /* RAPageNode.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,21 @@
<Bucket
type = "1"
version = "1.0">
<FileBreakpoints>
<FileBreakpoint
shouldBeEnabled = "Yes"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "Source/RATilePager.m"
timestampString = "357770597.47682"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "352"
endingLineNumber = "352"
landmarkName = "-requestPage:"
landmarkType = "5">
</FileBreakpoint>
</FileBreakpoints>
<ExceptionBreakpoints>
<ExceptionBreakpoint
shouldBeEnabled = "Yes"
Expand Down
13 changes: 7 additions & 6 deletions Source/RAGeometry.m
Original file line number Diff line number Diff line change
Expand Up @@ -120,12 +120,12 @@ - (void)setObjectData:(const void *)data withSize:(NSUInteger)length withStride:
@synchronized(self) {
_vertexData = [NSMutableData dataWithBytes:data length:length];
_vertexStride = stride;

// force re-gen of vertex buffer
_vertexDataDirty = YES;
}

[self dirtyBound];

// force re-gen of vertex buffer
_vertexDataDirty = YES;
}

- (void)setIndexData:(const void *)data withSize:(NSUInteger)length withStride:(NSUInteger)stride
Expand All @@ -135,12 +135,12 @@ - (void)setIndexData:(const void *)data withSize:(NSUInteger)length withStride:(
@synchronized(self) {
_indexData = [NSMutableData dataWithBytes:data length:length];
_indexStride = stride;

// force re-gen of index buffer
_indexDataDirty = YES;
}

[self dirtyBound];

// force re-gen of index buffer
_indexDataDirty = YES;
}

- (void)setupGL
Expand Down Expand Up @@ -212,6 +212,7 @@ - (void)setupGL
}

glBindVertexArrayOES(0);
//glFlush();
}
}

Expand Down
2 changes: 1 addition & 1 deletion Source/RAGroup.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
NSMutableArray * _children;
}

@property (readonly) NSArray * children;
@property (readonly, atomic) NSArray * children;

- (void)addChild:(RANode *)node;
- (void)removeChild:(RANode *)node;
Expand Down
2 changes: 2 additions & 0 deletions Source/RANodeVisitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#import "RAGroup.h"
#import "RATransform.h"
#import "RAGeometry.h"
#import "RAPageNode.h"


@interface RANodeVisitor : NSObject {
Expand All @@ -29,5 +30,6 @@
- (void)applyGroup:(RAGroup *)node;
- (void)applyTransform:(RATransform *)node;
- (void)applyGeometry:(RAGeometry *)node;
- (void)applyPageNode:(RAPageNode *)node;

@end
5 changes: 5 additions & 0 deletions Source/RANodeVisitor.m
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,9 @@ - (void)applyGeometry:(RAGeometry *)node
[self applyNode: node];
}

- (void)applyPageNode:(RAPageNode *)node
{
[self applyNode: node];
}

@end
5 changes: 5 additions & 0 deletions Source/RAPage.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#import "RAGeographicUtils.h"
#import "RABoundingSphere.h"
#import "RAGeometry.h"
#import "RACamera.h"
#import "RATileDatabase.h"


Expand Down Expand Up @@ -40,4 +41,8 @@

- (void)setCenter:(GLKVector3)center andRadius:(double)radius;

- (float)calculateTiltWithCamera:(RACamera *)camera;
- (float)calculateScreenSpaceErrorWithCamera:(RACamera *)camera;
- (BOOL)isOnscreenWithCamera:(RACamera *)camera;

@end
42 changes: 38 additions & 4 deletions Source/RAPage.m
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@
@implementation RAPage {
RABoundingSphere * _bound;
RAPage * _parent;

NSURLConnection * _connection;
NSMutableData * _imageData;
}

@synthesize tile, key;
Expand All @@ -35,7 +32,9 @@ - (RAPage *)initWithTileID:(TileID)t andParent:(RAPage *)parent;
}

- (void)dealloc {
[_connection cancel];
[imageryLoadOp cancel];
[terrainLoadOp cancel];
[updatePageOp cancel];
}

- (void)setCenter:(GLKVector3)center andRadius:(double)radius {
Expand All @@ -44,4 +43,39 @@ - (void)setCenter:(GLKVector3)center andRadius:(double)radius {
_bound.radius = radius;
}

- (float)calculateTiltWithCamera:(RACamera *)camera {
// calculate dot product between page normal and camera vector
const GLKVector3 unitZ = { 0, 0, -1 };
GLKVector3 pageNormal = GLKVector3Normalize(self.bound.center);
GLKVector3 cameraLook = GLKVector3Normalize(GLKMatrix4MultiplyAndProjectVector3( GLKMatrix4Invert(camera.modelViewMatrix, NULL), unitZ ));
return GLKVector3DotProduct(pageNormal, cameraLook);
}

- (float)calculateScreenSpaceErrorWithCamera:(RACamera *)camera {
// !!! this does not work so well on large, curved pages
// in this case, should test all four corners of the tile and take min distance
GLKVector3 center = GLKMatrix4MultiplyAndProjectVector3( camera.modelViewMatrix, self.bound.center );
double distance = GLKVector3Length(center);
//double distance = -center.z; // seem like this should be more accurate, but the math clearing isn't quite right, as it favors pages near the equator

// !!! this should be based upon the Camera parameters
double theta = GLKMathDegreesToRadians(65.0f);
double w = 2. * distance * tan(theta/2.);

// convert object error to screen error
double x = 1024; // screen size
double epsilon = ( 2. * self.bound.radius ) / 256.; // object error
return ( epsilon * x ) / w;
}

- (BOOL)isOnscreenWithCamera:(RACamera *)camera {
GLKMatrix4 modelViewProjectionMatrix = GLKMatrix4Multiply( camera.projectionMatrix, camera.modelViewMatrix );

RABoundingSphere * sb = [self.bound transform:modelViewProjectionMatrix];
if ( sb.center.x + sb.radius < -1.5 || sb.center.x - sb.radius > 1.5 ) return NO;
if ( sb.center.y + sb.radius < -1.5 || sb.center.y - sb.radius > 1.5 ) return NO;

return YES;
}

@end
17 changes: 17 additions & 0 deletions Source/RAPageNode.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//
// RAPageNode.h
// EarthViewExample
//
// Created by Ross Anderson on 5/3/12.
// Copyright (c) 2012 Ross Anderson. All rights reserved.
//

#import "RANode.h"

#import "RAPage.h"

@interface RAPageNode : RANode

@property (strong, nonatomic) RAPage * page;

@end
33 changes: 33 additions & 0 deletions Source/RAPageNode.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//
// RAPageNode.m
// EarthViewExample
//
// Created by Ross Anderson on 5/3/12.
// Copyright (c) 2012 Ross Anderson. All rights reserved.
//

#import "RAPageNode.h"

@implementation RAPageNode

@synthesize page = _page;


- (SEL)visitorSelector {
return @selector(applyPageNode:);
}

- (void)calculateBound {
_bound = self.page.bound;
}

- (RAPage *)page {
return _page;
}

- (void)setPage:(RAPage *)page {
_page = page;
[self dirtyBound];
}

@end
38 changes: 38 additions & 0 deletions Source/RARenderVisitor.m
Original file line number Diff line number Diff line change
Expand Up @@ -170,4 +170,42 @@ - (void)applyGeometry:(RAGeometry *)node
[renderQueue addObject: data];
}

- (void)applyPageNode:(RAPageNode *)node
{
[self traversePage: node.page];
}

- (void)traversePage:(RAPage *)page {
// is the page facing away from the camera?
if ( [page calculateTiltWithCamera:self.camera] < -0.5f ) return;

float texelError = 0.0f;
texelError = [page calculateScreenSpaceErrorWithCamera:self.camera];

// should we choose to display this page?
if ( texelError < 3.f && page.geometry ) {
// don't bother traversing if we are offscreen
if ( ! [page isOnscreenWithCamera:self.camera] ) return;

[self applyGeometry: page.geometry];
return;
}

// are the children available?
if ( page.child1.geometry && page.child2.geometry && page.child3.geometry && page.child4.geometry ) {
// traverse children
[self traversePage: page.child1];
[self traversePage: page.child2];
[self traversePage: page.child3];
[self traversePage: page.child4];
} else {
// don't bother traversing if we are offscreen
if ( ! [page isOnscreenWithCamera:self.camera] ) return;

[self applyGeometry: page.geometry];
return;
}
}


@end
8 changes: 4 additions & 4 deletions Source/RASceneGraphController.m
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,8 @@ - (void)viewDidLoad

manipulator.view = self.view;

// create another context to load textures into
self.pager.loadingContext = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2 sharegroup:[context sharegroup]];
// create another context for threaded operations
self.pager.auxilliaryContext = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2 sharegroup:[context sharegroup]];

[self.pager setup];
[self setupGL];
Expand Down Expand Up @@ -225,7 +225,7 @@ - (RAGeometry *)makeBoxWithHalfWidth:(GLfloat)half
- (RANode *)createBlueMarble
{
RAGroup * root = [RAGroup new];
[root addChild: self.pager.nodes];
[root addChild: self.pager.rootNode];

return root;
}
Expand Down Expand Up @@ -313,7 +313,7 @@ - (void)update
skybox.transform.projectionMatrix = projectionMatrix;
skybox.transform.modelviewMatrix = self.camera.modelViewMatrix;

[self.pager updateSceneGraph];
[self.pager update];
}

- (void)glkView:(GLKView *)view drawInRect:(CGRect)rect
Expand Down
6 changes: 3 additions & 3 deletions Source/RATilePager.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@

@property (strong) RATileDatabase * imageryDatabase;
@property (strong) RATileDatabase * terrainDatabase;
@property (strong) EAGLContext * loadingContext;
@property (strong) EAGLContext * auxilliaryContext;

@property (readonly) RAGroup * nodes;
@property (readonly) RAGroup * rootNode;
@property (readonly) NSSet * rootPages;
@property (strong) RACamera * camera;

- (void)setup; // call once the databases are configured
- (void)updateSceneGraph;
- (void)update;

@end
Loading

0 comments on commit aee1e40

Please sign in to comment.