CS_Assignment 4: Shadows, Reflection & Refraction

来源:互联网 发布:百度云盘mac破解版 编辑:程序博客网 时间:2024/04/29 21:13

Assignment 4: Shadows, Reflection & Refraction

 main 函数中除了调用raytracer外,其余不变:

信息隐藏 

·   Expand your Material classes to store the reflective & transparent multipliers and the index of refraction, which are necessary for recursive ray tracing. The prototype for the PhongMaterial constructor should be:

·           PhongMaterial::PhongMaterial(const Vec3f &diffuseColor,

·                                        const Vec3f &specularColor,

·                                        float exponent,

·                                        const Vec3f &reflectiveColor,

·                                        const Vec3f &transparentColor,

·                                        float indexOfRefraction);

You should also implement appropriate accessor functions for these fields.

按照给出的构造函数,定义类的私有变量,并且为每个变量提供一个GET接口函数。

信息隐藏


 

PHONG明暗处理

...........

 

实现后的函数:

信息隐藏

Add support for the new command line arguments: -shadows, which indicates that shadow rays are to be cast, and-bounces and-weight, which control the depth of recursion in your ray tracer.

对上一个版本的程序做了修改,写了ArgParser函数,对参数接受这个过程作了一个统一的封装。当然可以接收shadows 、Bounces、weight等传进来的参数。

The main method of this class is traceRay that, given a ray, computes the color seen from the origin along the direction. This computation is recursive for reflected or transparent materials. We therefore need a stopping criterion to prevent infinite recursion. traceRay takes as additional parameters the current number of bounces (recursion depth) and a ray weight that indicates the percent contribution of this ray to the final pixel color. The corresponding maximum recursion depth and the cutoff ray weight are fields of RayTracer, which are passed as command line arguments to the program. Note that weight is a scalar that corresponds to the magnitude of the color vector.

  Vec3f traceRay(Ray &ray, float tmin, int bounces, float weight,                  float indexOfRefraction, Hit &hit) const;

当然最主要的就是光线跟踪函数的编写了。

阴影效果:

为了计算阴影,需要在算法中判断点是否在阴影中,对于场景中的每个光源都创建一条阴影光线,再判断对场景中的所有物体是否相交,若相交,则说明物体在阴影中而光源是不可见的;否则,说明物体不再阴影中,光线可以照射到该点;简单起见equivalently set tmin asepsilon

hit2.getT()==distanceTolight (光源无遮挡)

信息隐藏 

 

 

 

反射效果:

镜面反射:

信息隐藏


 

折射效果:

简化起见,题目假设光线是在真空中传播,这样的话,可以根据

ray.getDirection().Dot3(_hit.getNormal())的乘积结果为正或者负,判断

是从物体外向里折射(乘积为负数)还是从物体内折射出简化起见,取真空中折射率

 

信息隐藏


最后的Gui调试界面木有实现,但是应用前面的实习知识,用诸多三角形把图形拼出来,应该可以实现。

 

原创粉丝点击