-
-
Save zakki/4044778 to your computer and use it in GitHub Desktop.
j2objc aobench
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // | |
| // Generated by the J2ObjC translator. DO NOT EDIT! | |
| // source: AO.java | |
| // | |
| // Created by mac on 12/11/09. | |
| // | |
| @class IOSIntArray; | |
| @class IOSObjectArray; | |
| @class Intersection; | |
| @class Plane; | |
| @class Ray; | |
| @class Vec; | |
| #import "JreEmulation.h" | |
| @interface Vec : NSObject { | |
| @public | |
| float x_, y_, z_; | |
| } | |
| @property (nonatomic, assign) float x; | |
| @property (nonatomic, assign) float y; | |
| @property (nonatomic, assign) float z; | |
| - (id)initWithFloat:(float)x | |
| withFloat:(float)y | |
| withFloat:(float)z; | |
| - (id)initWithVec:(Vec *)v; | |
| - (Vec *)addWithVec:(Vec *)b; | |
| - (Vec *)subWithVec:(Vec *)b; | |
| - (Vec *)crossWithVec:(Vec *)b; | |
| - (void)normalize; | |
| - (float)len; | |
| - (float)dotWithVec:(Vec *)b; | |
| - (Vec *)neg; | |
| @end | |
| @interface Ray : NSObject { | |
| @public | |
| Vec *org_, *dir_; | |
| } | |
| @property (nonatomic, retain) Vec *org; | |
| @property (nonatomic, retain) Vec *dir; | |
| - (id)initWithVec:(Vec *)o | |
| withVec:(Vec *)d; | |
| @end | |
| @interface Intersection : NSObject { | |
| @public | |
| float t_; | |
| Vec *p_; | |
| Vec *n_; | |
| BOOL hit_; | |
| } | |
| @property (nonatomic, assign) float t; | |
| @property (nonatomic, retain) Vec *p; | |
| @property (nonatomic, retain) Vec *n; | |
| @property (nonatomic, assign) BOOL hit; | |
| - (id)init; | |
| @end | |
| @interface Sphere : NSObject { | |
| @public | |
| Vec *center_; | |
| float radius_; | |
| } | |
| @property (nonatomic, retain) Vec *center; | |
| @property (nonatomic, assign) float radius; | |
| - (id)initWithVec:(Vec *)center | |
| withFloat:(float)radius; | |
| - (void)intersectWithIntersection:(Intersection *)isect | |
| withRay:(Ray *)ray; | |
| @end | |
| @interface Plane : NSObject { | |
| @public | |
| Vec *p_; | |
| Vec *n_; | |
| } | |
| @property (nonatomic, retain) Vec *p; | |
| @property (nonatomic, retain) Vec *n; | |
| - (id)initWithVec:(Vec *)p | |
| withVec:(Vec *)n; | |
| - (void)intersectWithIntersection:(Intersection *)isect | |
| withRay:(Ray *)ray; | |
| @end | |
| @interface AO : NSObject { | |
| @public | |
| int IMAGE_WIDTH_; | |
| int IMAGE_HEIGHT_; | |
| int NSUBSAMPLES_; | |
| int NAO_SAMPLES_; | |
| IOSObjectArray *spheres_; | |
| Plane *plane_; | |
| } | |
| @property (nonatomic, assign) int IMAGE_WIDTH; | |
| @property (nonatomic, assign) int IMAGE_HEIGHT; | |
| @property (nonatomic, assign) int NSUBSAMPLES; | |
| @property (nonatomic, assign) int NAO_SAMPLES; | |
| @property (nonatomic, retain) IOSObjectArray *spheres; | |
| @property (nonatomic, retain) Plane *plane; | |
| - (void)setup; | |
| - (unichar)clampWithFloat:(float)f; | |
| - (void)orthoBasisWithVecArray:(IOSObjectArray *)basis | |
| withVec:(Vec *)n; | |
| - (Vec *)ambientOcclusionWithIntersection:(Intersection *)isect; | |
| - (void)rowRenderWithJavaLangIntegerArray:(IOSIntArray *)row | |
| withInt:(int)width | |
| withInt:(int)height | |
| withInt:(int)y | |
| withInt:(int)nsubsamples; | |
| - (void)generateWithNSString:(NSString *)fileName; | |
| - (id)init; | |
| @end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // | |
| // Generated by the J2ObjC translator. DO NOT EDIT! | |
| // source: AO.java | |
| // | |
| // Created by mac on 12/11/09. | |
| // | |
| #import "AO.h" | |
| #import "IOSIntArray.h" | |
| #import "IOSObjectArray.h" | |
| #import "java/io/BufferedWriter.h" | |
| #import "java/io/FileWriter.h" | |
| #import "java/io/IOException.h" | |
| #import "java/io/PrintWriter.h" | |
| #import "java/lang/Math.h" | |
| @implementation Vec | |
| @synthesize x = x_; | |
| @synthesize y = y_; | |
| @synthesize z = z_; | |
| - (id)initWithFloat:(float)x | |
| withFloat:(float)y | |
| withFloat:(float)z { | |
| if ((self = [super init])) { | |
| self.x = x; | |
| self.y = y; | |
| self.z = z; | |
| } | |
| return self; | |
| } | |
| - (id)initWithVec:(Vec *)v { | |
| if ((self = [super init])) { | |
| self.x = ((Vec *) NIL_CHK(v)).x; | |
| self.y = ((Vec *) NIL_CHK(v)).y; | |
| self.z = ((Vec *) NIL_CHK(v)).z; | |
| } | |
| return self; | |
| } | |
| - (Vec *)addWithVec:(Vec *)b { | |
| return [[[Vec alloc] initWithFloat:self.x + ((Vec *) NIL_CHK(b)).x withFloat:self.y + ((Vec *) NIL_CHK(b)).y withFloat:self.z + ((Vec *) NIL_CHK(b)).z] autorelease]; | |
| } | |
| - (Vec *)subWithVec:(Vec *)b { | |
| return [[[Vec alloc] initWithFloat:self.x - ((Vec *) NIL_CHK(b)).x withFloat:self.y - ((Vec *) NIL_CHK(b)).y withFloat:self.z - ((Vec *) NIL_CHK(b)).z] autorelease]; | |
| } | |
| - (Vec *)crossWithVec:(Vec *)b { | |
| float u = self.y * ((Vec *) NIL_CHK(b)).z - ((Vec *) NIL_CHK(b)).y * self.z; | |
| float v = self.z * ((Vec *) NIL_CHK(b)).x - ((Vec *) NIL_CHK(b)).z * self.x; | |
| float w = self.x * ((Vec *) NIL_CHK(b)).y - ((Vec *) NIL_CHK(b)).x * self.y; | |
| return [[[Vec alloc] initWithFloat:u withFloat:v withFloat:w] autorelease]; | |
| } | |
| - (void)normalize { | |
| float d = [self len]; | |
| if ([JavaLangMath absWithFloat:d] > 1.0e-6) { | |
| float invlen = 1.0f / d; | |
| self.x *= invlen; | |
| self.y *= invlen; | |
| self.z *= invlen; | |
| } | |
| } | |
| - (float)len { | |
| float d = self.x * self.x + self.y * self.y + self.z * self.z; | |
| return (float) [JavaLangMath sqrtWithDouble:d]; | |
| } | |
| - (float)dotWithVec:(Vec *)b { | |
| return self.x * ((Vec *) NIL_CHK(b)).x + self.y * ((Vec *) NIL_CHK(b)).y + self.z * ((Vec *) NIL_CHK(b)).z; | |
| } | |
| - (Vec *)neg { | |
| return [[[Vec alloc] initWithFloat:-self.x withFloat:-self.y withFloat:-self.z] autorelease]; | |
| } | |
| - (void)copyAllPropertiesTo:(id)copy { | |
| [super copyAllPropertiesTo:copy]; | |
| Vec *typedCopy = (Vec *) copy; | |
| typedCopy.x = x_; | |
| typedCopy.y = y_; | |
| typedCopy.z = z_; | |
| } | |
| @end | |
| @implementation Ray | |
| @synthesize org = org_; | |
| @synthesize dir = dir_; | |
| - (id)initWithVec:(Vec *)o | |
| withVec:(Vec *)d { | |
| if ((self = [super init])) { | |
| JreOperatorRetainedAssign(&org_, o); | |
| JreOperatorRetainedAssign(&dir_, d); | |
| } | |
| return self; | |
| } | |
| - (void)dealloc { | |
| JreOperatorRetainedAssign(&dir_, nil); | |
| JreOperatorRetainedAssign(&org_, nil); | |
| [super dealloc]; | |
| } | |
| - (void)copyAllPropertiesTo:(id)copy { | |
| [super copyAllPropertiesTo:copy]; | |
| Ray *typedCopy = (Ray *) copy; | |
| typedCopy.org = org_; | |
| typedCopy.dir = dir_; | |
| } | |
| @end | |
| @implementation Intersection | |
| @synthesize t = t_; | |
| @synthesize p = p_; | |
| @synthesize n = n_; | |
| @synthesize hit = hit_; | |
| - (id)init { | |
| if ((self = [super init])) { | |
| hit_ = NO; | |
| t_ = 1.0e+30f; | |
| JreOperatorRetainedAssign(&n_, [[[Vec alloc] initWithFloat:0.0f withFloat:0.0f withFloat:0.0f] autorelease]); | |
| } | |
| return self; | |
| } | |
| - (void)dealloc { | |
| JreOperatorRetainedAssign(&n_, nil); | |
| JreOperatorRetainedAssign(&p_, nil); | |
| [super dealloc]; | |
| } | |
| - (void)copyAllPropertiesTo:(id)copy { | |
| [super copyAllPropertiesTo:copy]; | |
| Intersection *typedCopy = (Intersection *) copy; | |
| typedCopy.t = t_; | |
| typedCopy.p = p_; | |
| typedCopy.n = n_; | |
| typedCopy.hit = hit_; | |
| } | |
| @end | |
| @implementation Sphere | |
| @synthesize center = center_; | |
| @synthesize radius = radius_; | |
| - (id)initWithVec:(Vec *)center | |
| withFloat:(float)radius { | |
| if ((self = [super init])) { | |
| self.center = center; | |
| self.radius = radius; | |
| } | |
| return self; | |
| } | |
| - (void)intersectWithIntersection:(Intersection *)isect | |
| withRay:(Ray *)ray { | |
| Vec *rs = [((Vec *) NIL_CHK(ray.org)) subWithVec:self.center]; | |
| float B = [((Vec *) NIL_CHK(rs)) dotWithVec:((Ray *) NIL_CHK(ray)).dir]; | |
| float C = [((Vec *) NIL_CHK(rs)) dotWithVec:rs] - (self.radius * self.radius); | |
| float D = B * B - C; | |
| if (D > 0.0) { | |
| float t = -B - (float) [JavaLangMath sqrtWithDouble:D]; | |
| if ((t > 0.0) && (t < ((Intersection *) NIL_CHK(isect)).t)) { | |
| ((Intersection *) NIL_CHK(isect)).t = t; | |
| ((Intersection *) NIL_CHK(isect)).hit = YES; | |
| Vec *p = [[[Vec alloc] initWithFloat:((Vec *) NIL_CHK(ray.org)).x + ((Vec *) NIL_CHK(ray.dir)).x * t withFloat:((Vec *) NIL_CHK(ray.org)).y + ((Vec *) NIL_CHK(ray.dir)).y * t withFloat:((Vec *) NIL_CHK(ray.org)).z + ((Vec *) NIL_CHK(ray.dir)).z * t] autorelease]; | |
| Vec *n = [((Vec *) NIL_CHK(p)) subWithVec:center_]; | |
| [((Vec *) NIL_CHK(n)) normalize]; | |
| ((Intersection *) NIL_CHK(isect)).n = n; | |
| ((Intersection *) NIL_CHK(isect)).p = p; | |
| } | |
| } | |
| } | |
| - (void)dealloc { | |
| JreOperatorRetainedAssign(¢er_, nil); | |
| [super dealloc]; | |
| } | |
| - (void)copyAllPropertiesTo:(id)copy { | |
| [super copyAllPropertiesTo:copy]; | |
| Sphere *typedCopy = (Sphere *) copy; | |
| typedCopy.center = center_; | |
| typedCopy.radius = radius_; | |
| } | |
| @end | |
| @implementation Plane | |
| @synthesize p = p_; | |
| @synthesize n = n_; | |
| - (id)initWithVec:(Vec *)p | |
| withVec:(Vec *)n { | |
| if ((self = [super init])) { | |
| self.p = p; | |
| self.n = n; | |
| } | |
| return self; | |
| } | |
| - (void)intersectWithIntersection:(Intersection *)isect | |
| withRay:(Ray *)ray { | |
| float d = -[((Vec *) NIL_CHK(p_)) dotWithVec:n_]; | |
| float v = [((Vec *) NIL_CHK(ray.dir)) dotWithVec:n_]; | |
| if ([JavaLangMath absWithFloat:v] < 1.0e-6) return; | |
| float t = -([((Vec *) NIL_CHK(ray.org)) dotWithVec:n_] + d) / v; | |
| if ((t > 0) && (t < ((Intersection *) NIL_CHK(isect)).t)) { | |
| ((Intersection *) NIL_CHK(isect)).hit = YES; | |
| ((Intersection *) NIL_CHK(isect)).t = t; | |
| ((Intersection *) NIL_CHK(isect)).n = n_; | |
| Vec *p = [[[Vec alloc] initWithFloat:((Vec *) NIL_CHK(ray.org)).x + t * ((Vec *) NIL_CHK(ray.dir)).x withFloat:((Vec *) NIL_CHK(ray.org)).y + t * ((Vec *) NIL_CHK(ray.dir)).y withFloat:((Vec *) NIL_CHK(ray.org)).z + t * ((Vec *) NIL_CHK(ray.dir)).z] autorelease]; | |
| ((Intersection *) NIL_CHK(isect)).p = p; | |
| } | |
| } | |
| - (void)dealloc { | |
| JreOperatorRetainedAssign(&n_, nil); | |
| JreOperatorRetainedAssign(&p_, nil); | |
| [super dealloc]; | |
| } | |
| - (void)copyAllPropertiesTo:(id)copy { | |
| [super copyAllPropertiesTo:copy]; | |
| Plane *typedCopy = (Plane *) copy; | |
| typedCopy.p = p_; | |
| typedCopy.n = n_; | |
| } | |
| @end | |
| @implementation AO | |
| @synthesize IMAGE_WIDTH = IMAGE_WIDTH_; | |
| @synthesize IMAGE_HEIGHT = IMAGE_HEIGHT_; | |
| @synthesize NSUBSAMPLES = NSUBSAMPLES_; | |
| @synthesize NAO_SAMPLES = NAO_SAMPLES_; | |
| @synthesize spheres = spheres_; | |
| @synthesize plane = plane_; | |
| - (void)setup { | |
| JreOperatorRetainedAssign(&spheres_, [[[IOSObjectArray alloc] initWithLength:3 type:[IOSClass classWithClass:[Sphere class]]] autorelease]); | |
| [((IOSObjectArray *) NIL_CHK(spheres_)) replaceObjectAtIndex:0 withObject:[[[Sphere alloc] initWithVec:[[[Vec alloc] initWithFloat:-2.0f withFloat:0.0f withFloat:-3.5f] autorelease] withFloat:0.5f] autorelease]]; | |
| [((IOSObjectArray *) NIL_CHK(spheres_)) replaceObjectAtIndex:1 withObject:[[[Sphere alloc] initWithVec:[[[Vec alloc] initWithFloat:-0.5f withFloat:0.0f withFloat:-3.0f] autorelease] withFloat:0.5f] autorelease]]; | |
| [((IOSObjectArray *) NIL_CHK(spheres_)) replaceObjectAtIndex:2 withObject:[[[Sphere alloc] initWithVec:[[[Vec alloc] initWithFloat:1.0f withFloat:0.0f withFloat:-2.2f] autorelease] withFloat:0.5f] autorelease]]; | |
| JreOperatorRetainedAssign(&plane_, [[[Plane alloc] initWithVec:[[[Vec alloc] initWithFloat:0.0f withFloat:-0.5f withFloat:0.0f] autorelease] withVec:[[[Vec alloc] initWithFloat:0.0f withFloat:1.0f withFloat:0.0f] autorelease]] autorelease]); | |
| } | |
| - (unichar)clampWithFloat:(float)f { | |
| int i = (int) (f * 255.5); | |
| if (i < 0) i = 0; | |
| if (i > 255) i = 255; | |
| return (unichar) i; | |
| } | |
| - (void)orthoBasisWithVecArray:(IOSObjectArray *)basis | |
| withVec:(Vec *)n { | |
| int i; | |
| [((IOSObjectArray *) NIL_CHK(basis)) replaceObjectAtIndex:2 withObject:[[[Vec alloc] initWithVec:n] autorelease]]; | |
| [((IOSObjectArray *) NIL_CHK(basis)) replaceObjectAtIndex:1 withObject:[[[Vec alloc] initWithFloat:0.0f withFloat:0.0f withFloat:0.0f] autorelease]]; | |
| if ((((Vec *) NIL_CHK(n)).x < 0.6) && (((Vec *) NIL_CHK(n)).x > -0.6)) { | |
| ((Vec *) [((IOSObjectArray *) NIL_CHK(basis)) objectAtIndex:1]).x = 1.0f; | |
| } | |
| else if ((((Vec *) NIL_CHK(n)).y < 0.6) && (((Vec *) NIL_CHK(n)).y > -0.6)) { | |
| ((Vec *) [((IOSObjectArray *) NIL_CHK(basis)) objectAtIndex:1]).y = 1.0f; | |
| } | |
| else if ((((Vec *) NIL_CHK(n)).z < 0.6) && (((Vec *) NIL_CHK(n)).z > -0.6)) { | |
| ((Vec *) [((IOSObjectArray *) NIL_CHK(basis)) objectAtIndex:1]).z = 1.0f; | |
| } | |
| else { | |
| ((Vec *) [((IOSObjectArray *) NIL_CHK(basis)) objectAtIndex:1]).x = 1.0f; | |
| } | |
| [((IOSObjectArray *) NIL_CHK(basis)) replaceObjectAtIndex:0 withObject:[[((IOSObjectArray *) NIL_CHK(basis)) objectAtIndex:1] crossWithVec:[((IOSObjectArray *) NIL_CHK(basis)) objectAtIndex:2]]]; | |
| [[((IOSObjectArray *) NIL_CHK(basis)) objectAtIndex:0] normalize]; | |
| [((IOSObjectArray *) NIL_CHK(basis)) replaceObjectAtIndex:1 withObject:[[((IOSObjectArray *) NIL_CHK(basis)) objectAtIndex:2] crossWithVec:[((IOSObjectArray *) NIL_CHK(basis)) objectAtIndex:0]]]; | |
| [[((IOSObjectArray *) NIL_CHK(basis)) objectAtIndex:1] normalize]; | |
| } | |
| - (Vec *)ambientOcclusionWithIntersection:(Intersection *)isect { | |
| int i, j; | |
| int ntheta = NAO_SAMPLES_; | |
| int nphi = NAO_SAMPLES_; | |
| float eps = 0.0001f; | |
| Vec *p = [[[Vec alloc] initWithFloat:((Vec *) NIL_CHK(isect.p)).x + eps * ((Vec *) NIL_CHK(isect.n)).x withFloat:((Vec *) NIL_CHK(isect.p)).y + eps * ((Vec *) NIL_CHK(isect.n)).y withFloat:((Vec *) NIL_CHK(isect.p)).z + eps * ((Vec *) NIL_CHK(isect.n)).z] autorelease]; | |
| IOSObjectArray *basis; | |
| basis = [[[IOSObjectArray alloc] initWithLength:3 type:[IOSClass classWithClass:[Vec class]]] autorelease]; | |
| [self orthoBasisWithVecArray:basis withVec:((Intersection *) NIL_CHK(isect)).n]; | |
| float occlusion = 0.0f; | |
| for (j = 0; j < ntheta; j++) { | |
| for (i = 0; i < nphi; i++) { | |
| float r = (float) [JavaLangMath random]; | |
| float phi = 2.0f * (float) JavaLangMath_PI * (float) [JavaLangMath random]; | |
| float sq = (float) [JavaLangMath sqrtWithDouble:1.0 - r]; | |
| float x = (float) [JavaLangMath cosWithDouble:phi] * sq; | |
| float y = (float) [JavaLangMath sinWithDouble:phi] * sq; | |
| float z = (float) [JavaLangMath sqrtWithDouble:r]; | |
| float rx = x * ((Vec *) [((IOSObjectArray *) NIL_CHK(basis)) objectAtIndex:0]).x + y * ((Vec *) [((IOSObjectArray *) NIL_CHK(basis)) objectAtIndex:1]).x + z * ((Vec *) [((IOSObjectArray *) NIL_CHK(basis)) objectAtIndex:2]).x; | |
| float ry = x * ((Vec *) [((IOSObjectArray *) NIL_CHK(basis)) objectAtIndex:0]).y + y * ((Vec *) [((IOSObjectArray *) NIL_CHK(basis)) objectAtIndex:1]).y + z * ((Vec *) [((IOSObjectArray *) NIL_CHK(basis)) objectAtIndex:2]).y; | |
| float rz = x * ((Vec *) [((IOSObjectArray *) NIL_CHK(basis)) objectAtIndex:0]).z + y * ((Vec *) [((IOSObjectArray *) NIL_CHK(basis)) objectAtIndex:1]).z + z * ((Vec *) [((IOSObjectArray *) NIL_CHK(basis)) objectAtIndex:2]).z; | |
| Vec *raydir = [[[Vec alloc] initWithFloat:rx withFloat:ry withFloat:rz] autorelease]; | |
| Ray *ray = [[[Ray alloc] initWithVec:p withVec:raydir] autorelease]; | |
| Intersection *occIsect = [[[Intersection alloc] init] autorelease]; | |
| [[((IOSObjectArray *) NIL_CHK(spheres_)) objectAtIndex:0] intersectWithIntersection:occIsect withRay:ray]; | |
| [[((IOSObjectArray *) NIL_CHK(spheres_)) objectAtIndex:1] intersectWithIntersection:occIsect withRay:ray]; | |
| [[((IOSObjectArray *) NIL_CHK(spheres_)) objectAtIndex:2] intersectWithIntersection:occIsect withRay:ray]; | |
| [((Plane *) NIL_CHK(plane_)) intersectWithIntersection:occIsect withRay:ray]; | |
| if (((Intersection *) NIL_CHK(occIsect)).hit) occlusion += 1.0; | |
| } | |
| } | |
| occlusion = (ntheta * nphi - occlusion) / (float) (ntheta * nphi); | |
| return [[[Vec alloc] initWithFloat:occlusion withFloat:occlusion withFloat:occlusion] autorelease]; | |
| } | |
| - (void)rowRenderWithJavaLangIntegerArray:(IOSIntArray *)row | |
| withInt:(int)width | |
| withInt:(int)height | |
| withInt:(int)y | |
| withInt:(int)nsubsamples { | |
| for (int x = 0; x < width; x++) { | |
| float r = 0.0f; | |
| float g = 0.0f; | |
| float b = 0.0f; | |
| for (int v = 0; v < nsubsamples; v++) { | |
| for (int u = 0; u < nsubsamples; u++) { | |
| float px = (x + (u / (float) nsubsamples) - (width / 2.0f)) / (width / 2.0f); | |
| float py = (y + (v / (float) nsubsamples) - (height / 2.0f)) / (height / 2.0f); | |
| py = -py; | |
| float t = 10000.0f; | |
| Vec *eye = [[[Vec alloc] initWithFloat:px withFloat:py withFloat:-1.0f] autorelease]; | |
| [((Vec *) NIL_CHK(eye)) normalize]; | |
| Ray *ray = [[[Ray alloc] initWithVec:[[[Vec alloc] initWithFloat:0.0f withFloat:0.0f withFloat:0.0f] autorelease] withVec:[[[Vec alloc] initWithVec:eye] autorelease]] autorelease]; | |
| Intersection *isect = [[[Intersection alloc] init] autorelease]; | |
| [[((IOSObjectArray *) NIL_CHK(spheres_)) objectAtIndex:0] intersectWithIntersection:isect withRay:ray]; | |
| [[((IOSObjectArray *) NIL_CHK(spheres_)) objectAtIndex:1] intersectWithIntersection:isect withRay:ray]; | |
| [[((IOSObjectArray *) NIL_CHK(spheres_)) objectAtIndex:2] intersectWithIntersection:isect withRay:ray]; | |
| [((Plane *) NIL_CHK(plane_)) intersectWithIntersection:isect withRay:ray]; | |
| if (((Intersection *) NIL_CHK(isect)).hit) { | |
| t = ((Intersection *) NIL_CHK(isect)).t; | |
| Vec *col = [self ambientOcclusionWithIntersection:isect]; | |
| r += ((Vec *) NIL_CHK(col)).x; | |
| g += ((Vec *) NIL_CHK(col)).y; | |
| b += ((Vec *) NIL_CHK(col)).z; | |
| } | |
| } | |
| } | |
| [((IOSIntArray *) NIL_CHK(row)) replaceIntAtIndex:3 * x + 0 withInt:[self clampWithFloat:r / (nsubsamples * nsubsamples)]]; | |
| [((IOSIntArray *) NIL_CHK(row)) replaceIntAtIndex:3 * x + 1 withInt:[self clampWithFloat:g / (nsubsamples * nsubsamples)]]; | |
| [((IOSIntArray *) NIL_CHK(row)) replaceIntAtIndex:3 * x + 2 withInt:[self clampWithFloat:b / (nsubsamples * nsubsamples)]]; | |
| } | |
| } | |
| - (void)generateWithNSString:(NSString *)fileName { | |
| IOSIntArray *renderLine = [[[IOSIntArray alloc] initWithLength:IMAGE_WIDTH_ * 3] autorelease]; | |
| JavaIoPrintWriter *fout = [[[JavaIoPrintWriter alloc] initWithJavaIoWriter:[[[JavaIoBufferedWriter alloc] initWithJavaIoWriter:[[[JavaIoFileWriter alloc] initWithNSString:fileName] autorelease]] autorelease]] autorelease]; | |
| [((JavaIoPrintWriter *) NIL_CHK(fout)) printlnWithNSString:@"P3"]; | |
| [((JavaIoPrintWriter *) NIL_CHK(fout)) printlnWithNSString:[NSString stringWithFormat:@"%d %d", IMAGE_WIDTH_, IMAGE_HEIGHT_]]; | |
| [((JavaIoPrintWriter *) NIL_CHK(fout)) printlnWithNSString:@"255"]; | |
| for (int y = 0; y < IMAGE_HEIGHT_; y++) { | |
| [self rowRenderWithJavaLangIntegerArray:renderLine withInt:IMAGE_WIDTH_ withInt:IMAGE_HEIGHT_ withInt:y withInt:NSUBSAMPLES_]; | |
| for (int x = 0; x < (IMAGE_WIDTH_ * 3); x += 3) { | |
| [((JavaIoPrintWriter *) NIL_CHK(fout)) printWithNSString:[NSString stringWithFormat:@"%d ", [((IOSIntArray *) NIL_CHK(renderLine)) intAtIndex:x + 0]]]; | |
| [((JavaIoPrintWriter *) NIL_CHK(fout)) printWithNSString:[NSString stringWithFormat:@"%d ", [((IOSIntArray *) NIL_CHK(renderLine)) intAtIndex:x + 1]]]; | |
| [((JavaIoPrintWriter *) NIL_CHK(fout)) printlnWithInt:[((IOSIntArray *) NIL_CHK(renderLine)) intAtIndex:x + 2]]; | |
| } | |
| } | |
| [((JavaIoPrintWriter *) NIL_CHK(fout)) close]; | |
| } | |
| - (id)init { | |
| if ((self = [super init])) { | |
| IMAGE_WIDTH_ = 256; | |
| IMAGE_HEIGHT_ = 256; | |
| NSUBSAMPLES_ = 2; | |
| NAO_SAMPLES_ = 8; | |
| } | |
| return self; | |
| } | |
| - (void)dealloc { | |
| JreOperatorRetainedAssign(&plane_, nil); | |
| JreOperatorRetainedAssign(&spheres_, nil); | |
| [super dealloc]; | |
| } | |
| - (void)copyAllPropertiesTo:(id)copy { | |
| [super copyAllPropertiesTo:copy]; | |
| AO *typedCopy = (AO *) copy; | |
| typedCopy.IMAGE_WIDTH = IMAGE_WIDTH_; | |
| typedCopy.IMAGE_HEIGHT = IMAGE_HEIGHT_; | |
| typedCopy.NSUBSAMPLES = NSUBSAMPLES_; | |
| typedCopy.NAO_SAMPLES = NAO_SAMPLES_; | |
| typedCopy.spheres = spheres_; | |
| typedCopy.plane = plane_; | |
| } | |
| @end | |
| int main( int argc, const char *argv[] ) { | |
| int exitCode = 0; | |
| NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; | |
| IOSObjectArray *args = JreEmulationMainArguments(argc, argv); | |
| AO *ao = [[[AO alloc] init] autorelease]; | |
| [((AO *) NIL_CHK(ao)) setup]; | |
| @try { | |
| [((AO *) NIL_CHK(ao)) generateWithNSString:@"ao_java.ppm"]; | |
| } | |
| @catch (JavaIoIOException *e) { | |
| } | |
| [pool release]; | |
| return exitCode; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| $ time ./a.out | |
| ./a.out 62.40s user 2.40s system 99% cpu 1:05.04 total | |
| $ time java AO | |
| java AO 2.67s user 0.05s system 109% cpu 2.490 total |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment