tavli

view src/meshgen.cc @ 13:f3c5134b4914

generalized the surfaces of revolution evaluator a bit
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 27 Jun 2015 22:33:27 +0300
parents a8e26f163f99
children
line source
1 #include <stdio.h>
2 #include "meshgen.h"
3 #include "mesh.h"
5 // -------- sphere --------
7 #define SURAD(u) ((u) * 2.0 * M_PI)
8 #define SVRAD(v) ((v) * M_PI)
10 static Vector3 sphvec(float theta, float phi)
11 {
12 return Vector3(sin(theta) * sin(phi),
13 cos(phi),
14 cos(theta) * sin(phi));
15 }
17 void gen_sphere(Mesh *mesh, float rad, int usub, int vsub, float urange, float vrange)
18 {
19 if(usub < 4) usub = 4;
20 if(vsub < 2) vsub = 2;
22 int uverts = usub + 1;
23 int vverts = vsub + 1;
25 int num_verts = uverts * vverts;
26 int num_quads = usub * vsub;
27 int num_tri = num_quads * 2;
29 mesh->clear();
30 Vector3 *varr = (Vector3*)mesh->set_attrib_data(MESH_ATTR_VERTEX, 3, num_verts, 0);
31 Vector3 *narr = (Vector3*)mesh->set_attrib_data(MESH_ATTR_NORMAL, 3, num_verts, 0);
32 Vector3 *tarr = (Vector3*)mesh->set_attrib_data(MESH_ATTR_TANGENT, 3, num_verts, 0);
33 Vector2 *uvarr = (Vector2*)mesh->set_attrib_data(MESH_ATTR_TEXCOORD, 2, num_verts, 0);
34 unsigned int *idxarr = mesh->set_index_data(num_tri * 3, 0);
36 float du = urange / (float)(uverts - 1);
37 float dv = vrange / (float)(vverts - 1);
39 float u = 0.0;
40 for(int i=0; i<uverts; i++) {
41 float theta = SURAD(u * urange);
43 float v = 0.0;
44 for(int j=0; j<vverts; j++) {
45 float phi = SVRAD(v * vrange);
47 Vector3 pos = sphvec(theta, phi);
49 *varr++ = pos * rad;
50 *narr++ = pos;
51 *tarr++ = (sphvec(theta + 0.1f, (float)M_PI / 2.0f) - sphvec(theta - 0.1f, (float)M_PI / 2.0f)).normalized();
52 *uvarr++ = Vector2(u * urange, v * vrange);
54 if(i < usub && j < vsub) {
55 int idx = i * vverts + j;
56 *idxarr++ = idx;
57 *idxarr++ = idx + 1;
58 *idxarr++ = idx + vverts + 1;
60 *idxarr++ = idx;
61 *idxarr++ = idx + vverts + 1;
62 *idxarr++ = idx + vverts;
63 }
65 v += dv;
66 }
67 u += du;
68 }
69 }
72 // -------- cylinder --------
74 static Vector3 cylvec(float theta, float height)
75 {
76 return Vector3(sin(theta), height, cos(theta));
77 }
79 void gen_cylinder(Mesh *mesh, float rad, float height, int usub, int vsub, int capsub, float urange, float vrange)
80 {
81 if(usub < 4) usub = 4;
82 if(vsub < 1) vsub = 1;
84 int uverts = usub + 1;
85 int vverts = vsub + 1;
87 int num_body_verts = uverts * vverts;
88 int num_body_quads = usub * vsub;
89 int num_body_tri = num_body_quads * 2;
91 int capvverts = capsub ? capsub + 1 : 0;
92 int num_cap_verts = uverts * capvverts;
93 int num_cap_quads = usub * capsub;
94 int num_cap_tri = num_cap_quads * 2;
96 int num_verts = num_body_verts + num_cap_verts * 2;
97 int num_tri = num_body_tri + num_cap_tri * 2;
99 mesh->clear();
100 Vector3 *varr = (Vector3*)mesh->set_attrib_data(MESH_ATTR_VERTEX, 3, num_verts, 0);
101 Vector3 *narr = (Vector3*)mesh->set_attrib_data(MESH_ATTR_NORMAL, 3, num_verts, 0);
102 Vector3 *tarr = (Vector3*)mesh->set_attrib_data(MESH_ATTR_TANGENT, 3, num_verts, 0);
103 Vector2 *uvarr = (Vector2*)mesh->set_attrib_data(MESH_ATTR_TEXCOORD, 2, num_verts, 0);
104 unsigned int *idxarr = mesh->set_index_data(num_tri * 3, 0);
106 float du = urange / (float)(uverts - 1);
107 float dv = vrange / (float)(vverts - 1);
109 float u = 0.0;
110 for(int i=0; i<uverts; i++) {
111 float theta = SURAD(u);
113 float v = 0.0;
114 for(int j=0; j<vverts; j++) {
115 float y = (v - 0.5) * height;
116 Vector3 pos = cylvec(theta, y);
118 *varr++ = Vector3(pos.x * rad, pos.y, pos.z * rad);
119 *narr++ = Vector3(pos.x, 0.0, pos.z);
120 *tarr++ = (cylvec(theta + 0.1, 0.0) - cylvec(theta - 0.1, 0.0)).normalized();
121 *uvarr++ = Vector2(u * urange, v * vrange);
123 if(i < usub && j < vsub) {
124 int idx = i * vverts + j;
126 *idxarr++ = idx;
127 *idxarr++ = idx + vverts + 1;
128 *idxarr++ = idx + 1;
130 *idxarr++ = idx;
131 *idxarr++ = idx + vverts;
132 *idxarr++ = idx + vverts + 1;
133 }
135 v += dv;
136 }
137 u += du;
138 }
141 // now the cap!
142 if(!capsub) {
143 return;
144 }
146 dv = 1.0 / (float)(capvverts - 1);
148 u = 0.0;
149 for(int i=0; i<uverts; i++) {
150 float theta = SURAD(u);
152 float v = 0.0;
153 for(int j=0; j<capvverts; j++) {
154 float r = v * rad;
156 Vector3 pos = cylvec(theta, height / 2.0) * r;
157 pos.y = height / 2.0;
158 Vector3 tang = (cylvec(theta + 0.1, 0.0) - cylvec(theta - 0.1, 0.0)).normalized();
160 *varr++ = pos;
161 *narr++ = Vector3(0, 1, 0);
162 *tarr++ = tang;
163 *uvarr++ = Vector2(u * urange, v);
165 pos.y = -height / 2.0;
166 *varr++ = pos;
167 *narr++ = Vector3(0, -1, 0);
168 *tarr++ = -tang;
169 *uvarr++ = Vector2(u * urange, v);
171 if(i < usub && j < capsub) {
172 unsigned int idx = num_body_verts + (i * capvverts + j) * 2;
174 unsigned int vidx[4] = {
175 idx,
176 idx + capvverts * 2,
177 idx + (capvverts + 1) * 2,
178 idx + 2
179 };
181 *idxarr++ = vidx[0];
182 *idxarr++ = vidx[2];
183 *idxarr++ = vidx[1];
184 *idxarr++ = vidx[0];
185 *idxarr++ = vidx[3];
186 *idxarr++ = vidx[2];
188 *idxarr++ = vidx[0] + 1;
189 *idxarr++ = vidx[1] + 1;
190 *idxarr++ = vidx[2] + 1;
191 *idxarr++ = vidx[0] + 1;
192 *idxarr++ = vidx[2] + 1;
193 *idxarr++ = vidx[3] + 1;
194 }
196 v += dv;
197 }
198 u += du;
199 }
200 }
202 // -------- cone --------
204 static Vector3 conevec(float theta, float y, float height)
205 {
206 float scale = 1.0 - y / height;
207 return Vector3(sin(theta) * scale, y, cos(theta) * scale);
208 }
210 void gen_cone(Mesh *mesh, float rad, float height, int usub, int vsub, int capsub, float urange, float vrange)
211 {
212 if(usub < 4) usub = 4;
213 if(vsub < 1) vsub = 1;
215 int uverts = usub + 1;
216 int vverts = vsub + 1;
218 int num_body_verts = uverts * vverts;
219 int num_body_quads = usub * vsub;
220 int num_body_tri = num_body_quads * 2;
222 int capvverts = capsub ? capsub + 1 : 0;
223 int num_cap_verts = uverts * capvverts;
224 int num_cap_quads = usub * capsub;
225 int num_cap_tri = num_cap_quads * 2;
227 int num_verts = num_body_verts + num_cap_verts;
228 int num_tri = num_body_tri + num_cap_tri;
230 mesh->clear();
231 Vector3 *varr = (Vector3*)mesh->set_attrib_data(MESH_ATTR_VERTEX, 3, num_verts, 0);
232 Vector3 *narr = (Vector3*)mesh->set_attrib_data(MESH_ATTR_NORMAL, 3, num_verts, 0);
233 Vector3 *tarr = (Vector3*)mesh->set_attrib_data(MESH_ATTR_TANGENT, 3, num_verts, 0);
234 Vector2 *uvarr = (Vector2*)mesh->set_attrib_data(MESH_ATTR_TEXCOORD, 2, num_verts, 0);
235 unsigned int *idxarr = mesh->set_index_data(num_tri * 3, 0);
237 float du = urange / (float)(uverts - 1);
238 float dv = vrange / (float)(vverts - 1);
240 float u = 0.0;
241 for(int i=0; i<uverts; i++) {
242 float theta = SURAD(u);
244 float v = 0.0;
245 for(int j=0; j<vverts; j++) {
246 float y = v * height;
247 Vector3 pos = conevec(theta, y, height);
249 Vector3 tang = (conevec(theta + 0.1, 0.0, height) - conevec(theta - 0.1, 0.0, height)).normalized();
250 Vector3 bitang = (conevec(theta, y + 0.1, height) - pos).normalized();
252 *varr++ = Vector3(pos.x * rad, pos.y, pos.z * rad);
253 *narr++ = cross_product(tang, bitang);
254 *tarr++ = tang;
255 *uvarr++ = Vector2(u * urange, v * vrange);
257 if(i < usub && j < vsub) {
258 int idx = i * vverts + j;
260 *idxarr++ = idx;
261 *idxarr++ = idx + vverts + 1;
262 *idxarr++ = idx + 1;
264 *idxarr++ = idx;
265 *idxarr++ = idx + vverts;
266 *idxarr++ = idx + vverts + 1;
267 }
269 v += dv;
270 }
271 u += du;
272 }
275 // now the bottom cap!
276 if(!capsub) {
277 return;
278 }
280 dv = 1.0 / (float)(capvverts - 1);
282 u = 0.0;
283 for(int i=0; i<uverts; i++) {
284 float theta = SURAD(u);
286 float v = 0.0;
287 for(int j=0; j<capvverts; j++) {
288 float r = v * rad;
290 Vector3 pos = conevec(theta, 0.0, height) * r;
291 Vector3 tang = (cylvec(theta + 0.1, 0.0) - cylvec(theta - 0.1, 0.0)).normalized();
293 *varr++ = pos;
294 *narr++ = Vector3(0, -1, 0);
295 *tarr++ = tang;
296 *uvarr++ = Vector2(u * urange, v);
298 if(i < usub && j < capsub) {
299 unsigned int idx = num_body_verts + i * capvverts + j;
301 unsigned int vidx[4] = {
302 idx,
303 idx + capvverts,
304 idx + (capvverts + 1),
305 idx + 1
306 };
308 *idxarr++ = vidx[0];
309 *idxarr++ = vidx[1];
310 *idxarr++ = vidx[2];
311 *idxarr++ = vidx[0];
312 *idxarr++ = vidx[2];
313 *idxarr++ = vidx[3];
314 }
316 v += dv;
317 }
318 u += du;
319 }
320 }
323 // -------- plane --------
325 void gen_plane(Mesh *mesh, float width, float height, int usub, int vsub)
326 {
327 gen_heightmap(mesh, width, height, usub, vsub, 0);
328 }
331 // ----- heightmap ------
333 void gen_heightmap(Mesh *mesh, float width, float height, int usub, int vsub, float (*hf)(float, float, void*), void *hfdata)
334 {
335 if(usub < 1) usub = 1;
336 if(vsub < 1) vsub = 1;
338 mesh->clear();
340 int uverts = usub + 1;
341 int vverts = vsub + 1;
342 int num_verts = uverts * vverts;
344 int num_quads = usub * vsub;
345 int num_tri = num_quads * 2;
347 Vector3 *varr = (Vector3*)mesh->set_attrib_data(MESH_ATTR_VERTEX, 3, num_verts, 0);
348 Vector3 *narr = (Vector3*)mesh->set_attrib_data(MESH_ATTR_NORMAL, 3, num_verts, 0);
349 Vector3 *tarr = (Vector3*)mesh->set_attrib_data(MESH_ATTR_TANGENT, 3, num_verts, 0);
350 Vector2 *uvarr = (Vector2*)mesh->set_attrib_data(MESH_ATTR_TEXCOORD, 2, num_verts, 0);
351 unsigned int *idxarr = mesh->set_index_data(num_tri * 3, 0);
353 float du = 1.0 / (float)usub;
354 float dv = 1.0 / (float)vsub;
356 float u = 0.0;
357 for(int i=0; i<uverts; i++) {
358 float v = 0.0;
359 for(int j=0; j<vverts; j++) {
360 float x = (u - 0.5) * width;
361 float y = (v - 0.5) * height;
362 float z = hf ? hf(u, v, hfdata) : 0.0;
364 Vector3 normal = Vector3(0, 0, 1);
365 if(hf) {
366 float u1z = hf(u + du, v, hfdata);
367 float v1z = hf(u, v + dv, hfdata);
369 Vector3 tang = Vector3(du * width, 0, u1z - z);
370 Vector3 bitan = Vector3(0, dv * height, v1z - z);
371 normal = cross_product(tang, bitan).normalized();
372 }
374 *varr++ = Vector3(x, y, z);
375 *narr++ = normal;
376 *tarr++ = Vector3(1, 0, 0);
377 *uvarr++ = Vector2(u, v);
379 if(i < usub && j < vsub) {
380 int idx = i * vverts + j;
382 *idxarr++ = idx;
383 *idxarr++ = idx + vverts + 1;
384 *idxarr++ = idx + 1;
386 *idxarr++ = idx;
387 *idxarr++ = idx + vverts;
388 *idxarr++ = idx + vverts + 1;
389 }
391 v += dv;
392 }
393 u += du;
394 }
395 }
397 // ----- heightmap ------
399 void gen_box(Mesh *mesh, float xsz, float ysz, float zsz)
400 {
401 mesh->clear();
403 const int num_faces = 6;
404 int num_verts = num_faces * 4;
405 int num_tri = num_faces * 2;
407 float x = xsz / 2.0;
408 float y = ysz / 2.0;
409 float z = zsz / 2.0;
411 Vector3 *varr = (Vector3*)mesh->set_attrib_data(MESH_ATTR_VERTEX, 3, num_verts, 0);
412 Vector3 *narr = (Vector3*)mesh->set_attrib_data(MESH_ATTR_NORMAL, 3, num_verts, 0);
413 Vector3 *tarr = (Vector3*)mesh->set_attrib_data(MESH_ATTR_TANGENT, 3, num_verts, 0);
414 Vector2 *uvarr = (Vector2*)mesh->set_attrib_data(MESH_ATTR_TEXCOORD, 2, num_verts, 0);
415 unsigned int *idxarr = mesh->set_index_data(num_tri * 3, 0);
417 static const Vector2 uv[] = { Vector2(0, 0), Vector2(1, 0), Vector2(1, 1), Vector2(0, 1) };
419 // front
420 for(int i=0; i<4; i++) {
421 *narr++ = Vector3(0, 0, 1);
422 *tarr++ = Vector3(1, 0, 0);
423 *uvarr++ = uv[i];
424 }
425 *varr++ = Vector3(-x, -y, z);
426 *varr++ = Vector3(x, -y, z);
427 *varr++ = Vector3(x, y, z);
428 *varr++ = Vector3(-x, y, z);
429 // right
430 for(int i=0; i<4; i++) {
431 *narr++ = Vector3(1, 0, 0);
432 *tarr++ = Vector3(0, 0, -1);
433 *uvarr++ = uv[i];
434 }
435 *varr++ = Vector3(x, -y, z);
436 *varr++ = Vector3(x, -y, -z);
437 *varr++ = Vector3(x, y, -z);
438 *varr++ = Vector3(x, y, z);
439 // back
440 for(int i=0; i<4; i++) {
441 *narr++ = Vector3(0, 0, -1);
442 *tarr++ = Vector3(-1, 0, 0);
443 *uvarr++ = uv[i];
444 }
445 *varr++ = Vector3(x, -y, -z);
446 *varr++ = Vector3(-x, -y, -z);
447 *varr++ = Vector3(-x, y, -z);
448 *varr++ = Vector3(x, y, -z);
449 // left
450 for(int i=0; i<4; i++) {
451 *narr++ = Vector3(-1, 0, 0);
452 *tarr++ = Vector3(0, 0, 1);
453 *uvarr++ = uv[i];
454 }
455 *varr++ = Vector3(-x, -y, -z);
456 *varr++ = Vector3(-x, -y, z);
457 *varr++ = Vector3(-x, y, z);
458 *varr++ = Vector3(-x, y, -z);
459 // top
460 for(int i=0; i<4; i++) {
461 *narr++ = Vector3(0, 1, 0);
462 *tarr++ = Vector3(1, 0, 0);
463 *uvarr++ = uv[i];
464 }
465 *varr++ = Vector3(-x, y, z);
466 *varr++ = Vector3(x, y, z);
467 *varr++ = Vector3(x, y, -z);
468 *varr++ = Vector3(-x, y, -z);
469 // bottom
470 for(int i=0; i<4; i++) {
471 *narr++ = Vector3(0, -1, 0);
472 *tarr++ = Vector3(1, 0, 0);
473 *uvarr++ = uv[i];
474 }
475 *varr++ = Vector3(-x, -y, -z);
476 *varr++ = Vector3(x, -y, -z);
477 *varr++ = Vector3(x, -y, z);
478 *varr++ = Vector3(-x, -y, z);
480 // index array
481 static const int faceidx[] = {0, 1, 2, 0, 2, 3};
482 for(int i=0; i<num_faces; i++) {
483 for(int j=0; j<6; j++) {
484 *idxarr++ = faceidx[j] + i * 4;
485 }
486 }
487 }
489 static inline Vector3 rev_vert(float u, float v, Vector2 (*rf)(float, float, void*), void *cls)
490 {
491 Vector2 pos = rf(u, v, cls);
493 float angle = u * 2.0 * M_PI;
494 float x = pos.x * cos(angle);
495 float y = pos.y;
496 float z = pos.x * sin(angle);
498 return Vector3(x, y, z);
499 }
501 // ------ surface of revolution -------
502 void gen_revol(Mesh *mesh, int usub, int vsub, Vector2 (*rfunc)(float, float, void*), void *cls)
503 {
504 gen_revol(mesh, usub, vsub, rfunc, 0, cls);
505 }
507 void gen_revol(Mesh *mesh, int usub, int vsub, Vector2 (*rfunc)(float, float, void*),
508 Vector2 (*nfunc)(float, float, void*), void *cls)
509 {
510 if(!rfunc) return;
511 if(usub < 3) usub = 3;
512 if(vsub < 1) vsub = 1;
514 mesh->clear();
516 int uverts = usub + 1;
517 int vverts = vsub + 1;
518 int num_verts = uverts * vverts;
520 int num_quads = usub * vsub;
521 int num_tri = num_quads * 2;
523 Vector3 *varr = (Vector3*)mesh->set_attrib_data(MESH_ATTR_VERTEX, 3, num_verts, 0);
524 Vector3 *narr = (Vector3*)mesh->set_attrib_data(MESH_ATTR_NORMAL, 3, num_verts, 0);
525 Vector3 *tarr = (Vector3*)mesh->set_attrib_data(MESH_ATTR_TANGENT, 3, num_verts, 0);
526 Vector2 *uvarr = (Vector2*)mesh->set_attrib_data(MESH_ATTR_TEXCOORD, 2, num_verts, 0);
527 unsigned int *idxarr = mesh->set_index_data(num_tri * 3, 0);
529 float du = 1.0 / (float)(uverts - 1);
530 float dv = 1.0 / (float)(vverts - 1);
532 float u = 0.0;
533 for(int i=0; i<uverts; i++) {
534 float v = 0.0;
535 for(int j=0; j<vverts; j++) {
536 Vector3 pos = rev_vert(u, v, rfunc, cls);
538 Vector3 nextu = rev_vert(fmod(u + du, 1.0), v, rfunc, cls);
539 Vector3 tang = nextu - pos;
540 if(tang.length_sq() < 1e-6) {
541 float new_v = v > 0.5 ? v - dv * 0.25 : v + dv * 0.25;
542 nextu = rev_vert(fmod(u + du, 1.0), new_v, rfunc, cls);
543 tang = nextu - pos;
544 }
546 Vector3 normal;
547 if(nfunc) {
548 normal = rev_vert(u, v, nfunc, cls);
549 } else {
550 Vector3 nextv = rev_vert(u, v + dv, rfunc, cls);
551 Vector3 bitan = nextv - pos;
552 if(bitan.length_sq() < 1e-6) {
553 nextv = rev_vert(u, v - dv, rfunc, cls);
554 bitan = pos - nextv;
555 }
557 normal = cross_product(tang, bitan);
558 }
560 *varr++ = pos;
561 *narr++ = normal.normalized();
562 *tarr++ = tang.normalized();
563 *uvarr++ = Vector2(u, v);
565 if(i < usub && j < vsub) {
566 int idx = i * vverts + j;
568 *idxarr++ = idx;
569 *idxarr++ = idx + vverts + 1;
570 *idxarr++ = idx + 1;
572 *idxarr++ = idx;
573 *idxarr++ = idx + vverts;
574 *idxarr++ = idx + vverts + 1;
575 }
577 v += dv;
578 }
579 u += du;
580 }
581 }