Modify .clang-tidy, apply fixes/formatting

This commit is contained in:
Stephen Seo 2021-08-24 14:14:42 +09:00
parent ef72d87da3
commit d25e445147
7 changed files with 232 additions and 261 deletions

View file

@ -1,5 +1,5 @@
--- ---
Checks: 'clang-diagnostic-*,clang-analyzer-*,*' Checks: 'clang-diagnostic-*,clang-analyzer-*,*,-llvmlibc-*,-fuchsia-*,-readability-magic-numbers,-cppcoreguidelines-avoid-magic-numbers,-modernize-use-trailing-return-type,-llvm-header-guard,-cppcoreguidelines-pro-bounds-pointer-arithmetic,-altera-struct-pack-align,-misc-non-private-member-variables-in-classes,-modernize-use-nodiscard,-cppcoreguidelines-pro-type-union-access,-llvm-else-after-return,-readability-else-after-return'
WarningsAsErrors: '' WarningsAsErrors: ''
HeaderFilterRegex: '' HeaderFilterRegex: ''
AnalyzeTemporaryDtors: false AnalyzeTemporaryDtors: false
@ -10,18 +10,14 @@ CheckOptions:
value: '::realloc' value: '::realloc'
- key: cppcoreguidelines-owning-memory.LegacyResourceConsumers - key: cppcoreguidelines-owning-memory.LegacyResourceConsumers
value: '::free;::realloc;::freopen;::fclose' value: '::free;::realloc;::freopen;::fclose'
- key: bugprone-reserved-identifier.Invert
value: 'false'
- key: cert-dcl16-c.IgnoreMacros
value: 'true'
- key: bugprone-narrowing-conversions.PedanticMode
value: 'false'
- key: altera-struct-pack-align.MaxConfiguredAlignment
value: '128'
- key: bugprone-unused-return-value.CheckedFunctions
value: '::std::async;::std::launder;::std::remove;::std::remove_if;::std::unique;::std::unique_ptr::release;::std::basic_string::empty;::std::vector::empty;::std::back_inserter;::std::distance;::std::find;::std::find_if;::std::inserter;::std::lower_bound;::std::make_pair;::std::map::count;::std::map::find;::std::map::lower_bound;::std::multimap::equal_range;::std::multimap::upper_bound;::std::set::count;::std::set::find;::std::setfill;::std::setprecision;::std::setw;::std::upper_bound;::std::vector::at;::bsearch;::ferror;::feof;::isalnum;::isalpha;::isblank;::iscntrl;::isdigit;::isgraph;::islower;::isprint;::ispunct;::isspace;::isupper;::iswalnum;::iswprint;::iswspace;::isxdigit;::memchr;::memcmp;::strcmp;::strcoll;::strncmp;::strpbrk;::strrchr;::strspn;::strstr;::wcscmp;::access;::bind;::connect;::difftime;::dlsym;::fnmatch;::getaddrinfo;::getopt;::htonl;::htons;::iconv_open;::inet_addr;::isascii;::isatty;::mmap;::newlocale;::openat;::pathconf;::pthread_equal;::pthread_getspecific;::pthread_mutex_trylock;::readdir;::readlink;::recvmsg;::regexec;::scandir;::semget;::setjmp;::shm_open;::shmget;::sigismember;::strcasecmp;::strsignal;::ttyname'
- key: modernize-use-auto.MinTypeNameLength - key: modernize-use-auto.MinTypeNameLength
value: '5' value: '5'
- key: bugprone-reserved-identifier.Invert
value: 'false'
- key: bugprone-narrowing-conversions.PedanticMode
value: 'false'
- key: bugprone-unused-return-value.CheckedFunctions
value: '::std::async;::std::launder;::std::remove;::std::remove_if;::std::unique;::std::unique_ptr::release;::std::basic_string::empty;::std::vector::empty;::std::back_inserter;::std::distance;::std::find;::std::find_if;::std::inserter;::std::lower_bound;::std::make_pair;::std::map::count;::std::map::find;::std::map::lower_bound;::std::multimap::equal_range;::std::multimap::upper_bound;::std::set::count;::std::set::find;::std::setfill;::std::setprecision;::std::setw;::std::upper_bound;::std::vector::at;::bsearch;::ferror;::feof;::isalnum;::isalpha;::isblank;::iscntrl;::isdigit;::isgraph;::islower;::isprint;::ispunct;::isspace;::isupper;::iswalnum;::iswprint;::iswspace;::isxdigit;::memchr;::memcmp;::strcmp;::strcoll;::strncmp;::strpbrk;::strrchr;::strspn;::strstr;::wcscmp;::access;::bind;::connect;::difftime;::dlsym;::fnmatch;::getaddrinfo;::getopt;::htonl;::htons;::iconv_open;::inet_addr;::isascii;::isatty;::mmap;::newlocale;::openat;::pathconf;::pthread_equal;::pthread_getspecific;::pthread_mutex_trylock;::readdir;::readlink;::recvmsg;::regexec;::scandir;::semget;::setjmp;::shm_open;::shmget;::sigismember;::strcasecmp;::strsignal;::ttyname'
- key: cert-dcl51-cpp.AggressiveDependentMemberLookup - key: cert-dcl51-cpp.AggressiveDependentMemberLookup
value: 'false' value: 'false'
- key: hicpp-use-auto.MinTypeNameLength - key: hicpp-use-auto.MinTypeNameLength
@ -62,8 +58,6 @@ CheckOptions:
value: 'true' value: 'true'
- key: cppcoreguidelines-init-variables.IncludeStyle - key: cppcoreguidelines-init-variables.IncludeStyle
value: llvm value: llvm
- key: modernize-use-nodiscard.ReplacementString
value: '[[nodiscard]]'
- key: modernize-loop-convert.MakeReverseRangeHeader - key: modernize-loop-convert.MakeReverseRangeHeader
value: '' value: ''
- key: misc-definitions-in-headers.HeaderFileExtensions - key: misc-definitions-in-headers.HeaderFileExtensions
@ -88,10 +82,10 @@ CheckOptions:
value: 'c;cc;cpp;cxx' value: 'c;cc;cpp;cxx'
- key: modernize-loop-convert.MakeReverseRangeFunction - key: modernize-loop-convert.MakeReverseRangeFunction
value: '' value: ''
- key: bugprone-suspicious-missing-comma.SizeThreshold
value: '5'
- key: readability-inconsistent-declaration-parameter-name.IgnoreMacros - key: readability-inconsistent-declaration-parameter-name.IgnoreMacros
value: 'true' value: 'true'
- key: bugprone-suspicious-missing-comma.SizeThreshold
value: '5'
- key: readability-identifier-naming.IgnoreFailedSplit - key: readability-identifier-naming.IgnoreFailedSplit
value: 'false' value: 'false'
- key: hicpp-multiway-paths-covered.WarnOnMissingElse - key: hicpp-multiway-paths-covered.WarnOnMissingElse
@ -136,8 +130,6 @@ CheckOptions:
value: '8388608' value: '8388608'
- key: readability-simplify-boolean-expr.ChainedConditionalAssignment - key: readability-simplify-boolean-expr.ChainedConditionalAssignment
value: 'false' value: 'false'
- key: cppcoreguidelines-avoid-magic-numbers.IgnoreAllFloatingPointValues
value: 'false'
- key: cert-oop54-cpp.WarnOnlyIfThisHasSuspiciousField - key: cert-oop54-cpp.WarnOnlyIfThisHasSuspiciousField
value: 'false' value: 'false'
- key: cert-err09-cpp.CheckThrowTemporaries - key: cert-err09-cpp.CheckThrowTemporaries
@ -154,18 +146,16 @@ CheckOptions:
value: ';h;hh;hpp;hxx' value: ';h;hh;hpp;hxx'
- key: portability-simd-intrinsics.Suggest - key: portability-simd-intrinsics.Suggest
value: 'false' value: 'false'
- key: modernize-make-shared.MakeSmartPtrFunction
value: 'std::make_shared'
- key: readability-function-size.LineThreshold
value: '4294967295'
- key: cppcoreguidelines-pro-bounds-constant-array-index.GslHeader - key: cppcoreguidelines-pro-bounds-constant-array-index.GslHeader
value: '' value: ''
- key: readability-function-size.LineThreshold
value: '4294967295'
- key: modernize-make-shared.MakeSmartPtrFunctionHeader - key: modernize-make-shared.MakeSmartPtrFunctionHeader
value: '<memory>' value: '<memory>'
- key: modernize-make-shared.MakeSmartPtrFunction
value: 'std::make_shared'
- key: modernize-use-override.IgnoreDestructors - key: modernize-use-override.IgnoreDestructors
value: 'false' value: 'false'
- key: misc-non-private-member-variables-in-classes.IgnorePublicMemberVariables
value: 'false'
- key: bugprone-sizeof-expression.WarnOnSizeOfConstant - key: bugprone-sizeof-expression.WarnOnSizeOfConstant
value: 'true' value: 'true'
- key: readability-redundant-string-init.StringNames - key: readability-redundant-string-init.StringNames
@ -174,8 +164,6 @@ CheckOptions:
value: 'true' value: 'true'
- key: modernize-use-emplace.ContainersWithPushBack - key: modernize-use-emplace.ContainersWithPushBack
value: '::std::vector;::std::list;::std::deque' value: '::std::vector;::std::list;::std::deque'
- key: readability-magic-numbers.IgnoreBitFieldsWidths
value: 'true'
- key: modernize-make-unique.IncludeStyle - key: modernize-make-unique.IncludeStyle
value: llvm value: llvm
- key: modernize-use-override.OverrideSpelling - key: modernize-use-override.OverrideSpelling
@ -190,10 +178,6 @@ CheckOptions:
value: '' value: ''
- key: cppcoreguidelines-pro-type-member-init.IgnoreArrays - key: cppcoreguidelines-pro-type-member-init.IgnoreArrays
value: 'false' value: 'false'
- key: readability-else-after-return.WarnOnUnfixable
value: 'true'
- key: cppcoreguidelines-avoid-magic-numbers.IgnoredFloatingPointValues
value: '1.0;100.0;'
- key: modernize-use-emplace.IgnoreImplicitConstructors - key: modernize-use-emplace.IgnoreImplicitConstructors
value: 'false' value: 'false'
- key: cppcoreguidelines-macro-usage.IgnoreCommandLineMacros - key: cppcoreguidelines-macro-usage.IgnoreCommandLineMacros
@ -204,14 +188,12 @@ CheckOptions:
value: llvm value: llvm
- key: objc-forbidden-subclassing.ForbiddenSuperClassNames - key: objc-forbidden-subclassing.ForbiddenSuperClassNames
value: 'ABNewPersonViewController;ABPeoplePickerNavigationController;ABPersonViewController;ABUnknownPersonViewController;NSHashTable;NSMapTable;NSPointerArray;NSPointerFunctions;NSTimer;UIActionSheet;UIAlertView;UIImagePickerController;UITextInputMode;UIWebView' value: 'ABNewPersonViewController;ABPeoplePickerNavigationController;ABPersonViewController;ABUnknownPersonViewController;NSHashTable;NSMapTable;NSPointerArray;NSPointerFunctions;NSTimer;UIActionSheet;UIAlertView;UIImagePickerController;UITextInputMode;UIWebView'
- key: readability-magic-numbers.IgnoreAllFloatingPointValues
value: 'false'
- key: hicpp-use-auto.RemoveStars - key: hicpp-use-auto.RemoveStars
value: 'false' value: 'false'
- key: bugprone-misplaced-widening-cast.CheckImplicitCasts - key: bugprone-misplaced-widening-cast.CheckImplicitCasts
value: 'false' value: 'false'
- key: readability-uppercase-literal-suffix.NewSuffixes - key: bugprone-suspicious-missing-comma.RatioThreshold
value: '' value: '0.200000'
- key: modernize-loop-convert.MinConfidence - key: modernize-loop-convert.MinConfidence
value: reasonable value: reasonable
- key: performance-unnecessary-value-param.AllowedTypes - key: performance-unnecessary-value-param.AllowedTypes
@ -220,22 +202,18 @@ CheckOptions:
value: 'true' value: 'true'
- key: misc-throw-by-value-catch-by-reference.MaxSize - key: misc-throw-by-value-catch-by-reference.MaxSize
value: '-1' value: '-1'
- key: cppcoreguidelines-avoid-magic-numbers.IgnorePowersOf2IntegerValues - key: readability-uppercase-literal-suffix.NewSuffixes
value: 'false' value: ''
- key: google-readability-namespace-comments.SpacesBeforeComments - key: google-readability-namespace-comments.SpacesBeforeComments
value: '2' value: '2'
- key: cppcoreguidelines-avoid-magic-numbers.IgnoreBitFieldsWidths
value: 'true'
- key: cert-err61-cpp.CheckThrowTemporaries - key: cert-err61-cpp.CheckThrowTemporaries
value: 'true' value: 'true'
- key: cppcoreguidelines-avoid-magic-numbers.IgnoredIntegerValues - key: hicpp-use-emplace.TupleMakeFunctions
value: '1;2;3;4;' value: '::std::make_pair;::std::make_tuple'
- key: cppcoreguidelines-no-malloc.Allocations - key: cppcoreguidelines-no-malloc.Allocations
value: '::malloc;::calloc' value: '::malloc;::calloc'
- key: readability-function-size.BranchThreshold - key: readability-function-size.BranchThreshold
value: '4294967295' value: '4294967295'
- key: bugprone-suspicious-missing-comma.RatioThreshold
value: '0.200000'
- key: hicpp-function-size.LineThreshold - key: hicpp-function-size.LineThreshold
value: '4294967295' value: '4294967295'
- key: readability-implicit-bool-conversion.AllowIntegerConditions - key: readability-implicit-bool-conversion.AllowIntegerConditions
@ -254,8 +232,6 @@ CheckOptions:
value: '800' value: '800'
- key: cert-msc51-cpp.DisallowedSeedTypes - key: cert-msc51-cpp.DisallowedSeedTypes
value: 'time_t,std::time_t' value: 'time_t,std::time_t'
- key: hicpp-use-emplace.TupleMakeFunctions
value: '::std::make_pair;::std::make_tuple'
- key: bugprone-reserved-identifier.AggressiveDependentMemberLookup - key: bugprone-reserved-identifier.AggressiveDependentMemberLookup
value: 'false' value: 'false'
- key: modernize-use-equals-default.IgnoreMacros - key: modernize-use-equals-default.IgnoreMacros
@ -272,8 +248,6 @@ CheckOptions:
value: '::std::shared_ptr;::std::unique_ptr;::std::auto_ptr;::std::weak_ptr' value: '::std::shared_ptr;::std::unique_ptr;::std::auto_ptr;::std::weak_ptr'
- key: cppcoreguidelines-no-malloc.Deallocations - key: cppcoreguidelines-no-malloc.Deallocations
value: '::free' value: '::free'
- key: readability-magic-numbers.IgnorePowersOf2IntegerValues
value: 'false'
- key: misc-unused-parameters.StrictMode - key: misc-unused-parameters.StrictMode
value: 'false' value: 'false'
- key: cert-oop11-cpp.IncludeStyle - key: cert-oop11-cpp.IncludeStyle
@ -348,8 +322,6 @@ CheckOptions:
value: 'true' value: 'true'
- key: bugprone-argument-comment.StrictMode - key: bugprone-argument-comment.StrictMode
value: '0' value: '0'
- key: misc-non-private-member-variables-in-classes.IgnoreClassesWithAllMemberVariablesBeingPublic
value: 'false'
- key: modernize-replace-random-shuffle.IncludeStyle - key: modernize-replace-random-shuffle.IncludeStyle
value: llvm value: llvm
- key: modernize-use-bool-literals.IgnoreMacros - key: modernize-use-bool-literals.IgnoreMacros
@ -358,14 +330,14 @@ CheckOptions:
value: 'true' value: 'true'
- key: google-readability-namespace-comments.ShortNamespaceLines - key: google-readability-namespace-comments.ShortNamespaceLines
value: '10' value: '10'
- key: hicpp-use-override.OverrideSpelling
value: override
- key: modernize-avoid-bind.PermissiveParameterList
value: 'false'
- key: modernize-use-override.FinalSpelling
value: final
- key: bugprone-suspicious-string-compare.StringCompareLikeFunctions - key: bugprone-suspicious-string-compare.StringCompareLikeFunctions
value: '' value: ''
- key: modernize-avoid-bind.PermissiveParameterList
value: 'false'
- key: hicpp-use-override.OverrideSpelling
value: override
- key: modernize-use-override.FinalSpelling
value: final
- key: cert-err09-cpp.MaxSize - key: cert-err09-cpp.MaxSize
value: '-1' value: '-1'
- key: hicpp-use-equals-default.IgnoreMacros - key: hicpp-use-equals-default.IgnoreMacros
@ -390,16 +362,16 @@ CheckOptions:
value: 'false' value: 'false'
- key: hicpp-no-malloc.Reallocations - key: hicpp-no-malloc.Reallocations
value: '::realloc' value: '::realloc'
- key: google-runtime-int.UnsignedTypePrefix
value: uint
- key: bugprone-argument-comment.CommentCharacterLiterals
value: '0'
- key: bugprone-argument-comment.CommentIntegerLiterals
value: '0'
- key: performance-no-automatic-move.AllowedTypes - key: performance-no-automatic-move.AllowedTypes
value: '' value: ''
- key: bugprone-argument-comment.CommentCharacterLiterals
value: '0'
- key: google-runtime-int.UnsignedTypePrefix
value: uint
- key: modernize-pass-by-value.IncludeStyle - key: modernize-pass-by-value.IncludeStyle
value: llvm value: llvm
- key: bugprone-argument-comment.CommentIntegerLiterals
value: '0'
- key: bugprone-argument-comment.CommentFloatLiterals - key: bugprone-argument-comment.CommentFloatLiterals
value: '0' value: '0'
- key: bugprone-too-small-loop-variable.MagnitudeBitsUpperLimit - key: bugprone-too-small-loop-variable.MagnitudeBitsUpperLimit
@ -408,14 +380,10 @@ CheckOptions:
value: 'false' value: 'false'
- key: abseil-string-find-startswith.AbseilStringsMatchHeader - key: abseil-string-find-startswith.AbseilStringsMatchHeader
value: 'absl/strings/match.h' value: 'absl/strings/match.h'
- key: readability-else-after-return.WarnOnConditionVariables
value: 'true'
- key: modernize-use-nullptr.NullMacros - key: modernize-use-nullptr.NullMacros
value: 'NULL' value: 'NULL'
- key: cppcoreguidelines-macro-usage.AllowedRegexp - key: cppcoreguidelines-macro-usage.AllowedRegexp
value: '^DEBUG_*' value: '^DEBUG_*'
- key: llvm-header-guard.HeaderFileExtensions
value: ';h;hh;hpp;hxx'
- key: cppcoreguidelines-narrowing-conversions.PedanticMode - key: cppcoreguidelines-narrowing-conversions.PedanticMode
value: 'false' value: 'false'
- key: modernize-make-shared.IgnoreDefaultInitialization - key: modernize-make-shared.IgnoreDefaultInitialization
@ -454,12 +422,10 @@ CheckOptions:
value: 'false' value: 'false'
- key: performance-inefficient-string-concatenation.StrictMode - key: performance-inefficient-string-concatenation.StrictMode
value: 'false' value: 'false'
- key: fuchsia-header-anon-namespaces.HeaderFileExtensions
value: ';h;hh;hpp;hxx'
- key: readability-magic-numbers.IgnoredFloatingPointValues
value: '1.0;100.0;'
- key: readability-implicit-bool-conversion.AllowPointerConditions - key: readability-implicit-bool-conversion.AllowPointerConditions
value: 'false' value: 'false'
- key: readability-redundant-declaration.IgnoreMacros
value: 'true'
- key: modernize-make-unique.MakeSmartPtrFunction - key: modernize-make-unique.MakeSmartPtrFunction
value: 'std::make_unique' value: 'std::make_unique'
- key: google-runtime-int.TypeSuffix - key: google-runtime-int.TypeSuffix
@ -474,8 +440,6 @@ CheckOptions:
value: '' value: ''
- key: modernize-make-unique.MakeSmartPtrFunctionHeader - key: modernize-make-unique.MakeSmartPtrFunctionHeader
value: '<memory>' value: '<memory>'
- key: readability-redundant-declaration.IgnoreMacros
value: 'true'
- key: zircon-temporary-objects.Names - key: zircon-temporary-objects.Names
value: '' value: ''
- key: hicpp-use-emplace.TupleTypes - key: hicpp-use-emplace.TupleTypes
@ -517,7 +481,7 @@ CheckOptions:
- key: cert-str34-c.CharTypdefsToIgnore - key: cert-str34-c.CharTypdefsToIgnore
value: '' value: ''
- key: llvm-else-after-return.WarnOnConditionVariables - key: llvm-else-after-return.WarnOnConditionVariables
value: 'false' value: '0'
- key: bugprone-sizeof-expression.WarnOnSizeOfCompareToConstant - key: bugprone-sizeof-expression.WarnOnSizeOfCompareToConstant
value: 'true' value: 'true'
- key: modernize-raw-string-literal.DelimiterStem - key: modernize-raw-string-literal.DelimiterStem
@ -530,26 +494,24 @@ CheckOptions:
value: '1600' value: '1600'
- key: modernize-raw-string-literal.ReplaceShorterLiterals - key: modernize-raw-string-literal.ReplaceShorterLiterals
value: 'false' value: 'false'
- key: readability-magic-numbers.IgnoredIntegerValues - key: performance-inefficient-vector-operation.VectorLikeClasses
value: '1;2;3;4;' value: '::std::vector'
- key: modernize-use-auto.RemoveStars - key: modernize-use-auto.RemoveStars
value: 'false' value: 'false'
- key: abseil-string-find-str-contains.IncludeStyle - key: abseil-string-find-str-contains.IncludeStyle
value: llvm value: llvm
- key: performance-inefficient-vector-operation.VectorLikeClasses
value: '::std::vector'
- key: llvmlibc-restrict-system-libc-headers.Includes
value: '-*'
- key: readability-redundant-member-init.IgnoreBaseInCopyConstructors
value: 'false'
- key: hicpp-use-nullptr.NullMacros
value: ''
- key: portability-simd-intrinsics.Std - key: portability-simd-intrinsics.Std
value: '' value: ''
- key: hicpp-use-nullptr.NullMacros
value: ''
- key: readability-redundant-member-init.IgnoreBaseInCopyConstructors
value: 'false'
- key: cert-dcl16-c.IgnoreMacros
value: 'true'
- key: performance-unnecessary-value-param.IncludeStyle - key: performance-unnecessary-value-param.IncludeStyle
value: llvm value: llvm
- key: llvm-else-after-return.WarnOnUnfixable - key: llvm-else-after-return.WarnOnUnfixable
value: 'false' value: '0'
- key: cert-msc32-c.DisallowedSeedTypes - key: cert-msc32-c.DisallowedSeedTypes
value: 'time_t,std::time_t' value: 'time_t,std::time_t'
... ...

View file

@ -45,7 +45,7 @@ if(CLANG_TIDY)
clang-tidy clang-tidy
COMMAND /usr/bin/clang-tidy COMMAND /usr/bin/clang-tidy
${Example02_ALL_FILES} ${Example02_ALL_FILES}
-config='' -config-file="${CMAKE_CURRENT_SOURCE_DIR}/.clang-tidy"
-- --
-std=c++17 -std=c++17
) )

View file

@ -7,7 +7,7 @@ Ex02::ArgParse::parseArgs(int argc, char **argv,
const Ex02::ArgParse::ArgsType &singleArgs, const Ex02::ArgParse::ArgsType &singleArgs,
const Ex02::ArgParse::ArgsType &doubleArgs) { const Ex02::ArgParse::ArgsType &doubleArgs) {
ParseResult result; ParseResult result;
bool found; bool found = false;
--argc; --argc;
++argv; ++argv;
while (argc > 0) { while (argc > 0) {

View file

@ -1,22 +1,21 @@
#ifndef EXAMPLE_02_ARG_PARSE_HPP #ifndef EXAMPLE_02_ARG_PARSE_HPP
#define EXAMPLE_02_ARG_PARSE_HPP #define EXAMPLE_02_ARG_PARSE_HPP
#define EX02_ARG_PARSE_SINGLE_ARG_PLACEHOLDER "SINGLE_ARG_PLACEHOLDER" constexpr const char *EX02_ARG_PARSE_SINGLE_ARG_PLACEHOLDER =
"SINGLE_ARG_PLACEHOLDER";
#include <string> #include <string>
#include <unordered_map> #include <unordered_map>
#include <unordered_set> #include <unordered_set>
namespace Ex02 { namespace Ex02::ArgParse {
namespace ArgParse {
typedef std::unordered_set<std::string> ArgsType; using ArgsType = std::unordered_set<std::string>;
typedef std::unordered_map<std::string, std::string> ParseResult; using ParseResult = std::unordered_map<std::string, std::string>;
ParseResult parseArgs(int argc, char **argv, const ArgsType &singleArgs, ParseResult parseArgs(int argc, char **argv, const ArgsType &singleArgs,
const ArgsType &doubleArgs); const ArgsType &doubleArgs);
} // namespace ArgParse } // namespace Ex02::ArgParse
} // namespace Ex02
#endif #endif

View file

@ -1,3 +1,6 @@
constexpr unsigned int DEFAULT_WIDTH = 1600;
constexpr unsigned int DEFAULT_HEIGHT = 1600;
#include <iostream> #include <iostream>
#include <string> #include <string>
@ -20,8 +23,8 @@ void printHelp() {
int main(int argc, char **argv) { int main(int argc, char **argv) {
int threadCount = 1; int threadCount = 1;
unsigned int outputWidth = 1600; unsigned int outputWidth = DEFAULT_WIDTH;
unsigned int outputHeight = 1600; unsigned int outputHeight = DEFAULT_HEIGHT;
std::string outputFile = "raytrace_out"; std::string outputFile = "raytrace_out";
{ {
@ -44,7 +47,8 @@ int main(int argc, char **argv) {
if (auto iter = results.find("-h"); iter != results.end()) { if (auto iter = results.find("-h"); iter != results.end()) {
printHelp(); printHelp();
return 0; return 0;
} else if (auto iter = results.find("--help"); iter != results.end()) { }
if (auto iter = results.find("--help"); iter != results.end()) {
printHelp(); printHelp();
return 0; return 0;
} }

View file

@ -9,7 +9,7 @@
#include <glm/gtc/matrix_transform.hpp> #include <glm/gtc/matrix_transform.hpp>
#include <glm/matrix.hpp> #include <glm/matrix.hpp>
const float PI = std::acos(-1.0f); const float PI = std::acos(-1.0F);
Ex02::RT::Pixel::Pixel() : r(0), g(0), b(0) {} Ex02::RT::Pixel::Pixel() : r(0), g(0), b(0) {}
@ -32,9 +32,9 @@ void Ex02::RT::Image::writeToFile(const std::string &filename) const {
for (unsigned int j = 0; j < data.size() / width; ++j) { for (unsigned int j = 0; j < data.size() / width; ++j) {
for (unsigned int i = 0; i < width; ++i) { for (unsigned int i = 0; i < width; ++i) {
out << (int)data.at(i + j * width).r << ' ' out << static_cast<int>(data.at(i + j * width).r) << ' '
<< (int)data.at(i + j * width).g << ' ' << static_cast<int>(data.at(i + j * width).g) << ' '
<< (int)data.at(i + j * width).b << ' '; << static_cast<int>(data.at(i + j * width).b) << ' ';
} }
out << '\n'; out << '\n';
} }
@ -43,28 +43,28 @@ void Ex02::RT::Image::writeToFile(const std::string &filename) const {
/* /*
glm::mat4x4 Ex02::RT::Internal::defaultMVP() { glm::mat4x4 Ex02::RT::Internal::defaultMVP() {
glm::mat4x4 mvp = glm::perspective( glm::mat4x4 mvp = glm::perspective(
PI / 2.0f, PI / 2.0F,
1.0f, 1.0F,
EX02_RAY_TRACER_DEFAULT_NEAR_PLANE, EX02_RAY_TRACER_DEFAULT_NEAR_PLANE,
EX02_RAY_TRACER_DEFAULT_FAR_PLANE); EX02_RAY_TRACER_DEFAULT_FAR_PLANE);
mvp *= glm::lookAt( mvp *= glm::lookAt(
glm::vec3{0.0f, 0.0f, 0.0f}, glm::vec3{0.0F, 0.0F, 0.0F},
glm::vec3{0.0f, 0.0f, -1.0f}, glm::vec3{0.0F, 0.0F, -1.0F},
glm::vec3{0.0f, 1.0f, 0.0f}); glm::vec3{0.0F, 1.0F, 0.0F});
// model pushes back by 2 units // model pushes back by 2 units
mvp = glm::translate( mvp = glm::translate(
mvp, mvp,
glm::vec3{0.0f, 0.0f, 2.0f}); glm::vec3{0.0F, 0.0F, 2.0F});
return mvp; return mvp;
} }
*/ */
Ex02::RT::Internal::LightSource::LightSource() Ex02::RT::Internal::LightSource::LightSource()
: pos{0.0f, 0.0f, 0.0f}, falloffStart(2.0f), : pos{0.0F, 0.0F, 0.0F}, falloffStart(2.0F),
falloffEnd(7.0f), color{1.0f, 1.0f, 1.0f} {} falloffEnd(7.0F), color{1.0F, 1.0F, 1.0F} {}
void Ex02::RT::Internal::LightSource::applyLight(glm::vec3 pos, void Ex02::RT::Internal::LightSource::applyLight(glm::vec3 pos,
Pixel &pixelOut) const { Pixel &pixelOut) const {
@ -72,7 +72,8 @@ void Ex02::RT::Internal::LightSource::applyLight(glm::vec3 pos,
float dist = std::sqrt(pos.x * pos.x + pos.y * pos.y + pos.z * pos.z); float dist = std::sqrt(pos.x * pos.x + pos.y * pos.y + pos.z * pos.z);
if (dist < falloffStart) { if (dist < falloffStart) {
const auto applyColor = [](auto *color, unsigned char *out) { const auto applyColor = [](auto *color, unsigned char *out) {
unsigned int temp = (unsigned int)*out + (unsigned int)(*color * 255.0f); unsigned int temp = static_cast<unsigned int>(*out) +
static_cast<unsigned int>(*color * 255.0F);
if (temp > 255) { if (temp > 255) {
*out = 255; *out = 255;
} else { } else {
@ -85,15 +86,15 @@ void Ex02::RT::Internal::LightSource::applyLight(glm::vec3 pos,
} else if (dist >= falloffStart && dist <= falloffEnd) { } else if (dist >= falloffStart && dist <= falloffEnd) {
const auto applyFalloffColor = [](auto *color, unsigned char *out, const auto applyFalloffColor = [](auto *color, unsigned char *out,
float f) { float f) {
unsigned int temp = unsigned int temp = static_cast<unsigned int>(*out) +
(unsigned int)*out + (unsigned int)(*color * 255.0f * f); static_cast<unsigned int>(*color * 255.0F * f);
if (temp > 255) { if (temp > 255) {
*out = 255; *out = 255;
} else { } else {
*out = temp; *out = temp;
} }
}; };
float f = (1.0f - (dist - falloffStart) / (falloffEnd - falloffStart)); float f = (1.0F - (dist - falloffStart) / (falloffEnd - falloffStart));
applyFalloffColor(&color.x, &pixelOut.r, f); applyFalloffColor(&color.x, &pixelOut.r, f);
applyFalloffColor(&color.y, &pixelOut.g, f); applyFalloffColor(&color.y, &pixelOut.g, f);
applyFalloffColor(&color.z, &pixelOut.b, f); applyFalloffColor(&color.z, &pixelOut.b, f);
@ -106,14 +107,15 @@ void Ex02::RT::Internal::LightSource::applyLight(glm::vec3 pos, Pixel &pixelOut,
float dist = std::sqrt(pos.x * pos.x + pos.y * pos.y + pos.z * pos.z); float dist = std::sqrt(pos.x * pos.x + pos.y * pos.y + pos.z * pos.z);
if (dist < falloffStart) { if (dist < falloffStart) {
const auto applyColor = [](auto *color, unsigned char *out) { const auto applyColor = [](auto *color, unsigned char *out) {
unsigned int temp = (unsigned int)*out + (unsigned int)(*color * 255.0f); unsigned int temp = static_cast<unsigned int>(*out) +
static_cast<unsigned int>(*color * 255.0F);
if (temp > 255) { if (temp > 255) {
*out = 255; *out = 255;
} else { } else {
*out = temp; *out = temp;
} }
}; };
if (mutex) { if (mutex != nullptr) {
std::lock_guard<std::mutex> lock(*mutex); std::lock_guard<std::mutex> lock(*mutex);
applyColor(&color.x, &pixelOut.r); applyColor(&color.x, &pixelOut.r);
applyColor(&color.y, &pixelOut.g); applyColor(&color.y, &pixelOut.g);
@ -126,16 +128,16 @@ void Ex02::RT::Internal::LightSource::applyLight(glm::vec3 pos, Pixel &pixelOut,
} else if (dist >= falloffStart && dist <= falloffEnd) { } else if (dist >= falloffStart && dist <= falloffEnd) {
const auto applyFalloffColor = [](auto *color, unsigned char *out, const auto applyFalloffColor = [](auto *color, unsigned char *out,
float f) { float f) {
unsigned int temp = unsigned int temp = static_cast<unsigned int>(*out) +
(unsigned int)*out + (unsigned int)(*color * 255.0f * f); static_cast<unsigned int>(*color * 255.0F * f);
if (temp > 255) { if (temp > 255) {
*out = 255; *out = 255;
} else { } else {
*out = temp; *out = temp;
} }
}; };
float f = (1.0f - (dist - falloffStart) / (falloffEnd - falloffStart)); float f = (1.0F - (dist - falloffStart) / (falloffEnd - falloffStart));
if (mutex) { if (mutex != nullptr) {
std::lock_guard<std::mutex> lock(*mutex); std::lock_guard<std::mutex> lock(*mutex);
applyFalloffColor(&color.x, &pixelOut.r, f); applyFalloffColor(&color.x, &pixelOut.r, f);
applyFalloffColor(&color.y, &pixelOut.g, f); applyFalloffColor(&color.y, &pixelOut.g, f);
@ -148,7 +150,7 @@ void Ex02::RT::Internal::LightSource::applyLight(glm::vec3 pos, Pixel &pixelOut,
} }
} }
Ex02::RT::Internal::Sphere::Sphere() : pos{0.0f, 0.0f, 0.0f}, radius(2.5f) {} Ex02::RT::Internal::Sphere::Sphere() : pos{0.0F, 0.0F, 0.0F}, radius(2.5F) {}
std::optional<glm::vec3> std::optional<glm::vec3>
Ex02::RT::Internal::Sphere::rayToSphere(glm::vec3 rayPos, Ex02::RT::Internal::Sphere::rayToSphere(glm::vec3 rayPos,
@ -177,7 +179,7 @@ std::optional<glm::vec3> Ex02::RT::Internal::rayToSphere(glm::vec3 rayPos,
sphereRadius * sphereRadius; sphereRadius * sphereRadius;
delta -= temp; delta -= temp;
if (delta < 0.0f) { if (delta < 0.0F) {
return {}; return {};
} else { } else {
temp = rayDirUnit.x * tempVec.x + rayDirUnit.y * tempVec.y + temp = rayDirUnit.x * tempVec.x + rayDirUnit.y * tempVec.y +
@ -186,8 +188,8 @@ std::optional<glm::vec3> Ex02::RT::Internal::rayToSphere(glm::vec3 rayPos,
float dist2 = -temp + std::sqrt(delta); float dist2 = -temp + std::sqrt(delta);
float min = dist > dist2 ? dist2 : dist; float min = dist > dist2 ? dist2 : dist;
float max = dist > dist2 ? dist : dist2; float max = dist > dist2 ? dist : dist2;
if (min < 0.0f) { if (min < 0.0F) {
if (max < 0.0f) { if (max < 0.0F) {
return {}; return {};
} else { } else {
return {rayPos + rayDirUnit * max}; return {rayPos + rayDirUnit * max};
@ -221,7 +223,7 @@ Ex02::RT::Internal::rayToSphereVisible(glm::vec3 rayPos, glm::vec3 rayDirUnit,
glm::vec3 toLightUnit = glm::vec3 toLightUnit =
toLight / std::sqrt(toLight.x * toLight.x + toLight.y * toLight.y + toLight / std::sqrt(toLight.x * toLight.x + toLight.y * toLight.y +
toLight.z * toLight.z); toLight.z * toLight.z);
glm::vec3 toLightPos = *collPos + toLight / 3.0f; glm::vec3 toLightPos = *collPos + toLight / 3.0F;
auto collResult = auto collResult =
Internal::rayToSphere(toLightPos, toLightUnit, spherePos, sphereRadius); Internal::rayToSphere(toLightPos, toLightUnit, spherePos, sphereRadius);
if (collResult) { if (collResult) {
@ -236,20 +238,23 @@ Ex02::RT::Internal::rayToSphereVisible(glm::vec3 rayPos, glm::vec3 rayDirUnit,
Ex02::RT::Image Ex02::RT::renderGraySphere(unsigned int outputWidth, Ex02::RT::Image Ex02::RT::renderGraySphere(unsigned int outputWidth,
unsigned int outputHeight, unsigned int outputHeight,
unsigned int) { unsigned int /*unused*/) {
const glm::vec3 spherePos{0.0f, 0.0f, -2.5f}; const glm::vec3 spherePos{0.0F, 0.0F, -2.5F};
const glm::vec3 lightPos{4.0f, 4.0f, 0.0f}; const glm::vec3 lightPos{4.0F, 4.0F, 0.0F};
Image image(outputWidth, outputHeight); Image image(outputWidth, outputHeight);
const glm::vec3 rayPos{0.0f, 0.0f, 0.0f}; const glm::vec3 rayPos{0.0F, 0.0F, 0.0F};
const float lightFalloffStart = 4.5f; const float lightFalloffStart = 4.5F;
const float lightFalloffEnd = 7.0f; const float lightFalloffEnd = 7.0F;
// if(threadCount <= 1) { // if(threadCount <= 1) {
for (unsigned int j = 0; j < outputHeight; ++j) { for (unsigned int j = 0; j < outputHeight; ++j) {
float offsetY = ((float)j + 0.5f - ((float)outputHeight / 2.0f)); float offsetY = (static_cast<float>(j) + 0.5F -
(static_cast<float>(outputHeight) / 2.0F));
for (unsigned int i = 0; i < outputWidth; ++i) { for (unsigned int i = 0; i < outputWidth; ++i) {
float offsetX = ((float)i + 0.5f - ((float)outputWidth / 2.0f)); float offsetX = (static_cast<float>(i) + 0.5F -
(static_cast<float>(outputWidth) / 2.0F));
glm::vec3 rayDir{offsetX, offsetY, glm::vec3 rayDir{offsetX, offsetY,
-(float)outputHeight * EX02_RAY_TRACER_VIEW_RATIO}; -static_cast<float>(outputHeight) *
EX02_RAY_TRACER_VIEW_RATIO};
glm::vec3 rayDirUnit = glm::vec3 rayDirUnit =
rayDir / std::sqrt(rayDir.x * rayDir.x + rayDir.y * rayDir.y + rayDir / std::sqrt(rayDir.x * rayDir.x + rayDir.y * rayDir.y +
rayDir.z * rayDir.z); rayDir.z * rayDir.z);
@ -266,10 +271,10 @@ Ex02::RT::Image Ex02::RT::renderGraySphere(unsigned int outputWidth,
image.getPixel(i, j).g = 255; image.getPixel(i, j).g = 255;
image.getPixel(i, j).b = 255; image.getPixel(i, j).b = 255;
} else if (dist >= lightFalloffStart && dist <= lightFalloffEnd) { } else if (dist >= lightFalloffStart && dist <= lightFalloffEnd) {
image.getPixel(i, j).r = image.getPixel(i, j).r = static_cast<unsigned char>(
(1.0f - (dist - lightFalloffStart) / (1.0F - (dist - lightFalloffStart) /
(lightFalloffEnd - lightFalloffStart)) * (lightFalloffEnd - lightFalloffStart)) *
255.0f; 255.0F);
image.getPixel(i, j).g = image.getPixel(i, j).r; image.getPixel(i, j).g = image.getPixel(i, j).r;
image.getPixel(i, j).b = image.getPixel(i, j).r; image.getPixel(i, j).b = image.getPixel(i, j).r;
} }
@ -286,79 +291,82 @@ Ex02::RT::Image Ex02::RT::renderColorsWithSpheres(unsigned int outputWidth,
unsigned int outputHeight, unsigned int outputHeight,
unsigned int threadCount) { unsigned int threadCount) {
Image image(outputWidth, outputHeight); Image image(outputWidth, outputHeight);
const glm::vec3 rayPos{0.0f, 0.0f, 0.0f}; const glm::vec3 rayPos{0.0F, 0.0F, 0.0F};
std::array<Internal::Sphere, 7> spheres; std::array<Internal::Sphere, 7> spheres;
std::array<Internal::LightSource, 3> lights; std::array<Internal::LightSource, 3> lights;
spheres[0].pos.x = 2.0f; spheres[0].pos.x = 2.0F;
spheres[0].pos.y = -2.0f; spheres[0].pos.y = -2.0F;
spheres[0].pos.z = -4.5f; spheres[0].pos.z = -4.5F;
spheres[0].radius = 1.0f; spheres[0].radius = 1.0F;
spheres[1].pos.x = -2.0f; spheres[1].pos.x = -2.0F;
spheres[1].pos.y = 2.0f; spheres[1].pos.y = 2.0F;
spheres[1].pos.z = -4.5f; spheres[1].pos.z = -4.5F;
spheres[1].radius = 1.0f; spheres[1].radius = 1.0F;
spheres[2].pos.x = 0.0f; spheres[2].pos.x = 0.0F;
spheres[2].pos.y = 0.0f; spheres[2].pos.y = 0.0F;
spheres[2].pos.z = -6.0f; spheres[2].pos.z = -6.0F;
spheres[2].radius = 2.0f; spheres[2].radius = 2.0F;
spheres[3].pos.x = 2.0f; spheres[3].pos.x = 2.0F;
spheres[3].pos.y = 2.0f; spheres[3].pos.y = 2.0F;
spheres[3].pos.z = -2.5; spheres[3].pos.z = -2.5;
spheres[3].radius = 1.0f; spheres[3].radius = 1.0F;
spheres[4].pos.x = -2.0f; spheres[4].pos.x = -2.0F;
spheres[4].pos.y = -2.0f; spheres[4].pos.y = -2.0F;
spheres[4].pos.z = -2.5; spheres[4].pos.z = -2.5;
spheres[4].radius = 1.0f; spheres[4].radius = 1.0F;
spheres[5].pos.x = -0.7f; spheres[5].pos.x = -0.7F;
spheres[5].pos.y = -0.7f; spheres[5].pos.y = -0.7F;
spheres[5].pos.z = -4.0f; spheres[5].pos.z = -4.0F;
spheres[5].radius = -0.7f; spheres[5].radius = -0.7F;
spheres[6].pos.x = 0.7f; spheres[6].pos.x = 0.7F;
spheres[6].pos.y = 0.7f; spheres[6].pos.y = 0.7F;
spheres[6].pos.z = -4.0f; spheres[6].pos.z = -4.0F;
spheres[6].radius = -0.7f; spheres[6].radius = -0.7F;
lights[0].color.r = 1.0f; lights[0].color.r = 1.0F;
lights[0].color.g = 0.0f; lights[0].color.g = 0.0F;
lights[0].color.b = 0.0f; lights[0].color.b = 0.0F;
lights[0].pos.x = 0.0f; lights[0].pos.x = 0.0F;
lights[0].pos.y = -1.0f; lights[0].pos.y = -1.0F;
lights[0].pos.z = 0.0f; lights[0].pos.z = 0.0F;
lights[0].falloffStart = 3.0f; lights[0].falloffStart = 3.0F;
lights[0].falloffEnd = 7.0f; lights[0].falloffEnd = 7.0F;
lights[1].color.r = 0.0f; lights[1].color.r = 0.0F;
lights[1].color.g = 1.0f; lights[1].color.g = 1.0F;
lights[1].color.b = 0.0f; lights[1].color.b = 0.0F;
lights[1].pos.x = std::cos(PI / 3.0f); lights[1].pos.x = std::cos(PI / 3.0F);
lights[1].pos.y = std::sin(PI / 3.0f); lights[1].pos.y = std::sin(PI / 3.0F);
lights[1].pos.z = 0.0f; lights[1].pos.z = 0.0F;
lights[1].falloffStart = 3.0f; lights[1].falloffStart = 3.0F;
lights[1].falloffEnd = 7.0f; lights[1].falloffEnd = 7.0F;
lights[2].color.r = 0.0f; lights[2].color.r = 0.0F;
lights[2].color.g = 0.0f; lights[2].color.g = 0.0F;
lights[2].color.b = 1.0f; lights[2].color.b = 1.0F;
lights[2].pos.x = std::cos(PI * 2.0 / 3.0f); lights[2].pos.x = static_cast<float>(std::cos(PI * 2.0 / 3.0F));
lights[2].pos.y = std::sin(PI * 2.0 / 3.0f); lights[2].pos.y = static_cast<float>(std::sin(PI * 2.0 / 3.0F));
lights[2].pos.z = 0.0f; lights[2].pos.z = 0.0F;
lights[2].falloffStart = 3.0f; lights[2].falloffStart = 3.0F;
lights[2].falloffEnd = 7.0f; lights[2].falloffEnd = 7.0F;
const auto yIteration = [&spheres, &lights, &image, outputWidth, outputHeight, const auto yIteration = [&spheres, &lights, &image, outputWidth, outputHeight,
rayPos](unsigned int j, std::mutex *mutex) { rayPos](unsigned int j, std::mutex *mutex) {
float offsetY = ((float)j + 0.5f - ((float)outputHeight / 2.0f)); float offsetY = (static_cast<float>(j) + 0.5F -
(static_cast<float>(outputHeight) / 2.0F));
for (unsigned int i = 0; i < outputWidth; ++i) { for (unsigned int i = 0; i < outputWidth; ++i) {
float offsetX = ((float)i + 0.5f - ((float)outputWidth / 2.0f)); float offsetX = (static_cast<float>(i) + 0.5F -
(static_cast<float>(outputWidth) / 2.0F));
glm::vec3 rayDir{offsetX, offsetY, glm::vec3 rayDir{offsetX, offsetY,
-(float)outputHeight * EX02_RAY_TRACER_VIEW_RATIO}; -static_cast<float>(outputHeight) *
EX02_RAY_TRACER_VIEW_RATIO};
glm::vec3 rayDirUnit = glm::vec3 rayDirUnit =
rayDir / std::sqrt(rayDir.x * rayDir.x + rayDir.y * rayDir.y + rayDir / std::sqrt(rayDir.x * rayDir.x + rayDir.y * rayDir.y +
rayDir.z * rayDir.z); rayDir.z * rayDir.z);
@ -366,9 +374,10 @@ Ex02::RT::Image Ex02::RT::renderColorsWithSpheres(unsigned int outputWidth,
// cast ray to all spheres, finding closest result // cast ray to all spheres, finding closest result
std::optional<std::tuple<glm::vec3, float, unsigned int>> closestResult; std::optional<std::tuple<glm::vec3, float, unsigned int>> closestResult;
for (unsigned int idx = 0; idx < spheres.size(); ++idx) { for (unsigned int idx = 0; idx < spheres.size(); ++idx) {
auto result = spheres[idx].rayToSphere(rayPos, rayDirUnit); auto result = spheres.at(idx).rayToSphere(rayPos, rayDirUnit);
if (result) { if (result) {
float dist = Internal::distBetweenPositions(rayPos, spheres[idx].pos); float dist =
Internal::distBetweenPositions(rayPos, spheres.at(idx).pos);
if (closestResult) { if (closestResult) {
if (dist < std::get<1>(*closestResult)) { if (dist < std::get<1>(*closestResult)) {
closestResult = {{*result, dist, idx}}; closestResult = {{*result, dist, idx}};
@ -395,8 +404,8 @@ Ex02::RT::Image Ex02::RT::renderColorsWithSpheres(unsigned int outputWidth,
if (idx == std::get<2>(*closestResult)) { if (idx == std::get<2>(*closestResult)) {
continue; continue;
} }
auto result = spheres[idx].rayToSphere(std::get<0>(*closestResult), auto result = spheres.at(idx).rayToSphere(std::get<0>(*closestResult),
toLightUnit); toLightUnit);
if (result) { if (result) {
isBlocked = true; isBlocked = true;
break; break;

View file

@ -1,11 +1,11 @@
#ifndef EX02_RAY_TRACER_HPP #ifndef EX02_RAY_TRACER_HPP
#define EX02_RAY_TRACER_HPP #define EX02_RAY_TRACER_HPP
#define EX02_RAY_TRACER_VIEW_RATIO 0.5f constexpr float EX02_RAY_TRACER_VIEW_RATIO = 0.5F;
#define EX02_RAY_TRACER_DEFAULT_NEAR_PLANE 0.2f constexpr float EX02_RAY_TRACER_DEFAULT_NEAR_PLANE = 0.2F;
#define EX02_RAY_TRACER_DEFAULT_FAR_PLANE 4.0f constexpr float EX02_RAY_TRACER_DEFAULT_FAR_PLANE = 4.0F;
#define EX02_RAY_TRACER_COLL_INCREMENT 2.0f constexpr float EX02_RAY_TRACER_COLL_INCREMENT = 2.0F;
#define EX02_RAY_TRACER_GRAY_SPHERE_RADIUS 1.5f constexpr float EX02_RAY_TRACER_GRAY_SPHERE_RADIUS = 1.5F;
#include <mutex> #include <mutex>
#include <optional> #include <optional>
@ -17,80 +17,77 @@
#include <glm/matrix.hpp> #include <glm/matrix.hpp>
#include <glm/vec3.hpp> #include <glm/vec3.hpp>
namespace Ex02 { namespace Ex02::RT {
namespace RT {
struct Pixel { struct Pixel {
Pixel(); Pixel();
unsigned char r, g, b; unsigned char r, g, b;
};
class Image {
public:
Image(unsigned int width, unsigned int height);
Pixel &getPixel(unsigned int x, unsigned int y);
const Pixel &getPixel(unsigned int x, unsigned int y) const;
void writeToFile(const std::string &filename) const;
private:
unsigned int width;
std::vector<Pixel> data;
};
namespace Internal {
using RTSVisibleType = std::optional<std::tuple<glm::vec3, glm::vec3>>;
struct LightSource {
LightSource();
glm::vec3 pos;
float falloffStart;
float falloffEnd;
glm::vec3 color;
void applyLight(glm::vec3 pos, Pixel &pixelOut) const;
void applyLight(glm::vec3 pos, Pixel &pixelOut, std::mutex *mutex) const;
}; };
class Image { struct Sphere {
public: Sphere();
Image(unsigned int width, unsigned int height);
Pixel &getPixel(unsigned int x, unsigned int y); glm::vec3 pos;
const Pixel &getPixel(unsigned int x, unsigned int y) const; float radius;
void writeToFile(const std::string &filename) const; std::optional<glm::vec3> rayToSphere(glm::vec3 rayPos,
glm::vec3 rayDirUnit) const;
private:
unsigned int width;
std::vector<Pixel> data;
};
namespace Internal {
typedef std::optional<std::tuple<glm::vec3, glm::vec3>> RTSVisibleType;
struct LightSource {
LightSource();
glm::vec3 pos;
float falloffStart;
float falloffEnd;
glm::vec3 color;
void applyLight(glm::vec3 pos, Pixel &pixelOut) const;
void applyLight(glm::vec3 pos, Pixel &pixelOut, std::mutex *mutex) const;
};
struct Sphere {
Sphere();
glm::vec3 pos;
float radius;
std::optional<glm::vec3> rayToSphere(glm::vec3 rayPos,
glm::vec3 rayDirUnit) const;
RTSVisibleType rayToSphereVisible(glm::vec3 rayPos, glm::vec3 rayDirUnit,
const LightSource &light) const;
};
// returns pos of collision
std::optional<glm::vec3> rayToSphere(glm::vec3 rayPos, glm::vec3 rayDirUnit,
glm::vec3 spherePos,
float sphereRadius);
float angleBetweenRays(glm::vec3 a, glm::vec3 b);
float distBetweenPositions(glm::vec3 a, glm::vec3 b);
// first vec3 is result from rayToSphere(), second is ray to light source
RTSVisibleType rayToSphereVisible(glm::vec3 rayPos, glm::vec3 rayDirUnit, RTSVisibleType rayToSphereVisible(glm::vec3 rayPos, glm::vec3 rayDirUnit,
glm::vec3 spherePos, float sphereRadius, const LightSource &light) const;
glm::vec3 lightPos); };
} // namespace Internal
Image renderGraySphere(unsigned int outputWidth, unsigned int outputHeight, // returns pos of collision
unsigned int threadCount = 1); std::optional<glm::vec3> rayToSphere(glm::vec3 rayPos, glm::vec3 rayDirUnit,
glm::vec3 spherePos, float sphereRadius);
Image renderColorsWithSpheres(unsigned int outputWidth, float angleBetweenRays(glm::vec3 a, glm::vec3 b);
unsigned int outputHeight,
unsigned int threadCount = 1);
} // namespace RT float distBetweenPositions(glm::vec3 a, glm::vec3 b);
} // namespace Ex02
// first vec3 is result from rayToSphere(), second is ray to light source
RTSVisibleType rayToSphereVisible(glm::vec3 rayPos, glm::vec3 rayDirUnit,
glm::vec3 spherePos, float sphereRadius,
glm::vec3 lightPos);
} // namespace Internal
Image renderGraySphere(unsigned int outputWidth, unsigned int outputHeight,
unsigned int threadCount = 1);
Image renderColorsWithSpheres(unsigned int outputWidth,
unsigned int outputHeight,
unsigned int threadCount = 1);
} // namespace Ex02::RT
#endif #endif