Browse Source

More Ray work.

Godzil 4 years ago
parent
commit
8faf1db3be
3 changed files with 46 additions and 1 deletions
  1. 13 0
      source/include/ray.h
  2. 1 1
      tests/CMakeLists.txt
  3. 32 0
      tests/ray_test.cpp

+ 13 - 0
source/include/ray.h

@@ -9,4 +9,17 @@
 #ifndef DORAYME_RAY_H
 #define DORAYME_RAY_H
 
+#include <tuple.h>
+
+class Ray
+{
+public:
+    Vector direction;
+    Point origin;
+
+    Ray(Point origin, Vector direction) : origin(origin), direction(direction) { };
+
+    Tuple position(double t) { return this->origin + this->direction * t; };
+};
+
 #endif //DORAYME_RAY_H

+ 1 - 1
tests/CMakeLists.txt

@@ -3,7 +3,7 @@ project(DoRayTested)
 set(THREADS_PREFER_PTHREAD_FLAG ON)
 find_package(Threads REQUIRED)
 
-set(TESTS_SRC tuple_test.cpp colour_test.cpp canvas_test.cpp matrix_test.cpp transformation_test.cpp)
+set(TESTS_SRC tuple_test.cpp colour_test.cpp canvas_test.cpp matrix_test.cpp transformation_test.cpp ray_test.cpp)
 
 add_executable(testMyRays)
 target_include_directories(testMyRays PUBLIC ${gtest_SOURCE_DIR}/include ${gtest_SOURCE_DIR})

+ 32 - 0
tests/ray_test.cpp

@@ -0,0 +1,32 @@
+/*
+ *  DoRayMe - a quick and dirty Raytracer
+ *  Ray unit tests
+ *
+ *  Created by Manoël Trapier
+ *  Copyright (c) 2020 986-Studio.
+ *
+ */
+#include <ray.h>
+#include <gtest/gtest.h>
+
+
+TEST(RayTest, Creating_a_ray_and_querying_it)
+{
+    Point origin = Point(1, 2, 3);
+    Vector direction = Vector(4, 5, 6);
+
+    Ray r = Ray(origin, direction);
+
+    ASSERT_EQ(r.origin, origin);
+    ASSERT_EQ(r.direction, direction);
+}
+
+TEST(RayTest, Computing_a_point_from_a_distance)
+{
+    Ray r = Ray(Point(2, 3, 4), Vector(1, 0, 0));
+
+    ASSERT_EQ(r.position(0), Point(2, 3, 4));
+    ASSERT_EQ(r.position(1), Point(3, 3, 4));
+    ASSERT_EQ(r.position(-1), Point(1, 3, 4));
+    ASSERT_EQ(r.position(2.5), Point(4.5, 3, 4));
+}