summaryrefslogtreecommitdiff
path: root/src/mesh.h
blob: e0170ffe5b5f7b6001c0b66bdda5e6f25604d182 (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
161
162
163
164
165
166
167
168
169
170
#ifndef __MESH_H__
#define __MESH_H__

#include "intersect.h"

#define STACK_SIZE 500

/* flattened triangle mesh */
__device__ float3 *g_vertices;
__device__ uint4 *g_triangles;
__device__ unsigned int *g_colors;
__device__ unsigned int g_start_node;
__device__ unsigned int g_first_node;

/* lower/upper bounds for the bounding box associated with each node/leaf */
texture<float4, 1, cudaReadModeElementType> upper_bounds;
texture<float4, 1, cudaReadModeElementType> lower_bounds;

/* map to child nodes/triangles and the number of child nodes/triangles */
texture<unsigned int, 1, cudaReadModeElementType> node_map;
texture<unsigned int, 1, cudaReadModeElementType> node_length;

__device__ float3 make_float3(const float4 &a)
{
	return make_float3(a.x, a.y, a.z);
}

__device__ int convert(int c)
{
	if (c & 0x80)
		return (0xFFFFFF00 | c);
	else
		return c;
}

/* Test the intersection between a ray starting at `origin` traveling in the
   direction `direction` and the bounding box around node `i`. If the ray
   intersects the bounding box return true, else return false. */
__device__ bool intersect_node(const float3 &origin, const float3 &direction, const int &i)
{
	float3 lower_bound = make_float3(tex1Dfetch(lower_bounds, i));
	float3 upper_bound = make_float3(tex1Dfetch(upper_bounds, i));

	return intersect_box(origin, direction, lower_bound, upper_bound);
}

/* Find the intersection between a ray starting at `origin` traveling in the
   direction `direction` and the global mesh texture. If the ray intersects
   the texture return the index of the triangle which the ray intersected,
   else return -1. */
__device__ __noinline__ int intersect_mesh(const float3 &origin, const float3& direction, float &min_distance, int last_hit_triangle = -1)
{
	int triangle_index = -1;

	float distance;

	if (!intersect_node(origin, direction, g_start_node))
		return -1;

	int stack[STACK_SIZE];

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

	int i;

	do
	{
		int first_child = tex1Dfetch(node_map, *node);
		int child_count = tex1Dfetch(node_length, *node);

		while (*node >= g_first_node && child_count == 1)
		{
			*node = first_child;
			first_child = tex1Dfetch(node_map, *node);
			child_count = tex1Dfetch(node_length, *node);
		}
		
		if (*node >= g_first_node)
		{
			for (i=0; i < child_count; i++)
			{
				if (intersect_node(origin, direction, first_child+i))
				{
					*node = first_child+i;
					node++;
				}
			}

			node--;
		}
		else // node is a leaf
		{
			for (i=0; i < child_count; i++)
			{
				if (last_hit_triangle == first_child+i)
					continue;

				uint4 triangle_data = g_triangles[first_child+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))
				{
					if (triangle_index == -1)
					{
						triangle_index = first_child + i;
						min_distance = distance;
						continue;
					}

					if (distance < min_distance)
					{
						triangle_index = first_child + i;
						min_distance = distance;
					}
				}
			} // triangle loop

			node--;

		} // node is a leaf

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

	return triangle_index;
}

extern "C"
{

__global__ void set_global_mesh_variables(uint4 *triangles, float3 *vertices, unsigned int *colors, unsigned int start_node, unsigned int first_node)
{
	g_triangles = triangles;
	g_vertices = vertices;
	g_colors = colors;
	g_start_node = start_node;
	g_first_node = first_node;
}

__global__ void set_colors(unsigned int *colors)
{
	g_colors = colors;
}

__global__ void color_solids(int nthreads, unsigned int ntriangles, int *solid_id_map, int *solid_ids, unsigned int *solid_colors)
{
	int id = blockIdx.x*blockDim.x + threadIdx.x;

	if (id >= nthreads)
		return;

	int solid_id = solid_ids[id];
	unsigned int color = solid_colors[id];

	for (int i=0; i < ntriangles; i++)
	{
		if (solid_id_map[i] == solid_id)
			g_colors[i] = color;
	}
}

} // extern "c"

#endif