java - Writing a Raytracer, and cant get the image to be centred properly? -



java - Writing a Raytracer, and cant get the image to be centred properly? -

i'm writing raytracer in java, i've gotten point can create objects, rays, test intersections , colour pixels. i've got basic anti aliasing done. problem if create sphere, should in centre of world (i.e 0.0, 0.0, 0.0) , draw image, end image this.

when reddish circle should in middle of image.

main method public static void main(string[] args) { system.out.println("rendering..."); long start = system.nanotime(); // setting size of image rendered world = new world(1920, 1080, 1.0); image = new image("image.png"); sampler = new simplesampler(4); projector = new orthographicprojector(); // main loop of program, goes through each pixel in image , assigns colour value (int y = 0; y < world.viewplane.height; y++) { (int x = 0; x < world.viewplane.width; x++) { // render pixel colour trace(x, y); } } image.saveimage("png"); long end = system.nanotime(); system.out.print("loop time = " + ((end - start)/1000000000.0f)); } trace method public static void trace(int x, int y) { colour colour = new colour(); //int colour = raytracer.world.backgroundcolour.tointeger(); (int col = 0; col < sampler.samples; col++) { (int row = 0; row < sampler.samples; row++) { point2d point = sampler.sample(row, col, x, y); ray ray = projector.createray(point); double min = double.max_value; colour tempcolour = new colour(); (int = 0; < world.worldobjects.size(); i++) { double temp = world.worldobjects.get(i).intersect(ray); if (temp != 0 && temp < min) { min = temp; tempcolour = world.worldobjects.get(i).colour; } } colour.add(tempcolour); } } colour.divide(sampler.samples*sampler.samples); image.buffer.setrgb(x, y, colour.tointeger()); } world.java public class world { public viewplane viewplane; public arraylist<renderable> worldobjects; public colour backgroundcolour; public world(int width, int height, double size) { viewplane = new viewplane(width, height, size); backgroundcolour = new colour(0.0f, 0.0f, 0.0f); worldobjects = new arraylist<renderable>(); worldobjects.add(new sphere(new point3d(0.0, 0.0, 0.0), 50, new colour(1.0f, 0.0f, 0.0f))); //worldobjects.add(new sphere(new point3d(-150.0, 0.0, 0.0), 50, new colour(1.0f, 0.0f, 0.0f))); //worldobjects.add(new sphere(new point3d(0.0, -540.0, 0.0), 50, new colour(0.0f, 1.0f, 0.0f))); } } simplesampler.java public class simplesampler extends sampler { public simplesampler(int samples) { this.samples = samples; } public point2d sample(int row, int col, int x, int y) { point2d point = new point2d( x - raytracer.world.viewplane.width / 2 + (col + 0.5) / samples, y - raytracer.world.viewplane.width / 2 + (row + 0.5) / samples); homecoming point; } } orthographicprojector.java public class orthographicprojector extends projector{ public ray createray(point2d point) { ray ray = new ray(); ray.origin = new point3d( raytracer.world.viewplane.size * point.x, raytracer.world.viewplane.size * point.y, 100); ray.direction = new vector3d(0.0, 0.0, -1.0); homecoming ray; } }

i have feeling somewhere along way i've mixed x y , has rotated image, haven't been able track downwards problem. if see more of code happy show it.

in simplesampler.java:

point2d point = new point2d( x - raytracer.world.viewplane.width / 2 + (col + 0.5) / samples, y - raytracer.world.viewplane.width / 2 + (row + 0.5) / samples);

you utilize width both coordinates. maybe should utilize width , height.

java raytracing

Comments

Popular posts from this blog

java - How to set log4j.defaultInitOverride property to false in jboss server 6 -

c - GStreamer 1.0 1.4.5 RTSP Example Server sends 503 Service unavailable -

Using ajax with sonata admin list view pagination -