summaryrefslogtreecommitdiff
path: root/src/alpha.h
blob: e4a3a40ef60eaed175d5c29ad19ecf54f4d12991 (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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#ifndef __ALPHA_H__
#define __ALPHA_H__

#include "linalg.h"
#include "intersect.h"
#include "mesh.h"

template <class T>
__device__ void swap(T &a, T &b)
{
	T temp = a;
	a = b;
	b = temp;
}

#define ALPHA_DEPTH 10

struct HitList
{
	unsigned int size;
	unsigned int indices[ALPHA_DEPTH];
	float distances[ALPHA_DEPTH];
};

__device__ void add_to_hit_list(const float &distance, const int &index, HitList &h)
{
	unsigned int i;
	if (h.size >= ALPHA_DEPTH)
	{
		if (distance > h.distances[ALPHA_DEPTH-1])
			return;

		i = ALPHA_DEPTH-1;
	}
	else
	{
		i = h.size;
		h.size += 1;
	}

	h.indices[i] = index;
	h.distances[i] = distance;

	while (i > 0 && h.distances[i-1] > h.distances[i])
	{
		swap(h.distances[i-1], h.distances[i]);
		swap(h.indices[i-1], h.indices[i]);

		i -= 1;
	}
}

__device__ int get_color_alpha(const float3 &origin, const float3& direction)
{
	HitList h;
	h.size = 0;

	float distance;

	if (!intersect_node(origin, direction, g_start_node, -1.0f))
		return 0;

	unsigned int stack[STACK_SIZE];

	unsigned int *head = &stack[0];
	unsigned int *node = &stack[1];
	unsigned int *tail = &stack[STACK_SIZE-1];
	*node = g_start_node;

	unsigned int i;

	do
	{
		unsigned int first_child = tex1Dfetch(node_map, *node);
		unsigned int stop = tex1Dfetch(node_map_end, *node);

		while (*node >= g_first_node && stop == first_child+1)
		{
			*node = first_child;
			first_child = tex1Dfetch(node_map, *node);
			stop = tex1Dfetch(node_map_end, *node);
		}
		
		if (*node >= g_first_node)
		{
			for (i=first_child; i < stop; i++)
			{
				if (intersect_node(origin, direction, i, -1.0f))
				{
					*node = i;
					node++;
				}
			}

			node--;
		}
		else // node is a leaf
		{
			for (i=first_child; i < stop; i++)
			{
				uint4 triangle_data = g_triangles[i];

				float3 v0 = g_vertices[triangle_data.x];
				float3 v1 = g_vertices[triangle_data.y];
				float3 v2 = g_vertices[triangle_data.z];

				if (intersect_triangle(origin, direction, v0, v1, v2, distance))
				{
					add_to_hit_list(distance, i, h);
				}

			} // triangle loop

			node--;

		} // node is a leaf

	} // while loop
	while (node != head);

	if (h.size < 1)
		return 0;

	float scale = 1.0f;
	float fr = 0.0f;
	float fg = 0.0f;
	float fb = 0.0f;
	for (i=0; i < h.size; i++)
	{
		uint4 triangle_data = g_triangles[h.indices[i]];

		float3 v0 = g_vertices[triangle_data.x];
		float3 v1 = g_vertices[triangle_data.y];
		float3 v2 = g_vertices[triangle_data.z];

		float cos_theta = dot(normalize(cross(v1-v0,v2-v1)),-direction);

		if (cos_theta < 0.0f)
			cos_theta = dot(-normalize(cross(v1-v0,v2-v1)),-direction);

		unsigned int r0 = 0xff & (g_colors[h.indices[i]] >> 16);
		unsigned int g0 = 0xff & (g_colors[h.indices[i]] >> 8);
		unsigned int b0 = 0xff & g_colors[h.indices[i]];

		float alpha = (255 - (0xff & (g_colors[h.indices[i]] >> 24)))/255.0f;

		fr += r0*scale*cos_theta*alpha;
		fg += g0*scale*cos_theta*alpha;
		fb += b0*scale*cos_theta*alpha;

		scale *= (1.0f-alpha);
	}
	unsigned int r = floorf(fr);
	unsigned int g = floorf(fg);
	unsigned int b = floorf(fb);

	return r << 16 | g << 8 | b;
}

#endif