补充下列OpenGL用与判断是否被遮挡的代码Vector3f RayColor(const Ray& ray, Scene& scene, int depth = 0, bool test = false) { HitInfo closest_hp; closest_hp.t = FLT_MAX; closest_hp.objIdx = -1; //光线和球求交 for (int i = 0; i < scene.ObjectCount(); ++i) { HitInfo ht; bool bhit = scene.GetObjectPtr(i)->Hit(ray, ht); if (bhit) { if (ht.t > 0 && ht.t < closest_hp.t) { closest_hp = ht; closest_hp.objIdx = i; } } } //这里图省事,直接把光照参数写在这边 Vector3f lightpos(0.0, 4, 2); Vector3f lightAmbient(0.6, 0.6, 0.6); Vector3f lightDiffuse(1.0, 1.0, 1.0); Vector3f lightSpecular(1.0, 1.0, 1.0); if (closest_hp.objIdx != -1) { int idx = closest_hp.objIdx; Material mtl = scene._scene[idx].second; //环境光 Vector3f ambient = Vector3f(lightAmbient[0] * mtl._Ka[0], lightAmbient[1] * mtl._Ka[1], lightAmbient[2] * mtl._Ka[2]); Vector3f color = ambient; bool isShadow = false; //shadow ray 求交 Ray shadow_ray(closest_hp.position, lightpos - closest_hp.position); //请在以下部分加入对shadow ray是否被场景遮挡的判断,并对isShadow这个变量进行修改 //请在以上部分加入对shadow ray是否被场景遮挡的判断,并对isShadow这个变量进行修改 Vector3f eyedir = (Vector3f(0, 0, 0) - closest_hp.position).normalized(); if (!isShadow) //如果不是阴影,继续计算
时间: 2024-04-06 20:29:03 浏览: 15
diffuse和specular光照强度,最终得到光线的颜色。
对于shadow ray是否被场景遮挡的判断,可以在for循环中对每个物体与光线求交,如果有任何一个物体与光线相交且距离小于光线和光源之间的距离,那么就说明光线被该物体遮挡了,isShadow变量设为true。代码如下:
bool isShadow = false;
for (int i = 0; i < scene.ObjectCount(); ++i)
{
if (i == closest_hp.objIdx) continue; //跳过当前物体
HitInfo ht;
bool bhit = scene.GetObjectPtr(i)->Hit(shadow_ray, ht);
if (bhit)
{
float dist = (ht.position - closest_hp.position).norm();
if (dist < (lightpos - closest_hp.position).norm())
{
isShadow = true;
break;
}
}
}
如果isShadow为false,则说明该点没有被遮挡,可以继续计算diffuse和specular光照强度,最终得到光线的颜色。
相关问题
Vector3f RayColor(const Ray& ray, Scene& scene, int depth=0, bool test=false){ HitInfo closest_hp; closest_hp.t = FLT_MAX; closest_hp.objIdx = -1; //光线和球求交 for (int i = 0; i < scene.ObjectCount(); ++i) { HitInfo ht; bool bhit = scene.GetObjectPtr(i)->Hit(ray, ht); if (bhit) { if (ht.t > 0 && ht.t<closest_hp.t) { closest_hp = ht; closest_hp.objIdx = i; } } } //这里图省事,直接把光照参数写在这边 Vector3f lightpos(0.0, 4, 2); Vector3f lightAmbient(0.6, 0.6, 0.6); Vector3f lightDiffuse(1.0, 1.0, 1.0); Vector3f lightSpecular(1.0, 1.0, 1.0); if (closest_hp.objIdx != -1) { int idx = closest_hp.objIdx; Material mtl = scene._scene[idx].second; //环境光 Vector3f ambient = Vector3f(lightAmbient[0]* mtl._Ka[0], lightAmbient[1] * mtl._Ka[1], lightAmbient[2] * mtl._Ka[2]); Vector3f color = ambient; bool isShadow = false; //shadow ray 求交 Ray shadow_ray(closest_hp.position, lightpos - closest_hp.position); //请在以下部分加入对shadow ray是否被场景遮挡的判断,并对isShadow这个变量进行修改 //请在以上部分加入对shadow ray是否被场景遮挡的判断,并对isShadow这个变量进行修改 Vector3f eyedir = (Vector3f(0, 0, 0) - closest_hp.position).normalized(); if(!isShadow) //如果不是阴影,继续计算 { //漫反射 Vector3f lightdir = (lightpos - closest_hp.position).normalized(); float coscoef = lightdir.dot(closest_hp.normal); if (coscoef < 0) coscoef = 0; Vector3f diffuse = Vector3f(0.8* mtl._Kd[0] * coscoef, 0.8 * mtl._Kd[1] * coscoef, 0.8 * mtl._Kd[2] * coscoef); Vector3f half = (eyedir + shadow_ray.Direction().normalized()).normalized(); float specularcoef = half.dot(closest_hp.normal); if (specularcoef < 0) specularcoef = 0; else specularcoef = pow(specularcoef, mtl._shiness); Vector3f specular = Vector3f(lightSpecular[0]* mtl._Ks[0] * specularcoef, lightSpecular[1] * mtl._Ks[1] * specularcoef, lightSpecular[2] * mtl._Ks[2] * specularcoef); color += diffuse + specular; }漫反射和镜面反射递归函数具体代码是什么
以下是漫反射和镜面反射的递归函数代码:
```
if (depth >= max_depth) return color;
if (mtl._Kr.norm() == 0) {
//漫反射
Vector3f lightdir = (lightpos - closest_hp.position).normalized();
float coscoef = lightdir.dot(closest_hp.normal);
if (coscoef < 0) coscoef = 0;
Vector3f diffuse = Vector3f(mtl._Kd[0] * coscoef, mtl._Kd[1] * coscoef, mtl._Kd[2] * coscoef);
color += diffuse;
} else {
//镜面反射
Vector3f eyedir = (Vector3f(0, 0, 0) - closest_hp.position).normalized();
Vector3f reflect_dir = (eyedir - 2 * closest_hp.normal.dot(eyedir) * closest_hp.normal).normalized();
Ray reflect_ray(closest_hp.position, reflect_dir);
HitInfo reflect_hp;
bool bhit_reflect = scene.Intersect(reflect_ray, reflect_hp);
if (bhit_reflect) {
Material mtl_reflect = scene._scene[reflect_hp.objIdx].second;
color += mtl._Kr.cwiseProduct(RayColor(reflect_ray, scene, depth + 1));
} else {
color += mtl._Kr.cwiseProduct(Vector3f(0, 0, 0));
}
}
```
其中,`max_depth` 为递归深度的最大值,`mtl._Kr` 表示材质的镜面反射系数,若其为 0 则进行漫反射计算,否则进行镜面反射计算。在进行镜面反射计算时,需要计算反射光线,并进行递归计算反射光线的颜色,并将其与当前颜色相加。
const int &rval=rval1;
这段代码是将rval引用初始化为rval1的常量引用。这意味着rval将永远引用rval1,并且不能通过rval更改rval1的值。这是因为rval是一个常量引用,它只能读取rval1的值,而不能修改它。以下是一个示例代码:
```cpp
#include <iostream>
using namespace std;
int main() {
int i = 42;
const int &rval = i;
cout << "rval = " << rval << endl; // 输出:rval = 42
i = 0;
cout << "rval = " << rval << endl; // 输出:rval = 0
return 0;
}
```