Hi, experts.
I found a strange problem passing the attributes from the closest intersection programme. The shading is total wrong as the attributes are unreasonable. (The intersection is right because the projection of the scene is right). I simplified the problem code as follows:
// mesh file
#include <optix_world.h>
using namespace optix;
struct Vertex
{
float3 position;
float3 normal;
float2 tex;
};
rtBuffer vertex_buffer;
rtBuffer index_buffer;
rtBuffer material_buffer; // per-face material index
rtDeclareVariable(float3, texcoord, attribute texcoord, );
rtDeclareVariable(float3, geometric_normal, attribute geometric_normal, );
rtDeclareVariable(float3, shading_normal, attribute shading_normal, );
rtDeclareVariable(optix::Ray, ray, rtCurrentRay, );
RT_PROGRAM void mesh_intersect(int primIdx)
{
int3 v_idx = index_buffer[primIdx];
float3 p0 = vertex_buffer[v_idx.x].position;
float3 p1 = vertex_buffer[v_idx.y].position;
float3 p2 = vertex_buffer[v_idx.z].position;
// Intersect ray with triangle
float3 n;
float t, beta, gamma;
if (intersect_triangle(ray, p0, p1, p2, n, t, beta, gamma))
{
shading_normal = make_float3(1.0f, 0.0f, 0.0f);
geometric_normal = make_float3(1, 0, 0);
texcoord = make_float3(1, 0, 0);
rtReportIntersection(material_buffer[primIdx]);
}
}
RT_PROGRAM void mesh_bounds(int primIdx, optix::Aabb* aabb)
{
const int3 v_idx = index_buffer[primIdx];
const float3 v0 = vertex_buffer[v_idx.x].position;
const float3 v1 = vertex_buffer[v_idx.y].position;
const float3 v2 = vertex_buffer[v_idx.z].position;
const float area = length(cross(v1 - v0, v2 - v0));
if (area > 0.0f && !isinf(area))
{
aabb->m_min = fminf(fminf(v0, v1), v2);
aabb->m_max = fmaxf(fmaxf(v0, v1), v2);
}
else {
aabb->invalidate();
}
}
// shading file:
#include <optix.h>
#include <optixu/optixu_math_namespace.h>
using namespace optix;
rtDeclareVariable(float, scene_epsilon, , );
rtDeclareVariable(rtObject, reflectors, , );
rtDeclareVariable(uint, max_depth, , );
rtDeclareVariable(optix::Ray, ray, rtCurrentRay, );
rtDeclareVariable(float, t_hit, rtIntersectionDistance, );
device float myfmax(float a, float b) {
return ((a) > (b) ? a : b);
}
struct PerRayData_shadow
{
float3 attenuation;
float t_hit;
};
rtDeclareVariable(PerRayData_shadow, prd_shadow, rtPayload, );
rtDeclareVariable(float3, texcoord, attribute texcoord, );
rtDeclareVariable(float3, geometric_normal, attribute geometric_normal, );
rtDeclareVariable(float3, shading_normal, attribute shading_normal, );
RT_PROGRAM void closest_hit_radiance()
{
prd_shadow.attenuation = shading_normal;
}
RT_PROGRAM void any_hit_shadow()
{
prd_shadow.attenuation = make_float3(0, 0, 0);
rtTerminateRay();
}
The result image is like a irreasonable random image.
If I change the name of the attribute, runtime programe raises a error " Attribute “shading_normal1” is referenced in a hit program but is not produced in attached intersection program" , which means the attribute is actually attached. However, the value is wrong.
Could anyone guild me a direction to solve this problem?
The best