summaryrefslogtreecommitdiff
path: root/shadeop.c
blob: 5d669ef6fce560068565de0296f8057949f85aa6 (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
#include <u.h>
#include <libc.h>
#include <thread.h>
#include <draw.h>
#include <memdraw.h>
#include <geometry.h>
#include "graphics.h"
#include "internal.h"

double
sign(double n)
{
	return n == 0? 0: n < 0? -1: 1;
}

double
step(double edge, double n)
{
	if(n < edge)
		return 0;
	return 1;
}

double
smoothstep(double edge0, double edge1, double n)
{
	double t;

	t = fclamp((n-edge0)/(edge1-edge0), 0, 1);
	return t*t * (3 - 2*t);
}

/* TODO apply attenuation for punctual lights */
Color
getlightcolor(LightSource *l, Point3 dir)
{
	double cθs, cθu, cθp, t;

	/* see “Spotlights”, Real-Time Rendering 4th ed. § 5.2.2 */
	if(l->type == LightSpot){
		cθs = dotvec3(mulpt3(dir, -1), l->dir);
		cθu = cos(l->θu);
		cθp = cos(l->θp);
//		return mulpt3(l->c, smoothstep(cθu, cθp, cθs));
		t = fclamp((cθs - cθu)/(cθp - cθu), 0, 1);
		return mulpt3(l->c, t*t);
	}
	return l->c;
}