Skip to content

Commit

Permalink
updated
Browse files Browse the repository at this point in the history
  • Loading branch information
xelatihy committed Jan 31, 2024
1 parent 66f251b commit b070a1a
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 70 deletions.
32 changes: 16 additions & 16 deletions libs/yocto/yocto_raytracing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -992,11 +992,11 @@ static vec4f shade_raytrace(const scene_data& scene, const scene_bvh& bvh,

// evaluate geometry
auto& instance = scene.instances[intersection.instance];
auto position = eval_position(
auto position = instance_point(
scene, instance, intersection.element, intersection.uv);
auto normal = eval_normal(
auto normal = instance_normal(
scene, instance, intersection.element, intersection.uv);
auto texcoord = eval_texcoord(
auto texcoord = instance_texcoord(
scene, instance, intersection.element, intersection.uv);
auto outgoing = -ray.d;

Expand Down Expand Up @@ -1106,11 +1106,11 @@ static vec4f shade_matte(const scene_data& scene, const scene_bvh& bvh,

// evaluate geometry
auto& instance = scene.instances[intersection.instance];
auto position = eval_position(
auto position = instance_point(
scene, instance, intersection.element, intersection.uv);
auto normal = eval_normal(
auto normal = instance_normal(
scene, instance, intersection.element, intersection.uv);
auto texcoord = eval_texcoord(
auto texcoord = instance_texcoord(
scene, instance, intersection.element, intersection.uv);

// evaluate material
Expand Down Expand Up @@ -1150,11 +1150,11 @@ static vec4f shade_pointlight(const scene_data& scene, const scene_bvh& bvh,

// evaluate geometry
auto& instance = scene.instances[intersection.instance];
auto position = eval_position(
auto position = instance_point(
scene, instance, intersection.element, intersection.uv);
auto normal = eval_normal(
auto normal = instance_normal(
scene, instance, intersection.element, intersection.uv);
auto texcoord = eval_texcoord(
auto texcoord = instance_texcoord(
scene, instance, intersection.element, intersection.uv);
auto outgoing = -ray.d;

Expand Down Expand Up @@ -1208,11 +1208,11 @@ static vec4f shade_arealight(const scene_data& scene, const scene_bvh& bvh,

// evaluate geometry
auto& instance = scene.instances[intersection.instance];
auto position = eval_position(
auto position = instance_point(
scene, instance, intersection.element, intersection.uv);
auto normal = eval_normal(
auto normal = instance_normal(
scene, instance, intersection.element, intersection.uv);
auto texcoord = eval_texcoord(
auto texcoord = instance_texcoord(
scene, instance, intersection.element, intersection.uv);

// evaluate material
Expand Down Expand Up @@ -1280,9 +1280,9 @@ static vec4f shade_eyelight(const scene_data& scene, const scene_bvh& bvh,

// evaluate geometry
auto& instance = scene.instances[intersection.instance];
auto normal = eval_normal(
auto normal = instance_normal(
scene, instance, intersection.element, intersection.uv);
auto texcoord = eval_texcoord(
auto texcoord = instance_texcoord(
scene, instance, intersection.element, intersection.uv);
auto outgoing = -ray.d;

Expand All @@ -1308,7 +1308,7 @@ static vec4f shade_normal(const scene_data& scene, const scene_bvh& bvh,

// prepare shading point
auto& instance = scene.instances[intersection.instance];
auto normal = eval_normal(
auto normal = instance_normal(
scene, instance, intersection.element, intersection.uv);

auto radiance = normal * 0.5f + 0.5f;
Expand All @@ -1326,7 +1326,7 @@ static vec4f shade_texcoord(const scene_data& scene, const scene_bvh& bvh,

// prepare shading point
auto& instance = scene.instances[intersection.instance];
auto texcoord = eval_texcoord(
auto texcoord = instance_texcoord(
scene, instance, intersection.element, intersection.uv);
texcoord = {fmod(texcoord.x, 1), fmod(texcoord.y, 1)};

Expand Down
16 changes: 8 additions & 8 deletions libs/yocto/yocto_raytracing.h
Original file line number Diff line number Diff line change
Expand Up @@ -728,42 +728,42 @@ namespace yocto {
// Convenience functions
inline vec3f intersection_point(
const scene_data& scene, const intersection3f& intersection) {
return eval_position(scene, scene.instances[intersection.instance],
return instance_point(scene, scene.instances[intersection.instance],
intersection.element, intersection.uv);
}
inline vec3f intersection_normal(
const scene_data& scene, const intersection3f& intersection) {
return eval_normal(scene, scene.instances[intersection.instance],
return instance_normal(scene, scene.instances[intersection.instance],
intersection.element, intersection.uv);
}
inline vec3f intersection_element_normal(
const scene_data& scene, const intersection3f& intersection) {
return eval_element_normal(
return instance_element_normal(
scene, scene.instances[intersection.instance], intersection.element);
}
inline vec3f intersection_shading_point(const scene_data& scene,
const intersection3f& intersection, vec3f outgoing) {
return eval_shading_position(scene, scene.instances[intersection.instance],
return instance_shading_point(scene, scene.instances[intersection.instance],
intersection.element, intersection.uv, outgoing);
}
inline vec3f intersection_shading_normal(const scene_data& scene,
const intersection3f& intersection, vec3f outgoing) {
return eval_shading_normal(scene, scene.instances[intersection.instance],
return instance_shading_normal(scene, scene.instances[intersection.instance],
intersection.element, intersection.uv, outgoing);
}
inline vec2f intersection_texcoord(
const scene_data& scene, const intersection3f& intersection) {
return eval_texcoord(scene, scene.instances[intersection.instance],
return instance_texcoord(scene, scene.instances[intersection.instance],
intersection.element, intersection.uv);
}
inline material_point intersection_material(
const scene_data& scene, const intersection3f& intersection) {
return eval_material(scene, scene.instances[intersection.instance],
return instance_material(scene, scene.instances[intersection.instance],
intersection.element, intersection.uv);
}
inline bool is_volumetric_intersection(
const scene_data& scene, const intersection3f& intersection) {
return is_volumetric(scene, scene.instances[intersection.instance]);
return is_volumetric_instance(scene, scene.instances[intersection.instance]);
}

} // namespace yocto
Expand Down
47 changes: 24 additions & 23 deletions libs/yocto/yocto_scene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -864,7 +864,7 @@ bool has_volume(const material_point& material) {
namespace yocto {

// Eval position
vec3f eval_position(const scene_data& scene, const instance_data& instance,
vec3f instance_point(const scene_data& scene, const instance_data& instance,
int element, vec2f uv) {
auto& shape = scene.shapes[instance.shape];
if (!shape.triangles.empty()) {
Expand All @@ -890,7 +890,7 @@ vec3f eval_position(const scene_data& scene, const instance_data& instance,
}

// Shape element normal.
vec3f eval_element_normal(
vec3f instance_element_normal(
const scene_data& scene, const instance_data& instance, int element) {
auto& shape = scene.shapes[instance.shape];
if (!shape.triangles.empty()) {
Expand All @@ -915,11 +915,11 @@ vec3f eval_element_normal(
}

// Eval normal
vec3f eval_normal(const scene_data& scene, const instance_data& instance,
vec3f instance_normal(const scene_data& scene, const instance_data& instance,
int element, vec2f uv) {
auto& shape = scene.shapes[instance.shape];
if (shape.normals.empty())
return eval_element_normal(scene, instance, element);
return instance_element_normal(scene, instance, element);
if (!shape.triangles.empty()) {
auto t = shape.triangles[element];
return transform_normal(
Expand All @@ -944,7 +944,7 @@ vec3f eval_normal(const scene_data& scene, const instance_data& instance,
}

// Eval texcoord
vec2f eval_texcoord(const scene_data& scene, const instance_data& instance,
vec2f instance_texcoord(const scene_data& scene, const instance_data& instance,
int element, vec2f uv) {
auto& shape = scene.shapes[instance.shape];
if (shape.texcoords.empty()) return uv;
Expand Down Expand Up @@ -999,7 +999,7 @@ static pair<vec3f, vec3f> eval_tangents(
#endif

// Shape element normal.
pair<vec3f, vec3f> eval_element_tangents(
pair<vec3f, vec3f> instance_element_tangents(
const scene_data& scene, const instance_data& instance, int element) {
auto& shape = scene.shapes[instance.shape];
if (!shape.triangles.empty() && !shape.texcoords.empty()) {
Expand All @@ -1022,18 +1022,18 @@ pair<vec3f, vec3f> eval_element_tangents(
}
}

vec3f eval_normalmap(const scene_data& scene, const instance_data& instance,
vec3f instance_normalmap(const scene_data& scene, const instance_data& instance,
int element, vec2f uv) {
auto& shape = scene.shapes[instance.shape];
auto& material = scene.materials[instance.material];
// apply normal mapping
auto normal = eval_normal(scene, instance, element, uv);
auto texcoord = eval_texcoord(scene, instance, element, uv);
auto normal = instance_normal(scene, instance, element, uv);
auto texcoord = instance_texcoord(scene, instance, element, uv);
if (material.normal_tex != invalidid &&
(!shape.triangles.empty() || !shape.quads.empty())) {
auto& normal_tex = scene.textures[material.normal_tex];
auto normalmap = -1 + 2 * xyz(eval_texture(normal_tex, texcoord, true));
auto [tu, tv] = eval_element_tangents(scene, instance, element);
auto [tu, tv] = instance_element_tangents(scene, instance, element);
auto frame = frame3f{tu, tv, normal, {0, 0, 0}};
frame.x = orthonormalize(frame.x, frame.z);
frame.y = normalize(cross(frame.z, frame.x));
Expand All @@ -1045,34 +1045,34 @@ vec3f eval_normalmap(const scene_data& scene, const instance_data& instance,
}

// Eval shading position
vec3f eval_shading_position(const scene_data& scene,
vec3f instance_shading_point(const scene_data& scene,
const instance_data& instance, int element, vec2f uv, vec3f outgoing) {
auto& shape = scene.shapes[instance.shape];
if (!shape.triangles.empty() || !shape.quads.empty()) {
return eval_position(scene, instance, element, uv);
return instance_point(scene, instance, element, uv);
} else if (!shape.lines.empty()) {
return eval_position(scene, instance, element, uv);
return instance_point(scene, instance, element, uv);
} else if (!shape.points.empty()) {
return eval_position(scene, instance, element, uv);
return instance_point(scene, instance, element, uv);
} else {
return {0, 0, 0};
}
}

// Eval shading normal
vec3f eval_shading_normal(const scene_data& scene,
vec3f instance_shading_normal(const scene_data& scene,
const instance_data& instance, int element, vec2f uv, vec3f outgoing) {
auto& shape = scene.shapes[instance.shape];
auto& material = scene.materials[instance.material];
if (!shape.triangles.empty() || !shape.quads.empty()) {
auto normal = eval_normal(scene, instance, element, uv);
auto normal = instance_normal(scene, instance, element, uv);
if (material.normal_tex != invalidid) {
normal = eval_normalmap(scene, instance, element, uv);
normal = instance_normalmap(scene, instance, element, uv);
}
if (material.type == material_type::refractive) return normal;
return dot(normal, outgoing) >= 0 ? normal : -normal;
} else if (!shape.lines.empty()) {
auto normal = eval_normal(scene, instance, element, uv);
auto normal = instance_normal(scene, instance, element, uv);
return orthonormalize(outgoing, normal);
} else if (!shape.points.empty()) {
return outgoing;
Expand All @@ -1082,7 +1082,7 @@ vec3f eval_shading_normal(const scene_data& scene,
}

// Eval color
vec4f eval_color(const scene_data& scene, const instance_data& instance,
vec4f instance_color(const scene_data& scene, const instance_data& instance,
int element, vec2f uv) {
auto& shape = scene.shapes[instance.shape];
if (shape.colors.empty()) return {1, 1, 1, 1};
Expand All @@ -1105,14 +1105,14 @@ vec4f eval_color(const scene_data& scene, const instance_data& instance,
}

// Evaluate material
material_point eval_material(const scene_data& scene,
material_point instance_material(const scene_data& scene,
const instance_data& instance, int element, vec2f uv) {
auto& material = scene.materials[instance.material];
auto texcoord = eval_texcoord(scene, instance, element, uv);
auto texcoord = instance_texcoord(scene, instance, element, uv);

// evaluate textures
auto emission_tex = eval_texture(scene, material.emission_tex, texcoord);
auto color_shp = eval_color(scene, instance, element, uv);
auto color_shp = instance_color(scene, instance, element, uv);
auto color_tex = eval_texture(scene, material.color_tex, texcoord);
auto roughness_tex = eval_texture(
scene, material.roughness_tex, texcoord, true);
Expand Down Expand Up @@ -1156,7 +1156,8 @@ material_point eval_material(const scene_data& scene,
}

// check if an instance is volumetric
bool is_volumetric(const scene_data& scene, const instance_data& instance) {
bool is_volumetric_instance(
const scene_data& scene, const instance_data& instance) {
return is_volumetric(scene.materials[instance.material]);
}

Expand Down
23 changes: 12 additions & 11 deletions libs/yocto/yocto_scene.h
Original file line number Diff line number Diff line change
Expand Up @@ -443,30 +443,31 @@ bool is_volumetric(const scene_data& scene, const instance_data& instance);
namespace yocto {

// Evaluate instance properties
vec3f eval_position(const scene_data& scene, const instance_data& instance,
vec3f instance_point(const scene_data& scene, const instance_data& instance,
int element, vec2f uv);
vec3f eval_element_normal(
vec3f instance_element_normal(
const scene_data& scene, const instance_data& instance, int element);
vec3f eval_normal(const scene_data& scene, const instance_data& instance,
vec3f instance_normal(const scene_data& scene, const instance_data& instance,
int element, vec2f uv);
vec2f eval_texcoord(const scene_data& scene, const instance_data& instance,
vec2f instance_texcoord(const scene_data& scene, const instance_data& instance,
int element, vec2f uv);
pair<vec3f, vec3f> eval_element_tangents(
pair<vec3f, vec3f> instance_element_tangents(
const scene_data& scene, const instance_data& instance, int element);
vec3f eval_normalmap(const scene_data& scene, const instance_data& instance,
vec3f instance_normalmap(const scene_data& scene, const instance_data& instance,
int element, vec2f uv);
vec3f eval_shading_position(const scene_data& scene,
vec3f instance_shading_point(const scene_data& scene,
const instance_data& instance, int element, vec2f uv, vec3f outgoing);
vec3f eval_shading_normal(const scene_data& scene,
vec3f instance_shading_normal(const scene_data& scene,
const instance_data& instance, int element, vec2f uv, vec3f outgoing);
vec4f eval_color(const scene_data& scene, const instance_data& instance,
vec4f instance_color(const scene_data& scene, const instance_data& instance,
int element, vec2f uv);

// Eval material to obtain emission, brdf and opacity.
material_point eval_material(const scene_data& scene,
material_point instance_material(const scene_data& scene,
const instance_data& instance, int element, vec2f uv);
// check if a material has a volume
bool is_volumetric(const scene_data& scene, const instance_data& instance);
bool is_volumetric_instance(
const scene_data& scene, const instance_data& instance);

} // namespace yocto

Expand Down
Loading

0 comments on commit b070a1a

Please sign in to comment.