ccdemos/spheres_shadow.cc
author Radek Brich <radek.brich@devl.cz>
Sat, 08 Dec 2007 12:37:45 +0100
branchpyrit
changeset 28 ffe83ca074f3
parent 22 76b7bd51d64a
child 32 8af5c17d368b
permissions -rw-r--r--
smooth triangles (aka Phong shading) extend Python binding to support vertex normals and smooth triangles make bunny.py and realtime_dragon smooth, and fix other demos for new triangle constructor add Vector::operator/=

#include "raytracer.h"
#include "image.h"

int main()
{
	Raytracer rt;
	rt.setThreads(2);

	KdTree top;
	rt.setTop(&top);

	Light light1(Vector3(0.0, 5.0, -5.0), Colour(0.7, 0.3, 0.6));
	rt.addlight(&light1);

	Light light2(Vector3(-2.0, 10.0, -2.0), Colour(0.4, 0.6, 0.3));
	rt.addlight(&light2);

	Material mat0(Colour(0.7, 0.7, 0.7));

	Box box(Vector3(-20.0, -1.2, -20.0), Vector3(20.0, -1.0, 20.0), &mat0);
	rt.addshape(&box);

	Material mat1(Colour(1.0, 0.0, 0.0));
	Sphere bigsphere(Vector3(3.0, 2.0, -7.0), 3.0, &mat1);
	rt.addshape(&bigsphere);

	Material mat2(Colour(0.0, 1.0, 0.0));
	Sphere smallsphere(Vector3(-5.5, 1.5, -8.0), 2.0, &mat2);
	rt.addshape(&smallsphere);

	Material mat3(Colour(0.0, 0.0, 1.0));
	Sphere tinysphere(Vector3(-1.2, 0.0, -2.0), 0.5, &mat3);
	rt.addshape(&tinysphere);

	top.optimize();

	Camera cam;
	cam.setEye(Vector3(0,0,15));
	rt.setCamera(&cam);

	int w = 800;
	int h = 600;
	Float *fdata = (Float *) malloc(w*h*3*sizeof(Float));
	rt.render(w, h, fdata);

	struct image *img;
	new_image(&img, w, h, 3);

	Float *fd = fdata;
	for (char *cd = img->data; cd != img->data + w*h*3; cd++, fd++) {
		if (*fd > 1.0)
			*cd = 255;
		else
			*cd = (unsigned char)(*fd * 255.0);
	}
	free(fdata);
	save_png("spheres_shadow.png", img);
	destroy_image(&img);
}