added ex03 task

This commit is contained in:
jp 2022-11-18 11:48:08 +01:00
parent 8b8a53dde6
commit 8453d265e0
4 changed files with 143 additions and 0 deletions

View file

@ -38,6 +38,22 @@ target_link_libraries(tracey_ex1 tracey)
add_executable(tracey_ex2 ex2.cpp) add_executable(tracey_ex2 ex2.cpp)
target_link_libraries(tracey_ex2 tracey) 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()

104
ex3.cpp Normal file
View file

@ -0,0 +1,104 @@
#include <iostream>
#include <string>
#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<SimpleShadowShader>(Color(1.0f, 0.3f, 0.2f));
auto white = std::make_shared<SimpleShadowShader>(Color(1.0f, 1.0f, 1.0f));
auto blue = std::make_shared<SimpleShadowShader>(Color(0.2f, 0.3f, 1.0f));
auto orange = std::make_shared<SimpleShadowShader>(Color(1.0f, 0.5f, 0.0f));
// Set up the cornell box walls
scene.add(std::make_shared<InfinitePlane>(Vector3d(0.0f, 0.0f, +5.0f), Vector3d(0.0f, 0.0f, -1.0f), white));
scene.add(std::make_shared<InfinitePlane>(Vector3d(0.0f, 0.0f, -5.0f), Vector3d(0.0f, 0.0f, +1.0f), white));
scene.add(std::make_shared<InfinitePlane>(Vector3d(0.0f, +5.0f, 0.0f), Vector3d(0.0f, -1.0f, 0.0f), white));
scene.add(std::make_shared<InfinitePlane>(Vector3d(0.0f, -5.0f, 0.0f), Vector3d(0.0f, +1.0f, 0.0f), white));
scene.add(std::make_shared<InfinitePlane>(Vector3d(+5.0f, 0.0f, 0.0f), Vector3d(-1.0f, 0.0f, 0.0f), blue));
scene.add(std::make_shared<InfinitePlane>(Vector3d(-5.0f, 0.0f, 0.0f), Vector3d(+1.0f, 0.0f, 0.0f), red));
scene.add(std::make_shared<Box>(Vector3d(2.5f, -3.0f, 1.0f), Vector3d(3.0f, 4.0f, 3.0f), red));
scene.add(std::make_shared<Box>(Vector3d(-3.0f, -2.0f, 0.0f), Vector3d(1.0f, 6.0f, 1.0f), blue));
scene.add(std::make_shared<Box>(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<ObjModel>(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<ObjModel>(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<ObjModel>(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<AmbientLight>(0.15f));
#endif
// Lighting situation 1
#if SITUATION == 1
scene.add(std::make_shared<PointLight>(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<SpotLight>(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;
}

View file

@ -29,6 +29,24 @@ void Scene::add(const std::shared_ptr<Primitive> &primitive) {
this->primitives_.push_back(primitive); this->primitives_.push_back(primitive);
} }
void Scene::addObj(char const *fileName, Vector3d const &scale, Vector3d const &translation,
const std::shared_ptr<Shader> &shader, bool flipU, bool flipV) {
std::vector<std::shared_ptr<Primitive>> 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<std::shared_ptr<Primitive>> Scene::loadObj(char const *fileName, Vector3d const &scale,
Vector3d const &translation,
const std::shared_ptr<Shader> &shader, bool flipU, bool flipV) {
std::vector<std::shared_ptr<Primitive>> faces;
std::vector<std::array<int, 3>> indices;
// IMPLEMENT ME
return faces;
}
Color Scene::traceRay(Ray &ray) const { Color Scene::traceRay(Ray &ray) const {
if (this->findIntersection(ray) && ray.remainingBounces-- > 0) { if (this->findIntersection(ray) && ray.remainingBounces-- > 0) {
// If the ray has hit an object, call the shader ... // If the ray has hit an object, call the shader ...

View file

@ -32,6 +32,11 @@ public:
void add(const std::shared_ptr<Light> &light); void add(const std::shared_ptr<Light> &light);
void add(const std::shared_ptr<Primitive> &primitive); void add(const std::shared_ptr<Primitive> &primitive);
void addObj(char const *fileName, Vector3d const &scale, Vector3d const &translation, const std::shared_ptr<Shader> &shader, bool flipU = false, bool flipV = false);
// .obj loading function
static std::vector<std::shared_ptr<Primitive>> loadObj(char const *fileName, Vector3d const &scale, Vector3d const &translation, const std::shared_ptr<Shader> &shader, bool flipU = false, bool flipV = true);
// Raytracing functions // Raytracing functions
Color traceRay(Ray &ray) const; Color traceRay(Ray &ray) const;
virtual bool findIntersection(Ray &ray) const = 0; virtual bool findIntersection(Ray &ray) const = 0;