本文整理汇总了C++中Intersection::didHit方法的典型用法代码示例。如果您正苦于以下问题:C++ Intersection::didHit方法的具体用法?C++ Intersection::didHit怎么用?C++ Intersection::didHit使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Intersection
的用法示例。
在下文中一共展示了Intersection::didHit方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: getClosestIntersection
/**
* Given a ray, find the first place it intersects the scene geometry.
* @param ray [description]
* @return
*/
Intersection getClosestIntersection(Ray ray, bool isShadowRay) {
Intersection closestInters = Intersection();
for(std::vector<Object>::iterator obj = scene.objects.begin(); obj != scene.objects.end(); ++obj) {
Intersection inters = obj->bvh.intersect(ray, 0.0f, 800.0f);
// for(std::vector<Triangle>::iterator tris = obj->triangles.begin(); tris != obj->triangles.end(); ++tris) {
// Intersection inters = tris->intersect(ray);
if( inters.didHit() ) {
if( inters.distanceTraveled < closestInters.distanceTraveled || !closestInters.didHit() ) {
closestInters = inters;
closestInters.object = &*obj;
}
}
// }
for(std::vector<Circle>::iterator circ = obj->circles.begin(); circ != obj->circles.end(); ++circ) {
Intersection inters = circ->intersect(ray);
if( inters.didHit() ) {
if( inters.distanceTraveled < closestInters.distanceTraveled || !closestInters.didHit() ) {
closestInters = inters;
closestInters.object = &*obj;
}
}
}
}
return closestInters;
}
示例2: trace
/**
* Trace a ray and return the color of the given ray.
* @param ray [description]
* @return
*/
glm::vec3 trace(Ray ray, float distanceTraveled, int maxDepth) {
glm::vec3 color = glm::vec3(0.0f);
Intersection inters = getClosestIntersection(ray, false);
if(inters.didHit()) { //if we hit something figure out the color.
color += inters.object->material.aColor; // Ambient lighting.
if(inters.object->material.opacity < 1.0f && maxDepth > 0) { // Transmitted light
float rRatio;
float cosTheta = glm::dot(inters.incident.direction, inters.normal);
if(inters.inside) {
rRatio = inters.object->material.refractiveIndex;
cosTheta *= -1.0f;
}
else {
rRatio = 1.0f/inters.object->material.refractiveIndex;
}
float antiCos = sqrtf(1.0f - (1.0f-cosTheta*cosTheta)*rRatio*rRatio);
// printf("refraction with ratio: %f, cos: %f, antiCos: %f\n", rRatio, cosTheta, antiCos);
glm::vec3 refractedDir = rRatio*inters.incident.direction + (cosTheta*rRatio + antiCos)*inters.normal;
color += trace(Ray(inters.point, refractedDir), inters.distanceTraveled+distanceTraveled, maxDepth-1)*(1.0f-inters.object->material.opacity);
}
if(inters.object->material.reflectivity > 0.0f && maxDepth > 0) { // Reflected light
glm::vec3 projOntoNorm = -glm::dot(inters.incident.direction, inters.normal)*inters.normal;
glm::vec3 reflectDir = inters.incident.direction + projOntoNorm*2.0f;
color += trace(Ray(inters.point, reflectDir), inters.distanceTraveled+distanceTraveled, maxDepth-1)*inters.object->material.rColor*inters.object->material.reflectivity;
}
if(!inters.inside) { // GLORIOUS IMPROVEMENTS, Diffuse light
for(std::vector<Light>::iterator lightIter = scene.lights.begin(); lightIter != scene.lights.end(); ++lightIter) {
glm::vec3 lightDir = lightIter->location - inters.point;
float distanceToLight = glm::length(lightDir);
lightDir = glm::normalize(lightDir);
Ray shadowRay = Ray(inters.point, lightDir);
Intersection shadowIntersection = getClosestIntersection(shadowRay, true);
if((!shadowIntersection.didHit() || distanceToLight < shadowIntersection.distanceTraveled )) {// We hit something behind the light
float totalDistanceTraveled = distanceToLight + inters.distanceTraveled + distanceTraveled; // Distance from light to intersection + inters to eye + recursion(reflected/refracted)
float difIntensity = glm::dot(lightDir, inters.normal)*lightIter->power;
glm::vec3 halfAngle = glm::normalize(lightDir - inters.incident.direction); // incident is in the direction from eye, so negate
float NdotH = std::max(0.0f, glm::dot(inters.normal, halfAngle));
float specIntensity = powf(NdotH, inters.object->material.specHardness)*lightIter->power; // Spectral hardness of the material
color += (lightIter->color * inters.object->material.sColor)*specIntensity/powf(totalDistanceTraveled, 2);
color += (lightIter->color * inters.object->material.dColor)*difIntensity/powf(totalDistanceTraveled, 2);
}
}
}
}
return color;
}