예제 #1
0
		void saveImage(std::string filename){
			std::ofstream file;
			file.open(filename);

			file << "P3" << std::endl;
			file << width_ << " " << height_ << std::endl;
			file << "255" << std::endl;

			Hitable *list[4];
			//float R = cos(M_PI / 4);
			list[0] = new Sphere(Vec3<float>(-1.5,0.0,-1.0), 0.5, new Lambertian(Vec3<float>(0.1,0.2,0.5)));
			list[1] = new Sphere(Vec3<float>(1, -100.5, -1.0), 100, new Lambertian(Vec3<float>(0.1, 0.4, 0.3)));
			list[2] = new Sphere(Vec3<float>(1.5, 0.0, -1.0), 0.5, new Metal(Vec3<float>(0.8, 0.6, 0.2),1.0));
			//list[3] = new Sphere(Vec3<float>(0.0, 0.0, -1.0), 0.5, new Dieletric(1.5));
			list[3] = new Box(Vec3<float>(0.0, -0.5, -1.5), Vec3<float>(0.5, 0.0, -1.0), new Dieletric(1.5));
			//list[4] = new Sphere(Vec3<float>(0.0, 0.0, -1.0), -0.495, new Dieletric(1.5));
			//list[4] = new Plane(Vec3<float>(-4.0, 5.0, -2.0), Vec3<float>(4.0, -1.0, -2.0), new Dieletric(1.5));
			//list[4] = new Plane(Vec3<float>(0.0, 0.0, -4.0), Vec3<float>(0.0, 1.0, 1.0), new Dieletric(1.5));
			//list[4] = new Sphere(Vec3<float>(-1.0, 0.0, -5.0), 3.5, new Lambertian(Vec3<float>(0.6, 0.4, 0.5)));
			//list[5] = new Sphere(Vec3<float>(-1.0, 0.0, -1.0), 0.5, new Dieletric(10.5));

			Hitable * world = new Hitable_list(list, 4);
			//Hitable *world = random_scene();

			Vec3<float> lookfrom(2.0, 0.0, 1.0), lookat(0.0, 0.0, -1.0);
			float dist_to_focus = (lookfrom - lookat).length();
			float aperture = 2.0;

			Camera cam(lookfrom, lookat,Vec3<float>(0,1,0), 60, float(width_/height_));
			int ns = 100;
			for (int i = height_-1; i >= 0; i--){
				for (int j = 0; j < width_; j++){

					Vec3<float> col(0.0, 0.0, 0.0);
					for (int s = 0; s < ns; s++)
					{
						float u = float(i + Rand()) / float(height_);
						float v = float(j + Rand()) / float(width_);

						Ray<float> r = cam.getRay(u, v);
						Vec3<float> p = r.point_at_parameter(2.0);
						col += color(r, world, 0);
					}

					float n = float(ns);
					col /= n;
					col = Vec3<float>(sqrt(col.x()), sqrt(col.y()), sqrt(col.z()));
					int ir = int(255.99*col.x()), ig = int(255.99*col.y()), ib = int(255.99*col.z());

					file << ir << " " << ig << " " << ib << endl;
				}
			}
			file.close();
		}
예제 #2
0
파일: main.cpp 프로젝트: nakdai/samples
int main(int argc, char* argv[])
{
    if (argc >= 2) {
        g_maxdepth = atoi(argv[1]);
        g_maxdepth = (g_maxdepth == 0 ? 50 : g_maxdepth);
    }
    if (argc >= 3) {
        g_samplenum = atoi(argv[2]);
        g_samplenum = (g_samplenum == 0 ? 10 : g_samplenum);
    }

    timer::init();

    ::glfwSetErrorCallback(errorCallback);

    auto result = ::glfwInit();
    if (!result) {
        assert(false);
        return 1;
    }

    ::glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2);
    ::glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);

    auto window = ::glfwCreateWindow(
        WIDTH,
        HEIGHT,
        TITLE,
        NULL, NULL);

    if (!window) {
        ::glfwTerminate();
        assert(false);
        return 1;
    }

#if 0
    ::glfwSetWindowCloseCallback(
        window,
        closeCallback);

    ::glfwSetKeyCallback(
        window,
        keyCallback);

    ::glfwSetMouseButtonCallback(
        window,
        mouseCallback);

    ::glfwSetCursorPosCallback(
        window,
        motionCallback);

    ::glfwSetScrollCallback(
        window,
        wheelCallback);
#endif

    glfwMakeContextCurrent(window);
    glfwSwapInterval(1);

    SetCurrentDirectoryFromExe();

    // TODO
    Rand::Init(0);

    auto world = cornell_box();

    vec3 lookfrom(278, 278, -800);
    vec3 lookat(278, 278, 0);

    float dist_to_focus = 10.0;
    float aperture = 0.1;

    camera cam(
        lookfrom, 
        lookat, 
        vec3(0, 1, 0), 
        40,                     // vfov
        float(nx) / float(ny),  // aspect
        aperture, 
        dist_to_focus);

    Init();

    while (!glfwWindowShouldClose(window)) {
        display(world, cam);

        ::glfwSwapBuffers(window);
        ::glfwPollEvents();
    }

    for (int i = 0; i < thread_num; i++) {
        g_threads[i].Join();
    }

    ::glfwDestroyWindow(window);
    ::glfwTerminate();

    return 1;
}
예제 #3
0
파일: main.cpp 프로젝트: nakdai/samples
int main(int argc, char* argv[])
{
    ::glfwSetErrorCallback(errorCallback);

    auto result = ::glfwInit();
    if (!result) {
        assert(false);
        return 1;
    }

    ::glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2);
    ::glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);

    auto window = ::glfwCreateWindow(
        WIDTH,
        HEIGHT,
        TITLE,
        NULL, NULL);

    if (!window) {
        ::glfwTerminate();
        assert(false);
        return 1;
    }

#if 0
    ::glfwSetWindowCloseCallback(
        window,
        closeCallback);

    ::glfwSetKeyCallback(
        window,
        keyCallback);

    ::glfwSetMouseButtonCallback(
        window,
        mouseCallback);

    ::glfwSetCursorPosCallback(
        window,
        motionCallback);

    ::glfwSetScrollCallback(
        window,
        wheelCallback);
#endif

    glfwMakeContextCurrent(window);
    glfwSwapInterval(1);

    SetCurrentDirectoryFromExe();

    // TODO
    Rand::Init(0);

#ifdef ENABLE_RANDOM_SCENE
    HitableList* world = new HitableList();
    random_scene(world);

    vec3 lookfrom(13, 2, 3);
    vec3 lookat(0, 0, 0);
#else
    hitable *list[5];
    float R = cos(_MATH_PI / 4);
    list[0] = new sphere(vec3(0, 0, -1), 0.5, new lambertian(vec3(0.1, 0.2, 0.5)));
    list[1] = new sphere(vec3(0, -100.5, -1), 100, new lambertian(vec3(0.8, 0.8, 0.0)));
    list[2] = new sphere(vec3(1, 0, -1), 0.5, new metal(vec3(0.8, 0.6, 0.2), 1.0));
    list[3] = new sphere(vec3(-1, 0, -1), 0.5, new dielectric(1.5));
    //list[4] = new sphere(vec3(-1, 0, -1), -0.45, new dielectric(1.5));
    HitableList* world = new HitableList();

    world->add(list[0]);
    world->add(list[1]);
    world->add(list[2]);
    world->add(list[3]);

    vec3 lookfrom(0, 0, 1);
    vec3 lookat(0, 0, -1);
#endif

    float dist_to_focus = 10.0;
    float aperture = 0.1;

    camera cam(
        lookfrom, 
        lookat, 
        vec3(0, 1, 0), 
        60,                     // vfov
        float(nx) / float(ny),  // aspect
        aperture, 
        dist_to_focus);

    Init();

    while (!glfwWindowShouldClose(window)) {
        display(world, cam);

        ::glfwSwapBuffers(window);
        ::glfwPollEvents();
    }

    ::glfwDestroyWindow(window);
    ::glfwTerminate();

    return 1;
}