Skip to content

Commit

Permalink
Add UV mapping for cubes for textures, normals
Browse files Browse the repository at this point in the history
  • Loading branch information
terrynsun committed Oct 5, 2015
1 parent fcfde74 commit 91f21c6
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/interactions.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ __device__ glm::vec3 sampleSpecular(Ray &ray, glm::vec3 normal,
}

__device__ glm::vec3 sampleTexture(Texture t, glm::vec2 uv) {
int x = 1.5f * glm::clamp(uv.x, 0.f, 1.f) * (t.width - 1);
int x = glm::clamp(uv.x, 0.f, 1.f) * (t.width - 1);
int y = glm::clamp(uv.y, 0.f, 1.f) * (t.height - 1);
x %= t.width;
y %= t.height;
Expand Down
18 changes: 17 additions & 1 deletion src/intersections.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,23 @@ __host__ __device__ float boxIntersectionTest(Geom &box, Ray r,
tmin_n = tmax_n;
outside = false;
}
intersectionPoint = multiplyMV(box.transform, glm::vec4(getPointOnRay(q, tmin), 1.0f));
glm::vec3 localPoint = getPointOnRay(q, tmin);

if (glm::abs(glm::abs(localPoint.x) - 0.5) < 0.001) {
// xy plane
uv.x = localPoint.z + .5f;
uv.y = localPoint.y + .5f;
} else if (glm::abs(glm::abs(localPoint.y) - 0.5) < 0.001) {
// xz plane
uv.x = localPoint.x + 0.5f;
uv.y = localPoint.z + 0.5f;
} else if (glm::abs(glm::abs(localPoint.z) - 0.5) < 0.001) {
// xy plane
uv.x = (localPoint.x + 0.5f);
uv.y = - (localPoint.y - 0.5f);
}

intersectionPoint = multiplyMV(box.transform, glm::vec4(localPoint, 1.0f));
normal = glm::normalize(multiplyMV(box.transform,
glm::vec4(outside ? tmin_n : -tmin_n, 0.0f)));
return glm::length(r.origin - intersectionPoint);
Expand Down

0 comments on commit 91f21c6

Please sign in to comment.