forked from smcameron/space-nerds-in-space
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mesh.c
97 lines (78 loc) · 2.14 KB
/
mesh.c
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
#include <stdlib.h>
#include <string.h>
#include "vertex.h"
#include "triangle.h"
#include "mathutils.h"
#include "matrix.h"
#define DEFINE_MESH_GLOBALS 1
#include "mesh.h"
#undef DEFINE_MESH_GLOBALS
float mesh_compute_radius(struct mesh *m)
{
int i;
float r, max_radius = 0.0;
for (i = 0; i < m->nvertices; i++) {
r = dist3d(m->v[i].x, m->v[i].y, m->v[i].z);
if (r > max_radius)
max_radius = r;
}
return max_radius;
}
void mesh_distort(struct mesh *m, float distortion)
{
int i;
for (i = 0; i < m->nvertices; i++) {
float dx, dy, dz;
dx = (float) snis_randn(1000) / 1000.0 * distortion - 0.5;
dy = (float) snis_randn(1000) / 1000.0 * distortion - 0.5;
dz = (float) snis_randn(1000) / 1000.0 * distortion - 0.5;
m->v[i].x += m->v[i].x * dx;
m->v[i].y += m->v[i].y * dy;
m->v[i].z += m->v[i].z * dz;
}
m->radius = mesh_compute_radius(m);
}
void mesh_scale(struct mesh *m, float scale)
{
int i;
for (i = 0; i < m->nvertices; i++) {
m->v[i].x += m->v[i].x * scale;
m->v[i].y += m->v[i].y * scale;
m->v[i].z += m->v[i].z * scale;
}
m->radius = mesh_compute_radius(m);
}
static int lookup_vertex(struct mesh *m, struct vertex *v)
{
int i;
for (i = 0; i < m->nvertices; i++)
if (&m->v[i] == v)
return i;
return -1;
}
struct mesh *mesh_duplicate(struct mesh *original)
{
struct mesh *copy;
int i;
copy = malloc(sizeof(*copy));
copy->ntriangles = original->ntriangles;
copy->nvertices = original->nvertices;
copy->t = malloc(sizeof(*copy->t) * copy->ntriangles);
memset(copy->t, 0, sizeof(*copy->t) * copy->ntriangles);
copy->v = malloc(sizeof(*copy->v) * copy->nvertices);
memset(copy->v, 0, sizeof(*copy->v) * copy->nvertices);
for (i = 0; i < original->nvertices; i++)
copy->v[i] = original->v[i];
for (i = 0; i < original->ntriangles; i++) {
int v0, v1, v2;
v0 = lookup_vertex(original, original->t[i].v[0]);
v1 = lookup_vertex(original, original->t[i].v[1]);
v2 = lookup_vertex(original, original->t[i].v[2]);
copy->t[i].v[0] = ©->v[v0];
copy->t[i].v[1] = ©->v[v1];
copy->t[i].v[2] = ©->v[v2];
copy->t[i].n = original->t[i].n;
}
copy->radius = original->radius;
return copy;
}