Skip to content

Commit

Permalink
displacement mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
ssloy committed Jan 27, 2019
1 parent 45ea36a commit 2c9e70f
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 3 deletions.
Binary file modified out.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 5 additions & 3 deletions tinykaboom.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@
#include "geometry.h"

const float sphere_radius = 1.5;
const float noise_amplitude = 0.2;

float signed_distance(const Vec3f &p) {
return p.norm() - sphere_radius;
Vec3f s = Vec3f(p).normalize(sphere_radius);
float displacement = sin(16*s.x)*sin(16*s.y)*sin(16*s.z)*noise_amplitude;
return p.norm() - (sphere_radius + displacement);
}

bool sphere_trace(const Vec3f &orig, const Vec3f &dir, Vec3f &pos) {
Expand Down Expand Up @@ -48,8 +51,7 @@ int main() {
if (sphere_trace(Vec3f(0, 0, 3), Vec3f(dir_x, dir_y, dir_z).normalize(), hit)) { // the camera is placed to (0,0,3) and it looks along the -z axis
Vec3f light_dir = (Vec3f(10, 10, 10) - hit).normalize(); // one light is placed to (10,10,10)
float light_intensity = std::max(0.4f, light_dir*distance_field_normal(hit));
float displacement = (sin(16*hit.x)*sin(16*hit.y)*sin(16*hit.z) + 1.)/2.;
framebuffer[i+j*width] = Vec3f(1, 1, 1)*displacement*light_intensity;
framebuffer[i+j*width] = Vec3f(1, 1, 1)*light_intensity;
} else {
framebuffer[i+j*width] = Vec3f(0.2, 0.7, 0.8); // background color
}
Expand Down

0 comments on commit 2c9e70f

Please sign in to comment.