Skip to content
This repository has been archived by the owner on Oct 3, 2022. It is now read-only.

Commit

Permalink
Add functionality to roughness and reflectivity material parameters
Browse files Browse the repository at this point in the history
Add albedo color
  • Loading branch information
jonathonracz committed Feb 12, 2018
1 parent e2de03d commit cf2f2e0
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 40 deletions.
4 changes: 2 additions & 2 deletions jtracer/JTCGRender.mm
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,9 @@ - (void)render:(JTRenderer *)renderer state:(JTRenderState *)state sender:(JTDis

_isCurrentlyRendering = YES;
_shouldStartNewRender = NO;
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSDate* startTime = [NSDate new];
dispatch_apply(pixelsToProcess, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), pixelWork);
dispatch_apply(pixelsToProcess, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), pixelWork);
if (!_shouldStartNewRender) {
_lastRenderTime = -[startTime timeIntervalSinceNow]; // Only store the render time if the render completed.
}
Expand Down
19 changes: 17 additions & 2 deletions jtracer/JTRenderState.mm
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,23 @@ - (id)init {
self = [super init];
if (self) {
_uniformsInternal = std::unique_ptr<jt::Uniforms>(new jt::Uniforms);
_uniformsInternal->spheres[0] = jt::Sphere(make_float3(0.0f, 0.0f, -1.0f), 0.5f);
_uniformsInternal->spheres[1] = jt::Sphere(make_float3(0.0f, -100.5f, -1.0f), 100.0f);
_uniformsInternal->spheres[0] = jt::Sphere(make_float3(-1.0f, 0.0f, -1.0f), 0.5f);
_uniformsInternal->spheres[1] = jt::Sphere(make_float3(0.0f, 0.0f, -1.0f), 0.5f);
_uniformsInternal->spheres[2] = jt::Sphere(make_float3(1.0f, 0.0f, -1.0f), 0.5f);
_uniformsInternal->spheres[3] = jt::Sphere(make_float3(0.0f, -100.5f, -1.0f), 100.0f);


_uniformsInternal->spheres[0].materialParams.albedo = make_float3(1.0f, 0.0f, 0.0f);
_uniformsInternal->spheres[0].materialParams.roughness = 1.0f;
_uniformsInternal->spheres[0].materialParams.reflectivity = 1.0f;

_uniformsInternal->spheres[1].materialParams.albedo = make_float3(0.0f, 1.0f, 0.0f);
_uniformsInternal->spheres[1].materialParams.roughness = 0.5f;
_uniformsInternal->spheres[1].materialParams.reflectivity = 0.5f;

_uniformsInternal->spheres[2].materialParams.albedo = make_float3(0.0f, 0.0f, 1.0f);
_uniformsInternal->spheres[2].materialParams.roughness = 1.0f;
_uniformsInternal->spheres[2].materialParams.reflectivity = 1.0f;
}

return self;
Expand Down
4 changes: 2 additions & 2 deletions jtracer/JTRenderViewController.mm
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ - (void)viewDidLoad {

- (void)renderViews:(JTDisplayLink *)sender {
[_renderState update:_displayLink.timestamp];
[self.metalRenderer render:_renderState sender:_displayLink];
//[self.cgRenderer render:_renderState sender:_displayLink];
//[self.metalRenderer render:_renderState sender:_displayLink];
[self.cgRenderer render:_renderState sender:_displayLink];
}

- (void)setRepresentedObject:(id)representedObject {
Expand Down
16 changes: 12 additions & 4 deletions jtracer/Trace/JTBSDF.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,23 @@ namespace BSDF

struct Parameters
{
float3 baseColor;
float roughness = 0.5f;
float3 albedo = make_float3(1.0f, 1.0f, 1.0f);
float roughness = 1.0f;
float reflectivity = 0.5f;
};

inline float3 scatter(JT_THREAD PRNG& random, JT_THREAD const Parameters* params, float3 incident, float3 normal)
inline float3 scatter(JT_THREAD PRNG& random, JT_THREAD const Parameters& params, JT_THREAD const float3& incident, JT_THREAD const float3& normal)
{
float3 reflection = Math::reflect(incident, normal);
float3 diffuse = random.generateInUnitSphere();
float3 scattered = Math::slerp(reflection, diffuse, params->roughness);
// Make sure the diffuse ray is actually a reflection - i.e. leaving the
// object.
if (dot(diffuse, normal) < 0)
diffuse *= -1.0f;

// TODO: This may be better off as a slerp so there isn't a lopsided
// resolution between the center and edges of the interpolation...
float3 scattered = Math::lerp(reflection, diffuse, params.roughness);
return scattered;
}

Expand Down
2 changes: 1 addition & 1 deletion jtracer/Trace/JTShaderTypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ struct Uniforms
OpenSimplex::Context context;
uint32 frameCount = 0;
uint32 random;
Sphere spheres[2];
Sphere spheres[4];
};

}
6 changes: 3 additions & 3 deletions jtracer/Trace/JTSphere.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class Sphere
float t;
float3 p;
float3 normal;
JT_THREAD const BSDF::Parameters* materialParams;
BSDF::Parameters materialParams;
};

Sphere() = default;
Expand All @@ -46,7 +46,7 @@ class Sphere
record.t = root1;
record.p = ray.pointAtParam(root1);
record.normal = (record.p - center) / radius;
record.materialParams = &materialParams;
record.materialParams = materialParams;
return true;
}

Expand All @@ -56,7 +56,7 @@ class Sphere
record.t = root2;
record.p = ray.pointAtParam(root2);
record.normal = (record.p - center) / radius;
record.materialParams = &materialParams;
record.materialParams = materialParams;
return true;
}
}
Expand Down
9 changes: 5 additions & 4 deletions jtracer/Trace/JTSphereList.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,20 @@ class SphereList

bool hitTest(JT_THREAD const Ray& ray, JT_THREAD Sphere::HitRecord& record, float tMin = 0.0f, float tMax = INFINITY) const
{
Sphere::HitRecord tempRecord;
bool hitSomething = false;
float tClosest = tMax;
for (size_t i = 0; i < num; ++i)
{
Sphere::HitRecord testRecord;
Sphere sphere = spheres[i];
if (sphere.hitTest(ray, tempRecord, tMin, tClosest))
if (sphere.hitTest(ray, testRecord, tMin, tClosest))
{
hitSomething = true;
tClosest = tempRecord.t;
record = tempRecord;
tClosest = testRecord.t;
record = testRecord;
}
}

return hitSomething;
}

Expand Down
31 changes: 19 additions & 12 deletions jtracer/Trace/JTTrace.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,32 +41,39 @@ float4 runTrace(JT_CONSTANT const Uniforms& uniforms, uint2 pos, uint2 dimension
float v = (pos.y + (random.generateNormal() - 0.5f)) / static_cast<float>(dimensions.y);
Ray ray = camera.makeRay(u, v);
Sphere::HitRecord hitRecord;
float3 rayColor = make_float3(0.0f, 0.0f, 0.0f);
bool didHit = false;
bool finalColorComputed = false;
float3 finalRayColor = make_float3(0.0f, 0.0f, 0.0f);
float3 rayColorAccumulator = make_float3(0.0f, 0.0f, 0.0f);
uint64 numBounces = 0;
float reflection = 1.0f;
do
{
didHit = world.hitTest(ray, hitRecord, 0.001f);
if (didHit)
bool didHit = world.hitTest(ray, hitRecord, 0.001f);
if (didHit && reflection > 0.0f)
{
numBounces++;
float3 incidentRay = normalize(ray.directionAtOrigin());
float3 scattered = BSDF::scatter(random, hitRecord.materialParams, incidentRay, hitRecord.normal);
rayColorAccumulator += hitRecord.materialParams.albedo;
reflection *= hitRecord.materialParams.reflectivity; // TODO: Fresnel-based reflection
float3 incident = normalize(ray.directionAtOrigin());
float3 normal = normalize(hitRecord.normal); // Sometimes the floating point error is a bit off and freaks out asserts...
float3 scattered = BSDF::scatter(random, hitRecord.materialParams, incident, normal);
float3 target = hitRecord.p + scattered;
ray = Ray(hitRecord.p, target - hitRecord.p);
}
else
{
rayColor = gradient.color(ray);
float3 lightColor = gradient.color(ray);
if (numBounces > 0)
{
rayColor *= (0.5f / numBounces);
}
finalRayColor = lightColor * (rayColorAccumulator / numBounces) * reflection;
else
finalRayColor = lightColor;

finalColorComputed = true;
}
}
while (didHit);
while (!finalColorComputed);

pixelColor += rayColor;
pixelColor += finalRayColor;
}

pixelColor /= samplesPerPixel;
Expand Down
14 changes: 4 additions & 10 deletions jtracer/Trace/JTTypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,20 +112,14 @@ namespace jt
template<typename T>
inline T reflect(T i, T n)
{
assert(equal(normalize(n), n));
assert(equal(normalize(i), i));
return i - (2 * dot(n, i) * n);
}

inline float3 slerp(float3 x, float3 y, float t)
/** Lerp from x to y. */
template<typename T>
inline T lerp(T x, T y, float t)
{
assert(equal(normalize(x), x));
assert(equal(normalize(y), y));
assert(0.0f <= t && t <= 1.0f);
float omega = acos(dot(x, y));
float3 comp1 = (sin((1 - t) * omega) / sin(omega)) * x;
float3 comp2 = (sin(t * omega) / sin(omega)) * y;
return comp1 + comp2;
return ((y - x) * t) + x;
}

namespace Constants
Expand Down

0 comments on commit cf2f2e0

Please sign in to comment.