2023. 10. 11. 00:02ㆍRun/Computer Graphics
Ray Tracing #1 (번역) - https://serinyoon.tistory.com/41
[ 3. Adding Reflection and Refraction ]
Ray tracing의 장점은 ray propagation의 개념을 확장하여 reflection, refraction과 같은 효과를 쉽게 simulate 할 수 있다는 것이다.
Reflection, refraction은 glass material이나 mirror surface에서 유용하다.
An Improved Illumination Model for Shaded Display(1979) 에서 Turner Whitted는 Appel의 ray tracing 알고리즘을 확장하여 reflection과 refraction을 포함하였다.
Opctic(광학)에서 reflection, refraction은 잘 알려진 현상이다.
예시로 refractive 요소와 reflective 요소가 모두 있는 glass ball을 들어보자.
Ball을 intersect 하는 ray의 방향을 알면 그 다음에 어떤 현상이 일어나는지는 계산하기 쉽다.
Reflection, refraction 방향은 intersection point의 normal과 incoming ray(primary ray)의 방향에 의해 결정된다.
Refraction 방향을 계산하기 위해 material의 index of refraction(굴절률)을 추가로 정의해야 한다.
유리와 같은 물체는 reflective 함과 동시에 refractive 하다는 것을 유념해야 한다.
이 둘을 섞는 비율은 primary ray와 normal/index of refraction에 의해 결정된다.
이 둘을 섞는 공식으로 Fresnel equation이 있다.
만약 ray가 diffuse/opaque 하지 않은 object에 도달한 경우, 추가적인 계산을 해주어야 한다.
이 계산은 3가지 과정으로 구성되어 있다.
1) compute the reflection color
Intersection point에서의 normal과 primary ray의 방향이 필요하다.
Reflection direction이 구해지면 해당 방향에서 새로운 ray를 쏜다.
만약 그 ray가 다른 object에 도달하면, shadow ray를 light에 쏴 얼마나 많은 light가 그 point에 도달하는지 계산한다.
이를 통해 색상을 구할 수 있고 이후 light intensity만큼 곱해진다.
2) compute the refraction color
Intersection point에서의 normal, primary ray의 방향, refractive index이 필요하다.
Refraction direction이 구해지면 refraction ray는 object 내에서의 다른 쪽으로 계속 이동하고, 다시 한 번 굴절된다.
Refraction은 medium이 바뀔 때마다 발생한다.
Object에서 나온 reflacted ray가 다른 object에 도달한다고 하자.
이곳에서 다시 shadow ray를 통해 local illumination을 계산한다.
색상은 이후 light intensity만큼 곱해지고 처음 object에 적용된다.
3) apply the Fresnel equation
Refractive index, primary ray 사이의 각, intersection point에서의 normal이 필요하다.
공식을 적용하기 위해 dot prodcut(내적)을 사용한다.
pseudo code:
// compute reflection color
Color reflectionCol = computeReflectionColor();
// compute refraction color
Color refractionCol = computeRefractionColor();
float Kr; // reflection mix value
float Kt; // refraction mix value
fresnel(refractiveIndex, normalHit, primaryRayDirecton, &Kr, &Kt);
// mix
Color glassBallColorAtHit = Kr * reflectionColor + (1 - Kr) * refractionColor;
이 알고리즘의 장점은 recursive 하다는 것이다.
하지만 만약 reflective face밖에 없는 경우 ray가 무한으로 반사되어 이동하기 때문에 limit을 설정해야 한다.
[ 4. Writing a Basic Ray Tracer ]
pseudocode:
#define MAX_RAY_DEPTH 3
Color Trace(const Ray &ray, int depth) {
Object *object = NULL;
float minDist = INFINITY;
Point pHit;
Normal nHit;
for (int k = 0; k < objects.size(); ++k) {
if (intersect(objects[k], ray, &pHit, &nHit) {
float distance = Distance(ray.origin, pHit);
if (distance < minDistance) {
object = objects[k];
minDistance = distance;
}
}
}
if (object == NULL)
return 0;
// If the object material is glass, split the ray into a reflection & refraction ray
if (object->isGlass && depth < MAX_RAY_DEPTH) {
// Compute reflecton
Ray reflectionRay;
reflectionRay = computeReflectionRay(ray.direction, nHit);
// Recurse
Color reflectionColor = Trace(reflectionRay, depth + 1);
// Compute refraction
Ray refractionRay;
refractionRay = computeRefractionRay(object->indexOfRefraction, ray.direction, nHit);
// Recurse
Color refractionColor = Trace(refractionRay, depth + 1);
float Kr, Kt;
fresnel(object->indexOfRefraction, nHit, ray.direction, &Kr, &Kt);
return reflectionColor * Kr + refractionColor + (1 - Kr);
}
// If the object is a diffuse opaque object, compute illumination
Ray shadowRay;
shadowRay.direction = lightPosition - pHit;
bool isShadow = false;
for (int k = 0; k < objects.size(); ++k) {
if (intersect(objects[k], shadowRay)) {
// Hit point is in shadow
return 0;
}
}
// Point is illuminated
return object->color * light.brightness;
}
// For each pixel of the image
for (int j = 0; j < imageHeight; ++j) {
for (int i = 0; i < imageWidth; ++i) {
// Compute primary ray direction
Ray primRay;
computePrimRay(i, j, &primRay);
pixels[i][j] = Trace(primRay, 0);
}
}
에서 확인할 수 있다.
'Run > Computer Graphics' 카테고리의 다른 글
[Computer Graphics] Singularity (0) | 2023.10.11 |
---|---|
[Computer Graphics] Ray Tracing #1 (번역) (0) | 2023.10.11 |
[Computer Graphics] Mipmap (0) | 2023.10.10 |
[Computer Graphics] Magnification (Nearest neighbor sampling, Bilinear interpolation, Bicubic interpolation) (0) | 2023.10.10 |
[Computer Graphics] 오프라인 렌더링 & 실시간 렌더링 (1) | 2023.10.10 |