ld33_umonster

diff src/mesh.cc @ 5:1e8d90aeae34

added Mesh::texcoord_gen_cylinder()
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 23 Aug 2015 04:45:24 +0300
parents 35349df5392d
children
line diff
     1.1 --- a/src/mesh.cc	Sun Aug 23 04:31:29 2015 +0300
     1.2 +++ b/src/mesh.cc	Sun Aug 23 04:45:24 2015 +0300
     1.3 @@ -982,6 +982,29 @@
     1.4  	}
     1.5  }
     1.6  
     1.7 +void Mesh::texcoord_gen_cylinder()
     1.8 +{
     1.9 +	if(!nverts) return;
    1.10 +
    1.11 +	if(!has_attrib(MESH_ATTR_TEXCOORD)) {
    1.12 +		// allocate texture coordinate attribute array
    1.13 +		set_attrib_data(MESH_ATTR_TEXCOORD, 2, nverts);
    1.14 +	}
    1.15 +
    1.16 +	for(unsigned int i=0; i<nverts; i++) {
    1.17 +		Vector3 pos = get_attrib(MESH_ATTR_VERTEX, i);
    1.18 +
    1.19 +		float rho = sqrt(pos.x * pos.x + pos.z * pos.z);
    1.20 +		float theta = rho == 0.0 ? 0.0 : atan2(pos.z / rho, pos.x / rho);
    1.21 +
    1.22 +		float u = theta / (2.0 * M_PI) + 0.5;
    1.23 +		float v = pos.y;
    1.24 +
    1.25 +		set_attrib(MESH_ATTR_TEXCOORD, i, Vector4(u, v, 0, 1));
    1.26 +	}
    1.27 +}
    1.28 +
    1.29 +
    1.30  void Mesh::dump(FILE *fp) const
    1.31  {
    1.32  	if(!has_attrib(MESH_ATTR_VERTEX)) {