static void envmap_transmatrix(float mat[4][4], int part) { float tmat[4][4], eul[3], rotmat[4][4]; eul[0] = eul[1] = eul[2] = 0.0; if (part == 0) { /* neg z */ /* pass */ } else if (part == 1) { /* pos z */ eul[0] = M_PI; } else if (part == 2) { /* pos y */ eul[0] = M_PI / 2.0; } else if (part == 3) { /* neg x */ eul[0] = M_PI / 2.0; eul[2] = M_PI / 2.0; } else if (part == 4) { /* neg y */ eul[0] = M_PI / 2.0; eul[2] = M_PI; } else { /* pos x */ eul[0] = M_PI / 2.0; eul[2] = -M_PI / 2.0; } copy_m4_m4(tmat, mat); eul_to_mat4(rotmat, eul); mul_m4_m4m4(mat, tmat, rotmat); }
void init_tex_mapping(TexMapping *texmap) { float smat[4][4], rmat[4][4], tmat[4][4], proj[4][4], size[3]; if (texmap->projx == PROJ_X && texmap->projy == PROJ_Y && texmap->projz == PROJ_Z && is_zero_v3(texmap->loc) && is_zero_v3(texmap->rot) && is_one_v3(texmap->size)) { unit_m4(texmap->mat); texmap->flag |= TEXMAP_UNIT_MATRIX; } else { /* axis projection */ zero_m4(proj); proj[3][3] = 1.0f; if (texmap->projx != PROJ_N) proj[texmap->projx - 1][0] = 1.0f; if (texmap->projy != PROJ_N) proj[texmap->projy - 1][1] = 1.0f; if (texmap->projz != PROJ_N) proj[texmap->projz - 1][2] = 1.0f; /* scale */ copy_v3_v3(size, texmap->size); if (ELEM(texmap->type, TEXMAP_TYPE_TEXTURE, TEXMAP_TYPE_NORMAL)) { /* keep matrix invertible */ if (fabsf(size[0]) < 1e-5f) size[0] = signf(size[0]) * 1e-5f; if (fabsf(size[1]) < 1e-5f) size[1] = signf(size[1]) * 1e-5f; if (fabsf(size[2]) < 1e-5f) size[2] = signf(size[2]) * 1e-5f; } size_to_mat4(smat, texmap->size); /* rotation */ eul_to_mat4(rmat, texmap->rot); /* translation */ unit_m4(tmat); copy_v3_v3(tmat[3], texmap->loc); if (texmap->type == TEXMAP_TYPE_TEXTURE) { /* to transform a texture, the inverse transform needs * to be applied to the texture coordinate */ mul_serie_m4(texmap->mat, tmat, rmat, smat, 0, 0, 0, 0, 0); invert_m4(texmap->mat); } else if (texmap->type == TEXMAP_TYPE_POINT) { /* forward transform */ mul_serie_m4(texmap->mat, tmat, rmat, smat, 0, 0, 0, 0, 0); } else if (texmap->type == TEXMAP_TYPE_VECTOR) { /* no translation for vectors */ mul_m4_m4m4(texmap->mat, rmat, smat); } else if (texmap->type == TEXMAP_TYPE_NORMAL) { /* no translation for normals, and inverse transpose */ mul_m4_m4m4(texmap->mat, rmat, smat); invert_m4(texmap->mat); transpose_m4(texmap->mat); } /* projection last */ mul_m4_m4m4(texmap->mat, texmap->mat, proj); texmap->flag &= ~TEXMAP_UNIT_MATRIX; } }