Skip to content
This repository has been archived by the owner on Nov 26, 2022. It is now read-only.

Commit

Permalink
Add cornell box function and move out camera init
Browse files Browse the repository at this point in the history
  • Loading branch information
raywan committed May 28, 2019
1 parent 0ae8918 commit ab251a5
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 53 deletions.
32 changes: 29 additions & 3 deletions src/camera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,40 @@ Camera camera_init(Vec3 position, Vec3 target, Vec3 up, float fov, float aperatu

Camera camera_init_default() {
return camera_init(
rwm_v3_init(0.0, 0.0, 0.0), // position
rwm_v3_init(0.0, 0.0, -1.0), // target
rwm_v3_init(0.0, 1.0, 4.0), // position
rwm_v3_init(0.0, 1.0, -1.0), // target
rwm_v3_init(0.0, 1.0, 0.0), // up
43.0f, // fov
45.0f, // fov
2.0f // aperature
);
}

Camera create_scene_camera(int camera_shot) {
Camera camera;

if (camera_shot == 0) {
camera = camera_init(
rwm_v3_init(0.0, 1.0, 1.0), // position
rwm_v3_init(0.0, 0.0, -1.0), // target
rwm_v3_init(0.0, 1.0, 0.0), // up
90.0f, // fov
2.0f // aperature
);
} else if (camera_shot == 1) {
camera = camera_init(
rwm_v3_init(-1.0, 1.0, 1.0), // position
rwm_v3_init(0.0, 0.0, 0.0), // target
rwm_v3_init(0.0, 1.0, 0.0), // up
90.0f, // fov
2.0f // aperature
);
} else {
camera = camera_init_default();
}

return camera;
}

// For simplicity, instead of using PBRT camera, use the RTiaW camera
Ray camera_get_ray(Camera *camera, float res_x, float res_y) {
// Normalize raster space coordinates
Expand Down
1 change: 1 addition & 0 deletions src/camera.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ struct Camera {

Camera camera_init(Vec3 position, Vec3 target, Vec3 up, float fov, float aperature);
Camera camera_init_default();
Camera create_scene_camera(int camera_shot);

// For simplicity, instead of using PBRT camera, use the RTiaW camera
Ray camera_get_ray(Camera *camera, float res_x, float res_y);
Expand Down
4 changes: 2 additions & 2 deletions src/global.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
#define REFRACT_BIAS 0.00001f

#define MAX_DEPTH 3
#define SAMPLES_PER_PIXEL 4
#define SAMPLES_PER_PIXEL 2048

#define USE_GLOBAL_ILLUMINATION 1
#define NUM_PT_SAMPLES 16
#define NUM_PT_SAMPLES 1

#define TILE_X 16
#define TILE_Y 16
Expand Down
23 changes: 1 addition & 22 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,28 +56,7 @@ int main(int argc, char *argv[]) {

// Initialize the camera
puts("Initializing camera...");
Camera camera;
if (camera_shot == 0) {
#if 0
camera = camera_init_default();
} else if (camera_shot == 1) {
#endif
camera = camera_init(
rwm_v3_init(0.0, 1.0, 1.0), // position
rwm_v3_init(0.0, 0.0, -1.0), // target
rwm_v3_init(0.0, 1.0, 0.0), // up
90.0f, // fov
2.0f // aperature
);
} else if (camera_shot == 2) {
camera = camera_init(
rwm_v3_init(-1.0, 1.0, 1.0), // position
rwm_v3_init(0.0, 0.0, 0.0), // target
rwm_v3_init(0.0, 1.0, 0.0), // up
90.0f, // fov
2.0f // aperature
);
}
Camera camera = create_scene_camera(camera_shot);

// Intialize scene
World world;
Expand Down
4 changes: 2 additions & 2 deletions src/metrics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ void print_run_info() {
}

void print_post_run_metrics() {
printf("Render time (rwtm): %fs\n\n", rwtm_to_sec(rwtm_since(mtr_start_time)));
printf("Rays per second: %fR/s\n\n", mtr_num_rays/rwtm_to_sec(rwtm_since(mtr_start_time)));
printf("Render time (rwtm): %f s\n\n", rwtm_to_sec(rwtm_since(mtr_start_time)));
printf("Rays per second: %f r/s\n\n", mtr_num_rays/rwtm_to_sec(rwtm_since(mtr_start_time)));
printf("Total number of rays: %llu\n", mtr_num_rays);
printf("Total number of primary rays: %llu\n", mtr_num_primary_rays);
printf("Total number of GI rays: %llu\n", mtr_num_gi_rays);
Expand Down
2 changes: 1 addition & 1 deletion src/render.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,10 @@ Vec3 cast_ray(World *world, Ray *r, int cur_depth) {
}

Vec3 indirect_lighting = rwm_v3_zero();
float pdf = uniform_hemisphere_pdf();
#if USE_GLOBAL_ILLUMINATION
Vec3 Nt, Nb;
create_coordinate_system(ii.normal, &Nt, &Nb);
float pdf = uniform_hemisphere_pdf();

for (int n = 0; n < NUM_PT_SAMPLES; n++) {
Point2 u = rwm_v2_init(distribution(generator), distribution(generator));
Expand Down
25 changes: 2 additions & 23 deletions src/win32_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ unsigned int worker(void *t_arg) {
}

int main(int argc, char *argv[]) {
int camera_shot = 0;
int camera_shot = -1;
if (argc > 1) {
camera_shot = atoi(argv[1]);
if (argc == 3) {
Expand All @@ -60,28 +60,7 @@ int main(int argc, char *argv[]) {

// Initialize the camera
puts("Initializing camera...");
Camera camera;
if (camera_shot == 0) {
#if 0
camera = camera_init_default();
} else if (camera_shot == 1) {
#endif
camera = camera_init(
rwm_v3_init(0.0, 1.0, 1.0), // position
rwm_v3_init(0.0, 0.0, -1.0), // target
rwm_v3_init(0.0, 1.0, 0.0), // up
90.0f, // fov
2.0f // aperature
);
} else if (camera_shot == 2) {
camera = camera_init(
rwm_v3_init(-1.0, 1.0, 1.0), // position
rwm_v3_init(0.0, 0.0, 0.0), // target
rwm_v3_init(0.0, 1.0, 0.0), // up
90.0f, // fov
2.0f // aperature
);
}
Camera camera = create_scene_camera(camera_shot);

// Intialize scene
World world;
Expand Down
60 changes: 60 additions & 0 deletions src/world.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,63 @@

#include "bvh.h"

void create_cornell_box(World *world) {

Mesh *bottom_plane = new Mesh;
*bottom_plane = mesh_make_plane();
mesh_attach_transform(bottom_plane, rwtr_trs(
rwm_v3_init(0.0,0.0,0.0),
rwm_v3_init(2.0,2.0,2.0),
-1, 0)
);
Mesh *top_plane = new Mesh;
*top_plane = mesh_make_plane();
mesh_attach_transform(top_plane, rwtr_trs(
rwm_v3_init(0.0,2.0,0.0),
rwm_v3_init(2.0,2.0,2.0),
0, 180)
);
Mesh *back_plane = new Mesh;
*back_plane = mesh_make_plane();
mesh_attach_transform(back_plane, rwtr_trs(
rwm_v3_init(0.0,0.0,-2.0),
rwm_v3_init(2.0,2.0,2.0),
0, 90)
);
Mesh *left_plane = new Mesh;
*left_plane = mesh_make_plane();
mesh_attach_transform(left_plane, rwtr_trs(
rwm_v3_init(-1.5,0.0,0.0),
rwm_v3_init(2.0,2.0,2.0),
2, -90)
);
Mesh *right_plane = new Mesh;
*right_plane = mesh_make_plane();
mesh_attach_transform(right_plane, rwtr_trs(
rwm_v3_init(1.5,0.0,0.0),
rwm_v3_init(2.0,2.0,2.0),
2, 90)
);
world->meshes.push_back(bottom_plane);
world->meshes.push_back(top_plane);
world->meshes.push_back(back_plane);
world->meshes.push_back(left_plane);
world->meshes.push_back(right_plane);

world->mesh_materials.push_back({M_DIFFUSE, rwm_v3_init(1.0, 1.0, 1.0), 0, false});
world->mesh_materials.push_back({M_DIFFUSE, rwm_v3_init(1.0, 1.0, 1.0), 0, false});
world->mesh_materials.push_back({M_DIFFUSE, rwm_v3_init(1.0, 1.0, 1.0), 0, false});
world->mesh_materials.push_back({M_DIFFUSE, rwm_v3_init(1.0, 0.0, 0.0), 0, false});
world->mesh_materials.push_back({M_DIFFUSE, rwm_v3_init(0.0, 1.0, 0.0), 0, false});

world->lights.push_back({
LT_SPHERE,
rwm_v3_init(0.0, 1.9, -1.0), // position
rwm_v3_init(1.0f, 1.0f, 1.0f), // color
5.0f, // intensity
});
}

void create_world(World *world) {
printf("Loading OBJ files:\n\t");

Expand Down Expand Up @@ -48,6 +105,9 @@ void create_world(World *world) {
// world->mesh_materials.push_back({M_REFLECT, rwm_v3_zero()});
world->mesh_materials.push_back({M_DIFFUSE, rwm_v3_init(0.8, 0.8, 0.8), 0, false});
world->mesh_materials.push_back({M_DIFFUSE, rwm_v3_init(1.0, 1.0, 1.0), 0, true});

// create_cornell_box(world);

puts("Adding lights");

world->lights.push_back({
Expand Down

0 comments on commit ab251a5

Please sign in to comment.