Skip to content

Commit

Permalink
[feature] add basic scene w/ velocity/acceleration (#33)
Browse files Browse the repository at this point in the history
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
abdelrahim-hentabli and pre-commit-ci[bot] authored May 19, 2024
1 parent 389e425 commit d69a890
Show file tree
Hide file tree
Showing 15 changed files with 79 additions and 6 deletions.
2 changes: 2 additions & 0 deletions include/acceleration_structures/hierarchy.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,7 @@ class Hierarchy {
// bounding boxes intersect the ray.
void Intersection_Candidates(const Ray &ray,
std::vector<int> &candidates) const;

void Update();
};
#endif
5 changes: 5 additions & 0 deletions include/camera.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ class Camera {
ivec2 number_pixels; // number of pixels: x and y direction
Pixel *colors; // Pixel data; row-major order

// Describes updating the location and rotation of camera
vec3 velocity;
vec3 acceleration;

Camera();
~Camera();
Camera(const Camera &other);
Expand All @@ -65,5 +69,6 @@ class Camera {
int j = pixel_index[1];
colors[j * number_pixels[0] + i] = color;
}
void Update(double deltaT);
};
#endif
5 changes: 5 additions & 0 deletions include/objects/mesh.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ class Mesh : public Object {
std::vector<vec3> vertices;
std::vector<ivec3> triangles;
Box box;
// Describes updating the location and rotation of object
vec3 velocity;
vec3 acceleration;

public:
Mesh() {}
Expand All @@ -20,5 +23,7 @@ class Mesh : public Object {
bool Intersect_Triangle(const Ray &ray, int tri, double &dist) const;
void Read_Obj(const char *file);
Box Bounding_Box(int part) const override;
void Update(double deltaT) override;
};

#endif
2 changes: 2 additions & 0 deletions include/objects/object.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,7 @@ class Object {
// If part>=0, return the bounding box for the specified part.
// If part<0, return the bounding box for the whole object.
virtual Box Bounding_Box(int part) const = 0;
virtual void Update(double deltaT) = 0;
};

#endif
5 changes: 5 additions & 0 deletions include/objects/plane.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,17 @@ class Plane : public Object {
public:
vec3 x1;
vec3 normal;
// Describes updating the location and rotation of object
vec3 velocity;
vec3 acceleration;

Plane(const vec3 &point, const vec3 &normal)
: x1(point), normal(normal.normalized()) {}

virtual Hit Intersection(const Ray &ray, int part) const override;
virtual vec3 Normal(const vec3 &point, int part) const override;
virtual Box Bounding_Box(int part) const override;
void Update(double deltaT) override;
};

#endif
6 changes: 6 additions & 0 deletions include/objects/sphere.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,18 @@ class Sphere : public Object {
vec3 center;
double radius;

// Describes updating the location and rotation of object
vec3 velocity;
vec3 acceleration;

public:
Sphere(const vec3 &center_input, double radius_input)
: center(center_input), radius(radius_input) {}

virtual Hit Intersection(const Ray &ray, int part) const override;
virtual vec3 Normal(const vec3 &point, int part) const override;
virtual Box Bounding_Box(int part) const override;
void Update(double deltaT) override;
};

#endif
2 changes: 2 additions & 0 deletions include/render_world.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,7 @@ class Render_World {

vec3 Cast_Ray(const Ray &ray, int recursion_depth);
Hit Closest_Intersection(const Ray &ray);

void Update(double deltaT);
};
#endif
2 changes: 2 additions & 0 deletions src/acceleration_structures/hierarchy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,5 @@ void Hierarchy::Intersection_Candidates(const Ray &ray,
check_nodes.pop();
}
}

void Hierarchy::Update() {}
5 changes: 5 additions & 0 deletions src/camera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,8 @@ vec3 Camera::World_Position(const ivec2 &pixel_index) {
cellCenterScreenSpace[1] * vertical_vector);
return result;
}

void Camera::Update(double deltaT) {
position += deltaT * velocity;
velocity += deltaT * acceleration;
}
2 changes: 2 additions & 0 deletions src/objects/mesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,3 +148,5 @@ Box Mesh::Bounding_Box(int part) const {
b.Include_Point(vertices[triangles[part][2]]);
return b;
}

void Mesh::Update(double deltaT) {}
2 changes: 2 additions & 0 deletions src/objects/plane.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,5 @@ Box Plane::Bounding_Box(int part) const {
b.lo = -b.hi;
return b;
}

void Plane::Update(double deltaT) {}
2 changes: 2 additions & 0 deletions src/objects/sphere.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,5 @@ Box Sphere::Bounding_Box(int part) const {
box.lo = center - radius_vector;
return box;
}

void Sphere::Update(double deltaT) {}
11 changes: 11 additions & 0 deletions src/render_world.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,14 @@ void Render_World::Clear_Hierarchy() {
hierarchy.entries.clear();
hierarchy.tree.clear();
}

void Render_World::Update(double deltaT) {
if (abs(deltaT) < std::numeric_limits<double>::epsilon()) {
return;
}
camera.Update(deltaT);
for (Object *object : objects) {
object->Update(deltaT);
}
hierarchy.Update();
}
2 changes: 1 addition & 1 deletion tools/demos/image/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ int main(int argc, char **argv) {

// Parse commandline options
while (1) {
int opt = getopt(argc, argv, "s:i:m:o:x:y:h");
int opt = getopt(argc, argv, "s:i:o:x:y:h");
if (opt == -1) break;
switch (opt) {
case 's':
Expand Down
32 changes: 27 additions & 5 deletions tools/demos/video/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,36 @@
#include "parse.hpp"

void Usage(const char *exec) {
std::cerr << "Usage: " << exec
<< " -i <test-file> [ -s <solution-file> ] [ -o <stats-file> ] [ "
"-x <debug-x-coord> -y <debug-y-coord> ]"
<< std::endl;
std::cerr << "Usage: " << exec << " -i <test-file>" << std::endl;
exit(1);
}

int main(int argc, char **argv) {
const char *input_file = 0;
Pixel *data = nullptr;
Dump_mp4(data, 352, 288, "output.mp4");

// Parse commandline options
while (1) {
int opt = getopt(argc, argv, "s:i:m:o:x:y:h");
if (opt == -1) break;
switch (opt) {
case 'i':
input_file = optarg;
break;
default:
break;
}
}
if (!input_file) Usage(argv[0]);

int width = 0;
int height = 0;
Render_World world;

// Parse test scene file
Parse(world, width, height, input_file);

// Render the image
world.Render();
Dump_mp4(data, width, height, "output.mp4");
}

0 comments on commit d69a890

Please sign in to comment.