aboutsummaryrefslogtreecommitdiff
path: root/scene.c
blob: fd3881cdc2ba7680336ceb800af515693e928d0e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include "scene.h"

#include <stdlib.h>

static inline void link_object(space_t *s, object_t *o)
{
	if (s) {
		o->next = s->objects;
		s->objects = o;
	} else {
		o->next = 0;
	}
}

object_t *add_sphere(space_t *s, vector_t *c, COORD_T r, material_t *m)
{
	object_t *o = (object_t *) malloc(sizeof(object_t));

	o->type = TYPE_SPHERE;
	o->m = m;

	o->sph.center = c;
	o->sph.radius = r;

	link_object(s, o);

	return o;
}

object_t *add_plane(space_t *s, vector_t *start, vector_t *dir, material_t *m)
{
	object_t *o = (object_t *) malloc(sizeof(object_t));

	o->type = TYPE_PLANE;
	o->m = m;

	o->pl.start = start;
	o->pl.norm = dir;

	link_object(s, o);

	return o;
}

light_t *add_light(space_t *s, vector_t *pos, color_t *defuse, color_t *specular)
{
	light_t *o = (light_t *) malloc(sizeof(light_t));

	o->pos = pos;
	o->defuse = defuse;
	o->specular = specular;

	if (s) {
		o->next = s->lights;
		s->lights = o;
	} else {
		o->next = NULL;
	}

	return o;
}

void material_set_phong(material_t *m, COORD_T defuse, COORD_T specular, unsigned int shine) {
	m->light_type = LIGHT_PHONG;
	m->phong.defuse = defuse;
	m->phong.specular = specular;
	m->phong.shine = shine;
}

void obj_norm_at(object_t *o, vector_t *dest, vector_t *point)
{
	switch(o->type) {
		case TYPE_SPHERE:
			vector_sub(dest, point, o->sph.center);
			vector_scale_inv(dest, dest, vector_len(dest));
			break;
		case TYPE_PLANE:
			vector_copy(dest, o->pl.norm);
			break;
	}

	return;
}