Skip to content

Commit

Permalink
crude shading
Browse files Browse the repository at this point in the history
  • Loading branch information
ssloy committed Jan 27, 2019
1 parent c45987e commit ef22c62
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
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.
13 changes: 12 additions & 1 deletion tinykaboom.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@ bool sphere_trace(const Vec3f &orig, const Vec3f &dir, Vec3f &pos) {
return false;
}

Vec3f distance_field_normal(const Vec3f &pos) {
const float eps = 0.1;
float d = signed_distance(pos);
float nx = signed_distance(pos + Vec3f(eps, 0, 0)) - d;
float ny = signed_distance(pos + Vec3f(0, eps, 0)) - d;
float nz = signed_distance(pos + Vec3f(0, 0, eps)) - d;
return Vec3f(nx, ny, nz).normalize();
}

int main() {
const int width = 640;
const int height = 480;
Expand All @@ -37,7 +46,9 @@ int main() {
float dir_z = -height/(2.*tan(fov/2.));
Vec3f hit;
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
framebuffer[i+j*width] = Vec3f(1, 1, 1);
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));
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 ef22c62

Please sign in to comment.