-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmesh.c
166 lines (137 loc) · 3.12 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
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
#include <stdlib.h>
#include <string.h>
#include "buf.h"
#include "mathx.h"
#include "mesh.h"
struct idx {
int vi, ni;
};
struct mesh {
float *vbuf;
float *nbuf;
struct idx *ibuf;
int *faces;
};
struct mesh *mesh_create(void)
{
struct mesh *mesh = malloc(sizeof(*mesh));
mesh->vbuf = NULL;
mesh->nbuf = NULL;
mesh->ibuf = NULL;
mesh->faces = NULL;
return mesh;
}
void mesh_free(struct mesh *mesh)
{
if (!mesh)
return;
buf_free(mesh->vbuf);
buf_free(mesh->nbuf);
buf_free(mesh->ibuf);
buf_free(mesh->faces);
free(mesh);
}
void mesh_add_vertex(struct mesh *mesh, const float *v)
{
buf_push(mesh->vbuf, v[0]);
buf_push(mesh->vbuf, v[1]);
buf_push(mesh->vbuf, v[2]);
}
void mesh_add_normal(struct mesh *mesh, const float *n)
{
buf_push(mesh->nbuf, n[0]);
buf_push(mesh->nbuf, n[1]);
buf_push(mesh->nbuf, n[2]);
}
void mesh_begin_face(struct mesh *mesh)
{
buf_push(mesh->faces, buf_len(mesh->ibuf));
}
void mesh_add_index(struct mesh *mesh, int vi, int ni)
{
struct idx idx;
idx.vi = vi;
idx.ni = ni;
buf_push(mesh->ibuf, idx);
}
void mesh_end_face(struct mesh *mesh)
{
/* noop */
}
int mesh_vertex_buffer(const struct mesh *mesh, const float **buf)
{
if (buf)
*buf = mesh->vbuf;
return buf_len(mesh->vbuf) / 3;
}
int mesh_normal_buffer(const struct mesh *mesh, const float **buf)
{
if (buf)
*buf = mesh->nbuf;
return buf_len(mesh->nbuf) / 3;
}
int mesh_face_count(const struct mesh *mesh)
{
return buf_len(mesh->faces);
}
int mesh_face_vertex_count(const struct mesh *mesh, int face)
{
int beg, end;
beg = mesh->faces[face];
end = face != buf_len(mesh->faces) - 1 ?
mesh->faces[face + 1] : buf_len(mesh->ibuf);
return end - beg;
}
void mesh_face_vertex_index(const struct mesh *mesh, int face, int vert,
int *vertex_idx, int *normal_idx)
{
int beg;
struct idx *idx;
beg = mesh->faces[face];
idx = &mesh->ibuf[beg + vert];
*vertex_idx = idx->vi;
*normal_idx = idx->ni;
}
float *mesh_get_vertex(const struct mesh *mesh, int face, int vert)
{
int vi, ni;
mesh_face_vertex_index(mesh, face, vert, &vi, &ni);
return &mesh->vbuf[vi * 3];
}
float *mesh_get_normal(const struct mesh *mesh, int face, int vert)
{
int vi, ni;
mesh_face_vertex_index(mesh, face, vert, &vi, &ni);
return ni != -1 ? &mesh->nbuf[ni * 3] : NULL;
}
void mesh_compute_normals(struct mesh *mesh)
{
int i, nr_faces;
struct idx *idx;
buf_resize(mesh->nbuf, buf_len(mesh->vbuf));
memset(mesh->nbuf, 0, buf_len(mesh->nbuf) * sizeof(*mesh->nbuf));
buf_foreach(idx, mesh->ibuf)
idx->ni = idx->vi;
nr_faces = mesh_face_count(mesh);
for (i = 0; i < nr_faces; i++) {
int j, nr_verts;
nr_verts = mesh_face_vertex_count(mesh, i);
for (j = 0; j < nr_verts; j++) {
vector u, v, n;
float *v0, *v1, *v2, *vn;
v0 = mesh_get_vertex(mesh, i, j);
v1 = mesh_get_vertex(mesh, i, (j + 1) % nr_verts);
v2 = mesh_get_vertex(mesh, i, (j + nr_verts - 1) % nr_verts);
vec_sub(u, v1, v0);
vec_sub(v, v2, v0);
vec_cross(n, u, v);
vec_normalize(n, n);
vn = mesh_get_normal(mesh, i, j);
vec_add(vn, vn, n);
}
}
for (i = 0; i < buf_len(mesh->nbuf); i += 3) {
float *n = mesh->nbuf + i;
vec_normalize(n, n);
}
}