ccdemos/spheres_shadow.cc
author Radek Brich <radek.brich@devl.cz>
Tue, 04 Dec 2007 08:58:05 +0100
branchpyrit
changeset 23 7e258561a690
parent 22 76b7bd51d64a
child 32 8af5c17d368b
permissions -rw-r--r--
fix a bug in KdNode::subdivide and remove some extra variables realtime.cc - use SW surface
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
15
a0a3e334744f C++ demos: prepare infrastructure, add spheres_shadow.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     1
#include "raytracer.h"
a0a3e334744f C++ demos: prepare infrastructure, add spheres_shadow.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     2
#include "image.h"
a0a3e334744f C++ demos: prepare infrastructure, add spheres_shadow.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     3
a0a3e334744f C++ demos: prepare infrastructure, add spheres_shadow.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     4
int main()
a0a3e334744f C++ demos: prepare infrastructure, add spheres_shadow.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     5
{
a0a3e334744f C++ demos: prepare infrastructure, add spheres_shadow.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     6
	Raytracer rt;
22
76b7bd51d64a new make infrastructure
Radek Brich <radek.brich@devl.cz>
parents: 20
diff changeset
     7
	rt.setThreads(2);
76b7bd51d64a new make infrastructure
Radek Brich <radek.brich@devl.cz>
parents: 20
diff changeset
     8
76b7bd51d64a new make infrastructure
Radek Brich <radek.brich@devl.cz>
parents: 20
diff changeset
     9
	KdTree top;
76b7bd51d64a new make infrastructure
Radek Brich <radek.brich@devl.cz>
parents: 20
diff changeset
    10
	rt.setTop(&top);
16
20bceb605f48 add Raytracer::setThreads()
Radek Brich <radek.brich@devl.cz>
parents: 15
diff changeset
    11
19
4e0955fca797 added Camera, currently w/o Python binding
Radek Brich <radek.brich@devl.cz>
parents: 16
diff changeset
    12
	Light light1(Vector3(0.0, 5.0, -5.0), Colour(0.7, 0.3, 0.6));
15
a0a3e334744f C++ demos: prepare infrastructure, add spheres_shadow.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    13
	rt.addlight(&light1);
a0a3e334744f C++ demos: prepare infrastructure, add spheres_shadow.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    14
19
4e0955fca797 added Camera, currently w/o Python binding
Radek Brich <radek.brich@devl.cz>
parents: 16
diff changeset
    15
	Light light2(Vector3(-2.0, 10.0, -2.0), Colour(0.4, 0.6, 0.3));
15
a0a3e334744f C++ demos: prepare infrastructure, add spheres_shadow.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    16
	rt.addlight(&light2);
a0a3e334744f C++ demos: prepare infrastructure, add spheres_shadow.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    17
a0a3e334744f C++ demos: prepare infrastructure, add spheres_shadow.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    18
	Material mat0(Colour(0.7, 0.7, 0.7));
a0a3e334744f C++ demos: prepare infrastructure, add spheres_shadow.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    19
a0a3e334744f C++ demos: prepare infrastructure, add spheres_shadow.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    20
	Box box(Vector3(-20.0, -1.2, -20.0), Vector3(20.0, -1.0, 20.0), &mat0);
a0a3e334744f C++ demos: prepare infrastructure, add spheres_shadow.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    21
	rt.addshape(&box);
a0a3e334744f C++ demos: prepare infrastructure, add spheres_shadow.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    22
a0a3e334744f C++ demos: prepare infrastructure, add spheres_shadow.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    23
	Material mat1(Colour(1.0, 0.0, 0.0));
19
4e0955fca797 added Camera, currently w/o Python binding
Radek Brich <radek.brich@devl.cz>
parents: 16
diff changeset
    24
	Sphere bigsphere(Vector3(3.0, 2.0, -7.0), 3.0, &mat1);
15
a0a3e334744f C++ demos: prepare infrastructure, add spheres_shadow.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    25
	rt.addshape(&bigsphere);
a0a3e334744f C++ demos: prepare infrastructure, add spheres_shadow.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    26
a0a3e334744f C++ demos: prepare infrastructure, add spheres_shadow.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    27
	Material mat2(Colour(0.0, 1.0, 0.0));
19
4e0955fca797 added Camera, currently w/o Python binding
Radek Brich <radek.brich@devl.cz>
parents: 16
diff changeset
    28
	Sphere smallsphere(Vector3(-5.5, 1.5, -8.0), 2.0, &mat2);
15
a0a3e334744f C++ demos: prepare infrastructure, add spheres_shadow.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    29
	rt.addshape(&smallsphere);
a0a3e334744f C++ demos: prepare infrastructure, add spheres_shadow.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    30
a0a3e334744f C++ demos: prepare infrastructure, add spheres_shadow.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    31
	Material mat3(Colour(0.0, 0.0, 1.0));
19
4e0955fca797 added Camera, currently w/o Python binding
Radek Brich <radek.brich@devl.cz>
parents: 16
diff changeset
    32
	Sphere tinysphere(Vector3(-1.2, 0.0, -2.0), 0.5, &mat3);
15
a0a3e334744f C++ demos: prepare infrastructure, add spheres_shadow.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    33
	rt.addshape(&tinysphere);
a0a3e334744f C++ demos: prepare infrastructure, add spheres_shadow.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    34
22
76b7bd51d64a new make infrastructure
Radek Brich <radek.brich@devl.cz>
parents: 20
diff changeset
    35
	top.optimize();
76b7bd51d64a new make infrastructure
Radek Brich <radek.brich@devl.cz>
parents: 20
diff changeset
    36
19
4e0955fca797 added Camera, currently w/o Python binding
Radek Brich <radek.brich@devl.cz>
parents: 16
diff changeset
    37
	Camera cam;
4e0955fca797 added Camera, currently w/o Python binding
Radek Brich <radek.brich@devl.cz>
parents: 16
diff changeset
    38
	cam.setEye(Vector3(0,0,15));
4e0955fca797 added Camera, currently w/o Python binding
Radek Brich <radek.brich@devl.cz>
parents: 16
diff changeset
    39
	rt.setCamera(&cam);
4e0955fca797 added Camera, currently w/o Python binding
Radek Brich <radek.brich@devl.cz>
parents: 16
diff changeset
    40
15
a0a3e334744f C++ demos: prepare infrastructure, add spheres_shadow.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    41
	int w = 800;
a0a3e334744f C++ demos: prepare infrastructure, add spheres_shadow.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    42
	int h = 600;
22
76b7bd51d64a new make infrastructure
Radek Brich <radek.brich@devl.cz>
parents: 20
diff changeset
    43
	Float *fdata = (Float *) malloc(w*h*3*sizeof(Float));
20
f22952603f29 new C++ demo: realtime.cc (real-time scene viewer using SDL)
Radek Brich <radek.brich@devl.cz>
parents: 19
diff changeset
    44
	rt.render(w, h, fdata);
15
a0a3e334744f C++ demos: prepare infrastructure, add spheres_shadow.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    45
a0a3e334744f C++ demos: prepare infrastructure, add spheres_shadow.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    46
	struct image *img;
a0a3e334744f C++ demos: prepare infrastructure, add spheres_shadow.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    47
	new_image(&img, w, h, 3);
a0a3e334744f C++ demos: prepare infrastructure, add spheres_shadow.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    48
22
76b7bd51d64a new make infrastructure
Radek Brich <radek.brich@devl.cz>
parents: 20
diff changeset
    49
	Float *fd = fdata;
15
a0a3e334744f C++ demos: prepare infrastructure, add spheres_shadow.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    50
	for (char *cd = img->data; cd != img->data + w*h*3; cd++, fd++) {
a0a3e334744f C++ demos: prepare infrastructure, add spheres_shadow.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    51
		if (*fd > 1.0)
a0a3e334744f C++ demos: prepare infrastructure, add spheres_shadow.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    52
			*cd = 255;
a0a3e334744f C++ demos: prepare infrastructure, add spheres_shadow.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    53
		else
a0a3e334744f C++ demos: prepare infrastructure, add spheres_shadow.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    54
			*cd = (unsigned char)(*fd * 255.0);
a0a3e334744f C++ demos: prepare infrastructure, add spheres_shadow.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    55
	}
a0a3e334744f C++ demos: prepare infrastructure, add spheres_shadow.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    56
	free(fdata);
a0a3e334744f C++ demos: prepare infrastructure, add spheres_shadow.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    57
	save_png("spheres_shadow.png", img);
a0a3e334744f C++ demos: prepare infrastructure, add spheres_shadow.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    58
	destroy_image(&img);
a0a3e334744f C++ demos: prepare infrastructure, add spheres_shadow.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    59
}