From: Roland Hieber Date: Wed, 20 Jan 2010 01:41:59 +0000 (+0100) Subject: simple anti-aliasing X-Git-Url: https://git.rohieb.name/MicroTrace.git/commitdiff_plain/6f4cba3fdf962f3215cdd916ecff13ffade26847 simple anti-aliasing --- diff --git a/MicroTrace.cxx b/MicroTrace.cxx index 53c56ee..252ec18 100644 --- a/MicroTrace.cxx +++ b/MicroTrace.cxx @@ -17,7 +17,7 @@ void RenderFrameFlat(const std::string& fileName) /* Scene definition */ Scene scene; - + /* Flat shaders */ FlatShader shd1(&scene, Vec3f(1,0,0)); // red surface FlatShader shd2(&scene, Vec3f(0,1,0)); // green surface @@ -32,34 +32,39 @@ void RenderFrameFlat(const std::string& fileName) Sphere s3(Vec3f(3,0.8,-2), 2, &shd3); InfinitePlane p1(Vec3f(0,-1,0),Vec3f(0,1,0), &shd4); - - Triangle t1(Vec3f(-2,3,1),Vec3f(1,2,1),Vec3f(3,2.8,3), &shd5); - + + Triangle t1(Vec3f(-2,3,1),Vec3f(1,2,1),Vec3f(3,2.8,3), &shd5); + /* add to scene */ scene.Add(&s1); scene.Add(&s2); scene.Add(&s3); scene.Add(&p1); scene.Add(&t1); - - + + Image img(scene.camera()->resX(),scene.camera()->resY()); // image array Ray ray; // primary ray - + for(int y = 0; y < scene.camera()->resY(); y++) { - for (int x = 0; x < scene.camera()->resX(); x++) - { - - /* Initialize your ray here */ - - scene.camera()->InitRay(x+0.5,y+0.5,ray); // initialize ray - - Vec3f col = scene.RayTrace(ray); - - img(x,y) = col; // store pixel color - //std::cerr << "Main: Image color = " << img(x,y) << std::endl; - } + for (int x = 0; x < scene.camera()->resX(); x++) + { + + /* Initialize your ray here */ + // shoot four rays for antialiasing + scene.camera()->InitRay(x+0.5,y+0.5, ray); // initialize ray + Vec3f col1 = scene.RayTrace(ray); + scene.camera()->InitRay(x-0.5,y+0.5, ray); // initialize ray + Vec3f col2 = scene.RayTrace(ray); + scene.camera()->InitRay(x-0.5,y-0.5, ray); // initialize ray + Vec3f col3 = scene.RayTrace(ray); + scene.camera()->InitRay(x-0.5,y-0.5, ray); // initialize ray + Vec3f col4 = scene.RayTrace(ray); + + img(x,y) = (col1 + col2 + col3 + col4) / 4.0; // store pixel color + //std::cerr << "Main: Image color = " << img(x,y) << std::endl; + } } img.WritePPM(fileName); // write final image } @@ -70,7 +75,7 @@ void RenderFrameEyeLight(const std::string& fileName) /* Scene definition */ Scene scene; - + /* Flat shaders */ EyeLightShader shd1(&scene, Vec3f(1,0,0)); // red surface EyeLightShader shd2(&scene, Vec3f(0,1,0)); // green surface @@ -85,33 +90,37 @@ void RenderFrameEyeLight(const std::string& fileName) Sphere s3(Vec3f(3,0.8,-2), 2, &shd3); InfinitePlane p1(Vec3f(0,-1,0),Vec3f(0,1,0), &shd4); - - Triangle t1(Vec3f(-2,3,1),Vec3f(1,2,1),Vec3f(3,2.8,3), &shd5); - + + Triangle t1(Vec3f(-2,3,1),Vec3f(1,2,1),Vec3f(3,2.8,3), &shd5); + /* add to scene */ scene.Add(&s1); scene.Add(&s2); scene.Add(&s3); scene.Add(&p1); scene.Add(&t1); - + Image img(scene.camera()->resX(),scene.camera()->resY()); // image array Ray ray; // primary ray - + for(int y = 0; y < scene.camera()->resY(); y++) { - for (int x = 0; x < scene.camera()->resX(); x++) - { - - /* Initialize your ray here */ - - scene.camera()->InitRay(x+0.5,y+0.5,ray); // initialize ray - - Vec3f col = scene.RayTrace(ray); - - img(x,y) = col; // store pixel color - //std::cerr << "Main: Image color = " << img(x,y) << std::endl; - } + for (int x = 0; x < scene.camera()->resX(); x++) + { + + /* Initialize your ray here */ + // shoot four rays for antialiasing + scene.camera()->InitRay(x+0.5,y+0.5, ray); // initialize ray + Vec3f col1 = scene.RayTrace(ray); + scene.camera()->InitRay(x-0.5,y+0.5, ray); // initialize ray + Vec3f col2 = scene.RayTrace(ray); + scene.camera()->InitRay(x-0.5,y-0.5, ray); // initialize ray + Vec3f col3 = scene.RayTrace(ray); + scene.camera()->InitRay(x-0.5,y-0.5, ray); // initialize ray + Vec3f col4 = scene.RayTrace(ray); + + img(x,y) = (col1 + col2 + col3 + col4) / 4.0; // store pixel color + } } img.WritePPM(fileName); // write final image } @@ -121,7 +130,7 @@ void RenderFrameReflectiveEyeLight(const std::string& fileName) /* Scene definition */ Scene scene; - + /* Flat shaders */ ReflectiveEyeLightShader shd1(&scene, 1.0, Vec3f(1,0,0)); // red surface ReflectiveEyeLightShader shd2(&scene, 1.0, Vec3f(0.0,1.0,0.0)); // green surface @@ -136,34 +145,38 @@ void RenderFrameReflectiveEyeLight(const std::string& fileName) Sphere s3(Vec3f(3,0.8,-2), 2, &shd3); InfinitePlane p1(Vec3f(0,-1,0),Vec3f(0,1,0), &shd4); - - Triangle t1(Vec3f(-2,3,1),Vec3f(1,2,1),Vec3f(3,2.8,3), &shd5); - + + Triangle t1(Vec3f(-2,3,1),Vec3f(1,2,1),Vec3f(3,2.8,3), &shd5); + /* add to scene */ scene.Add(&s1); scene.Add(&s2); scene.Add(&s3); scene.Add(&p1); scene.Add(&t1); - - + + Image img(scene.camera()->resX(),scene.camera()->resY()); // image array Ray ray; // primary ray - + for(int y = 0; y < scene.camera()->resY(); y++) { - for (int x = 0; x < scene.camera()->resX(); x++) - { - - /* Initialize your ray here */ - - scene.camera()->InitRay(x+0.5,y+0.5,ray); // initialize ray - - Vec3f col = scene.RayTrace(ray); - - img(x,y) = col; // store pixel color - //std::cerr << "Main: Image color = " << img(x,y) << std::endl; - } + for (int x = 0; x < scene.camera()->resX(); x++) + { + /* Initialize your ray here */ + // shoot four rays for antialiasing + scene.camera()->InitRay(x+0.5,y+0.5, ray); // initialize ray + Vec3f col1 = scene.RayTrace(ray); + scene.camera()->InitRay(x-0.5,y+0.5, ray); // initialize ray + Vec3f col2 = scene.RayTrace(ray); + scene.camera()->InitRay(x-0.5,y-0.5, ray); // initialize ray + Vec3f col3 = scene.RayTrace(ray); + scene.camera()->InitRay(x-0.5,y-0.5, ray); // initialize ray + Vec3f col4 = scene.RayTrace(ray); + + img(x,y) = (col1 + col2 + col3 + col4) / 4.0; // store pixel color + //std::cerr << "Main: Image color = " << img(x,y) << std::endl; + } } img.WritePPM(fileName); // write final image }