From 8453d265e0791dbd961fb9ab2b3fff48b47048e7 Mon Sep 17 00:00:00 2001 From: jp Date: Fri, 18 Nov 2022 11:48:08 +0100 Subject: [PATCH] added ex03 task --- CMakeLists.txt | 16 ++++++++ ex3.cpp | 104 ++++++++++++++++++++++++++++++++++++++++++++++++ scene/scene.cpp | 18 +++++++++ scene/scene.h | 5 +++ 4 files changed, 143 insertions(+) create mode 100644 ex3.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index b37baeb..4a62839 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -38,6 +38,22 @@ target_link_libraries(tracey_ex1 tracey) add_executable(tracey_ex2 ex2.cpp) target_link_libraries(tracey_ex2 tracey) +add_executable(tracey_ex3_1 ex3.cpp) +target_link_libraries(tracey_ex3_1 tracey) +target_compile_definitions(tracey_ex3_1 PUBLIC SITUATION=1) +add_executable(tracey_ex3_2 ex3.cpp) +target_link_libraries(tracey_ex3_2 tracey) +target_compile_definitions(tracey_ex3_2 PUBLIC SITUATION=2) +if("${CMAKE_CURRENT_LIST_DIR}/light/ambientlight.cpp" IN_LIST light_src) + add_definitions(-DAMBIENTLIGHT_FOUND) +endif() +if("${CMAKE_CURRENT_LIST_DIR}/light/spotlight.cpp" IN_LIST light_src) + add_definitions(-DSPOTLIGHT_FOUND) +endif() +if("${CMAKE_CURRENT_LIST_DIR}/primitive/objmodel.cpp" IN_LIST primitive_src) + add_definitions(-DOBJMODEL_FOUND) +endif() + diff --git a/ex3.cpp b/ex3.cpp new file mode 100644 index 0000000..513d035 --- /dev/null +++ b/ex3.cpp @@ -0,0 +1,104 @@ +#include +#include + +#include "camera/perspectivecamera.h" + +#include "renderer/simplerenderer.h" + +#include "scene/simplescene.h" + +#include "primitive/box.h" +#include "primitive/infiniteplane.h" +#include "primitive/sphere.h" +#include "primitive/triangle.h" +#ifdef OBJMODEL_FOUND +#include "primitive/objmodel.h" +#endif + +#include "shader/simpleshadowshader.h" + +#include "light/pointlight.h" +#ifdef AMBIENTLIGHT_FOUND +#include "light/ambientlight.h" +#endif +#ifdef SPOTLIGHT_FOUND +#include "light/spotlight.h" +#endif + + +int main() { + // Let's create a simple cornell box scene... + SimpleScene scene; + + // Add shaders for the walls + auto red = std::make_shared(Color(1.0f, 0.3f, 0.2f)); + auto white = std::make_shared(Color(1.0f, 1.0f, 1.0f)); + auto blue = std::make_shared(Color(0.2f, 0.3f, 1.0f)); + auto orange = std::make_shared(Color(1.0f, 0.5f, 0.0f)); + + // Set up the cornell box walls + scene.add(std::make_shared(Vector3d(0.0f, 0.0f, +5.0f), Vector3d(0.0f, 0.0f, -1.0f), white)); + scene.add(std::make_shared(Vector3d(0.0f, 0.0f, -5.0f), Vector3d(0.0f, 0.0f, +1.0f), white)); + scene.add(std::make_shared(Vector3d(0.0f, +5.0f, 0.0f), Vector3d(0.0f, -1.0f, 0.0f), white)); + scene.add(std::make_shared(Vector3d(0.0f, -5.0f, 0.0f), Vector3d(0.0f, +1.0f, 0.0f), white)); + scene.add(std::make_shared(Vector3d(+5.0f, 0.0f, 0.0f), Vector3d(-1.0f, 0.0f, 0.0f), blue)); + scene.add(std::make_shared(Vector3d(-5.0f, 0.0f, 0.0f), Vector3d(+1.0f, 0.0f, 0.0f), red)); + + scene.add(std::make_shared(Vector3d(2.5f, -3.0f, 1.0f), Vector3d(3.0f, 4.0f, 3.0f), red)); + scene.add(std::make_shared(Vector3d(-3.0f, -2.0f, 0.0f), Vector3d(1.0f, 6.0f, 1.0f), blue)); + scene.add(std::make_shared(Vector3d(-0.5f, -4.0f, -2.0f), Vector3d(2.0f, 2.0f, 2.0f), orange)); + + // Add teapots (for faster rendering: try commenting two of them out and + // compile in release mode) +#ifdef OBJMODEL_FOUND + auto teapot0 = std::make_shared(orange); + teapot0->loadObj("data/teapot.obj", Vector3d(2.0f, 2.0f, 2.0f), Vector3d(2.5f, -1.0f, 1.0f)); + + auto teapot1 = std::make_shared(red); + teapot1->loadObj("data/teapot.obj", Vector3d(2.0f, 2.0f, 2.0f), Vector3d(-3.0f, 1.0f, 0.0f)); + + auto teapot2 = std::make_shared(blue); + teapot2->loadObj("data/teapot.obj", Vector3d(2.0f, 2.0f, 2.0f), Vector3d(-0.5f, -3.0f, -2.0f)); + + scene.add(teapot0); + scene.add(teapot1); + scene.add(teapot2); +#else + scene.addObj("data/teapot.obj", Vector3d(2.0f, 2.0f, 2.0f), Vector3d(2.5f, -1.0f, 1.0f), orange); + scene.addObj("data/teapot.obj", Vector3d(2.0f, 2.0f, 2.0f), Vector3d(-3.0f, 1.0f, 0.0f), red); + scene.addObj("data/teapot.obj", Vector3d(2.0f, 2.0f, 2.0f), Vector3d(-0.5f, -3.0f, -2.0f), blue); +#endif + + // Add ambient light +#ifdef AMBIENTLIGHT_FOUND + scene.add(std::make_shared(0.15f)); +#endif + + // Lighting situation 1 +#if SITUATION == 1 + scene.add(std::make_shared(Vector3d(0.0f, 4.0f, 0.0f), 10.0f)); +#endif + + // Lighting situation 2 +#if SITUATION == 2 +#ifdef SPOTLIGHT_FOUND + // parameters are: position, direction, inner cone angle, outer cone angle (in + // degrees), intensity + scene.add(std::make_shared(Vector3d(1.0f, 0.0f, -4.0f), Vector3d(0.0f, 0.0f, 1.0f), 50.0f, 70.0f, 20.0f)); +#endif +#endif + + + // Set up the camera + PerspectiveCamera camera; + camera.setFovAngle(90.0f); + camera.setPosition(Vector3d(0.0f, 0.0f, -10.0f)); + camera.setForwardDirection(Vector3d(0.0f, 0.0f, 1.0f)); + camera.setUpDirection(Vector3d(0.0f, 1.0f, 0.0f)); + + // Render the scene + SimpleRenderer renderer; + renderer.renderImage(scene, camera, 512, 512).save("result.png"); + + return 0; +} diff --git a/scene/scene.cpp b/scene/scene.cpp index d9e183b..68e0924 100644 --- a/scene/scene.cpp +++ b/scene/scene.cpp @@ -29,6 +29,24 @@ void Scene::add(const std::shared_ptr &primitive) { this->primitives_.push_back(primitive); } +void Scene::addObj(char const *fileName, Vector3d const &scale, Vector3d const &translation, + const std::shared_ptr &shader, bool flipU, bool flipV) { + std::vector> triangles = loadObj(fileName, scale, translation, shader, flipU, flipV); + this->primitives_.insert(this->primitives_.end(), std::make_move_iterator(triangles.begin()), + std::make_move_iterator(triangles.end())); +} + +std::vector> Scene::loadObj(char const *fileName, Vector3d const &scale, + Vector3d const &translation, + const std::shared_ptr &shader, bool flipU, bool flipV) { + std::vector> faces; + std::vector> indices; + + // IMPLEMENT ME + + return faces; +} + Color Scene::traceRay(Ray &ray) const { if (this->findIntersection(ray) && ray.remainingBounces-- > 0) { // If the ray has hit an object, call the shader ... diff --git a/scene/scene.h b/scene/scene.h index 2851914..a373c26 100644 --- a/scene/scene.h +++ b/scene/scene.h @@ -32,6 +32,11 @@ public: void add(const std::shared_ptr &light); void add(const std::shared_ptr &primitive); + void addObj(char const *fileName, Vector3d const &scale, Vector3d const &translation, const std::shared_ptr &shader, bool flipU = false, bool flipV = false); + + // .obj loading function + static std::vector> loadObj(char const *fileName, Vector3d const &scale, Vector3d const &translation, const std::shared_ptr &shader, bool flipU = false, bool flipV = true); + // Raytracing functions Color traceRay(Ray &ray) const; virtual bool findIntersection(Ray &ray) const = 0;