Exemplo n.º 1
0
void create_frustum_m4(float R[16], float l, float r, float b, float t, float n, float f)
{
	float M[16] = {	(2.0*n)/(r-f), 0.0, (r+l)/(r-l), 0.0,
			0.0, (2.0*n)/(t-b), (t+b)/(t-b), 0.0,
			0.0, 0.0, -(f+n)/(f-n), (-2.0*f*n)/(f-n),
			0.0, 0.0, -1.0, 0.0};
	transpose_m4(M);
	copy_m4_m4(R, M);
}
Exemplo n.º 2
0
void create_ortho_m4(float R[16], float l, float r, float b, float t, float n, float f)
{
	float M[16] = {	2.0/(r-l), 0.0, 0.0, -((r+l)/(r-l)),
			0.0, 2.0/(t-b), 0.0, -((t+b)/(t-b)),
			0.0, 0.0, -2.0/(f-n), -((f+n)/(f-n)),
			0.0, 0.0, 0.0, 1.0};
	transpose_m4(M);
	copy_m4_m4(R, M);
}
Exemplo n.º 3
0
void create_scale_m4(float R[16], float x, float y, float z)
{
	float M[16] = {	x, 0.0, 0.0, 0.0,
			0.0, y, 0.0, 0.0,
			0.0, 0.0, z, 0.0,
			0.0, 0.0, 0.0, 1.0};
	transpose_m4(M);
	copy_m4_m4(R, M);
}
Exemplo n.º 4
0
void create_perspective_m4(float R[16], float fov, float aspect, float near, float far)
{
	float f = 1.0/tan((fov*(M_PI/180.0))/2.0);
	float M[16] = {	f/aspect, 0.0, 0.0, 0.0,
			0.0, f, 0.0, 0.0,
			0.0, 0.0, (near+far)/(near-far), (2.0*near*far)/(near-far),
			0.0, 0.0, -1.0, 0.0};
	transpose_m4(M);
	copy_m4_m4(R, M);
}
Exemplo n.º 5
0
void create_rotate_m4(float R[16], float a, float x, float y, float z)
{
	float f = 1.0/sqrtf(x*x+y*y+z*z);
	x *= f; y *= f; z *= f;
	a *= M_PI/180.0;
	float c = cosf(a);
	float s = sinf(a);
	float M[16] = {	x*x*(1-c)+c,	x*y*(1-c)-z*s,	x*z*(1-c)+y*s,	0.0,
			y*x*(1-c)+z*s,	y*y*(1-c)+c,	y*z*(1-c)-x*s,	0.0,
			z*x*(1-c)-y*s,	z*y*(1-c)+x*s,	z*z*(1-c)+c,	0.0,
			0.0,		0.0,		0.0,		1.0};
	transpose_m4(M);
	copy_m4_m4(R, M);
}
Exemplo n.º 6
0
static void camera_frame_fit_data_init(
        const Scene *scene, const Object *ob,
        CameraParams *params, CameraViewFrameData *data)
{
	float camera_rotmat_transposed_inversed[4][4];
	unsigned int i;

	/* setup parameters */
	BKE_camera_params_init(params);
	BKE_camera_params_from_object(params, ob);

	/* compute matrix, viewplane, .. */
	if (scene) {
		BKE_camera_params_compute_viewplane(params, scene->r.xsch, scene->r.ysch, scene->r.xasp, scene->r.yasp);
	}
	else {
		BKE_camera_params_compute_viewplane(params, 1, 1, 1.0f, 1.0f);
	}
	BKE_camera_params_compute_matrix(params);

	/* initialize callback data */
	copy_m3_m4(data->camera_rotmat, (float (*)[4])ob->obmat);
	normalize_m3(data->camera_rotmat);
	/* To transform a plane which is in its homogeneous representation (4d vector),
	 * we need the inverse of the transpose of the transform matrix... */
	copy_m4_m3(camera_rotmat_transposed_inversed, data->camera_rotmat);
	transpose_m4(camera_rotmat_transposed_inversed);
	invert_m4(camera_rotmat_transposed_inversed);

	/* Extract frustum planes from projection matrix. */
	planes_from_projmat(params->winmat,
	                    /*   left              right                 top              bottom        near  far */
	                    data->plane_tx[2], data->plane_tx[0], data->plane_tx[3], data->plane_tx[1], NULL, NULL);

	/* Rotate planes and get normals from them */
	for (i = 0; i < CAMERA_VIEWFRAME_NUM_PLANES; i++) {
		mul_m4_v4(camera_rotmat_transposed_inversed, data->plane_tx[i]);
		normalize_v3_v3(data->normal_tx[i], data->plane_tx[i]);
	}

	copy_v4_fl(data->dist_vals_sq, FLT_MAX);
	data->tot = 0;
	data->is_ortho = params->is_ortho;
	if (params->is_ortho) {
		/* we want (0, 0, -1) transformed by camera_rotmat, this is a quicker shortcut. */
		negate_v3_v3(data->camera_no, data->camera_rotmat[2]);
		data->dist_to_cam = FLT_MAX;
	}
}
Exemplo n.º 7
0
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;
	}
}
Exemplo n.º 8
0
void UnitConverter::mat4_to_dae(float out[][4], float in[][4])
{
	copy_m4_m4(out, in);
	transpose_m4(out);
}