- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的第一篇文章在这里。 :)我目前正在用 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 = (射线->ori->x - 圆柱体->基体->x) et z = (射线->ori->z - 圆柱体->基体->z) et r = 圆柱体->半径。
我计算法 vector 的函数是:
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
反射“看起来”不错,形状也不错,但光线不好。正常问题吗?我和我的一个 friend 谈论过这个问题,他告诉我像他一样:在我的第一个函数中删除一些数字。所以它变成:
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²。
建立方程后,我得到了这个函数(基于我 friend 的第二个函数)
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);
}
轮换成功!但现在我必须改变这个交点上的法线 vector 。为此,我对交点应用反向旋转,然后像以前一样计算法线 vector ,然后重新对该点应用旋转以找到旋转圆柱体的法线 vector 。
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,二次错误编码现已更正。法 vector 的问题依然存在。
这里我添加了一些旋转功能:
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:
在计算法线 vector 的函数中,我将 45 替换为 -45,将 45 替换为 -45。现在它可以工作了...我的 z 轴一定有问题。好像正z和负z颠倒了……
最佳答案
OP 似乎错误地编码了二次方程(-1 * b - sqrt(delta))/2 * a
而不是(-1 * b - sqrt(delta))/(2 * a)
。
建议使用辅助函数,因为该方程在代码中重复使用。
示例快速、未经测试的代码。
#include <assert.h>
#include <math.h>
int quadratic(double a, double b, double c, double x[2]) {
if (a == 0.0) return 0;
double d = b*b - 4*a*c;
if (d < 0.0) return -1;
d = sqrt(d);
x[0] = (-b + d) / (2 * a);
x[1] = (-b - d) / (2 * a);
return 2;
}
关于c - C 中的光线追踪 - 圆柱体、法向 vector 和旋转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39670452/
我想做的是让 JTextPane 在 JPanel 中占用尽可能多的空间。对于我使用的 UpdateInfoPanel: public class UpdateInfoPanel extends JP
我在 JPanel 中有一个 JTextArea,我想将其与 JScrollPane 一起使用。我正在使用 GridBagLayout。当我运行它时,框架似乎为 JScrollPane 腾出了空间,但
我想在 xcode 中实现以下功能。 我有一个 View Controller 。在这个 UIViewController 中,我有一个 UITabBar。它们下面是一个 UIView。将 UITab
有谁知道Firebird 2.5有没有类似于SQL中“STUFF”函数的功能? 我有一个包含父用户记录的表,另一个表包含与父相关的子用户记录。我希望能够提取用户拥有的“ROLES”的逗号分隔字符串,而
我想使用 JSON 作为 mirth channel 的输入和输出,例如详细信息保存在数据库中或创建 HL7 消息。 简而言之,输入为 JSON 解析它并输出为任何格式。 最佳答案 var objec
通常我会使用 R 并执行 merge.by,但这个文件似乎太大了,部门中的任何一台计算机都无法处理它! (任何从事遗传学工作的人的附加信息)本质上,插补似乎删除了 snp ID 的 rs 数字,我只剩
我有一个以前可能被问过的问题,但我很难找到正确的描述。我希望有人能帮助我。 在下面的代码中,我设置了varprice,我想添加javascript变量accu_id以通过rails在我的数据库中查找记
我有一个简单的 SVG 文件,在 Firefox 中可以正常查看 - 它的一些包装文本使用 foreignObject 包含一些 HTML - 文本包装在 div 中:
所以我正在为学校编写一个 Ruby 程序,如果某个值是 1 或 3,则将 bool 值更改为 true,如果是 0 或 2,则更改为 false。由于我有 Java 背景,所以我认为这段代码应该有效:
我做了什么: 我在这些账户之间创建了 VPC 对等连接 互联网网关也连接到每个 VPC 还配置了路由表(以允许来自双方的流量) 情况1: 当这两个 VPC 在同一个账户中时,我成功测试了从另一个 La
我有一个名为 contacts 的表: user_id contact_id 10294 10295 10294 10293 10293 10294 102
我正在使用 Magento 中的新模板。为避免重复代码,我想为每个产品预览使用相同的子模板。 特别是我做了这样一个展示: $products = Mage::getModel('catalog/pro
“for”是否总是检查协议(protocol)中定义的每个函数中第一个参数的类型? 编辑(改写): 当协议(protocol)方法只有一个参数时,根据该单个参数的类型(直接或任意)找到实现。当协议(p
我想从我的 PHP 代码中调用 JavaScript 函数。我通过使用以下方法实现了这一点: echo ' drawChart($id); '; 这工作正常,但我想从我的 PHP 代码中获取数据,我使
这个问题已经有答案了: Event binding on dynamically created elements? (23 个回答) 已关闭 5 年前。 我有一个动态表单,我想在其中附加一些其他 h
我正在尝试找到一种解决方案,以在 componentDidMount 中的映射项上使用 setState。 我正在使用 GraphQL连同 Gatsby返回许多 data 项目,但要求在特定的 pat
我在 ScrollView 中有一个 View 。只要用户按住该 View ,我想每 80 毫秒调用一次方法。这是我已经实现的: final Runnable vibrate = new Runnab
我用 jni 开发了一个 android 应用程序。我在 GetStringUTFChars 的 dvmDecodeIndirectRef 中得到了一个 dvmabort。我只中止了一次。 为什么会这
当我到达我的 Activity 时,我调用 FragmentPagerAdapter 来处理我的不同选项卡。在我的一个选项卡中,我想显示一个 RecyclerView,但他从未出现过,有了断点,我看到
当我按下 Activity 中的按钮时,会弹出一个 DialogFragment。在对话框 fragment 中,有一个看起来像普通 ListView 的 RecyclerView。 我想要的行为是当
我是一名优秀的程序员,十分优秀!