Skip to content

Commit

Permalink
Started working on 2D patterns.
Browse files Browse the repository at this point in the history
  • Loading branch information
Godzil committed Mar 4, 2020
1 parent 83c12db commit 5c10d65
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 3 deletions.
3 changes: 2 additions & 1 deletion source/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ endif()
# First most is build as a library
add_library(rayonnement STATIC)

file(GLOB RAY_HEADERS ${CMAKE_CURRENT_SOURCE_DIR}/include/*.h ${CMAKE_CURRENT_SOURCE_DIR}/pattern/*.h)
file(GLOB RAY_HEADERS ${CMAKE_CURRENT_SOURCE_DIR}/include/*.h ${CMAKE_CURRENT_SOURCE_DIR}/pattern/*.h
${CMAKE_CURRENT_SOURCE_DIR}/uvpattern/*.h)

file(GLOB RAY_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/*.cpp ${CMAKE_CURRENT_SOURCE_DIR}/shapes/*.cpp
${CMAKE_CURRENT_SOURCE_DIR}/worldbuilder/*.cpp)
Expand Down
28 changes: 28 additions & 0 deletions source/include/uv_pattern.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* DoRayMe - a quick and dirty Raytracer
* UV Pattern header
*
* Created by Manoël Trapier
* Copyright (c) 2020 986-Studio.
*
*/
#ifndef DORAYME_UV_PATTERN_H
#define DORAYME_UV_PATTERN_H

#include <colour.h>

class UVPattern
{
public:
Colour a;
Colour b;
double width;
double height;

UVPattern(double width, double height, Colour a, Colour b) : a(a), b(b),
width(width), height(height) {};

virtual Colour uvPatternAt(double u, double v) = 0;
};

#endif /* DORAYME_UV_PATTERN_H */
32 changes: 32 additions & 0 deletions source/uvpattern/uv_checkers.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* DoRayMe - a quick and dirty Raytracer
* UV Checkers header
*
* Created by Manoël Trapier
* Copyright (c) 2020 986-Studio.
*
*/
#ifndef DORAYME_UV_CHECKERS_H
#define DORAYME_UV_CHECKERS_H

#include <uv_pattern.h>
#include <math.h>

class UVCheckers : public UVPattern
{
public:
UVCheckers(double width, double height, Colour a, Colour b) : UVPattern(width, height, a, b) {};

Colour uvPatternAt(double u, double v) {
double u2 = floor(u * this->width);
double v2 = floor(v * this->width);

if (fmod((u2 + v2), 2) == 0)
{
return this->a;
}
return this->b;
};
};

#endif /* DORAYME_UV_CHECKERS_H */
2 changes: 1 addition & 1 deletion tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ set(TESTS_SRC math_test.cpp tuple_test.cpp colour_test.cpp canvas_test.cpp matri

add_executable(testMyRays)
target_include_directories(testMyRays PUBLIC ${gtest_SOURCE_DIR}/include ${gtest_SOURCE_DIR})
target_include_directories(testMyRays PUBLIC ../source/include)
target_include_directories(testMyRays PUBLIC ../source/include ../source/patter ../source/uvpattern)
target_sources(testMyRays PRIVATE ${TESTS_SRC})
target_link_libraries(testMyRays gtest gtest_main rayonnement Threads::Threads)

Expand Down
15 changes: 14 additions & 1 deletion tests/pattern_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
#include <sphere.h>
#include <gtest/gtest.h>
#include <material.h>
#include <uv_pattern.h>
#include <uv_checkers.h>

#ifdef ENABLE_LUA_SUPPORT
extern "C" {
Expand Down Expand Up @@ -260,4 +262,15 @@ TEST(PatternTest, Simple_test_of_a_lua_pattern)

lua_close(L);
}
#endif
#endif

TEST(PatternTest, Checkers_pattern_in_2D)
{
UVCheckers checkers = UVCheckers(2, 2, black, white);

ASSERT_EQ(checkers.uvPatternAt(0.0, 0.0), black);
ASSERT_EQ(checkers.uvPatternAt(0.5, 0.0), white);
ASSERT_EQ(checkers.uvPatternAt(0.0, 0.5), white);
ASSERT_EQ(checkers.uvPatternAt(0.5, 0.5), black);
ASSERT_EQ(checkers.uvPatternAt(1.0, 1.0), black);
}

0 comments on commit 5c10d65

Please sign in to comment.