ld33_umonster

changeset 5:1e8d90aeae34

added Mesh::texcoord_gen_cylinder()
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 23 Aug 2015 04:45:24 +0300
parents 7b1fa527b51b
children 3b4460b34d43
files src/mesh.cc src/mesh.h
diffstat 2 files changed, 24 insertions(+), 0 deletions(-) [+]
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)) {
     2.1 --- a/src/mesh.h	Sun Aug 23 04:31:29 2015 +0300
     2.2 +++ b/src/mesh.h	Sun Aug 23 04:45:24 2015 +0300
     2.3 @@ -232,6 +232,7 @@
     2.4  	void texcoord_apply_xform(const Matrix4x4 &xform);
     2.5  	void texcoord_gen_plane(const Vector3 &norm, const Vector3 &tang);
     2.6  	void texcoord_gen_box();
     2.7 +	void texcoord_gen_cylinder();
     2.8  
     2.9  	void dump(FILE *fp) const;
    2.10  };