-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathshadow_volume.cpp
More file actions
286 lines (248 loc) · 7.94 KB
/
Copy pathshadow_volume.cpp
File metadata and controls
286 lines (248 loc) · 7.94 KB
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
#include "shadow_volume.hpp"
#include "math/float_types.hpp"
#include "assert.h"
#include "printf/printf.h"
static inline void face_indicators(const vec3 light,
const vec3 * position,
const vec3 * polygon_normal,
const mesh * mesh,
float * indicators)
{
for (int i = 0; i < mesh->polygons_length; i++) {
vec3 n = polygon_normal[i];
vec3 p = position[mesh->polygons[i].a];
float indicator = dot(n, (light - p));
indicators[i] = indicator;
}
}
static inline int object_silhouette(const float * indicators,
const mesh * mesh,
int * edge_indices)
{
int ix = 0;
for (int i = 0; i < mesh->edge_polygons_length; i++) {
const edge_polygon * ep = &mesh->edge_polygons[i];
if ((indicators[ep->polygon_index.a] > 0) != (indicators[ep->polygon_index.b] > 0)) {
edge_indices[ix] = i;
ix += 1;
}
}
return ix;
}
struct graph {
int a;
int b;
};
void graph_append(graph * g, int v)
{
if (!(g->a == -1 || g->b == -1)) {
return;
}
if (g->a == -1) {
g->a = v;
} else {
g->b = v;
}
}
void edge_loop_graph(const mesh * mesh,
const int * edge_indices,
const int edge_indices_length,
graph * graph)
{
for (int i = 0; i < mesh->position_length; i++) {
graph[i].a = -1;
graph[i].b = -1;
}
for (int i = 0; i < edge_indices_length; i++) {
int edge_index = edge_indices[i];
const edge& edge = mesh->edge_polygons[edge_index].edge;
graph_append(&graph[edge.a], i);
graph_append(&graph[edge.b], i);
}
}
int graph_next_neighbor(const graph& g, int ix)
{
if (g.a == ix)
return g.b;
else
return g.a;
}
int edge_loop_inner(const mesh * mesh,
const int * edge_indices,
const graph * graph,
bool * visited_edge_indices,
int ix,
int * edge_loop)
{
int edge_loop_ix = 0;
const edge& e = mesh->edge_polygons[edge_indices[ix]].edge;
edge_loop[edge_loop_ix] = e.b;
edge_loop_ix += 1;
while (true) {
visited_edge_indices[ix] = true;
const edge& e = mesh->edge_polygons[edge_indices[ix]].edge;
int next_ix_a = graph_next_neighbor(graph[e.a], ix);
int next_ix_b = graph_next_neighbor(graph[e.b], ix);
if (visited_edge_indices[next_ix_a] == false) {
edge_loop[edge_loop_ix] = e.a;
edge_loop_ix += 1;
ix = next_ix_a;
} else if (visited_edge_indices[next_ix_b] == false) {
edge_loop[edge_loop_ix] = e.b;
edge_loop_ix += 1;
ix = next_ix_b;
} else {
break;
}
}
return edge_loop_ix;
}
int next_unvisited(const bool * visited_edge_indices,
const int edge_indices_length)
{
for (int i = 0; i < edge_indices_length; i++) {
if (visited_edge_indices[i] == false)
return i;
}
return -1;
}
int edge_loop(const mesh * mesh,
const int * edge_indices,
const int edge_indices_length,
const graph * graph,
int * edge_loops,
int * edge_loop_lengths,
int max_edge_loops)
{
bool visited_edge_indices[edge_indices_length];
for (int i = 0; i < edge_indices_length; i++) {
visited_edge_indices[i] = false;
}
int edge_loop_ix = 0;
int i;
for (i = 0; i < max_edge_loops; i++) {
int start = next_unvisited(visited_edge_indices, edge_indices_length);
if (start == -1)
break;
int length = edge_loop_inner(mesh, edge_indices, graph, visited_edge_indices, start,
&edge_loops[edge_loop_ix]);
edge_loop_lengths[i] = length;
edge_loop_ix += length;
}
return i;
}
static inline vec3 cast_ray(const vec3 light,
const vec3 start)
{
vec3 ray = start - light;
return start + (normalize(ray) * 7.f);
}
void shadow_volume_mesh_rays(const vec3 * position,
const vec3 * cast_position,
const int * edge_loop,
const int edge_loop_length,
bool last_edge_loop,
void(*render_quad)(vec3 a, vec3 b, vec3 c, vec3 d, bool l))
{
for (int i = 0; i < edge_loop_length; i++) {
int j = i + 1;
if (j >= edge_loop_length) j = 0;
int i1 = edge_loop[i];
int i2 = edge_loop[j];
vec3 a = position[i1];
vec3 b = position[i2];
vec3 c = cast_position[i2];
vec3 d = cast_position[i1];
bool last_in_volume = last_edge_loop && (i == (edge_loop_length - 1));
render_quad(a, b, c, d, last_in_volume);
}
}
void shadow_volume_end_caps(const vec3 * position,
const vec3 * cast_position,
const mesh * mesh,
const float * indicators,
void(*render_quad)(vec3 a, vec3 b, vec3 c, vec3 d, bool l))
{
for (int i = 0; i < mesh->polygons_length; i++) {
const polygon * p = &mesh->polygons[i];
if (indicators[i] > 0) {
vec3 a = position[p->a];
vec3 b = position[p->b];
vec3 c = position[p->c];
vec3 d = position[p->d];
render_quad(a, b, c, d, false);
} else {
vec3 a = cast_position[p->a];
vec3 b = cast_position[p->b];
vec3 c = cast_position[p->c];
vec3 d = cast_position[p->d];
render_quad(a, b, c, d, false);
}
}
}
void shadow_volume_mesh(const vec3 light,
const vec3 * position,
const vec3 * polygon_normal,
const mesh * mesh,
void(*render_quad)(vec3 a, vec3 b, vec3 c, vec3 d, bool l))
{
// light in world space
float indicators[mesh->polygon_normal_length];
face_indicators(light, position, polygon_normal, mesh, indicators);
// edge_indicies: mesh->edge_polygons indices
int edge_indices[mesh->edge_polygons_length];
int edge_indices_length = object_silhouette(indicators, mesh, edge_indices);
// graph contains indexes to edge_indices (not mesh edge indices)
graph graph[mesh->position_length];
edge_loop_graph(mesh, edge_indices, edge_indices_length, graph);
const int max_edge_loops = 2;
int edge_loops[edge_indices_length];
int edge_loop_lengths[max_edge_loops];
int edge_loop_count = edge_loop(mesh,
edge_indices,
edge_indices_length,
graph,
edge_loops,
edge_loop_lengths,
max_edge_loops);
vec3 cast_position[mesh->position_length];
for (int i = 0; i < mesh->position_length; i++) {
cast_position[i] = cast_ray(light, position[i]);
}
shadow_volume_end_caps(position,
cast_position,
mesh,
indicators,
render_quad);
// edge_loops contains position indices
int edge_loop_ix = 0;
for (int i = 0; i < edge_loop_count; i++) {
int edge_loop_length = edge_loop_lengths[i];
int * edge_loop = &edge_loops[edge_loop_ix];
bool last = i == (edge_loop_count - 1);
shadow_volume_mesh_rays(position,
cast_position,
edge_loop,
edge_loop_length,
last,
render_quad);
edge_loop_ix += edge_loop_length;
}
if (0) {
int edge_loop_ix = 0;
for (int i = 0; i < edge_loop_count; i++) {
int length = edge_loop_lengths[i];
printf("loop %d: %d\n", i, length);
for (int j = 0; j < length; j++) {
printf(" %d", edge_loops[j + edge_loop_ix]);
}
printf("\n\n");
edge_loop_ix += length;
}
}
/*
for (int i = 0; i < silhouette_length; i++) {
out_edges[edges[i]] = 1;
}
*/
}