I don’t have access to my pc right now to show you a screen shot, but I can copy paste the code in.
#include "PerRayData.cuh"
#include <curand_kernel.h>
rtTextureSampler<uchar4, 2, cudaReadModeNormalizedFloat> diffuseTexture;
rtDeclareVariable(rtObject, top_object, , );
rtDeclareVariable(float, roughness, , );
rtDeclareVariable(float, isothropy, , );
rtDeclareVariable(float2, texcoord, attribute texcoord, );
rtDeclareVariable(float3, normal, attribute normal, );
rtDeclareVariable(optix::Ray, ray, rtCurrentRay, );
rtDeclareVariable(PerRayData, perRayData, rtPayload, );
rtDeclareVariable(float, t_hit, rtIntersectionDistance, );
rtBuffer<float, 1> rng;
rtDeclareVariable(uint, rngSize, , );
rtDeclareVariable(uint2, launch_index, rtLaunchIndex, );
rtDeclareVariable(uint2, launch_dim, rtLaunchDim, );
RT_PROGRAM void closestHit()
{
if (perRayData.recursion > 0) {
float3 color = make_float3(tex2D(diffuseTexture, texcoord.x, texcoord.y));
uint index = (launch_index.x + launch_index.y*launch_dim.x + perRayData.recursion) % rngSize;
float a = rng[index];
float b = rng[index + 1];
float c = rng[index + 2]*4.f;
float cosAlpha = sqrtf(a / (roughness - a*roughness + a));
float sinAlpha = sqrtf(1 - cosAlpha*cosAlpha);
float phi = M_PI_2f*sqrtf(isothropy*isothropy*b*b / (1 - b*b + b*b*isothropy*isothropy));
if (c >= 1.f && c < 2) {
phi = M_PIf - phi;
} else if(c<3) {
phi = M_PIf + phi;
} else {
phi = M_PIf * 2 - phi;
}
float3 V_in = -ray.direction;
float3 N = normalize(rtTransformNormal(RT_OBJECT_TO_WORLD, normal));
if (dot(N, ray.direction) > 0) N = -N;
float3 B = normalize(cross(V_in, N));
float3 T = cross(N, B);
float3 V_out = B * sinAlpha * cosf(phi) + T * sinAlpha * sinf(phi) + N * cosAlpha;
float3 H = normalize(V_in + V_out);
float3 H_proj = normalize(H - dot(H, N)*N);
float t = dot(H,N);
float u = dot(V_in,H);
float v_in = dot(V_in,N);
float v_out = dot(V_out,N);
float w = dot(H_proj, T); //ez az abran rosszul van
//spectral
float3 S = color + (1 - color)*powf(1 - u, 5);
//directional
float Z = roughness / powf(1 + roughness*t*t - t*t, 2);
float A = sqrtf(isothropy / (isothropy*isothropy - isothropy*isothropy*w*w + w*w));
float G_in = v_in / (roughness - roughness*v_in + v_in);
float G_out = v_out / (roughness - roughness*v_out + v_out);
float G = G_in * G_out;
float D = (Z*A*G + 1 - G) / (4 * M_PIf*v_in*v_out);
//reflectance
float3 R = S*D;
float3 start = ray.origin + t_hit * ray.direction;
optix::Ray outray = optix::make_Ray(start, V_out, 0, 0.0001f, RT_DEFAULT_MAX);
PerRayData prd;
prd.recursion = perRayData.recursion - 1;
rtTrace(top_object, outray, prd);
float pdf = v_in*M_1_PIf*0.5f + Z*A / (8.f * M_PIf * dot(V_out, H));
perRayData.result = fminf(prd.result*R/pdf*v_in, make_float3(1,1,1));
} else {
perRayData.result = make_float3(0,0,0);
}
}
#define NOMINMAX
#include <optix.h>
#include <optixu/optixu_math_namespace.h>
using namespace optix;
rtDeclareVariable(unsigned int, MAX_RECURSION, , );
struct PerRayData {
float3 result;
int recursion;
};
#include "PerRayData.cuh"
rtDeclareVariable(uint2, launch_index, rtLaunchIndex, );
rtDeclareVariable(uint2, launch_dim, rtLaunchDim, );
rtBuffer<float4, 2> result_buffer;
rtDeclareVariable(float3, eye, , );
rtDeclareVariable(float3, lookat, , );
rtDeclareVariable(float3, up, , );
rtDeclareVariable(float3, right, , );
rtDeclareVariable(rtObject, top_object, , );
rtDeclareVariable(float, frameCount, , );
rtBuffer<float, 1> rng;
rtDeclareVariable(uint, rngSize, , );
RT_PROGRAM void camera()
{
uint index = (launch_index.x + launch_index.y*launch_dim.x) % rngSize;
float2 d = (make_float2(launch_index)+make_float2(rng[index], rng[index + 1])) / make_float2(launch_dim) * 2.f - 1.f;
float aspectRatio = (float)launch_dim.x / (float)launch_dim.y;
float3 dir = normalize(lookat + right * d.x * aspectRatio + up * d.y);
optix::Ray ray = optix::make_Ray(eye, dir, 0, 0.0001f, RT_DEFAULT_MAX);
PerRayData prd;
prd.recursion = MAX_RECURSION;
rtTrace(top_object, ray, prd);
result_buffer[launch_index] = make_float4((make_float3(result_buffer[launch_index]) * frameCount + prd.result) / (frameCount + 1.f), 0);
}
The any hit program for the other material may as well be a rtignoreintersection.
I just realized there are a few useless includes left in… Oh well.