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: ''
HeaderFilterRegex: ''
AnalyzeTemporaryDtors: false
@ -10,18 +10,14 @@ CheckOptions:
value: '::realloc'
- key: cppcoreguidelines-owning-memory.LegacyResourceConsumers
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
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
value: 'false'
- key: hicpp-use-auto.MinTypeNameLength
@ -62,8 +58,6 @@ CheckOptions:
value: 'true'
- key: cppcoreguidelines-init-variables.IncludeStyle
value: llvm
- key: modernize-use-nodiscard.ReplacementString
value: '[[nodiscard]]'
- key: modernize-loop-convert.MakeReverseRangeHeader
value: ''
- key: misc-definitions-in-headers.HeaderFileExtensions
@ -88,10 +82,10 @@ CheckOptions:
value: 'c;cc;cpp;cxx'
- key: modernize-loop-convert.MakeReverseRangeFunction
value: ''
- key: bugprone-suspicious-missing-comma.SizeThreshold
value: '5'
- key: readability-inconsistent-declaration-parameter-name.IgnoreMacros
value: 'true'
- key: bugprone-suspicious-missing-comma.SizeThreshold
value: '5'
- key: readability-identifier-naming.IgnoreFailedSplit
value: 'false'
- key: hicpp-multiway-paths-covered.WarnOnMissingElse
@ -136,8 +130,6 @@ CheckOptions:
value: '8388608'
- key: readability-simplify-boolean-expr.ChainedConditionalAssignment
value: 'false'
- key: cppcoreguidelines-avoid-magic-numbers.IgnoreAllFloatingPointValues
value: 'false'
- key: cert-oop54-cpp.WarnOnlyIfThisHasSuspiciousField
value: 'false'
- key: cert-err09-cpp.CheckThrowTemporaries
@ -154,18 +146,16 @@ CheckOptions:
value: ';h;hh;hpp;hxx'
- key: portability-simd-intrinsics.Suggest
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
value: ''
- key: readability-function-size.LineThreshold
value: '4294967295'
- key: modernize-make-shared.MakeSmartPtrFunctionHeader
value: '<memory>'
- key: modernize-make-shared.MakeSmartPtrFunction
value: 'std::make_shared'
- key: modernize-use-override.IgnoreDestructors
value: 'false'
- key: misc-non-private-member-variables-in-classes.IgnorePublicMemberVariables
value: 'false'
- key: bugprone-sizeof-expression.WarnOnSizeOfConstant
value: 'true'
- key: readability-redundant-string-init.StringNames
@ -174,8 +164,6 @@ CheckOptions:
value: 'true'
- key: modernize-use-emplace.ContainersWithPushBack
value: '::std::vector;::std::list;::std::deque'
- key: readability-magic-numbers.IgnoreBitFieldsWidths
value: 'true'
- key: modernize-make-unique.IncludeStyle
value: llvm
- key: modernize-use-override.OverrideSpelling
@ -190,10 +178,6 @@ CheckOptions:
value: ''
- key: cppcoreguidelines-pro-type-member-init.IgnoreArrays
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
value: 'false'
- key: cppcoreguidelines-macro-usage.IgnoreCommandLineMacros
@ -204,14 +188,12 @@ CheckOptions:
value: llvm
- key: objc-forbidden-subclassing.ForbiddenSuperClassNames
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
value: 'false'
- key: bugprone-misplaced-widening-cast.CheckImplicitCasts
value: 'false'
- key: readability-uppercase-literal-suffix.NewSuffixes
value: ''
- key: bugprone-suspicious-missing-comma.RatioThreshold
value: '0.200000'
- key: modernize-loop-convert.MinConfidence
value: reasonable
- key: performance-unnecessary-value-param.AllowedTypes
@ -220,22 +202,18 @@ CheckOptions:
value: 'true'
- key: misc-throw-by-value-catch-by-reference.MaxSize
value: '-1'
- key: cppcoreguidelines-avoid-magic-numbers.IgnorePowersOf2IntegerValues
value: 'false'
- key: readability-uppercase-literal-suffix.NewSuffixes
value: ''
- key: google-readability-namespace-comments.SpacesBeforeComments
value: '2'
- key: cppcoreguidelines-avoid-magic-numbers.IgnoreBitFieldsWidths
value: 'true'
- key: cert-err61-cpp.CheckThrowTemporaries
value: 'true'
- key: cppcoreguidelines-avoid-magic-numbers.IgnoredIntegerValues
value: '1;2;3;4;'
- key: hicpp-use-emplace.TupleMakeFunctions
value: '::std::make_pair;::std::make_tuple'
- key: cppcoreguidelines-no-malloc.Allocations
value: '::malloc;::calloc'
- key: readability-function-size.BranchThreshold
value: '4294967295'
- key: bugprone-suspicious-missing-comma.RatioThreshold
value: '0.200000'
- key: hicpp-function-size.LineThreshold
value: '4294967295'
- key: readability-implicit-bool-conversion.AllowIntegerConditions
@ -254,8 +232,6 @@ CheckOptions:
value: '800'
- key: cert-msc51-cpp.DisallowedSeedTypes
value: 'time_t,std::time_t'
- key: hicpp-use-emplace.TupleMakeFunctions
value: '::std::make_pair;::std::make_tuple'
- key: bugprone-reserved-identifier.AggressiveDependentMemberLookup
value: 'false'
- key: modernize-use-equals-default.IgnoreMacros
@ -272,8 +248,6 @@ CheckOptions:
value: '::std::shared_ptr;::std::unique_ptr;::std::auto_ptr;::std::weak_ptr'
- key: cppcoreguidelines-no-malloc.Deallocations
value: '::free'
- key: readability-magic-numbers.IgnorePowersOf2IntegerValues
value: 'false'
- key: misc-unused-parameters.StrictMode
value: 'false'
- key: cert-oop11-cpp.IncludeStyle
@ -348,8 +322,6 @@ CheckOptions:
value: 'true'
- key: bugprone-argument-comment.StrictMode
value: '0'
- key: misc-non-private-member-variables-in-classes.IgnoreClassesWithAllMemberVariablesBeingPublic
value: 'false'
- key: modernize-replace-random-shuffle.IncludeStyle
value: llvm
- key: modernize-use-bool-literals.IgnoreMacros
@ -358,14 +330,14 @@ CheckOptions:
value: 'true'
- key: google-readability-namespace-comments.ShortNamespaceLines
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
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
value: '-1'
- key: hicpp-use-equals-default.IgnoreMacros
@ -390,16 +362,16 @@ CheckOptions:
value: 'false'
- key: hicpp-no-malloc.Reallocations
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
value: ''
- key: bugprone-argument-comment.CommentCharacterLiterals
value: '0'
- key: google-runtime-int.UnsignedTypePrefix
value: uint
- key: modernize-pass-by-value.IncludeStyle
value: llvm
- key: bugprone-argument-comment.CommentIntegerLiterals
value: '0'
- key: bugprone-argument-comment.CommentFloatLiterals
value: '0'
- key: bugprone-too-small-loop-variable.MagnitudeBitsUpperLimit
@ -408,14 +380,10 @@ CheckOptions:
value: 'false'
- key: abseil-string-find-startswith.AbseilStringsMatchHeader
value: 'absl/strings/match.h'
- key: readability-else-after-return.WarnOnConditionVariables
value: 'true'
- key: modernize-use-nullptr.NullMacros
value: 'NULL'
- key: cppcoreguidelines-macro-usage.AllowedRegexp
value: '^DEBUG_*'
- key: llvm-header-guard.HeaderFileExtensions
value: ';h;hh;hpp;hxx'
- key: cppcoreguidelines-narrowing-conversions.PedanticMode
value: 'false'
- key: modernize-make-shared.IgnoreDefaultInitialization
@ -454,12 +422,10 @@ CheckOptions:
value: 'false'
- key: performance-inefficient-string-concatenation.StrictMode
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
value: 'false'
- key: readability-redundant-declaration.IgnoreMacros
value: 'true'
- key: modernize-make-unique.MakeSmartPtrFunction
value: 'std::make_unique'
- key: google-runtime-int.TypeSuffix
@ -474,8 +440,6 @@ CheckOptions:
value: ''
- key: modernize-make-unique.MakeSmartPtrFunctionHeader
value: '<memory>'
- key: readability-redundant-declaration.IgnoreMacros
value: 'true'
- key: zircon-temporary-objects.Names
value: ''
- key: hicpp-use-emplace.TupleTypes
@ -517,7 +481,7 @@ CheckOptions:
- key: cert-str34-c.CharTypdefsToIgnore
value: ''
- key: llvm-else-after-return.WarnOnConditionVariables
value: 'false'
value: '0'
- key: bugprone-sizeof-expression.WarnOnSizeOfCompareToConstant
value: 'true'
- key: modernize-raw-string-literal.DelimiterStem
@ -530,26 +494,24 @@ CheckOptions:
value: '1600'
- key: modernize-raw-string-literal.ReplaceShorterLiterals
value: 'false'
- key: readability-magic-numbers.IgnoredIntegerValues
value: '1;2;3;4;'
- key: performance-inefficient-vector-operation.VectorLikeClasses
value: '::std::vector'
- key: modernize-use-auto.RemoveStars
value: 'false'
- key: abseil-string-find-str-contains.IncludeStyle
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
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
value: llvm
- key: llvm-else-after-return.WarnOnUnfixable
value: 'false'
value: '0'
- key: cert-msc32-c.DisallowedSeedTypes
value: 'time_t,std::time_t'
...

View file

@ -45,7 +45,7 @@ if(CLANG_TIDY)
clang-tidy
COMMAND /usr/bin/clang-tidy
${Example02_ALL_FILES}
-config=''
-config-file="${CMAKE_CURRENT_SOURCE_DIR}/.clang-tidy"
--
-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 &doubleArgs) {
ParseResult result;
bool found;
bool found = false;
--argc;
++argv;
while (argc > 0) {

View file

@ -1,22 +1,21 @@
#ifndef 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 <unordered_map>
#include <unordered_set>
namespace Ex02 {
namespace ArgParse {
namespace Ex02::ArgParse {
typedef std::unordered_set<std::string> ArgsType;
typedef std::unordered_map<std::string, std::string> ParseResult;
using ArgsType = std::unordered_set<std::string>;
using ParseResult = std::unordered_map<std::string, std::string>;
ParseResult parseArgs(int argc, char **argv, const ArgsType &singleArgs,
const ArgsType &doubleArgs);
ParseResult parseArgs(int argc, char **argv, const ArgsType &singleArgs,
const ArgsType &doubleArgs);
} // namespace ArgParse
} // namespace Ex02
} // namespace Ex02::ArgParse
#endif

View file

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

View file

@ -9,7 +9,7 @@
#include <glm/gtc/matrix_transform.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) {}
@ -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 i = 0; i < width; ++i) {
out << (int)data.at(i + j * width).r << ' '
<< (int)data.at(i + j * width).g << ' '
<< (int)data.at(i + j * width).b << ' ';
out << static_cast<int>(data.at(i + j * width).r) << ' '
<< static_cast<int>(data.at(i + j * width).g) << ' '
<< static_cast<int>(data.at(i + j * width).b) << ' ';
}
out << '\n';
}
@ -43,28 +43,28 @@ void Ex02::RT::Image::writeToFile(const std::string &filename) const {
/*
glm::mat4x4 Ex02::RT::Internal::defaultMVP() {
glm::mat4x4 mvp = glm::perspective(
PI / 2.0f,
1.0f,
PI / 2.0F,
1.0F,
EX02_RAY_TRACER_DEFAULT_NEAR_PLANE,
EX02_RAY_TRACER_DEFAULT_FAR_PLANE);
mvp *= glm::lookAt(
glm::vec3{0.0f, 0.0f, 0.0f},
glm::vec3{0.0f, 0.0f, -1.0f},
glm::vec3{0.0f, 1.0f, 0.0f});
glm::vec3{0.0F, 0.0F, 0.0F},
glm::vec3{0.0F, 0.0F, -1.0F},
glm::vec3{0.0F, 1.0F, 0.0F});
// model pushes back by 2 units
mvp = glm::translate(
mvp,
glm::vec3{0.0f, 0.0f, 2.0f});
glm::vec3{0.0F, 0.0F, 2.0F});
return mvp;
}
*/
Ex02::RT::Internal::LightSource::LightSource()
: pos{0.0f, 0.0f, 0.0f}, falloffStart(2.0f),
falloffEnd(7.0f), color{1.0f, 1.0f, 1.0f} {}
: pos{0.0F, 0.0F, 0.0F}, falloffStart(2.0F),
falloffEnd(7.0F), color{1.0F, 1.0F, 1.0F} {}
void Ex02::RT::Internal::LightSource::applyLight(glm::vec3 pos,
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);
if (dist < falloffStart) {
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) {
*out = 255;
} else {
@ -85,15 +86,15 @@ void Ex02::RT::Internal::LightSource::applyLight(glm::vec3 pos,
} else if (dist >= falloffStart && dist <= falloffEnd) {
const auto applyFalloffColor = [](auto *color, unsigned char *out,
float f) {
unsigned int temp =
(unsigned int)*out + (unsigned int)(*color * 255.0f * f);
unsigned int temp = static_cast<unsigned int>(*out) +
static_cast<unsigned int>(*color * 255.0F * f);
if (temp > 255) {
*out = 255;
} else {
*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.y, &pixelOut.g, 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);
if (dist < falloffStart) {
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) {
*out = 255;
} else {
*out = temp;
}
};
if (mutex) {
if (mutex != nullptr) {
std::lock_guard<std::mutex> lock(*mutex);
applyColor(&color.x, &pixelOut.r);
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) {
const auto applyFalloffColor = [](auto *color, unsigned char *out,
float f) {
unsigned int temp =
(unsigned int)*out + (unsigned int)(*color * 255.0f * f);
unsigned int temp = static_cast<unsigned int>(*out) +
static_cast<unsigned int>(*color * 255.0F * f);
if (temp > 255) {
*out = 255;
} else {
*out = temp;
}
};
float f = (1.0f - (dist - falloffStart) / (falloffEnd - falloffStart));
if (mutex) {
float f = (1.0F - (dist - falloffStart) / (falloffEnd - falloffStart));
if (mutex != nullptr) {
std::lock_guard<std::mutex> lock(*mutex);
applyFalloffColor(&color.x, &pixelOut.r, 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>
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;
delta -= temp;
if (delta < 0.0f) {
if (delta < 0.0F) {
return {};
} else {
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 min = dist > dist2 ? dist2 : dist;
float max = dist > dist2 ? dist : dist2;
if (min < 0.0f) {
if (max < 0.0f) {
if (min < 0.0F) {
if (max < 0.0F) {
return {};
} else {
return {rayPos + rayDirUnit * max};
@ -221,7 +223,7 @@ Ex02::RT::Internal::rayToSphereVisible(glm::vec3 rayPos, glm::vec3 rayDirUnit,
glm::vec3 toLightUnit =
toLight / std::sqrt(toLight.x * toLight.x + toLight.y * toLight.y +
toLight.z * toLight.z);
glm::vec3 toLightPos = *collPos + toLight / 3.0f;
glm::vec3 toLightPos = *collPos + toLight / 3.0F;
auto collResult =
Internal::rayToSphere(toLightPos, toLightUnit, spherePos, sphereRadius);
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,
unsigned int outputHeight,
unsigned int) {
const glm::vec3 spherePos{0.0f, 0.0f, -2.5f};
const glm::vec3 lightPos{4.0f, 4.0f, 0.0f};
unsigned int /*unused*/) {
const glm::vec3 spherePos{0.0F, 0.0F, -2.5F};
const glm::vec3 lightPos{4.0F, 4.0F, 0.0F};
Image image(outputWidth, outputHeight);
const glm::vec3 rayPos{0.0f, 0.0f, 0.0f};
const float lightFalloffStart = 4.5f;
const float lightFalloffEnd = 7.0f;
const glm::vec3 rayPos{0.0F, 0.0F, 0.0F};
const float lightFalloffStart = 4.5F;
const float lightFalloffEnd = 7.0F;
// if(threadCount <= 1) {
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) {
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,
-(float)outputHeight * EX02_RAY_TRACER_VIEW_RATIO};
-static_cast<float>(outputHeight) *
EX02_RAY_TRACER_VIEW_RATIO};
glm::vec3 rayDirUnit =
rayDir / std::sqrt(rayDir.x * rayDir.x + rayDir.y * rayDir.y +
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).b = 255;
} else if (dist >= lightFalloffStart && dist <= lightFalloffEnd) {
image.getPixel(i, j).r =
(1.0f - (dist - lightFalloffStart) /
image.getPixel(i, j).r = static_cast<unsigned char>(
(1.0F - (dist - lightFalloffStart) /
(lightFalloffEnd - lightFalloffStart)) *
255.0f;
255.0F);
image.getPixel(i, j).g = 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 threadCount) {
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::LightSource, 3> lights;
spheres[0].pos.x = 2.0f;
spheres[0].pos.y = -2.0f;
spheres[0].pos.z = -4.5f;
spheres[0].radius = 1.0f;
spheres[0].pos.x = 2.0F;
spheres[0].pos.y = -2.0F;
spheres[0].pos.z = -4.5F;
spheres[0].radius = 1.0F;
spheres[1].pos.x = -2.0f;
spheres[1].pos.y = 2.0f;
spheres[1].pos.z = -4.5f;
spheres[1].radius = 1.0f;
spheres[1].pos.x = -2.0F;
spheres[1].pos.y = 2.0F;
spheres[1].pos.z = -4.5F;
spheres[1].radius = 1.0F;
spheres[2].pos.x = 0.0f;
spheres[2].pos.y = 0.0f;
spheres[2].pos.z = -6.0f;
spheres[2].radius = 2.0f;
spheres[2].pos.x = 0.0F;
spheres[2].pos.y = 0.0F;
spheres[2].pos.z = -6.0F;
spheres[2].radius = 2.0F;
spheres[3].pos.x = 2.0f;
spheres[3].pos.y = 2.0f;
spheres[3].pos.x = 2.0F;
spheres[3].pos.y = 2.0F;
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.y = -2.0f;
spheres[4].pos.x = -2.0F;
spheres[4].pos.y = -2.0F;
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.y = -0.7f;
spheres[5].pos.z = -4.0f;
spheres[5].radius = -0.7f;
spheres[5].pos.x = -0.7F;
spheres[5].pos.y = -0.7F;
spheres[5].pos.z = -4.0F;
spheres[5].radius = -0.7F;
spheres[6].pos.x = 0.7f;
spheres[6].pos.y = 0.7f;
spheres[6].pos.z = -4.0f;
spheres[6].radius = -0.7f;
spheres[6].pos.x = 0.7F;
spheres[6].pos.y = 0.7F;
spheres[6].pos.z = -4.0F;
spheres[6].radius = -0.7F;
lights[0].color.r = 1.0f;
lights[0].color.g = 0.0f;
lights[0].color.b = 0.0f;
lights[0].pos.x = 0.0f;
lights[0].pos.y = -1.0f;
lights[0].pos.z = 0.0f;
lights[0].falloffStart = 3.0f;
lights[0].falloffEnd = 7.0f;
lights[0].color.r = 1.0F;
lights[0].color.g = 0.0F;
lights[0].color.b = 0.0F;
lights[0].pos.x = 0.0F;
lights[0].pos.y = -1.0F;
lights[0].pos.z = 0.0F;
lights[0].falloffStart = 3.0F;
lights[0].falloffEnd = 7.0F;
lights[1].color.r = 0.0f;
lights[1].color.g = 1.0f;
lights[1].color.b = 0.0f;
lights[1].pos.x = std::cos(PI / 3.0f);
lights[1].pos.y = std::sin(PI / 3.0f);
lights[1].pos.z = 0.0f;
lights[1].falloffStart = 3.0f;
lights[1].falloffEnd = 7.0f;
lights[1].color.r = 0.0F;
lights[1].color.g = 1.0F;
lights[1].color.b = 0.0F;
lights[1].pos.x = std::cos(PI / 3.0F);
lights[1].pos.y = std::sin(PI / 3.0F);
lights[1].pos.z = 0.0F;
lights[1].falloffStart = 3.0F;
lights[1].falloffEnd = 7.0F;
lights[2].color.r = 0.0f;
lights[2].color.g = 0.0f;
lights[2].color.b = 1.0f;
lights[2].pos.x = std::cos(PI * 2.0 / 3.0f);
lights[2].pos.y = std::sin(PI * 2.0 / 3.0f);
lights[2].pos.z = 0.0f;
lights[2].falloffStart = 3.0f;
lights[2].falloffEnd = 7.0f;
lights[2].color.r = 0.0F;
lights[2].color.g = 0.0F;
lights[2].color.b = 1.0F;
lights[2].pos.x = static_cast<float>(std::cos(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].falloffStart = 3.0F;
lights[2].falloffEnd = 7.0F;
const auto yIteration = [&spheres, &lights, &image, outputWidth, outputHeight,
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) {
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,
-(float)outputHeight * EX02_RAY_TRACER_VIEW_RATIO};
-static_cast<float>(outputHeight) *
EX02_RAY_TRACER_VIEW_RATIO};
glm::vec3 rayDirUnit =
rayDir / std::sqrt(rayDir.x * rayDir.x + rayDir.y * rayDir.y +
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
std::optional<std::tuple<glm::vec3, float, unsigned int>> closestResult;
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) {
float dist = Internal::distBetweenPositions(rayPos, spheres[idx].pos);
float dist =
Internal::distBetweenPositions(rayPos, spheres.at(idx).pos);
if (closestResult) {
if (dist < std::get<1>(*closestResult)) {
closestResult = {{*result, dist, idx}};
@ -395,8 +404,8 @@ Ex02::RT::Image Ex02::RT::renderColorsWithSpheres(unsigned int outputWidth,
if (idx == std::get<2>(*closestResult)) {
continue;
}
auto result = spheres[idx].rayToSphere(std::get<0>(*closestResult),
toLightUnit);
auto result = spheres.at(idx).rayToSphere(std::get<0>(*closestResult),
toLightUnit);
if (result) {
isBlocked = true;
break;

View file

@ -1,11 +1,11 @@
#ifndef EX02_RAY_TRACER_HPP
#define EX02_RAY_TRACER_HPP
#define EX02_RAY_TRACER_VIEW_RATIO 0.5f
#define EX02_RAY_TRACER_DEFAULT_NEAR_PLANE 0.2f
#define EX02_RAY_TRACER_DEFAULT_FAR_PLANE 4.0f
#define EX02_RAY_TRACER_COLL_INCREMENT 2.0f
#define EX02_RAY_TRACER_GRAY_SPHERE_RADIUS 1.5f
constexpr float EX02_RAY_TRACER_VIEW_RATIO = 0.5F;
constexpr float EX02_RAY_TRACER_DEFAULT_NEAR_PLANE = 0.2F;
constexpr float EX02_RAY_TRACER_DEFAULT_FAR_PLANE = 4.0F;
constexpr float EX02_RAY_TRACER_COLL_INCREMENT = 2.0F;
constexpr float EX02_RAY_TRACER_GRAY_SPHERE_RADIUS = 1.5F;
#include <mutex>
#include <optional>
@ -17,80 +17,77 @@
#include <glm/matrix.hpp>
#include <glm/vec3.hpp>
namespace Ex02 {
namespace RT {
namespace Ex02::RT {
struct Pixel {
Pixel();
struct 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 {
public:
Image(unsigned int width, unsigned int height);
struct Sphere {
Sphere();
Pixel &getPixel(unsigned int x, unsigned int y);
const Pixel &getPixel(unsigned int x, unsigned int y) const;
glm::vec3 pos;
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,
glm::vec3 spherePos, float sphereRadius,
glm::vec3 lightPos);
} // namespace Internal
const LightSource &light) const;
};
Image renderGraySphere(unsigned int outputWidth, unsigned int outputHeight,
unsigned int threadCount = 1);
// returns pos of collision
std::optional<glm::vec3> rayToSphere(glm::vec3 rayPos, glm::vec3 rayDirUnit,
glm::vec3 spherePos, float sphereRadius);
Image renderColorsWithSpheres(unsigned int outputWidth,
unsigned int outputHeight,
unsigned int threadCount = 1);
float angleBetweenRays(glm::vec3 a, glm::vec3 b);
} // namespace RT
} // namespace Ex02
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,
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