tavli
diff src/mesh.cc @ 6:a0d30f6f20d4
- texture coordinate generation in class Mesh
- wood texture
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Fri, 26 Jun 2015 04:22:06 +0300 |
parents | 3fcd7b4d631f |
children | f1ecc2439802 |
line diff
1.1 --- a/src/mesh.cc Thu Jun 25 20:43:34 2015 +0300 1.2 +++ b/src/mesh.cc Fri Jun 26 04:22:06 2015 +0300 1.3 @@ -931,6 +931,72 @@ 1.4 } 1.5 1.6 1.7 +// texture coordinate manipulation 1.8 +void Mesh::texcoord_apply_xform(const Matrix4x4 &xform) 1.9 +{ 1.10 + if(!has_attrib(MESH_ATTR_TEXCOORD)) { 1.11 + return; 1.12 + } 1.13 + 1.14 + for(unsigned int i=0; i<nverts; i++) { 1.15 + Vector4 tc = get_attrib(MESH_ATTR_TEXCOORD, i); 1.16 + set_attrib(MESH_ATTR_TEXCOORD, i, tc.transformed(xform)); 1.17 + } 1.18 +} 1.19 + 1.20 +void Mesh::texcoord_gen_plane(const Vector3 &norm, const Vector3 &tang) 1.21 +{ 1.22 + if(!nverts) return; 1.23 + 1.24 + if(!has_attrib(MESH_ATTR_TEXCOORD)) { 1.25 + // allocate texture coordinate attribute array 1.26 + set_attrib_data(MESH_ATTR_TEXCOORD, 2, nverts); 1.27 + } 1.28 + 1.29 + Vector3 n = norm.normalized(); 1.30 + Vector3 b = cross_product(n, tang).normalized(); 1.31 + Vector3 t = cross_product(b, n); 1.32 + 1.33 + for(unsigned int i=0; i<nverts; i++) { 1.34 + Vector3 pos = get_attrib(MESH_ATTR_VERTEX, i); 1.35 + 1.36 + // distance along the tangent direction 1.37 + float u = dot_product(pos, t); 1.38 + // distance along the bitangent direction 1.39 + float v = dot_product(pos, b); 1.40 + 1.41 + set_attrib(MESH_ATTR_TEXCOORD, i, Vector4(u, v, 0, 1)); 1.42 + } 1.43 +} 1.44 + 1.45 +void Mesh::texcoord_gen_box() 1.46 +{ 1.47 + if(!nverts || !has_attrib(MESH_ATTR_NORMAL)) return; 1.48 + 1.49 + if(!has_attrib(MESH_ATTR_TEXCOORD)) { 1.50 + // allocate texture coordinate attribute array 1.51 + set_attrib_data(MESH_ATTR_TEXCOORD, 2, nverts); 1.52 + } 1.53 + 1.54 + for(unsigned int i=0; i<nverts; i++) { 1.55 + Vector3 pos = Vector3(get_attrib(MESH_ATTR_VERTEX, i)) * 0.5 + Vector3(0.5, 0.5, 0.5); 1.56 + Vector3 norm = get_attrib(MESH_ATTR_NORMAL, i); 1.57 + 1.58 + float abs_nx = fabs(norm.x); 1.59 + float abs_ny = fabs(norm.y); 1.60 + float abs_nz = fabs(norm.z); 1.61 + int dom = abs_nx > abs_ny && abs_nx > abs_nz ? 0 : (abs_ny > abs_nz ? 1 : 2); 1.62 + 1.63 + float uv[2], *uvptr = uv; 1.64 + for(int j=0; j<3; j++) { 1.65 + if(j == dom) continue; // skip dominant axis 1.66 + 1.67 + *uvptr++ = pos[j]; 1.68 + } 1.69 + set_attrib(MESH_ATTR_TEXCOORD, i, Vector4(uv[0], uv[1], 0, 1)); 1.70 + } 1.71 +} 1.72 + 1.73 // ------ private member functions ------ 1.74 1.75 void Mesh::calc_aabb()