本文整理汇总了C++中Ray::direction方法的典型用法代码示例。如果您正苦于以下问题:C++ Ray::direction方法的具体用法?C++ Ray::direction怎么用?C++ Ray::direction使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Ray
的用法示例。
在下文中一共展示了Ray::direction方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: shadowHit
bool UVSphere::shadowHit(const Ray& r, precision tMin, precision tMax, precision time) const{
Vector3 temp = r.origin() - center;
double a = dot(r.direction(), r.direction());
double b = 2 * dot(r.direction(), temp);
double c = dot(temp, temp) - radius * radius;
double discriminant = b * b - 4 * a * c;
//First check to see if the ray intersects the sphere
if(discriminant > 0){
discriminant = sqrt(discriminant);
double t = (- b - discriminant) / (2 * a);
//Now check for a valid interval
if(t < tMin)
t = (- b + discriminant) / (2 * a);
if(t < tMin || t > tMax)
return false;
//We have a valid hit
return true;
}
return false;
}
示例2: if
Option<Vec2> sb::Intersection::get(const Ray& a, const Polygon& b) {
Option<Vec2> pt = nullptr;
Option<bool> inside = nullptr;
float t = std::numeric_limits<float>::max();
auto eCount = b.edgeCount();
for (ptrdiff_t i = 0; i < eCount; i++) {
const auto ed = b.edge(i);
auto r = Intersection::get(a, ed);
if (!r)
continue;
if (aeq(r.value(), ed.pointA())) {
if (!inside.hasValue())
inside = test(b, a.m_point);
if (!inside.value() && !(-a.direction()).isBetween(b.normal(i), b.normal(i - 1)))
continue;
}
else if (aeq(r.value(), ed.pointB())) {
if (!inside.hasValue())
inside = test(b, a.m_point);
if (!inside.value() && !(-a.direction()).isBetween(b.normal(i), b.normal(i + 1)))
continue;
}
auto u = a.computeT(r.value());
if (u < t) {
t = u;
pt = r;
}
}
return pt;
}
示例3: playSculpture
void App::playSculpture(const Ray& playRay) {
int maxDistance = 0;
int startIndex = g_sampleWindowIndex;
for (auto piece : m_sonicSculpturePieces) {
const shared_ptr<AudioSample>& sample = piece->getAudioSampleFromRay(playRay);
if (sample->buffer.size() > 0) {
maxDistance = max(maxDistance, (int)ceil(sample->buffer.size() / 512.0f));
Synthesizer::global->queueSound(sample);
m_lastInterestingEventTime = System::time();
}
}
if (maxDistance > 0) {
PlayPlane pp;
pp.direction = playRay.direction();
pp.origin = playRay.origin();
pp.beginWindowIndex = startIndex;
pp.endWindowIndex = startIndex + maxDistance;
Vector3 zAxis = -playRay.direction();
Vector3 xAxis;
if (abs(zAxis.dot(Vector3::unitY())) < 0.9f) {
xAxis = zAxis.unitCross(Vector3::unitY());
} else {
xAxis = zAxis.unitCross(Vector3::unitX());
}
Vector3 yAxis = zAxis.cross(xAxis);
pp.frame = CFrame(Matrix3::fromColumns(xAxis, yAxis, zAxis), pp.origin);
m_playPlanes.append(pp);
}
}
示例4: scatter
bool Metal::scatter(const Ray& r_in, const HitRecord& record, vec3& attenuation, Ray& scattered) const
{
vec3 reflected = reflect( glm::normalize(r_in.direction()), record.normal );
scattered = Ray(record.point, reflected + roughness * randomInUnitSphere());
attenuation = Color3(m_color);
return (glm::dot(scattered.direction(), record.normal) > 0);
}
示例5: scatter
bool Metal::scatter(const Ray& r_in, const HitRecord& record, vec3& attenuation, Ray& scattered) const
{
vec3 reflected = reflect( glm::normalize(r_in.direction()), record.normal );
scattered = Ray(record.point, reflected + roughness * random_in_unit_sphere());
attenuation = albedo;
return (dot(scattered.direction(), record.normal) > 0);
}
示例6: dot
Option<Vec2> Intersection::get(const Ray& a, const Circle& b) {
const auto f = a.point() - b.center();
float _a = dot(a.direction(), a.direction());
float _b = 2 * dot(f, a.direction());
float _c = dot(f, f) - b.radius() * b.radius();
float d = _b * _b - 4 * _a * _c;
if (d <= 0) //if d == 0, this means that the ray is tangent to the circle... we don't wanna this Intersection...
return nullptr;
else {
d = sqrtf(d);
float t1 = (-_b - d) / (2 * _a);
float t2 = (-_b + d) / (2 * _a);
if (t1 >= 0) {
if (t2 >= 0) {
if (t1 < t2)
return a.sampleAlongRay(t1);
else
return a.sampleAlongRay(t2);
}
else {
return a.sampleAlongRay(t1);
}
}
else {
if (t2 >= 0)
return a.sampleAlongRay(t2);
else
return nullptr;
}
}
}
示例7: traceOnePixel
void RayTracer::traceOnePixel(int x, int y, int threadID) {
//used for constructing viewport
Vector2 tmp(m_settings.width, m_settings.height);
Ray primaryRay;
// If one ray per pixel: (kinda debugging mode with blue color for places with no surfel hit
if (m_settings.raysPerPixel == 1){
//Get the primary ray from the pixel x,y
primaryRay = m_camera->worldRay(x + 0.5f, y + 0.5f, Rect2D(tmp));
//Get the first surfel hit.
//Can't call L_i unfortunately because we want the blue background for debugging
const shared_ptr<Surfel>& s = RayTracer::castRay(primaryRay, finf(), 0);
//If there is a surfel hit, get the direct illumination value and apply to the pixel
if (s){
//Call L_scatteredDirect to get direct illumination. Invert primaryRay to get the direction for incident light
m_image->set(Point2int32(x,y), L_o(s, -primaryRay.direction(), m_settings.recursiveBounces, *(m_rnd[threadID])));
} else{
//Set the pixels with no surfel hit. Include this line so we could make it a specific color for debug purposes.
m_image->set(Point2int32(x,y), Color3(0,0,1));
}
} else {
Radiance3 L(0,0,0);
//If more than one ray, randomly generate required number of rays within the pixel
for (int i = 0; i < m_settings.raysPerPixel; ++i){
primaryRay = m_camera->worldRay(x + m_rnd[threadID]->uniform(), y + m_rnd[threadID]->uniform(), Rect2D(tmp));
L += L_i(primaryRay.origin(), primaryRay.direction(), m_settings.recursiveBounces, *(m_rnd[threadID]));
}
m_image->set(Point2int32(x,y), L/m_settings.raysPerPixel);
}
}
示例8: intersect
bool Mesh::intersect(const Ray& ray, Intersection& j) const
{
bool intersected = false;
// Check if bounding ball has been intersected first
// If not then the mesh cannot have been intersected
Intersection k;
bool bball_intersected = m_boundingBall.intersect(ray, k);
if(bball_intersected)
{
// Loop through each face and check if there is an intersection
double prev_t = std::numeric_limits<double>::infinity();
for(auto face : m_faces)
{
// Compute the normal for the face
Point3D P0 = m_verts[face[0]];
Point3D P1 = m_verts[face[1]];
Point3D P2 = m_verts[face[2]];
Vector3D n = (P1-P0).cross(P2-P0).normalized();
// Now check if the ray intersects the polygon containing the face
// If denom is 0 then the ray does not intersect the plane at all
double denom = n.dot(ray.direction());
if(fabs(denom) < std::numeric_limits<double>::epsilon()) continue;
// If t is negative or a previous intersection has a smaller t (meaning it is closer to the
// ray's origin) then disregard this face and continue
double t = n.dot(P0 - ray.origin()) / denom;
if(t < 0 || prev_t < t) continue;
// Calculate intersection point
Point3D Q = ray.origin() + t*ray.direction();
bool outside = false;
for(size_t i = 0; i < face.size(); i++)
{
Point3D Q0 = (i == 0) ? m_verts[face.back()] : m_verts[face[i-1]];
Point3D Q1 = m_verts[face[i]];
if((Q1-Q0).cross(Q-Q0).dot(n) < 0)
{
outside = true;
break;
}
}
if(!outside)
{
// It is within the bounds of the polygon
intersected = true;
prev_t = t;
j.q = Q;
j.n = n;
}
}
}
return intersected;
}
示例9: intersect
bool UVSphere::intersect(const Ray& r, float tmin, float tmax, float time, HitRecord& record)const{
Vec temp = r.origin() - center;
float a = r.direction().dot(r.direction());
float b = 2 * r.direction().dot(temp);
float c = temp.dot(temp) - radius * radius;
float discriminant = b*b - 4*a*c;
if(discriminant > 0){
discriminant = sqrt(discriminant);
float t = (-b -discriminant) /(2*a);
if(t < tmin) t = (-b + discriminant) / (2*a);
if(t < tmin || t > tmax) return false;
record.t = t;
record.hit_p = r.origin() + r.direction() * t;
record.reflect = reflectionCoef;
record.transparency = refractionCoef;
Vec n = record.normal = normalize(r.origin() + r.direction()*t - center);
//calculate UV coordinates
float theta = acos(n.y);
float phi = atan2(n.z, n.x);
if(phi < 0) phi += M_PI * 2;
record.uv = Vec(phi/(2* M_PI), (M_PI - theta)/M_PI, 0);
record.hit_tex = tex;
return true;
}
return false;
}
示例10: traceNoDepthMod
Color Scene::traceNoDepthMod(Ray &ray, bool &hitSomething, Color &Oi) const
{
if (ray.traceDepth == maxTraceDetph)
{
hitSomething = false;
return Color(all_zero());
}
ray.traceDepth++;
const float prevMaxT = ray.maxT;
IntersectionInfo info;
const Geometry *nearestObj = bvhRoot->findClosest(ray, info);
if (!nearestObj)
{
hitSomething = false;
return Color(all_zero());
}
hitSomething = true;
// Object doesn't have shader, make it appear even for blind people!
if (!nearestObj->hasShader())
{
Oi = Color(sse::all_one);
return Color(1, 0, 1);
}
CompiledShader shader(nearestObj->getShader(), true);
shader.setCurrentDepth(ray.traceDepth);
shader.setRTVarValueByIndex(CompiledShader::Cs, info.Cs);
shader.setRTVarValueByIndex(CompiledShader::Os, info.Os);
shader.setRTVarValueByIndex(CompiledShader::P, Vector3(info.P));
shader.setRTVarValueByIndex(CompiledShader::N, info.N);
shader.setRTVarValueByIndex(CompiledShader::Ng, info.Ng);
shader.setRTVarValueByIndex(CompiledShader::s, info.s);
shader.setRTVarValueByIndex(CompiledShader::t, info.t);
shader.setRTVarValueByIndex(CompiledShader::I, Vector3((cam.WorldToCamN * ray.direction()).get128()));
shader.exec();
Color Ci, thisOi;
shader.getOutput(Ci, thisOi);
Oi += thisOi;
if (isOpaque(Oi))
return Ci;
ray.origin = info.P + (ray.direction() * 0.001f);
ray.maxT = prevMaxT - ray.maxT;
bool nextHit;
const Color nextColor = traceNoDepthMod(ray, nextHit, Oi);
if (!nextHit)
return Ci;
return Ci + mulPerElem((Vector3(1) - thisOi), nextColor);
}
示例11: find_intersection_for_components
Intersection<Plane> Plane::find_intersection(const Ray& r) const
{
if (_span == Span::XY)
return find_intersection_for_components(r.origin().z, r.direction().z);
if (_span == Span::XZ)
return find_intersection_for_components(r.origin().y, r.direction().y);
return find_intersection_for_components(r.origin().x, r.direction().x);
}
示例12: intersect
bool BVH::intersect(Ray &ray, IntersectionPtr isect)
{
if (nodes.empty())
return false;
bool hit = false;
Vector directionDivider;
directionDivider << 1.0f/ray.direction(0), 1.0f/ray.direction(1), 1.0f/ray.direction(2);
bool directionIsNegative[3] = {directionDivider(0) < 0,
directionDivider(1) < 0,
directionDivider(2) < 0};
// follow the ray through the tree's nodes to find intersections
unsigned int toVisitOffset = 0;
unsigned int nodeNumber = 0;
unsigned int toVisit[64];
while(true)
{
const LinearNodePtr node = nodes[nodeNumber];
// check ray against the node
if (intersectSlabs(node->bbox, ray, directionDivider, directionIsNegative))
{
if (node->numberPrimitives > 0)
{
// proceed to intersect ray with primitives in leaf
for (int i = 0; i < node->numberPrimitives; ++i) {
if (objects[node->primOffset+i]->intersect(ray, isect))
hit = true;
}
if (toVisitOffset == 0)
break;
nodeNumber = toVisit[--toVisitOffset];
}
else
{
// put distant node on stack, advance to next node
if (directionIsNegative[node->axis])
{
toVisit[toVisitOffset++] = nodeNumber + 1;
nodeNumber = node->secondChildOffset;
}
else
{
toVisit[toVisitOffset++] = node->secondChildOffset;
nodeNumber = nodeNumber + 1;
}
}
}
else
{
if (toVisitOffset == 0)
break;
nodeNumber = toVisit[--toVisitOffset];
}
}
return hit;
}
示例13: intersect
bool Cone::intersect(const Ray &ray, double &t)
{
Ray localRay = ray * m_worldToObject;
double dx = localRay.direction().x();
double dy = localRay.direction().y();
double dz = localRay.direction().z();
double ox = localRay.origin().x();
double oy = localRay.origin().y();
double oz = localRay.origin().z();
double a = dx * dx - dy * dy + dz * dz;
double b = 2.0 * (dx * ox - dy * oy + dy + dz * oz);
double c = ox * ox - oy * oy + 2 * oy + oz * oz - 1.0;
double t1, t2, t3;
// Calculate bottom disc t
t3 = -oy / dy;
double discX, discZ;
discX = ox + dx * t3;
discZ = oz + dz * t3;
if (discX * discX + discZ * discZ > 1.0)
t3 = -1.0; // Didn't hit the disc
if (MathUtils::solveQuadratic(a, b, c, t1, t2)) {
// Set cone tees to negative if the hit point y isn't between 0 and 1
double hy = oy + dy * t1;
if (!(hy >= 0.0 && hy <= 1.0))
t1 = -1.0;
hy = oy + dy * t2;
if (!(hy >= 0.0 && hy <= 1.0))
t2 = -1.0;
bool hit = false; t = std::numeric_limits<double>::max();
if (t1 > MathUtils::dEpsilon) {
hit = true;
t = t1;
}
if (t2 < t && t2 > MathUtils::dEpsilon) {
hit = true;
t = t2;
}
if (t3 < t && t3 > MathUtils::dEpsilon) {
hit = true;
t = t3;
}
return hit;
}
// Can't hit bottom disc without hitting the infinite cone too
return false;
}
示例14: intersect
bool World::intersect(Ray & r, double & bestT, vec3 &outn, MaterialInfo &outm) {
bestT = numeric_limits<double>::infinity();
if (_DEBUG_INTERSECT1
&& abs(r.direction()[0] - 0.146734796) < 0.05
&& abs(r.direction()[1] + 0.146734796) < 0.05
&& abs(r.direction()[2] + 0.978231971) < 0.05)
int i = 0; //debug statement, stops at a ray aimed at the center of one of the spheres in threespheres.scd
if (_DEBUG_INTERSECT2
&& abs(r.direction()[0] + 0.114776942) < 0.02
&& abs(r.direction()[1] - 0.0619082097) < 0.02
&& abs(r.direction()[2] + 0.991460351) < 0.02)
int i = 0; // debug statement, stops at a ray aimed for the left eye of the bunny in ellipsoids.scd
if (_ASSIGNMENT <= 5 || _FINAL_PROJ) {
// iterate through spheres to see if any of them are intersected
for (vector<Sphere>::iterator sphere = _spheres.begin(); sphere != _spheres.end(); sphere++) {
double intersect = sphere->intersect(r);
if (intersect < bestT) {
//cout << "intersect found" << endl;
bestT = intersect;
//cout << intersect << " ";
vec4 pos = r.getPos(intersect);
//cout << pos[0] << "," << pos[1] << "," << pos[2] << " ";
outn = vec3(sphere->calculateNormal(pos), VW);
//cout << outn[0] << "," << outn[1] << "," << outn[2] << endl;
outm = sphere->getMaterial();
//AS4 stuff
//if (sphere == _spheres.begin()) {
// outm.k[MAT_KSM] *= ksmMod;
// outm.k[MAT_KSP] *= kspMod;
// if (outm.k[MAT_KSP] < 1.0) outm.k[MAT_KSP] = 1.0;
//}
}
}
for (vector<Cube>::iterator cube = _cubes.begin(); cube != _cubes.end(); cube++) {
double intersect = cube->intersect(r);
if (intersect < bestT) {
bestT = intersect;
vec4 pos = r.getPos(intersect);
outn = vec3(cube->calculateNormal(pos), VW);
outm = cube->getMaterial();
outm.color = cube->calculateColor(pos);
}
}
return bestT < numeric_limits<double>::infinity();
}
else
return _bb->intersect(r, bestT, outn, outm);
}
示例15: make_reflection_ray
Ray Raytracer::make_reflection_ray(const Vector3d &normal,
const Ray &ray,
const Point3d &intersection)
{
Ray reflection(intersection,
normalize(
vector_subtract(ray.direction(),
scalar_multiply(normal,
2 * dot_product(ray.direction(), normal)))));
return reflection;
}