Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //----Render loop-----//
- this.canvas = this.surfaceHolder.lockCanvas();
- int width = canvas.getWidth();
- int height = canvas.getHeight();
- float fov = 160.0f * (float)Math.PI / 180.0f;
- float zdir = 1.0f / (float)Math.tan(fov);
- float aspect = (float)height / (float)width;
- for(int x = 0; x < width; x++){
- for(int y = 0; y < height; y++){
- float xdir = (x / (float)width) * 2.0f - 1.0f;
- float ydir = ((y / (float)height) * 2.0f - 1.0f) * aspect;
- Vector3D direction = new Vector3D(xdir, ydir, zdir).normalize();
- Ray ray = new Ray(Camera, direction);
- float r = 0, g = 0, b = 0;
- for(int i = 0; i < RAYS_PER_PIXEL; i++){
- CColor color = Trace(ray, 0);
- r += color.r;
- g += color.g;
- b += color.b;
- }
- r/= RAYS_PER_PIXEL;
- g/= RAYS_PER_PIXEL;
- b/= RAYS_PER_PIXEL;
- Paint paint = new Paint();
- paint.setColor(Color.argb(255,(int)r,(int)g,(int)b));
- canvas.drawPoint(x,y,paint);
- //---Trace Function---//
- public CColor Trace(Ray ray, int depth){
- float hitDistance = 5000.0f;
- BaseObject hitObject = null;
- for(BaseObject obj: scene.SceneObjects){
- float intersect = obj.intersect(ray);
- if(intersect < hitDistance && intersect > -1.0f) {
- hitDistance = intersect;
- hitObject = obj;
- }
- }
- if(hitDistance == 5000.0f) {
- return new CColor(0,0,0);
- }
- if(hitObject.isEmitter) {
- return hitObject.color;
- }
- if(depth == MAX_DEPTH) {
- return new CColor(0,0,0);
- }
- Vector3D hitPoint = ray.origin.add(ray.direction.mult(hitDistance * 0.99f));
- Vector3D normal = hitObject.normal(hitPoint);
- Vector3D randomVector = Vector3D.getRandomVectorInHemisphere();
- if(randomVector.dot(normal) < 0.0f)
- randomVector = randomVector.negate();
- Ray reflectionRay = new Ray(hitPoint, randomVector.normalize());
- CColor returnColor = Trace(reflectionRay, depth + 1);
- float r = hitObject.color.r * returnColor.r;
- float g = hitObject.color.g * returnColor.g;
- float b = hitObject.color.b * returnColor.b;
- r /= 255.0f;
- g /= 255.0f;
- b /= 255.0f;
- return new CColor(r,g,b);
- }
- //---Vector Class---//
- public class Vector3D {
- public float x;
- public float y;
- public float z;
- private static Random rnd = new Random(new Date().getTime());
- public Vector3D(float x, float y, float z){
- this.x = x;
- this.y = y;
- this.z = z;
- }
- public Vector3D add(Vector3D addVector){
- return new Vector3D(this.x + addVector.x, this.y + addVector.y, this.z + addVector.z);
- }
- public Vector3D sub(Vector3D subVector){
- return new Vector3D(this.x - subVector.x, this.y - subVector.y, this.z - subVector.z);
- }
- public Vector3D mult(Vector3D multVector){
- return new Vector3D(this.x * multVector.x, this.y * multVector.y, this.z * multVector.z);
- }
- public Vector3D mult(float mult){
- return mult(new Vector3D(mult,mult,mult));
- }
- public float dot(Vector3D dotVector){
- return this.x * dotVector.x + this.y * dotVector.y + this.z * dotVector.z;
- }
- public Vector3D normalize(){
- float f = (float)(1.0f / Math.sqrt(this.dot(this)));
- x *= f;
- y *= f;
- z *= f;
- return new Vector3D(x,y,z);
- }
- public Vector3D negate(){
- return new Vector3D(this.x = -x, this.y = -y, this.z = -z);
- }
- public static Vector3D getRandomVectorInHemisphere(){
- float x = (float)rnd.nextDouble() * 2.0f - 1.0f;
- float y = (float)rnd.nextDouble() * 2.0f - 1.0f;
- float z = (float)rnd.nextDouble() * 2.0f - 1.0f;
- int negate = rnd.nextInt();
- if(negate % 2 > 0){
- x *= -1;
- y *= -1;
- z *= -1;
- }
- return new Vector3D(x,y,z).normalize();
- }
- }
- //---Sphere---//
- public class Sphere extends BaseObject {
- public Vector3D center;
- public float radius;
- public Sphere(CColor color, boolean isEmitter, Vector3D center, float radius){
- this.color = color;
- this.isEmitter = isEmitter;
- this.center = center;
- this.radius = radius;
- }
- public float intersect(Ray ray){
- Vector3D v = ray.origin.sub(this.center);
- float a = ray.direction.dot(ray.direction);
- float b = 2.0f * ray.direction.dot(v);
- float c = v.dot(v) - (this.radius * this.radius);
- float discriminant = (b*b) - (4.0f * a * c);
- if(discriminant > 0){
- float x1 = (-b - (float)Math.sqrt(discriminant) / (2.0f * a));
- float x2 = (-b + (float)Math.sqrt(discriminant) / (2.0f * a));
- if (x1 >= 0 && x2 >= 0) return x1;
- if (x1 < 0 && x2 >= 0) return x2;
- else return -1.0f;
- }
- else
- return -1.0f;
- }
- public Vector3D normal(Vector3D intersectionVector){
- return intersectionVector.sub(this.center).normalize();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement