<commit_before>/*
 * This file is part of `et engine`
 * Copyright 2009-2016 by <NAME>
 * Please, modify content only if you know what are you doing.
 *
 */

#include <et-ext/rt/integrator.h>

#define VISUALIZE_BRDF 0

namespace et
{
namespace rt
{
   
inline float lambert(const float4& n, const float4& Wo, float r)
{
    return n.dot(Wo) / PI;
}

inline float phong(const float4& n, const float4& Wi, const float4& Wo, const float4& r, float roughness)
{
    float Ns = std::max(0.0f, 2.0f / (sqr(roughness) + Constants::epsilon) - 2.0f);
    auto RdotW = clamp(r.dot(Wo), 0.0f, 1.0f);
    return clamp(std::pow(RdotW, Ns) * (Ns + 2.0f) / DOUBLE_PI, 0.0f, 1.0f);
}
    
float4 AmbientOcclusionIntegrator::gather(const Ray& inRay, size_t depth, size_t& maxDepth,
    KDTree& tree, EnvironmentSampler::Pointer& env, const Material::Collection&)
{
    KDTree::TraverseResult hit0 = tree.traverse(inRay);
    if (hit0.triangleIndex == InvalidIndex)
        return env->sampleInDirection(inRay.direction);
    
    const auto& tri = tree.triangleAtIndex(hit0.triangleIndex);
    float4 surfaceNormal = tri.interpolatedNormal(hit0.intersectionPointBarycentric);
	float4 nextDirection = randomVectorOnHemisphere(surfaceNormal, HALF_PI);
	float4 nextOrigin = hit0.intersectionPoint + nextDirection * Constants::epsilon;
	if (tree.traverse(Ray(nextOrigin, nextDirection)).triangleIndex == InvalidIndex)
	{
		++maxDepth;
		return env->sampleInDirection(nextDirection);// * nextDirection.dot(surfaceNormal);
	}

    return float4(0.0f);
}
    
/*
 * PathTraceIntegrator
 */
struct ET_ALIGNED(16) Bounce
{
    float4 scale;
    float4 add;
};
    
float4 PathTraceIntegrator::gather(const Ray& inRay, size_t depth, size_t& maxDepth,
    KDTree& tree, EnvironmentSampler::Pointer& env, const Material::Collection& materials)
{
    ++maxDepth;
    float4 l(0.0f, 1.0f, 0.0f, 0.0f);
    
    auto currentRay = inRay;
    
    FastStack<PathTraceIntegrator::MaxTraverseDepth, Bounce> bounces;
    while (bounces.size() < MaxTraverseDepth)
    {
        auto& bounce = bounces.emplace_back();
        KDTree::TraverseResult traverse = tree.traverse(currentRay);
        if (traverse.triangleIndex == InvalidIndex)
        {
            bounce.add = env->sampleInDirection(currentRay.direction);
            break;
        }
        
		const auto& tri = tree.triangleAtIndex(traverse.triangleIndex);
        const auto& mat = materials[tri.materialIndex];
        auto nrm = tri.interpolatedNormal(traverse.intersectionPointBarycentric);

#   if (VISUALIZE_BRDF)
        auto r = reflect(currentRay.direction, nrm);
        return float4(phong(nrm, currentRay.direction, l, r, mat.roughnessValue));
#   else
        float4 color;
        float brdf = 0.0f;
        currentRay.direction = reflectance(currentRay.direction, nrm, mat, color, brdf);
		currentRay.origin = traverse.intersectionPoint + currentRay.direction * Constants::epsilon;
        bounce.add = mat.emissive;
        bounce.scale = color * brdf;
#   endif
        
    }
	maxDepth = bounces.size();
    
    float4 result(0.0f);
    do
    {<commit_msg>rt demo: added missing n.dot(Wi)<commit_after>/*
 * This file is part of `et engine`
 * Copyright 2009-2016 by <NAME>
 * Please, modify content only if you know what are you doing.
 *
 */

#include <et-ext/rt/integrator.h>

#define VISUALIZE_BRDF 0

namespace et
{
namespace rt
{
   
inline float lambert(const float4& n, const float4& Wo, float r)
{
    return 1.0f / PI;
}

inline float phong(const float4& n, const float4& Wi, const float4& Wo, const float4& r, float roughness)
{
    float Ns = std::max(0.0f, 2.0f / (sqr(roughness) + Constants::epsilon) - 2.0f);
    auto RdotW = clamp(r.dot(Wo), 0.0f, 1.0f);
    return clamp(std::pow(RdotW, Ns) * (Ns + 2.0f) / DOUBLE_PI, 0.0f, 1.0f);
}
    
float4 AmbientOcclusionIntegrator::gather(const Ray& inRay, size_t depth, size_t& maxDepth,
    KDTree& tree, EnvironmentSampler::Pointer& env, const Material::Collection&)
{
    KDTree::TraverseResult hit0 = tree.traverse(inRay);
    if (hit0.triangleIndex == InvalidIndex)
        return env->sampleInDirection(inRay.direction);
    
    const auto& tri = tree.triangleAtIndex(hit0.triangleIndex);
    float4 surfaceNormal = tri.interpolatedNormal(hit0.intersectionPointBarycentric);
	float4 nextDirection = randomVectorOnHemisphere(surfaceNormal, HALF_PI);
	float4 nextOrigin = hit0.intersectionPoint + nextDirection * Constants::epsilon;
	if (tree.traverse(Ray(nextOrigin, nextDirection)).triangleIndex == InvalidIndex)
	{
		++maxDepth;
		return env->sampleInDirection(nextDirection);// * nextDirection.dot(surfaceNormal);
	}

    return float4(0.0f);
}
    
/*
 * PathTraceIntegrator
 */
struct ET_ALIGNED(16) Bounce
{
    float4 scale;
    float4 add;
};
    
float4 PathTraceIntegrator::gather(const Ray& inRay, size_t depth, size_t& maxDepth,
    KDTree& tree, EnvironmentSampler::Pointer& env, const Material::Collection& materials)
{
    ++maxDepth;
    float4 l(0.0f, 1.0f, 0.0f, 0.0f);
    
    auto currentRay = inRay;
    
    FastStack<PathTraceIntegrator::MaxTraverseDepth, Bounce> bounces;
    while (bounces.size() < MaxTraverseDepth)
    {
        auto& bounce = bounces.emplace_back();
        KDTree::TraverseResult traverse = tree.traverse(currentRay);
        if (traverse.triangleIndex == InvalidIndex)
        {
            bounce.add = env->sampleInDirection(currentRay.direction);
            break;
        }
        
		const auto& tri = tree.triangleAtIndex(traverse.triangleIndex);
        const auto& mat = materials[tri.materialIndex];
        auto nrm = tri.interpolatedNormal(traverse.intersectionPointBarycentric);

#   if (VISUALIZE_BRDF)
        auto r = reflect(currentRay.direction, nrm);
        return float4(phong(nrm, currentRay.direction, l, r, mat.roughnessValue));
#   else
        float4 color;
        float brdf = 0.0f;
        currentRay.direction = reflectance(currentRay.direction, nrm, mat, color, brdf);
		currentRay.origin = traverse.intersectionPoint + currentRay.direction * Constants::epsilon;
        bounce.add = mat.emissive;
        bounce.scale = color * (brdf * std::max(0.0f, nrm.dot(currentRay.direction)));
#   endif
        
    }
	maxDepth = bounces.size();
    
    float4 result(0.0f);
    do
    {
{'fraction_non_alphanumeric': 0.11941964285714286, 'fraction_numerical': 0.021045918367346938, 'mean_word_length': 3.5, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5189513', 'n_tokens_mistral': 2192, 'n_tokens_neox': 2085, 'n_words': 464}