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

Commit

Permalink
BIG refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
raywan committed May 17, 2019
1 parent 96baad9 commit abc38cb
Show file tree
Hide file tree
Showing 24 changed files with 1,110 additions and 997 deletions.
4 changes: 4 additions & 0 deletions TODO.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[ ] build.bat for Windows
[ ] Create Windows entry point i.e. win32_main.cpp
[ ] Tile size independent multithreaded rendering
- Height and width are must be multiples of the tile size currently
12 changes: 11 additions & 1 deletion build.ninja
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,19 @@ rule create_sym_link
build dirs: make_dirs
build fresh: clean_all
build sym: create_sym_link
build build/bvh.o: compile src/bvh.cpp
build build/render.o: compile src/render.cpp
build build/utils.o: compile src/utils.cpp
build build/mesh.o: compile src/mesh.cpp
build build/camera.o: compile src/camera.cpp
build build/metrics.o: compile src/metrics.cpp
build build/ray.o: compile src/ray.cpp
build build/primitive.o: compile src/primitive.cpp
build build/world.o: compile src/world.cpp
build build/main.o: compile src/main.cpp
build toy: link build/mesh.o build/main.o
build toy: link build/bvh.o build/render.o build/utils.o build/mesh.o $
build/camera.o build/metrics.o build/ray.o build/primitive.o $
build/world.o build/main.o

default dirs
default toy
Expand Down
2 changes: 1 addition & 1 deletion include/rw
Submodule rw updated 5 files
+9 −1 rw_math.h
+2 −2 rw_th.h
+3 −3 rw_time.h
+1 −1 rw_transform.h
+1 −1 rw_types.h
65 changes: 50 additions & 15 deletions src/bvh.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "bvh.h"
#include "mesh.h"
#include "world.h"

#define BOUND_MESH 1

Expand All @@ -17,26 +18,21 @@ std::vector<BVHPrimitive> bvh_preprocess_world(World *w) {
// Spheres
for (int i = 0; i < w->spheres.size(); i++) {
BVHPrimitive prim;
prim.type = PT_SPHERE;
prim.idx = i;
prim.type = PT_SPHERE;
prim.bounds = sphere_get_bounds(&w->spheres[i]);
prim.centroid = 0.5 * prim.bounds.min_p + 0.5f * prim.bounds.max_p;
result.push_back(prim);
result.push_back(prim);
}
// Meshes
for (int i = 0; i < w->meshes.size(); i++) {
#if BOUND_MESH
BVHPrimitive prim;
prim.idx = i;
prim.type = PT_MESH;
prim.bounds = mesh_get_bounds(w->meshes[i]);
prim.centroid = 0.5 * prim.bounds.min_p + 0.5f * prim.bounds.max_p;
result.push_back(prim);
#else
// Loop through each triangle in the mesh
int cur_v_idx = 0;
Mesh *cur_mesh = w->meshes[i];
BVHPrimitive prim;
prim.idx = i;
for (int j = 0; j < cur_mesh->f.size(); j++) {
prim.f_idx = j;
// Intialize the bounds with one point of the triangle
prim.bounds = rwm_r3_init_p(cur_mesh->v[cur_mesh->v_idx[cur_v_idx++]]);
for (int k = 1; k < cur_mesh->f[j]; k++) {
Expand All @@ -47,19 +43,58 @@ std::vector<BVHPrimitive> bvh_preprocess_world(World *w) {
prim.centroid = 0.5 * prim.bounds.min_p + 0.5f * prim.bounds.max_p;
result.push_back(prim);
}
#endif
}

return result;
}

BVHNode *bvh_recursive_build() {
BVHNode *bvh_recursive_build(std::vector<BVHPrimitive> *prims, int lo, int hi) {
// Calculate total bounds of the primitives
BVHNode *node = (BVHNode *) malloc(sizeof(BVHNode));
// NOTE(ray): I don't think i need to do this computation here
Rect3 total_bounds = (*prims)[lo].bounds;
for (int i = lo + 1; i < hi; i++) {
total_bounds = rwm_r3_union(total_bounds, (*prims)[i].bounds);
}

int n_primitives = hi - lo;

if (n_primitives == 1) {
node->leaf = true;
node->left = NULL;
node->right = NULL;
node->bounds = total_bounds;
node->prim_idx = lo;
} else {
// Partition based on the centroid
Rect3 centroid_bounds = rwm_r3_init_p((*prims)[lo].centroid);
for (int i = lo + 1; i < hi; i++) {
centroid_bounds = rwm_r3_union_p(centroid_bounds, (*prims)[i].centroid);
}
int axis = rwm_r3_max_extent(centroid_bounds);
int mid = (hi - lo)/2;
if (centroid_bounds.max_p.e[axis] == centroid_bounds.min_p.e[axis]) {
node->leaf = true;
node->left = NULL;
node->right = NULL;
node->bounds = total_bounds;
node->prim_idx = lo;
} else {
node->left = bvh_recursive_build(prims, lo, mid);
node->right = bvh_recursive_build(prims, mid, hi);
node->bounds = rwm_r3_union(node->left->bounds, node->right->bounds);
node->split_axis = axis;
}
}
return node;
}

BVHNode *bvh_build(World *world) {
// Process world, loop over all primitives and meshes to create BVHPrimitives
// bvh_recursive_build();
std::vector<BVHPrimitive> prims = bvh_preprocess_world(world);
BVHNode *root = bvh_recursive_build(&prims, 0, prims.size());
return root;
}

bool bvh_intersect(Ray *r, IntersectInfo *ii) {

return false;
}
18 changes: 8 additions & 10 deletions src/bvh.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,19 @@
#define __BVH_H__

#include <rw/rw_math.h>
#include "primitive.h"

struct World;

struct BVHNode {
Rect3 bounds;
int prim_idx;
int split_axis;
int left;
int right;
BVHNode *left;
BVHNode *right;
bool leaf;
};

enum PrimitiveType {
PT_SPHERE,
PT_TRIANGLE,
PT_PLANE,
PT_MESH,
};


struct BVHPrimitive {
int idx;
int f_idx; // for meshes
Expand All @@ -28,5 +24,7 @@ struct BVHPrimitive {
};

std::vector<BVHPrimitive> bvh_preprocess_world(World *w);
BVHNode *bvh_build(World *world);
bool bvh_intersect(Ray *r, IntersectInfo *ii);

#endif
56 changes: 56 additions & 0 deletions src/camera.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#include "camera.h"
#include <math.h>
#include <float.h>

#include "global.h"

Camera camera_init(Vec3 position, Vec3 target, Vec3 up, float fov, float aperature) {
Camera result;
result.position = position;
result.target = target;
result.up = up;
result.lens_radius = aperature/2.0f;
// result.focus_dist = rwm_v3_length(position - target);
// TODO(ray): Add depth of field later
float focus_dist = 1.0f;
Vec3 dir = rwm_v3_normalize(position - target);
Vec3 right = rwm_v3_normalize(rwm_v3_cross(up, dir));
Vec3 new_up = rwm_v3_cross(dir, right);

float theta = rwm_to_radians(fov);
float half_height = tan(theta/2);
float half_width = ASPECT * half_height;
// result.lower_left = position - (half_width * right) - (half_height * new_up) - dir;
result.lower_left = position - (half_width * focus_dist * right) - (half_height * focus_dist * new_up) - dir * focus_dist;
result.horizontal = 2 * half_width * focus_dist * right;
result.vertical = 2 * half_height * focus_dist * new_up;
// result.position = rwm_v3_zero();
// result.lower_left = rwm_v3_init(-2.0, -1.0, -1.0);
// result.horizontal = rwm_v3_init(4.0, 0.0, 0.0);
// result.vertical = rwm_v3_init(0.0, 2.0, 0.0);
return result;
}

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, 0.0), // up
90.0f, // fov
2.0f // aperature
);
}

// 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
float u = (float) res_x/(float) WIDTH;
float v = (float) res_y/(float) HEIGHT;

Ray result = ray_init(
camera->position,
camera->lower_left + (u*camera->horizontal) + (v*camera->vertical) - camera->position
);
result.type = RT_CAMERA;
return result;
}
28 changes: 28 additions & 0 deletions src/camera.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#ifndef __CAMERA_H__
#define __CAMERA_H__

#include <rw/rw_math.h>
#include "ray.h"

struct Camera {
Vec3 position;
Vec3 target;
Vec3 up;
float fov;

// Computed
Vec3 lower_left;
Vec3 horizontal;
Vec3 vertical;
float lens_radius;
float focus_dist;
};

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

// For simplicity, instead of using PBRT camera, use the RTiaW camera
Ray camera_get_ray(Camera *camera, float res_x, float res_y);


#endif
22 changes: 22 additions & 0 deletions src/global.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#ifndef __GLOBAL_H__
#define __GLOBAL_H__

#define WIDTH 640
#define HEIGHT 480
#define ASPECT ((float)WIDTH/(float)HEIGHT)

#define EPSILON 0.00001f
#define BIAS 0.0001f
#define REFRACT_BIAS 0.00001f

#define MAX_DEPTH 2
#define SAMPLES_PER_PIXEL 4

#define USE_GLOBAL_ILLUMINATION 1
#define NUM_PT_SAMPLES 32

#define TILE_X 16
#define TILE_Y 16
#define NUM_THREADS 8

#endif
Loading

0 comments on commit abc38cb

Please sign in to comment.