【发布时间】:2017-02-01 21:15:13
【问题描述】:
我在这里的第一篇文章。 :) 我目前正在用 C 为我的学校项目编写光线追踪器。我已经可以显示具有一些光效的球体、三角形和平面。现在我想显示圆柱体(然后是圆锥体,但首先是圆柱体!)。 我选择有一个平行于 Y 轴的圆柱体。所以我必须解决: x² + z² = r²。所以从技术上讲,我返回相机和交点之间距离的函数是:
double n_ray_cylinder(t_ray *ray, t_cylinder *cylinder, t_data *d)
{
double a;
double b;
double c;
double delta;
double root;
a = ray->dir->x * ray->dir->x + ray->dir->z * ray->dir->z;
b = 2 * ray->dir->x * (ray->ori->x - cylinder->base->x) + 2 * ray->dir->z * (ray->ori->z - cylinder->base->z);
c = (ray->ori->x - cylinder->base->x) * (ray->ori->x - cylinder->base->x) + (ray->ori->z - cylinder->base->z) * (ray->ori->z - cylinder->base->z) - cylinder->radius * cylinder->radius;
delta = b * b - 4 * a * c;
if (delta > ACC)
{
root = (-1 * b - sqrt(delta)) / 2 * a - ACC;
if (root <= ACC)
root = (-1 * b + sqrt(delta)) / 2 * a - ACC;
return (root);
}
return (-1);
}
其中 x = (ray->ori->x - 圆柱体->base->x) et z = (ray->ori->z - 圆柱体->base->z) et r = 圆柱体->radius .
而我计算法线向量的函数是:
t_vect *cylinder_normal_at(t_cylinder *cylinder, t_vect *intersection)
{
t_vect *base_tmp;
t_vect *normal;
t_vect *intersection_tmp;
base_tmp = copy_vector(cylinder->base);
intersection_tmp = copy_vector(intersection);
base_tmp->y = intersection_tmp->y;
normal = init_vector(intersection->x - base_tmp->x, intersection->y - base_tmp->y, intersection-> z - base_tmp->z);
normalize_vector(normal);
return (normal);
}
使用这些函数,结果是:Image 1
反光“看起来”不错,形状也不错,但光线不好。正常问题? 我和我的一个朋友谈过这个,他告诉我像他一样:在我的第一个函数中删除一些数字。所以它变成了:
double n_ray_cylinder(t_ray *ray, t_cylinder *cylinder, t_data *d)
{
double a;
double b;
double c;
double delta;
double root;
a = ray->dir->x * ray->dir->x + ray->dir->z * ray->dir->z;
b = ray->dir->x * (ray->ori->x - cylinder->base->x) +
ray->dir->z * (ray->ori->z - cylinder->base->z);
c = (ray->ori->x - cylinder->base->x) * (ray->ori->x - cylinder->base->x) +
(ray->ori->z - cylinder->base->z) * (ray->ori->z - cylinder->base->z) -
cylinder->radius * cylinder->radius;
delta = b * b - a * c;
if (delta > ACC)
{
root = (-1 * b - sqrt(delta)) / a - ACC;
if (root <= ACC)
root = (-1 * b + sqrt(delta)) / a - ACC;
return (root);
}
return (-1);
}
正常功能保持不变。然后灯就ok了!
第二个功能似乎工作正常。但为什么 ?第一个不是圆柱方程的确切“应用”吗?
这是第二个问题。我想围绕 Ox 轴旋转我的圆柱体。 x²+(y.cosθ+z.sinθ)² = r²。
开发方程后,我有了这个函数(基于我朋友的第二个函数)
double nn_ray_cylinder(t_ray *ray, t_cylinder *cylinder, t_data *d)
{
double a;
double b;
double c;
double delta;
double root;
double deg = M_PI * 45 / 180;
a = ray->dir->x * ray->dir->x + cos(deg) * cos(deg) * ray->dir->z * ray->dir->z +
2 * ray->dir->z * cos(deg) * ray->dir->y * sin(deg) + ray->dir->y * ray->dir->y *
sin(deg) * sin(deg);
b = (ray->ori->x - cylinder->base->x) * ray->dir->x + cos(deg) * cos(deg) * (ray->ori->z - cylinder->base->z) * ray->dir->z +
2 * (ray->ori->z - cylinder->base->z) * cos(deg) * ray->dir->y * sin(deg) + 2 * ray->dir->z * cos(deg) *
(ray->ori->y - cylinder->base->y) * sin(deg) + 2 * (ray->ori->y - cylinder->base->y) * ray->dir->y * sin(deg) * sin(deg);
c = (ray->ori->x - cylinder->base->x) * (ray->ori->x - cylinder->base->x) + (ray->ori->z - cylinder->base->z) * (ray->ori->z - cylinder->base->z)* cos(deg) * cos(deg) +
2 * (ray->ori->z - cylinder->base->z) * cos(deg) * (ray->ori->y - cylinder->base->y) * sin(deg) + (ray->ori->y - cylinder->base->y) * (ray->ori->y - cylinder->base->y) *
sin(deg) * sin(deg) - cylinder->radius * cylinder->radius;
delta = b * b - a * c;
if (delta > ACC)
{
root = (-1 * b - sqrt(delta)) / a - ACC;
if (root <= ACC)
root = (-1 * b + sqrt(delta)) / a - ACC;
return (root);
}
return (-1);
}
轮换成功!但是现在我必须改变这个交点上的法线向量。为此,我对交点应用反向旋转,然后像以前一样计算法向量,然后对点重新应用旋转以找到旋转圆柱体的法向量。
t_vect *cylinder_normal_at(t_cylinder *cylinder, t_vect *intersection)
{
t_vect *base_tmp;
t_vect *normal;
t_vect *intersection_tmp;
t_matrix *rotate = init_rotation_matrix(45, 1, 0, 0);
t_matrix *rotate_inverted = init_rotation_matrix(-45, 1, 0, 0);
base_tmp = copy_vector(cylinder->base);
intersection_tmp = copy_vector(intersection);
apply_matrix(rotate_inverted, intersection_tmp);
base_tmp->y = intersection_tmp->y;
apply_matrix(rotate, intersection_tmp);
apply_matrix(rotate, base_tmp);
normal = init_vector(intersection->x - base_tmp->x,
intersection->y - base_tmp->y,
intersection->z - base_tmp->z);
normalize_vector(normal);
return (normal);
}
例如,对于基本圆柱体和旋转 90 度的圆柱体,结果似乎很好。 但是 45 度的反射是完全错误的......
那么我的错误在哪里?是正常功能错了,还是另一个?为什么? 非常感谢那些可以帮助我的人。我沉浸在数学中……
编辑:
感谢 chux,二次错误编码现已更正。 关于法线向量的问题还在。
在这里我添加一些我的旋转功能:
t_matrix *init_rotation_matrix(double theta, double x, double y, double z)
{
t_matrix *matrix;
double rad;
if ((matrix = malloc(sizeof *matrix)) == NULL)
return (NULL);
rad = theta * M_PI / 180;
matrix->m11 = x * x * (1 - cos(rad)) + cos(rad);
matrix->m12 = x * y * (1 - cos(rad)) - z * sin(rad);
matrix->m13 = x * z * (1 - cos(rad)) + y * sin(rad);
matrix->m14 = 0;
matrix->m21 = y * x * (1 - cos(rad)) + z * sin(rad);
matrix->m22 = y * y * (1 - cos(rad)) + cos(rad);
matrix->m23 = y * z * (1 - cos(rad)) - x * sin(rad);
matrix->m24 = 0;
matrix->m31 = x * z * (1 - cos(rad)) - y * sin(rad);
matrix->m32 = y * z * (1 - cos(rad)) + x * sin(rad);
matrix->m33 = z * z * (1 - cos(rad)) + cos(rad);
matrix->m34 = 0;
matrix->m41 = 0;
matrix->m42 = 0;
matrix->m43 = 0;
matrix->m44 = 1;
return (matrix);
}
void apply_matrix(t_matrix *matrix, t_vect *v)
{
double x;
double y;
double z;
x = v->x;
y = v->y;
z = v->z;
v->x = matrix->m11 * x + matrix->m12 * y + matrix->m13 * z + matrix->m14;
v->y = matrix->m21 * x + matrix->m22 * y + matrix->m23 * z + matrix->m24;
v->z = matrix->m31 * x + matrix->m32 * y + matrix->m33 * z + matrix->m34;
}
编辑 2:
在计算法线向量的函数中,我将 45 替换为 -45 并将 45 替换为 -45。现在它可以工作了……我的 z 轴一定有问题。好像正z和负z倒过来了……
【问题讨论】:
-
也许
(-1 * b - sqrt(delta)) / 2 * a-->(-1 * b - sqrt(delta)) / (2 * a)?看起来像二次方程的错误编码。你不是吗? -
喂。 :) 谢谢 !!但是 45 度的反射仍然是错误的......
-
对于旋转部分,法线向量的变换与切线向量不同。从技术上讲,它算作伪向量或轴向向量。 (en.wikipedia.org/wiki/Pseudovector)。构造法线的一种安全方法是将两个切向量应用于曲面,对它们应用变换,然后使用叉积来构造新法线。更好的方法是使用[逆矩阵的转置] (stackoverflow.com/questions/13654401/…)
-
听起来有点难……在我看来,我的想法还可以。它应该工作。我需要一个例子,所以我明白为什么它不起作用......
-
小想法:
t_matrix *rotate = init_rotation_matrix(45, 1, 0, 0); ... base_tmp->y = intersection_tmp->y;--> 为什么旋转使用x单位向量,而后面的代码调整y?