-
Notifications
You must be signed in to change notification settings - Fork 0
/
GeometryNode.cpp
48 lines (40 loc) · 1.3 KB
/
GeometryNode.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
// Termm--Fall 2020
#include "GeometryNode.hpp"
//---------------------------------------------------------------------------------------
GeometryNode::GeometryNode(
const std::string & name, Primitive *prim, Material *mat )
: SceneNode( name )
, m_material( mat )
, m_primitive( prim )
{
m_nodeType = NodeType::GeometryNode;
}
void GeometryNode::setMaterial( Material *mat )
{
// Obviously, there's a potential memory leak here. A good solution
// would be to use some kind of reference counting, as in the
// C++ shared_ptr. But I'm going to punt on that problem here.
// Why? Two reasons:
// (a) In practice we expect the scene to be constructed exactly
// once. There's no reason to believe that materials will be
// repeatedly overwritten in a GeometryNode.
// (b) A ray tracer is a program in which you compute once, and
// throw away all your data. A memory leak won't build up and
// crash the program.
m_material = mat;
}
bool GeometryNode::intersect(const Ray &ray, double tMin, double tMax, HitInfo &hit)
{
if (ray.opaqueOnly && m_material->transparency > 0.0)
{
return false;
}
Ray tRay = transformRay(ray);
bool hitFound = m_primitive->intersect(tRay, tMin, tMax, hit);
if (hitFound)
{
hit = transformHitInfo(hit);
hit.hitObject = this;
}
return hitFound;
}