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

Commit

Permalink
Add mesh transform
Browse files Browse the repository at this point in the history
  • Loading branch information
raywan committed May 24, 2019
1 parent c39b286 commit b6b58d8
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 45 deletions.
4 changes: 2 additions & 2 deletions TODO.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ Build:
Uncategorized:
[ ] Tile size independent multithreaded rendering
Height and width are must be multiples of the tile size currently
[ ] Add transform to Mesh
[ ] Use memory arena for allocations
[ ] Render suzanne
[ ] Render Cornell Box
[x] Add transform to Mesh @done (19-05-24 01:02)
[x] Render suzanne @done (19-05-24 01:02)

Miscellaneous:
[x] Fix off by one error in tile compositing @BUG @done (19-05-23 13:31)
2 changes: 1 addition & 1 deletion src/camera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ Camera camera_init_default() {
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
43.0f, // fov
2.0f // aperature
);
}
Expand Down
6 changes: 3 additions & 3 deletions src/global.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@
#define BIAS 0.0001f
#define REFRACT_BIAS 0.00001f

#define MAX_DEPTH 5
#define MAX_DEPTH 3
#define SAMPLES_PER_PIXEL 4

#define USE_GLOBAL_ILLUMINATION 0
#define NUM_PT_SAMPLES 32
#define USE_GLOBAL_ILLUMINATION 1
#define NUM_PT_SAMPLES 16

#define TILE_X 16
#define TILE_Y 16
Expand Down
11 changes: 11 additions & 0 deletions src/mesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -221,3 +221,14 @@ Mesh mesh_make_plane() {

return result;
}

void mesh_attach_transform(Mesh *mesh, Transform obj_world_tr) {
mesh->obj_world_tr = obj_world_tr;
for (int i = 0; i < mesh->v.size(); i++) {
mesh->v[i] = rwtr_pt3_apply(&mesh->obj_world_tr, mesh->v[i]);
}

for (int i = 0; i < mesh->n.size(); i++) {
mesh->n[i] = rwtr_n3_apply(&mesh->obj_world_tr, mesh->n[i]);
}
}
5 changes: 5 additions & 0 deletions src/mesh.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,19 @@ typedef struct Mesh {
std::vector<int> v_idx;
std::vector<int> uv_idx;
std::vector<int> n_idx;
std::vector<int> t_idx;
// Raw data from the OBJ file
std::vector<Vec3> v;
std::vector<Vec2> uv;
std::vector<Vec3> n;
std::vector<Vec3> t;
// Processed face data, index aligned and for GL_ARRAY_BUFFER rendering
// NOTE(ray): This is useful for debugging, but we'll be using the packed data
// most of the time
std::vector<Vec3> buf_v;
std::vector<Vec2> buf_uv;
std::vector<Vec3> buf_n;
std::vector<Vec3> buf_t;

// For EBO
float *packed;
Expand All @@ -47,4 +50,6 @@ Rect3 mesh_get_bounds(Mesh *m);

Mesh mesh_make_plane();

void mesh_attach_transform(Mesh *mesh, Transform obj_world_tr);

#endif
42 changes: 24 additions & 18 deletions src/primitive.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,27 +144,33 @@ bool disk_intersect(Plane *plane, float radius, Ray *r, IntersectInfo *out_ii) {
return false;
}

bool mesh_tri_intersect(Mesh *mesh, int f_idx, Ray *r, IntersectInfo *out_ii) {
Triangle triangle;
triangle.v0 = mesh->v[mesh->v_idx[f_idx * 3]];
triangle.v1 = mesh->v[mesh->v_idx[f_idx * 3 + 1]];
triangle.v2 = mesh->v[mesh->v_idx[f_idx * 3 + 2]];
if (triangle_intersect(&triangle, r, out_ii)) {
rwth_atomic_add_i64((int64_t volatile *) &mtr_num_triangle_isect, 1);
// Get surface properties: texture coordinates, vertex normal
Vec3 col = {triangle.u , triangle.v, triangle.w};
out_ii->color = col;
Vec2 uv0 = mesh->uv[mesh->uv_idx[f_idx * 3]];
Vec2 uv1 = mesh->uv[mesh->uv_idx[f_idx * 3 + 1]];
Vec2 uv2 = mesh->uv[mesh->uv_idx[f_idx * 3 + 2]];
out_ii->tex_coord = (triangle.w * uv0) + (triangle.u * uv1) + (triangle.v * uv2);
Vec3 n0 = mesh->n[mesh->n_idx[f_idx * 3]];
Vec3 n1 = mesh->n[mesh->n_idx[f_idx * 3 + 1]];
Vec3 n2 = mesh->n[mesh->n_idx[f_idx * 3 + 2]];
out_ii->normal = (triangle.w * n0) + (triangle.u * n1) + (triangle.v * n2);
return true;
}
return false;
}

bool mesh_intersect(Mesh *mesh, Ray *r, IntersectInfo *out_ii) {
bool did_intersect = false;
for (int j = 0; j < mesh->f.size(); j++) {
Triangle triangle;
triangle.v0 = mesh->v[mesh->v_idx[j * 3]];
triangle.v1 = mesh->v[mesh->v_idx[j * 3 + 1]];
triangle.v2 = mesh->v[mesh->v_idx[j * 3 + 2]];
if (triangle_intersect(&triangle, r, out_ii)) {
rwth_atomic_add_i64((int64_t volatile *) &mtr_num_triangle_isect, 1);
// Get surface properties: texture coordinates, vertex normal
Vec3 col = {triangle.u , triangle.v, triangle.w};
out_ii->color = col;
Vec2 uv0 = mesh->uv[mesh->uv_idx[j * 3]];
Vec2 uv1 = mesh->uv[mesh->uv_idx[j * 3 + 1]];
Vec2 uv2 = mesh->uv[mesh->uv_idx[j * 3 + 2]];
out_ii->tex_coord = (triangle.w * uv0) + (triangle.u * uv1) + (triangle.v * uv2);
Vec3 n0 = mesh->n[mesh->n_idx[j * 3]];
Vec3 n1 = mesh->n[mesh->n_idx[j * 3 + 1]];
Vec3 n2 = mesh->n[mesh->n_idx[j * 3 + 2]];
out_ii->normal = (triangle.w * n0) + (triangle.u * n1) + (triangle.v * n2);

if (mesh_tri_intersect(mesh, j, r, out_ii)) {
did_intersect = true;
}
}
Expand Down
5 changes: 4 additions & 1 deletion src/render.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,10 @@ Vec3 cast_ray(World *world, Ray *r, int cur_depth) {
} else {
// "Skybox", just a gradient
float t = 0.5f*(r->dir.y + 1.0f);
color = ((1.0f - t)*rwm_v3_init(1.0f, 1.0f, 1.0f) + t * rwm_v3_init(0.5f, 0.7f, 1.0f)) * 0.5f;
// NOTE(ray): Hack? Don't sample skybox if we're sampling indirect light
if (r->type != RT_GI) {
color = ((1.0f - t)*rwm_v3_init(1.0f, 1.0f, 1.0f) + t * rwm_v3_init(0.5f, 0.7f, 1.0f)) * 0.5f;
}
}
return color;
}
Expand Down
43 changes: 23 additions & 20 deletions src/world.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,30 @@ void create_world(World *world) {

Mesh *suz = new Mesh;
load_obj(suz, "suzanne.obj", false);
suz->obj_world_tr = rwtr_trs(
rwm_v3_init(0.0,1.0,-1.0),
rwm_v3_init(1.0,1.0,1.0),
-1, 0);
mesh_attach_transform(suz, rwtr_trs(
rwm_v3_init(-1.0,0.75,-0.80),
rwm_v3_init(0.5, 0.5, 0.5),
1, 45.0)
);
Mesh *plane = new Mesh;
*plane = mesh_make_plane();
plane->obj_world_tr = rwtr_trs(
rwm_v3_init(0.0,1.0,-1.0),
rwm_v3_init(1.0,1.0,1.0),
-1, 0);
mesh_attach_transform(plane, rwtr_trs(
rwm_v3_init(0.0,0.0,0.0),
rwm_v3_init(2.0,2.0,2.0),
0, 0)
);

puts("Creating world...");
// world->spheres.push_back({rwm_v3_init(0,0.25,-0.8), 0.5});
// world->spheres.push_back({rwm_v3_init(-1,0.25,-0.5), 0.5});
world->spheres.push_back(sphere_create(rwm_v3_init(0.6,0.12,0.0), 0.25));
world->spheres.push_back(sphere_create(rwm_v3_init(0,0.25,-0.5), 0.5));
// world->spheres.push_back({rwm_v3_init(1,0.25,-1), 0.5});
world->spheres.push_back({rwm_v3_init(1,0.25,-1), 0.5});
// world->spheres.push_back({rwm_v3_init(-1,0.25,-1), 0.5});
// world->spheres.push_back({rwm_v3_init(-1,0.25,-2), 1.5});
// world->sphere_materials.push_back({M_REFLECT, rwm_v3_init(1.0, 1.0, 1.0)});
world->sphere_materials.push_back({M_RR, rwm_v3_init(1.0, 1.0, 1.0), 1.55});
world->sphere_materials.push_back({M_DIFFUSE, rwm_v3_init(1.0, 0.0, 0.0)});
world->sphere_materials.push_back({M_REFLECT, rwm_v3_init(1.0, 1.0, 1.0)});
// world->sphere_materials.push_back({M_DIFFUSE, rwm_v3_init(1.0, 1.0, 1.0)});

// world->planes.push_back({rwm_v3_init(0,-0.5,-1), rwm_v3_init(0,1.0,0)});
Expand All @@ -41,24 +43,25 @@ void create_world(World *world) {
// rwm_v3_init(1,-1.0,-5),
// rwm_v3_init(0,1.0,-5)
// });
// world->meshes.push_back(&suz);
world->meshes.push_back(suz);
world->meshes.push_back(plane);
// 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});
puts("Adding lights");

world->lights.push_back({
LT_SPHERE,
rwm_v3_init(2.0, 2.0, 1.0), // position
rwm_v3_init(1.0f, 1.0f, 1.0f), // color
300.0f, // intensity
LT_SPHERE,
rwm_v3_init(2.0, 2.0, 1.0), // position
rwm_v3_init(1.0f, 1.0f, 1.0f), // color
100.0f, // intensity
});

// world->lights.push_back((Light) {
// LT_SPHERE,
// rwm_v3_init(-2.0, 2.0, -3.0), // position
// rwm_v3_init(0.0f, 0.0f, 1.0f), // color
// 600.0f, // intensity
// world->lights.push_back({
// LT_SPHERE,
// rwm_v3_init(-2.0, 2.0, -3.0), // position
// rwm_v3_init(0.0f, 0.0f, 1.0f), // color
// 600.0f, // intensity
// });

#if 1
Expand Down

0 comments on commit b6b58d8

Please sign in to comment.