ld33_umonster
changeset 4:7b1fa527b51b
added torus mesh gen
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sun, 23 Aug 2015 04:31:29 +0300 |
parents | 93ff21458a16 |
children | 1e8d90aeae34 |
files | src/meshgen.cc src/meshgen.h |
diffstat | 2 files changed, 78 insertions(+), 2 deletions(-) [+] |
line diff
1.1 --- a/src/meshgen.cc Sun Aug 23 02:51:39 2015 +0300 1.2 +++ b/src/meshgen.cc Sun Aug 23 04:31:29 2015 +0300 1.3 @@ -38,11 +38,11 @@ 1.4 1.5 float u = 0.0; 1.6 for(int i=0; i<uverts; i++) { 1.7 - float theta = SURAD(u * urange); 1.8 + float theta = u * 2.0 * M_PI; 1.9 1.10 float v = 0.0; 1.11 for(int j=0; j<vverts; j++) { 1.12 - float phi = SVRAD(v * vrange); 1.13 + float phi = v * M_PI; 1.14 1.15 Vector3 pos = sphvec(theta, phi); 1.16 1.17 @@ -68,6 +68,81 @@ 1.18 } 1.19 } 1.20 1.21 +// -------- torus ----------- 1.22 +static Vector3 torusvec(float theta, float phi, float mr, float rr) 1.23 +{ 1.24 + theta = -theta; 1.25 + 1.26 + float rx = -cos(phi) * rr + mr; 1.27 + float ry = sin(phi) * rr; 1.28 + float rz = 0.0; 1.29 + 1.30 + float x = rx * sin(theta) + rz * cos(theta); 1.31 + float y = ry; 1.32 + float z = -rx * cos(theta) + rz * sin(theta); 1.33 + 1.34 + return Vector3(x, y, z); 1.35 +} 1.36 + 1.37 +void gen_torus(Mesh *mesh, float mainrad, float ringrad, int usub, int vsub, float urange, float vrange) 1.38 +{ 1.39 + if(usub < 4) usub = 4; 1.40 + if(vsub < 2) vsub = 2; 1.41 + 1.42 + int uverts = usub + 1; 1.43 + int vverts = vsub + 1; 1.44 + 1.45 + int num_verts = uverts * vverts; 1.46 + int num_quads = usub * vsub; 1.47 + int num_tri = num_quads * 2; 1.48 + 1.49 + mesh->clear(); 1.50 + Vector3 *varr = (Vector3*)mesh->set_attrib_data(MESH_ATTR_VERTEX, 3, num_verts, 0); 1.51 + Vector3 *narr = (Vector3*)mesh->set_attrib_data(MESH_ATTR_NORMAL, 3, num_verts, 0); 1.52 + Vector3 *tarr = (Vector3*)mesh->set_attrib_data(MESH_ATTR_TANGENT, 3, num_verts, 0); 1.53 + Vector2 *uvarr = (Vector2*)mesh->set_attrib_data(MESH_ATTR_TEXCOORD, 2, num_verts, 0); 1.54 + unsigned int *idxarr = mesh->set_index_data(num_tri * 3, 0); 1.55 + 1.56 + float du = urange / (float)(uverts - 1); 1.57 + float dv = vrange / (float)(vverts - 1); 1.58 + 1.59 + float u = 0.0; 1.60 + for(int i=0; i<uverts; i++) { 1.61 + float theta = u * 2.0 * M_PI; 1.62 + 1.63 + float v = 0.0; 1.64 + for(int j=0; j<vverts; j++) { 1.65 + float phi = v * 2.0 * M_PI; 1.66 + 1.67 + Vector3 pos = torusvec(theta, phi, mainrad, ringrad); 1.68 + Vector3 cent = torusvec(theta, phi, mainrad, 0.0); 1.69 + 1.70 + *varr++ = pos; 1.71 + *narr++ = (pos - cent) / ringrad; 1.72 + 1.73 + Vector3 pprev = torusvec(theta - 0.1f, phi, mainrad, ringrad); 1.74 + Vector3 pnext = torusvec(theta + 0.1f, phi, mainrad, ringrad); 1.75 + 1.76 + *tarr++ = (pnext - pprev).normalized(); 1.77 + *uvarr++ = Vector2(u * urange, v * vrange); 1.78 + 1.79 + if(i < usub && j < vsub) { 1.80 + int idx = i * vverts + j; 1.81 + *idxarr++ = idx; 1.82 + *idxarr++ = idx + 1; 1.83 + *idxarr++ = idx + vverts + 1; 1.84 + 1.85 + *idxarr++ = idx; 1.86 + *idxarr++ = idx + vverts + 1; 1.87 + *idxarr++ = idx + vverts; 1.88 + } 1.89 + 1.90 + v += dv; 1.91 + } 1.92 + u += du; 1.93 + } 1.94 +} 1.95 + 1.96 1.97 // -------- cylinder -------- 1.98
2.1 --- a/src/meshgen.h Sun Aug 23 02:51:39 2015 +0300 2.2 +++ b/src/meshgen.h Sun Aug 23 04:31:29 2015 +0300 2.3 @@ -6,6 +6,7 @@ 2.4 class Mesh; 2.5 2.6 void gen_sphere(Mesh *mesh, float rad, int usub, int vsub, float urange = 1.0, float vrange = 1.0); 2.7 +void gen_torus(Mesh *mesh, float mainrad, float ringrad, int usub, int vsub, float urange = 1.0, float vrange = 1.0); 2.8 void gen_cylinder(Mesh *mesh, float rad, float height, int usub, int vsub, int capsub = 0, float urange = 1.0, float vrange = 1.0); 2.9 void gen_cone(Mesh *mesh, float rad, float height, int usub, int vsub, int capsub = 0, float urange = 1.0, float vrange = 1.0); 2.10 void gen_plane(Mesh *mesh, float width, float height, int usub = 1, int vsub = 1);