ld33_umonster

diff src/meshgen.cc @ 10:1b30bd381667

sweep curve mesh gen and dragon horns
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 27 Aug 2015 05:25:04 +0300
parents 7b1fa527b51b
children
line diff
     1.1 --- a/src/meshgen.cc	Thu Aug 27 01:58:26 2015 +0300
     1.2 +++ b/src/meshgen.cc	Thu Aug 27 05:25:04 2015 +0300
     1.3 @@ -689,3 +689,87 @@
     1.4  		u += du;
     1.5  	}
     1.6  }
     1.7 +
     1.8 +
     1.9 +static inline Vector3 sweep_vert(float u, float v, float height, Vector2 (*sf)(float, float, void*), void *cls)
    1.10 +{
    1.11 +	Vector2 pos = sf(u, v, cls);
    1.12 +
    1.13 +	float x = pos.x;
    1.14 +	float y = v * height;
    1.15 +	float z = pos.y;
    1.16 +
    1.17 +	return Vector3(x, y, z);
    1.18 +}
    1.19 +
    1.20 +// ---- sweep shape along a path ----
    1.21 +void gen_sweep(Mesh *mesh, float height, int usub, int vsub, Vector2 (*sfunc)(float, float, void*), void *cls)
    1.22 +{
    1.23 +	if(!sfunc) return;
    1.24 +	if(usub < 3) usub = 3;
    1.25 +	if(vsub < 1) vsub = 1;
    1.26 +
    1.27 +	mesh->clear();
    1.28 +
    1.29 +	int uverts = usub + 1;
    1.30 +	int vverts = vsub + 1;
    1.31 +	int num_verts = uverts * vverts;
    1.32 +
    1.33 +	int num_quads = usub * vsub;
    1.34 +	int num_tri = num_quads * 2;
    1.35 +
    1.36 +	Vector3 *varr = (Vector3*)mesh->set_attrib_data(MESH_ATTR_VERTEX, 3, num_verts, 0);
    1.37 +	Vector3 *narr = (Vector3*)mesh->set_attrib_data(MESH_ATTR_NORMAL, 3, num_verts, 0);
    1.38 +	Vector3 *tarr = (Vector3*)mesh->set_attrib_data(MESH_ATTR_TANGENT, 3, num_verts, 0);
    1.39 +	Vector2 *uvarr = (Vector2*)mesh->set_attrib_data(MESH_ATTR_TEXCOORD, 2, num_verts, 0);
    1.40 +	unsigned int *idxarr = mesh->set_index_data(num_tri * 3, 0);
    1.41 +
    1.42 +	float du = 1.0 / (float)(uverts - 1);
    1.43 +	float dv = 1.0 / (float)(vverts - 1);
    1.44 +
    1.45 +	float u = 0.0;
    1.46 +	for(int i=0; i<uverts; i++) {
    1.47 +		float v = 0.0;
    1.48 +		for(int j=0; j<vverts; j++) {
    1.49 +			Vector3 pos = sweep_vert(u, v, height, sfunc, cls);
    1.50 +
    1.51 +			Vector3 nextu = sweep_vert(fmod(u + du, 1.0), v, height, sfunc, cls);
    1.52 +			Vector3 tang = nextu - pos;
    1.53 +			if(tang.length_sq() < 1e-6) {
    1.54 +				float new_v = v > 0.5 ? v - dv * 0.25 : v + dv * 0.25;
    1.55 +				nextu = sweep_vert(fmod(u + du, 1.0), new_v, height, sfunc, cls);
    1.56 +				tang = nextu - pos;
    1.57 +			}
    1.58 +
    1.59 +			Vector3 normal;
    1.60 +			Vector3 nextv = sweep_vert(u, v + dv, height, sfunc, cls);
    1.61 +			Vector3 bitan = nextv - pos;
    1.62 +			if(bitan.length_sq() < 1e-6) {
    1.63 +				nextv = sweep_vert(u, v - dv, height, sfunc, cls);
    1.64 +				bitan = pos - nextv;
    1.65 +			}
    1.66 +
    1.67 +			normal = cross_product(tang, bitan);
    1.68 +
    1.69 +			*varr++ = pos;
    1.70 +			*narr++ = normal.normalized();
    1.71 +			*tarr++ = tang.normalized();
    1.72 +			*uvarr++ = Vector2(u, v);
    1.73 +
    1.74 +			if(i < usub && j < vsub) {
    1.75 +				int idx = i * vverts + j;
    1.76 +
    1.77 +				*idxarr++ = idx;
    1.78 +				*idxarr++ = idx + vverts + 1;
    1.79 +				*idxarr++ = idx + 1;
    1.80 +
    1.81 +				*idxarr++ = idx;
    1.82 +				*idxarr++ = idx + vverts;
    1.83 +				*idxarr++ = idx + vverts + 1;
    1.84 +			}
    1.85 +
    1.86 +			v += dv;
    1.87 +		}
    1.88 +		u += du;
    1.89 +	}
    1.90 +}