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 1818539 commit 5307158
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 47 deletions.
2 changes: 1 addition & 1 deletion apps/ysamples.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ void run(const vector<string>& args) {
for (auto& point : points) {
sshape.points.push_back((int)sshape.points.size());
sshape.positions.push_back(
eval_position(shape, point.first, point.second));
shape_position(shape, point.first, point.second));
sshape.radius.push_back(radius * 10);
}

Expand Down
61 changes: 24 additions & 37 deletions libs/yocto/yocto_scene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ ray3f camera_ray(const camera_data& camera, vec2f image_uv, vec2f lens_uv) {
namespace yocto {

// Interpolate vertex data
vec3f eval_position(const shape_data& shape, int element, vec2f uv) {
vec3f shape_position(const shape_data& shape, int element, vec2f uv) {
if (!shape.points.empty()) {
auto& point = shape.points[element];
return shape.positions[point];
Expand All @@ -152,8 +152,8 @@ vec3f eval_position(const shape_data& shape, int element, vec2f uv) {
}
}

vec3f eval_normal(const shape_data& shape, int element, vec2f uv) {
if (shape.normals.empty()) return eval_element_normal(shape, element);
vec3f shape_normal(const shape_data& shape, int element, vec2f uv) {
if (shape.normals.empty()) return shape_element_normal(shape, element);
if (!shape.points.empty()) {
auto& point = shape.points[element];
return normalize(shape.normals[point]);
Expand All @@ -175,11 +175,11 @@ vec3f eval_normal(const shape_data& shape, int element, vec2f uv) {
}
}

vec3f eval_tangent(const shape_data& shape, int element, vec2f uv) {
return eval_normal(shape, element, uv);
vec3f shape_tangent(const shape_data& shape, int element, vec2f uv) {
return shape_normal(shape, element, uv);
}

vec2f eval_texcoord(const shape_data& shape, int element, vec2f uv) {
vec2f shape_texcoord(const shape_data& shape, int element, vec2f uv) {
if (shape.texcoords.empty()) return uv;
if (!shape.points.empty()) {
auto& point = shape.points[element];
Expand All @@ -201,7 +201,7 @@ vec2f eval_texcoord(const shape_data& shape, int element, vec2f uv) {
}
}

vec4f eval_color(const shape_data& shape, int element, vec2f uv) {
vec4f shape_color(const shape_data& shape, int element, vec2f uv) {
if (shape.colors.empty()) return {1, 1, 1, 1};
if (!shape.points.empty()) {
auto& point = shape.points[element];
Expand All @@ -222,7 +222,7 @@ vec4f eval_color(const shape_data& shape, int element, vec2f uv) {
}
}

float eval_radius(const shape_data& shape, int element, vec2f uv) {
float shape_radius(const shape_data& shape, int element, vec2f uv) {
if (shape.radius.empty()) return 0;
if (!shape.points.empty()) {
auto& point = shape.points[element];
Expand All @@ -244,7 +244,7 @@ float eval_radius(const shape_data& shape, int element, vec2f uv) {
}

// Evaluate element normals
vec3f eval_element_normal(const shape_data& shape, int element) {
vec3f shape_element_normal(const shape_data& shape, int element) {
if (!shape.points.empty()) {
return {0, 0, 1};
} else if (!shape.lines.empty()) {
Expand All @@ -264,7 +264,7 @@ vec3f eval_element_normal(const shape_data& shape, int element) {
}

// Compute per-vertex normals/tangents for lines/triangles/quads.
vector<vec3f> compute_normals(const shape_data& shape) {
vector<vec3f> shape_normals(const shape_data& shape) {
if (!shape.points.empty()) {
return vector<vec3f>(shape.positions.size(), {0, 0, 1});
} else if (!shape.lines.empty()) {
Expand All @@ -277,19 +277,6 @@ vector<vec3f> compute_normals(const shape_data& shape) {
return vector<vec3f>(shape.positions.size(), {0, 0, 1});
}
}
void compute_normals(vector<vec3f>& normals, const shape_data& shape) {
if (!shape.points.empty()) {
normals.assign(shape.positions.size(), {0, 0, 1});
} else if (!shape.lines.empty()) {
lines_tangents(normals, shape.lines, shape.positions);
} else if (!shape.triangles.empty()) {
triangles_normals(normals, shape.triangles, shape.positions);
} else if (!shape.quads.empty()) {
quads_normals(normals, shape.quads, shape.positions);
} else {
normals.assign(shape.positions.size(), {0, 0, 1});
}
}

// Conversions
shape_data quads_to_triangles(const shape_data& shape) {
Expand Down Expand Up @@ -344,7 +331,7 @@ shape_data remove_normals(const shape_data& shape) {
}
shape_data add_normals(const shape_data& shape) {
auto transformed = shape;
transformed.normals = compute_normals(shape);
transformed.normals = shape_normals(shape);
return transformed;
}

Expand Down Expand Up @@ -416,7 +403,7 @@ shape_data displace_shape(const shape_data& shape,
auto displaced = shape;
displaced.positions = displace_vertices(displaced.positions,
displaced.normals, displaced.texcoords, displacement, height, offset);
displaced.normals = compute_normals(displaced);
displaced.normals = shape_normals(displaced);
return displaced;
}
shape_data displace_shape(const shape_data& shape,
Expand All @@ -427,7 +414,7 @@ shape_data displace_shape(const shape_data& shape,
auto displaced = shape;
displaced.positions = displace_vertices(displaced.positions,
displaced.normals, displaced.texcoords, displacement, height, offset);
displaced.normals = compute_normals(displaced);
displaced.normals = shape_normals(displaced);
return displaced;
}

Expand Down Expand Up @@ -1073,7 +1060,7 @@ vec3f eval_shading_position(const scene_data& scene,
} else if (!shape.lines.empty()) {
return eval_position(scene, instance, element, uv);
} else if (!shape.points.empty()) {
return eval_position(shape, element, uv);
return eval_position(scene, instance, element, uv);
} else {
return {0, 0, 0};
}
Expand Down Expand Up @@ -2640,9 +2627,9 @@ shape_data make_hair(const shape_data& base, vec2i steps, vec2f len, vec2f rad,
auto bnorm = vector<vec3f>{};
auto btexcoord = vector<vec2f>{};
for (auto& point : points) {
bpos.push_back(eval_position(base, point.first, point.second));
bnorm.push_back(eval_normal(base, point.first, point.second));
btexcoord.push_back(eval_texcoord(base, point.first, point.second));
bpos.push_back(shape_position(base, point.first, point.second));
bnorm.push_back(shape_normal(base, point.first, point.second));
btexcoord.push_back(shape_texcoord(base, point.first, point.second));
}

auto rng = make_rng(seed, 3);
Expand Down Expand Up @@ -2711,9 +2698,9 @@ shape_data make_random_points(
auto samples = sample_shape(shape, num, seed);
auto points = make_points(num, 1, radius);
for (auto idx : range(num)) {
points.positions[idx] = eval_position(
points.positions[idx] = shape_position(
shape, samples[idx].first, samples[idx].second);
points.normals[idx] = eval_normal(
points.normals[idx] = shape_normal(
shape, samples[idx].first, samples[idx].second);
}
return points;
Expand All @@ -2727,9 +2714,9 @@ shape_data make_random_hairs(const shape_data& shape, int num, int steps,
auto rng = make_rng(seed);
for (auto idx : range(num)) {
auto offset = idx * (steps + 1);
auto position = eval_position(
auto position = shape_position(
shape, samples[idx].first, samples[idx].second);
auto direction = eval_normal(
auto direction = shape_normal(
shape, samples[idx].first, samples[idx].second);
auto length = lerp(len.x, len.y, rand1f(rng));
hairs.positions[offset] = position;
Expand All @@ -2756,9 +2743,9 @@ shape_data make_hair2(const shape_data& base, vec2i steps, vec2f len,
auto bnormals = vector<vec3f>{};
auto btexcoord = vector<vec2f>{};
for (auto& point : points) {
bpositions.push_back(eval_position(base, point.first, point.second));
bnormals.push_back(eval_normal(base, point.first, point.second));
btexcoord.push_back(eval_texcoord(base, point.first, point.second));
bpositions.push_back(shape_position(base, point.first, point.second));
bnormals.push_back(shape_normal(base, point.first, point.second));
btexcoord.push_back(shape_texcoord(base, point.first, point.second));
}

auto shape = make_lines(steps.y, steps.x, {1, 1}, {1, 1}, radius);
Expand Down
17 changes: 8 additions & 9 deletions libs/yocto/yocto_scene.h
Original file line number Diff line number Diff line change
Expand Up @@ -282,19 +282,18 @@ template <typename PFunc, typename NFunc>
inline shape_data make_quads(int steps, PFunc&& position, NFunc&& normal);

// Interpolate vertex data
vec3f eval_position(const shape_data& shape, int element, vec2f uv);
vec3f eval_normal(const shape_data& shape, int element, vec2f uv);
vec3f eval_tangent(const shape_data& shape, int element, vec2f uv);
vec2f eval_texcoord(const shape_data& shape, int element, vec2f uv);
vec4f eval_color(const shape_data& shape, int element, vec2f uv);
float eval_radius(const shape_data& shape, int element, vec2f uv);
vec3f shape_position(const shape_data& shape, int element, vec2f uv);
vec3f shape_normal(const shape_data& shape, int element, vec2f uv);
vec3f shape_tangent(const shape_data& shape, int element, vec2f uv);
vec2f shape_texcoord(const shape_data& shape, int element, vec2f uv);
vec4f shape_color(const shape_data& shape, int element, vec2f uv);
float shape_radius(const shape_data& shape, int element, vec2f uv);

// Evaluate element normals
vec3f eval_element_normal(const shape_data& shape, int element);
vec3f shape_element_normal(const shape_data& shape, int element);

// Compute per-vertex normals/tangents for lines/triangles/quads.
vector<vec3f> compute_normals(const shape_data& shape);
void compute_normals(vector<vec3f>& normals, const shape_data& shape);
vector<vec3f> shape_normals(const shape_data& shape);

// Conversions
shape_data quads_to_triangles(const shape_data& shape);
Expand Down

0 comments on commit 5307158

Please sign in to comment.