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