nuclear@0: #include nuclear@0: #include "meshgen.h" nuclear@0: #include "mesh.h" nuclear@0: nuclear@0: // -------- sphere -------- nuclear@0: nuclear@0: #define SURAD(u) ((u) * 2.0 * M_PI) nuclear@0: #define SVRAD(v) ((v) * M_PI) nuclear@0: nuclear@0: static Vector3 sphvec(float theta, float phi) nuclear@0: { nuclear@0: return Vector3(sin(theta) * sin(phi), nuclear@0: cos(phi), nuclear@0: cos(theta) * sin(phi)); nuclear@0: } nuclear@0: nuclear@0: void gen_sphere(Mesh *mesh, float rad, int usub, int vsub, float urange, float vrange) nuclear@0: { nuclear@0: if(usub < 4) usub = 4; nuclear@0: if(vsub < 2) vsub = 2; nuclear@0: nuclear@0: int uverts = usub + 1; nuclear@0: int vverts = vsub + 1; nuclear@0: nuclear@0: int num_verts = uverts * vverts; nuclear@0: int num_quads = usub * vsub; nuclear@0: int num_tri = num_quads * 2; nuclear@0: nuclear@0: mesh->clear(); nuclear@0: Vector3 *varr = (Vector3*)mesh->set_attrib_data(MESH_ATTR_VERTEX, 3, num_verts, 0); nuclear@0: Vector3 *narr = (Vector3*)mesh->set_attrib_data(MESH_ATTR_NORMAL, 3, num_verts, 0); nuclear@0: Vector3 *tarr = (Vector3*)mesh->set_attrib_data(MESH_ATTR_TANGENT, 3, num_verts, 0); nuclear@0: Vector2 *uvarr = (Vector2*)mesh->set_attrib_data(MESH_ATTR_TEXCOORD, 2, num_verts, 0); nuclear@0: unsigned int *idxarr = mesh->set_index_data(num_tri * 3, 0); nuclear@0: nuclear@0: float du = urange / (float)(uverts - 1); nuclear@0: float dv = vrange / (float)(vverts - 1); nuclear@0: nuclear@0: float u = 0.0; nuclear@0: for(int i=0; iclear(); nuclear@0: Vector3 *varr = (Vector3*)mesh->set_attrib_data(MESH_ATTR_VERTEX, 3, num_verts, 0); nuclear@0: Vector3 *narr = (Vector3*)mesh->set_attrib_data(MESH_ATTR_NORMAL, 3, num_verts, 0); nuclear@0: Vector3 *tarr = (Vector3*)mesh->set_attrib_data(MESH_ATTR_TANGENT, 3, num_verts, 0); nuclear@0: Vector2 *uvarr = (Vector2*)mesh->set_attrib_data(MESH_ATTR_TEXCOORD, 2, num_verts, 0); nuclear@0: unsigned int *idxarr = mesh->set_index_data(num_tri * 3, 0); nuclear@0: nuclear@0: float du = urange / (float)(uverts - 1); nuclear@0: float dv = vrange / (float)(vverts - 1); nuclear@0: nuclear@0: float u = 0.0; nuclear@0: for(int i=0; iclear(); nuclear@0: Vector3 *varr = (Vector3*)mesh->set_attrib_data(MESH_ATTR_VERTEX, 3, num_verts, 0); nuclear@0: Vector3 *narr = (Vector3*)mesh->set_attrib_data(MESH_ATTR_NORMAL, 3, num_verts, 0); nuclear@0: Vector3 *tarr = (Vector3*)mesh->set_attrib_data(MESH_ATTR_TANGENT, 3, num_verts, 0); nuclear@0: Vector2 *uvarr = (Vector2*)mesh->set_attrib_data(MESH_ATTR_TEXCOORD, 2, num_verts, 0); nuclear@0: unsigned int *idxarr = mesh->set_index_data(num_tri * 3, 0); nuclear@0: nuclear@0: float du = urange / (float)(uverts - 1); nuclear@0: float dv = vrange / (float)(vverts - 1); nuclear@0: nuclear@0: float u = 0.0; nuclear@0: for(int i=0; iclear(); nuclear@0: nuclear@0: int uverts = usub + 1; nuclear@0: int vverts = vsub + 1; nuclear@0: int num_verts = uverts * vverts; nuclear@0: nuclear@0: int num_quads = usub * vsub; nuclear@0: int num_tri = num_quads * 2; nuclear@0: nuclear@0: Vector3 *varr = (Vector3*)mesh->set_attrib_data(MESH_ATTR_VERTEX, 3, num_verts, 0); nuclear@0: Vector3 *narr = (Vector3*)mesh->set_attrib_data(MESH_ATTR_NORMAL, 3, num_verts, 0); nuclear@0: Vector3 *tarr = (Vector3*)mesh->set_attrib_data(MESH_ATTR_TANGENT, 3, num_verts, 0); nuclear@0: Vector2 *uvarr = (Vector2*)mesh->set_attrib_data(MESH_ATTR_TEXCOORD, 2, num_verts, 0); nuclear@0: unsigned int *idxarr = mesh->set_index_data(num_tri * 3, 0); nuclear@0: nuclear@0: float du = 1.0 / (float)usub; nuclear@0: float dv = 1.0 / (float)vsub; nuclear@0: nuclear@0: float u = 0.0; nuclear@0: for(int i=0; iclear(); nuclear@1: nuclear@1: for(int i=0; i<6; i++) { nuclear@2: Matrix4x4 xform, dir_xform; nuclear@1: Mesh m; nuclear@1: nuclear@1: gen_plane(&m, 1, 1, usub, vsub); nuclear@1: xform.rotate(Vector3(face_angles[i][1], face_angles[i][0], 0)); nuclear@2: dir_xform = xform; nuclear@1: xform.translate(Vector3(0, 0, 0.5)); nuclear@2: m.apply_xform(xform, dir_xform); nuclear@1: nuclear@1: mesh->append(m); nuclear@1: } nuclear@1: nuclear@1: Matrix4x4 scale; nuclear@1: scale.set_scaling(Vector3(xsz, ysz, zsz)); nuclear@1: mesh->apply_xform(scale, Matrix4x4::identity); nuclear@1: } nuclear@1: nuclear@1: /* nuclear@0: void gen_box(Mesh *mesh, float xsz, float ysz, float zsz) nuclear@0: { nuclear@0: mesh->clear(); nuclear@0: nuclear@0: const int num_faces = 6; nuclear@0: int num_verts = num_faces * 4; nuclear@0: int num_tri = num_faces * 2; nuclear@0: nuclear@0: float x = xsz / 2.0; nuclear@0: float y = ysz / 2.0; nuclear@0: float z = zsz / 2.0; nuclear@0: nuclear@0: Vector3 *varr = (Vector3*)mesh->set_attrib_data(MESH_ATTR_VERTEX, 3, num_verts, 0); nuclear@0: Vector3 *narr = (Vector3*)mesh->set_attrib_data(MESH_ATTR_NORMAL, 3, num_verts, 0); nuclear@0: Vector3 *tarr = (Vector3*)mesh->set_attrib_data(MESH_ATTR_TANGENT, 3, num_verts, 0); nuclear@0: Vector2 *uvarr = (Vector2*)mesh->set_attrib_data(MESH_ATTR_TEXCOORD, 2, num_verts, 0); nuclear@0: unsigned int *idxarr = mesh->set_index_data(num_tri * 3, 0); nuclear@0: nuclear@0: static const Vector2 uv[] = { Vector2(0, 0), Vector2(1, 0), Vector2(1, 1), Vector2(0, 1) }; nuclear@0: nuclear@0: // front nuclear@0: for(int i=0; i<4; i++) { nuclear@0: *narr++ = Vector3(0, 0, 1); nuclear@0: *tarr++ = Vector3(1, 0, 0); nuclear@0: *uvarr++ = uv[i]; nuclear@0: } nuclear@0: *varr++ = Vector3(-x, -y, z); nuclear@0: *varr++ = Vector3(x, -y, z); nuclear@0: *varr++ = Vector3(x, y, z); nuclear@0: *varr++ = Vector3(-x, y, z); nuclear@0: // right nuclear@0: for(int i=0; i<4; i++) { nuclear@0: *narr++ = Vector3(1, 0, 0); nuclear@0: *tarr++ = Vector3(0, 0, -1); nuclear@0: *uvarr++ = uv[i]; nuclear@0: } nuclear@0: *varr++ = Vector3(x, -y, z); nuclear@0: *varr++ = Vector3(x, -y, -z); nuclear@0: *varr++ = Vector3(x, y, -z); nuclear@0: *varr++ = Vector3(x, y, z); nuclear@0: // back nuclear@0: for(int i=0; i<4; i++) { nuclear@0: *narr++ = Vector3(0, 0, -1); nuclear@0: *tarr++ = Vector3(-1, 0, 0); nuclear@0: *uvarr++ = uv[i]; nuclear@0: } nuclear@0: *varr++ = Vector3(x, -y, -z); nuclear@0: *varr++ = Vector3(-x, -y, -z); nuclear@0: *varr++ = Vector3(-x, y, -z); nuclear@0: *varr++ = Vector3(x, y, -z); nuclear@0: // left nuclear@0: for(int i=0; i<4; i++) { nuclear@0: *narr++ = Vector3(-1, 0, 0); nuclear@0: *tarr++ = Vector3(0, 0, 1); nuclear@0: *uvarr++ = uv[i]; nuclear@0: } nuclear@0: *varr++ = Vector3(-x, -y, -z); nuclear@0: *varr++ = Vector3(-x, -y, z); nuclear@0: *varr++ = Vector3(-x, y, z); nuclear@0: *varr++ = Vector3(-x, y, -z); nuclear@0: // top nuclear@0: for(int i=0; i<4; i++) { nuclear@0: *narr++ = Vector3(0, 1, 0); nuclear@0: *tarr++ = Vector3(1, 0, 0); nuclear@0: *uvarr++ = uv[i]; nuclear@0: } nuclear@0: *varr++ = Vector3(-x, y, z); nuclear@0: *varr++ = Vector3(x, y, z); nuclear@0: *varr++ = Vector3(x, y, -z); nuclear@0: *varr++ = Vector3(-x, y, -z); nuclear@0: // bottom nuclear@0: for(int i=0; i<4; i++) { nuclear@0: *narr++ = Vector3(0, -1, 0); nuclear@0: *tarr++ = Vector3(1, 0, 0); nuclear@0: *uvarr++ = uv[i]; nuclear@0: } nuclear@0: *varr++ = Vector3(-x, -y, -z); nuclear@0: *varr++ = Vector3(x, -y, -z); nuclear@0: *varr++ = Vector3(x, -y, z); nuclear@0: *varr++ = Vector3(-x, -y, z); nuclear@0: nuclear@0: // index array nuclear@0: static const int faceidx[] = {0, 1, 2, 0, 2, 3}; nuclear@0: for(int i=0; iclear(); nuclear@0: nuclear@0: int uverts = usub + 1; nuclear@0: int vverts = vsub + 1; nuclear@0: int num_verts = uverts * vverts; nuclear@0: nuclear@0: int num_quads = usub * vsub; nuclear@0: int num_tri = num_quads * 2; nuclear@0: nuclear@0: Vector3 *varr = (Vector3*)mesh->set_attrib_data(MESH_ATTR_VERTEX, 3, num_verts, 0); nuclear@0: Vector3 *narr = (Vector3*)mesh->set_attrib_data(MESH_ATTR_NORMAL, 3, num_verts, 0); nuclear@0: Vector3 *tarr = (Vector3*)mesh->set_attrib_data(MESH_ATTR_TANGENT, 3, num_verts, 0); nuclear@0: Vector2 *uvarr = (Vector2*)mesh->set_attrib_data(MESH_ATTR_TEXCOORD, 2, num_verts, 0); nuclear@0: unsigned int *idxarr = mesh->set_index_data(num_tri * 3, 0); nuclear@0: nuclear@0: float du = 1.0 / (float)(uverts - 1); nuclear@0: float dv = 1.0 / (float)(vverts - 1); nuclear@0: nuclear@0: float u = 0.0; nuclear@0: for(int i=0; i 0.5 ? v - dv * 0.25 : v + dv * 0.25; nuclear@0: nextu = rev_vert(fmod(u + du, 1.0), new_v, rfunc, cls); nuclear@0: tang = nextu - pos; nuclear@0: } nuclear@0: nuclear@0: Vector3 normal; nuclear@0: if(nfunc) { nuclear@0: normal = rev_vert(u, v, nfunc, cls); nuclear@0: } else { nuclear@0: Vector3 nextv = rev_vert(u, v + dv, rfunc, cls); nuclear@0: Vector3 bitan = nextv - pos; nuclear@0: if(bitan.length_sq() < 1e-6) { nuclear@0: nextv = rev_vert(u, v - dv, rfunc, cls); nuclear@0: bitan = pos - nextv; nuclear@0: } nuclear@0: nuclear@0: normal = cross_product(tang, bitan); nuclear@0: } nuclear@0: nuclear@0: *varr++ = pos; nuclear@0: *narr++ = normal.normalized(); nuclear@0: *tarr++ = tang.normalized(); nuclear@0: *uvarr++ = Vector2(u, v); nuclear@0: nuclear@0: if(i < usub && j < vsub) { nuclear@0: int idx = i * vverts + j; nuclear@0: nuclear@0: *idxarr++ = idx; nuclear@0: *idxarr++ = idx + vverts + 1; nuclear@0: *idxarr++ = idx + 1; nuclear@0: nuclear@0: *idxarr++ = idx; nuclear@0: *idxarr++ = idx + vverts; nuclear@0: *idxarr++ = idx + vverts + 1; nuclear@0: } nuclear@0: nuclear@0: v += dv; nuclear@0: } nuclear@0: u += du; nuclear@0: } nuclear@0: }