├── src ├── Assignment6 │ ├── README.md │ ├── Vector.cpp │ ├── Light.hpp │ ├── Renderer.hpp │ ├── xmake.lua │ ├── Intersection.hpp │ ├── AreaLight.hpp │ ├── Object.hpp │ ├── Ray.hpp │ ├── Material.hpp │ ├── main.cpp │ ├── global.hpp │ ├── BVH.hpp │ ├── Renderer.cpp │ ├── Sphere.hpp │ ├── Vector.hpp │ ├── BVH.cpp │ ├── Bounds3.hpp │ └── Scene.hpp ├── Assignment7 │ ├── README.md │ ├── Vector.cpp │ ├── Light.hpp │ ├── Renderer.hpp │ ├── xmake.lua │ ├── Intersection.hpp │ ├── AreaLight.hpp │ ├── Ray.hpp │ ├── Object.hpp │ ├── global.hpp │ ├── Scene.cpp │ ├── BVH.hpp │ ├── Renderer.cpp │ ├── main.cpp │ ├── Vector.hpp │ ├── Sphere.hpp │ ├── Bounds3.hpp │ ├── Scene.hpp │ └── BVH.cpp ├── Assignment3 │ ├── Texture.cpp │ ├── global.hpp │ ├── xmake.lua │ ├── Shader.hpp │ ├── Texture.hpp │ ├── Triangle.hpp │ ├── Triangle.cpp │ └── rasterizer.hpp ├── Assignment5 │ ├── Scene.cpp │ ├── Renderer.hpp │ ├── Light.hpp │ ├── xmake.lua │ ├── Object.hpp │ ├── Scene.hpp │ ├── Sphere.hpp │ ├── main.cpp │ ├── global.hpp │ ├── Vector.hpp │ └── Triangle.hpp ├── Assignment8 │ ├── CGL │ │ ├── src │ │ │ ├── base64.h │ │ │ ├── vector2D.cpp │ │ │ ├── vector3D.cpp │ │ │ ├── quaternion.cpp │ │ │ ├── vector4D.cpp │ │ │ ├── CGL.h │ │ │ ├── complex.cpp │ │ │ ├── console.h │ │ │ ├── color.cpp │ │ │ ├── complex.h │ │ │ ├── matrix3x3.h │ │ │ ├── vector2D.h │ │ │ ├── viewer.h │ │ │ ├── matrix4x4.h │ │ │ ├── color.h │ │ │ ├── vector3D.h │ │ │ ├── base64.cpp │ │ │ └── vector4D.h │ │ └── include │ │ │ └── CGL │ │ │ ├── base64.h │ │ │ ├── CGL.h │ │ │ ├── misc.h │ │ │ ├── complex.h │ │ │ ├── matrix3x3.h │ │ │ ├── vector2D.h │ │ │ ├── viewer.h │ │ │ ├── matrix4x4.h │ │ │ ├── color.h │ │ │ ├── vector3D.h │ │ │ ├── vector4D.h │ │ │ └── osdtext.h │ ├── getopt-for-windows │ │ └── README.md │ ├── src │ │ ├── spring.h │ │ ├── mass.h │ │ ├── rope.h │ │ ├── application.h │ │ ├── main.cpp │ │ ├── rope.cpp │ │ └── application.cpp │ └── xmake.lua ├── Assignment2 │ ├── global.hpp │ ├── xmake.lua │ ├── Triangle.hpp │ ├── Triangle.cpp │ ├── rasterizer.hpp │ └── main.cpp ├── Assignment0 │ ├── xmake.lua │ └── main.cpp ├── Assignment4 │ ├── xmake.lua │ └── main.cpp └── Assignment1 │ ├── xmake.lua │ ├── Triangle.hpp │ ├── Triangle.cpp │ ├── rasterizer.hpp │ └── main.cpp ├── doc ├── Assignment0.pdf ├── Assignment1.pdf ├── Assignment2.pdf ├── Assignment3.pdf ├── Assignment4.pdf ├── Assignment5.pdf ├── Assignment6.pdf ├── Assignment7.pdf ├── Assignment8.pdf └── FinalProject.pdf ├── models ├── cube │ ├── wall.tif │ ├── wall1.tif │ ├── cube.mtl │ └── cube.obj ├── rock │ ├── rock.png │ └── rock.mtl ├── spot │ ├── hmap.jpg │ ├── spot_texture.png │ ├── README.txt │ └── spot_texture.svg ├── Crate │ ├── Crate1.3ds │ ├── crate_1.jpg │ ├── Crate1.blend │ ├── CrateImage1.JPG │ ├── CrateImage2.JPG │ ├── Crate1.mtl │ └── Crate1.obj └── cornellbox │ ├── right.obj │ ├── left.obj │ ├── light.obj │ ├── floor.obj │ ├── shortbox.obj │ └── tallbox.obj ├── .gitignore └── README.md /src/Assignment6/README.md: -------------------------------------------------------------------------------- 1 | 2 | I've finished task ... 3 | 4 | -------------------------------------------------------------------------------- /src/Assignment7/README.md: -------------------------------------------------------------------------------- 1 | 2 | I've finished task ... 3 | 4 | -------------------------------------------------------------------------------- /src/Assignment3/Texture.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by LEI XU on 4/27/19. 3 | // 4 | 5 | -------------------------------------------------------------------------------- /doc/Assignment0.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/star-hengxing/GAMES101-xmake/HEAD/doc/Assignment0.pdf -------------------------------------------------------------------------------- /doc/Assignment1.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/star-hengxing/GAMES101-xmake/HEAD/doc/Assignment1.pdf -------------------------------------------------------------------------------- /doc/Assignment2.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/star-hengxing/GAMES101-xmake/HEAD/doc/Assignment2.pdf -------------------------------------------------------------------------------- /doc/Assignment3.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/star-hengxing/GAMES101-xmake/HEAD/doc/Assignment3.pdf -------------------------------------------------------------------------------- /doc/Assignment4.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/star-hengxing/GAMES101-xmake/HEAD/doc/Assignment4.pdf -------------------------------------------------------------------------------- /doc/Assignment5.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/star-hengxing/GAMES101-xmake/HEAD/doc/Assignment5.pdf -------------------------------------------------------------------------------- /doc/Assignment6.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/star-hengxing/GAMES101-xmake/HEAD/doc/Assignment6.pdf -------------------------------------------------------------------------------- /doc/Assignment7.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/star-hengxing/GAMES101-xmake/HEAD/doc/Assignment7.pdf -------------------------------------------------------------------------------- /doc/Assignment8.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/star-hengxing/GAMES101-xmake/HEAD/doc/Assignment8.pdf -------------------------------------------------------------------------------- /doc/FinalProject.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/star-hengxing/GAMES101-xmake/HEAD/doc/FinalProject.pdf -------------------------------------------------------------------------------- /models/cube/wall.tif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/star-hengxing/GAMES101-xmake/HEAD/models/cube/wall.tif -------------------------------------------------------------------------------- /models/cube/wall1.tif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/star-hengxing/GAMES101-xmake/HEAD/models/cube/wall1.tif -------------------------------------------------------------------------------- /models/rock/rock.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/star-hengxing/GAMES101-xmake/HEAD/models/rock/rock.png -------------------------------------------------------------------------------- /models/spot/hmap.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/star-hengxing/GAMES101-xmake/HEAD/models/spot/hmap.jpg -------------------------------------------------------------------------------- /src/Assignment6/Vector.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by LEI XU on 5/13/19. 3 | // 4 | 5 | #include "Vector.hpp" 6 | -------------------------------------------------------------------------------- /src/Assignment7/Vector.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by LEI XU on 5/13/19. 3 | // 4 | 5 | #include "Vector.hpp" 6 | -------------------------------------------------------------------------------- /models/Crate/Crate1.3ds: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/star-hengxing/GAMES101-xmake/HEAD/models/Crate/Crate1.3ds -------------------------------------------------------------------------------- /models/Crate/crate_1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/star-hengxing/GAMES101-xmake/HEAD/models/Crate/crate_1.jpg -------------------------------------------------------------------------------- /models/Crate/Crate1.blend: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/star-hengxing/GAMES101-xmake/HEAD/models/Crate/Crate1.blend -------------------------------------------------------------------------------- /models/Crate/CrateImage1.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/star-hengxing/GAMES101-xmake/HEAD/models/Crate/CrateImage1.JPG -------------------------------------------------------------------------------- /models/Crate/CrateImage2.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/star-hengxing/GAMES101-xmake/HEAD/models/Crate/CrateImage2.JPG -------------------------------------------------------------------------------- /models/spot/spot_texture.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/star-hengxing/GAMES101-xmake/HEAD/models/spot/spot_texture.png -------------------------------------------------------------------------------- /src/Assignment5/Scene.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by Göksu Güvendiren on 2019-05-14. 3 | // 4 | 5 | #include "Scene.hpp" 6 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .vs 2 | .vscode 3 | .xmake 4 | .cache 5 | 6 | # build 7 | build 8 | out 9 | compile_commands.json 10 | binary.ppm -------------------------------------------------------------------------------- /models/cornellbox/right.obj: -------------------------------------------------------------------------------- 1 | v 0.0 0.0 559.2 2 | v 0.0 0.0 0.0 3 | v 0.0 548.8 0.0 4 | v 0.0 548.8 559.2 5 | f 1 2 3 6 | f 1 3 4 -------------------------------------------------------------------------------- /models/cornellbox/left.obj: -------------------------------------------------------------------------------- 1 | v 552.8 0.0 0.0 2 | v 549.6 0.0 559.2 3 | v 556.0 548.8 559.2 4 | v 556.0 548.8 0.0 5 | f 1 2 3 6 | f 1 3 4 -------------------------------------------------------------------------------- /models/cornellbox/light.obj: -------------------------------------------------------------------------------- 1 | v 343.0 548.7 227.0 2 | v 343.0 548.7 332.0 3 | v 213.0 548.7 332.0 4 | v 213.0 548.7 227.0 5 | f 1 2 3 6 | f 1 3 4 -------------------------------------------------------------------------------- /src/Assignment8/CGL/src/base64.h: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | std::string base64_encode(unsigned char const* , unsigned int len); 4 | std::string base64_decode(std::string const& s); -------------------------------------------------------------------------------- /src/Assignment8/CGL/include/CGL/base64.h: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | std::string base64_encode(unsigned char const* , unsigned int len); 4 | std::string base64_decode(std::string const& s); -------------------------------------------------------------------------------- /src/Assignment2/global.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by LEI XU on 4/9/19. 3 | // 4 | 5 | #ifndef RASTERIZER_GLOBAL_H 6 | #define RASTERIZER_GLOBAL_H 7 | 8 | //#define MY_PI 3.1415926 9 | //#define TWO_PI (2.0* MY_PI) 10 | 11 | 12 | #endif //RASTERIZER_GLOBAL_H 13 | -------------------------------------------------------------------------------- /src/Assignment8/CGL/src/vector2D.cpp: -------------------------------------------------------------------------------- 1 | #include "vector2D.h" 2 | 3 | namespace CGL { 4 | 5 | std::ostream& operator<<( std::ostream& os, const Vector2D& v ) { 6 | os << "( " << v.x << ", " << v.y << " )"; 7 | return os; 8 | } 9 | 10 | } // namespace CGL 11 | -------------------------------------------------------------------------------- /src/Assignment8/CGL/src/vector3D.cpp: -------------------------------------------------------------------------------- 1 | #include "vector3D.h" 2 | 3 | namespace CGL { 4 | 5 | std::ostream& operator<<( std::ostream& os, const Vector3D& v ) { 6 | os << "{ " << v.x << ", " << v.y << ", " << v.z << " }"; 7 | return os; 8 | } 9 | 10 | } // namespace CGL 11 | -------------------------------------------------------------------------------- /models/cube/cube.mtl: -------------------------------------------------------------------------------- 1 | # Blender MTL File: 'Crate1.blend' 2 | # Material Count: 1 3 | 4 | newmtl Material.001 5 | Ns 96.078431 6 | Ka 0.000000 0.000000 0.000000 7 | Kd 0.640000 0.640000 0.640000 8 | Ks 0.500000 0.500000 0.500000 9 | Ni 1.000000 10 | d 1.000000 11 | illum 2 12 | map_Kd wall1.tif 13 | -------------------------------------------------------------------------------- /src/Assignment3/global.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by LEI XU on 4/9/19. 3 | // 4 | 5 | #ifndef RASTERIZER_GLOBAL_H 6 | #define RASTERIZER_GLOBAL_H 7 | 8 | typedef unsigned char u08; 9 | #define MY_PI 3.1415926 10 | #define TWO_PI (2.0* MY_PI) 11 | 12 | 13 | 14 | #endif //RASTERIZER_GLOBAL_H 15 | -------------------------------------------------------------------------------- /src/Assignment0/xmake.lua: -------------------------------------------------------------------------------- 1 | set_project("Transformation") 2 | 3 | add_rules("mode.debug") 4 | 5 | add_requires("eigen") 6 | 7 | set_languages("cxx17") 8 | 9 | target("Transformation") 10 | set_kind("binary") 11 | add_packages("eigen") 12 | add_files("*.cpp") 13 | set_rundir("$(projectdir)") -------------------------------------------------------------------------------- /src/Assignment5/Renderer.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "Scene.hpp" 3 | 4 | struct hit_payload 5 | { 6 | float tNear; 7 | uint32_t index; 8 | Vector2f uv; 9 | Object* hit_obj; 10 | }; 11 | 12 | class Renderer 13 | { 14 | public: 15 | void Render(const Scene& scene); 16 | 17 | private: 18 | }; -------------------------------------------------------------------------------- /src/Assignment8/CGL/src/quaternion.cpp: -------------------------------------------------------------------------------- 1 | #include "quaternion.h" 2 | 3 | namespace CGL { 4 | 5 | std::ostream& operator<<( std::ostream& os, const Quaternion& v ) { 6 | os << "{ " << v.x << "i, " << v.y << "j, " << v.z << "k, " << v.w << " }"; 7 | return os; 8 | } 9 | 10 | } // namespace CGL 11 | -------------------------------------------------------------------------------- /models/rock/rock.mtl: -------------------------------------------------------------------------------- 1 | # Blender MTL File: 'Rock1.blend' 2 | # Material Count: 1 3 | 4 | newmtl Material 5 | Ns 13.725490 6 | Ka 0.000000 0.000000 0.000000 7 | Kd 0.640000 0.640000 0.640000 8 | Ks 0.007937 0.007937 0.007937 9 | Ni 1.000000 10 | d 1.000000 11 | illum 2 12 | map_Kd rock.png 13 | map_Bump rock.png 14 | -------------------------------------------------------------------------------- /src/Assignment5/Light.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "Vector.hpp" 4 | 5 | class Light 6 | { 7 | public: 8 | Light(const Vector3f& p, const Vector3f& i) 9 | : position(p) 10 | , intensity(i) 11 | {} 12 | virtual ~Light() = default; 13 | Vector3f position; 14 | Vector3f intensity; 15 | }; 16 | -------------------------------------------------------------------------------- /src/Assignment8/getopt-for-windows/README.md: -------------------------------------------------------------------------------- 1 | # getopt-for-windows 2 | getopt.h and getopt.c is very often used in linux, to make it easy for windows user, two files were extracted from glibc. In order to make it works properly in windows, some modification was done and you may compare the change using original source files. Enjoy it! 3 | -------------------------------------------------------------------------------- /models/Crate/Crate1.mtl: -------------------------------------------------------------------------------- 1 | # Blender MTL File: 'Crate1.blend' 2 | # Material Count: 1 3 | 4 | newmtl Material.001 5 | Ns 96.078431 6 | Ka 0.000000 0.000000 0.000000 7 | Kd 0.640000 0.640000 0.640000 8 | Ks 0.500000 0.500000 0.500000 9 | Ni 1.000000 10 | d 1.000000 11 | illum 2 12 | map_Kd C:\\Users\\Elijah\\Pictures\\ToBeTextures\\crate_1.jpg 13 | -------------------------------------------------------------------------------- /models/cornellbox/floor.obj: -------------------------------------------------------------------------------- 1 | v 552.8 0.0 0.0 2 | v 0.0 0.0 0.0 3 | v 0.0 0.0 559.2 4 | v 549.6 0.0 559.2 5 | v 556.0 548.8 0.0 6 | v 556.0 548.8 559.2 7 | v 0.0 548.8 559.2 8 | v 0.0 548.8 0.0 9 | v 549.6 0.0 559.2 10 | v 0.0 0.0 559.2 11 | v 0.0 548.8 559.2 12 | v 556.0 548.8 559.2 13 | f 1 2 3 14 | f 3 4 1 15 | f 5 6 7 16 | f 7 8 5 17 | f 9 10 11 18 | f 11 12 9 -------------------------------------------------------------------------------- /src/Assignment6/Light.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by Göksu Güvendiren on 2019-05-14. 3 | // 4 | 5 | #pragma once 6 | 7 | #include "Vector.hpp" 8 | 9 | class Light 10 | { 11 | public: 12 | Light(const Vector3f &p, const Vector3f &i) : position(p), intensity(i) {} 13 | virtual ~Light() = default; 14 | Vector3f position; 15 | Vector3f intensity; 16 | }; 17 | -------------------------------------------------------------------------------- /src/Assignment6/Renderer.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by goksu on 2/25/20. 3 | // 4 | #include "Scene.hpp" 5 | 6 | #pragma once 7 | struct hit_payload 8 | { 9 | float tNear; 10 | uint32_t index; 11 | Vector2f uv; 12 | Object* hit_obj; 13 | }; 14 | 15 | class Renderer 16 | { 17 | public: 18 | void Render(const Scene& scene); 19 | 20 | private: 21 | }; 22 | -------------------------------------------------------------------------------- /src/Assignment7/Light.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by Göksu Güvendiren on 2019-05-14. 3 | // 4 | 5 | #pragma once 6 | 7 | #include "Vector.hpp" 8 | 9 | class Light 10 | { 11 | public: 12 | Light(const Vector3f &p, const Vector3f &i) : position(p), intensity(i) {} 13 | virtual ~Light() = default; 14 | Vector3f position; 15 | Vector3f intensity; 16 | }; 17 | -------------------------------------------------------------------------------- /src/Assignment7/Renderer.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by goksu on 2/25/20. 3 | // 4 | #include "Scene.hpp" 5 | 6 | #pragma once 7 | struct hit_payload 8 | { 9 | float tNear; 10 | uint32_t index; 11 | Vector2f uv; 12 | Object* hit_obj; 13 | }; 14 | 15 | class Renderer 16 | { 17 | public: 18 | void Render(const Scene& scene); 19 | 20 | private: 21 | }; 22 | -------------------------------------------------------------------------------- /src/Assignment8/CGL/src/vector4D.cpp: -------------------------------------------------------------------------------- 1 | #include "vector4D.h" 2 | 3 | namespace CGL { 4 | 5 | std::ostream& operator<<( std::ostream& os, const Vector4D& v ) { 6 | os << "{ " << v.x << ", " << v.y << ", " << v.z << ", " << v.w << " }"; 7 | return os; 8 | } 9 | 10 | Vector3D Vector4D::to3D() 11 | { 12 | return Vector3D(x, y, z); 13 | } 14 | 15 | } // namespace CGL 16 | -------------------------------------------------------------------------------- /src/Assignment8/CGL/src/CGL.h: -------------------------------------------------------------------------------- 1 | #include "vector2D.h" 2 | #include "complex.h" 3 | 4 | #include "vector3D.h" 5 | #include "matrix3x3.h" 6 | 7 | #include "vector4D.h" 8 | #include "matrix4x4.h" 9 | 10 | // quaternions seem pretty cool. 11 | #include "quaternion.h" 12 | 13 | 14 | #include "color.h" 15 | #include "renderer.h" 16 | #include "viewer.h" 17 | 18 | #include "base64.h" 19 | #include "tinyxml2.h" 20 | -------------------------------------------------------------------------------- /src/Assignment8/CGL/include/CGL/CGL.h: -------------------------------------------------------------------------------- 1 | #include "vector2D.h" 2 | #include "complex.h" 3 | 4 | #include "vector3D.h" 5 | #include "matrix3x3.h" 6 | 7 | #include "vector4D.h" 8 | #include "matrix4x4.h" 9 | 10 | // quaternions seem pretty cool. 11 | #include "quaternion.h" 12 | 13 | 14 | #include "color.h" 15 | #include "renderer.h" 16 | #include "viewer.h" 17 | 18 | #include "base64.h" 19 | #include "tinyxml2.h" 20 | -------------------------------------------------------------------------------- /src/Assignment8/CGL/src/complex.cpp: -------------------------------------------------------------------------------- 1 | #include "complex.h" 2 | 3 | namespace CGL { 4 | 5 | std::ostream& operator<<( std::ostream& os, const Complex& z ) { 6 | if( z.y > 0 ) { 7 | os << z.x << " + " << z.y << "i"; 8 | } else if( z.y < 0 ) { 9 | os << z.x << " - " << -z.y << "i"; 10 | } else { 11 | os << z.x; 12 | } 13 | return os; 14 | } 15 | 16 | } // namespace CGL 17 | -------------------------------------------------------------------------------- /src/Assignment4/xmake.lua: -------------------------------------------------------------------------------- 1 | set_project("BezierCurve") 2 | 3 | add_rules("mode.debug", "mode.release") 4 | 5 | add_requires("opencv") 6 | 7 | set_languages("cxx14") 8 | 9 | if is_os("windows") then 10 | add_defines("_CRT_SECURE_NO_WARNINGS") 11 | add_cxflags("/utf-8") 12 | end 13 | 14 | target("BezierCurve") 15 | set_kind("binary") 16 | add_packages("opencv") 17 | add_files("*.cpp") 18 | set_rundir("$(projectdir)") -------------------------------------------------------------------------------- /src/Assignment6/xmake.lua: -------------------------------------------------------------------------------- 1 | set_project("RayTracing") 2 | 3 | add_rules("mode.debug", "mode.release") 4 | 5 | set_languages("cxx17") 6 | 7 | set_warnings("all") 8 | 9 | if is_os("windows") then 10 | add_defines("_CRT_SECURE_NO_WARNINGS") 11 | add_cxflags("/utf-8") 12 | end 13 | 14 | target("RayTracing") 15 | set_kind("binary") 16 | add_files("*.cpp") 17 | add_headerfiles("*.hpp") 18 | set_rundir("$(projectdir)") 19 | -------------------------------------------------------------------------------- /src/Assignment7/xmake.lua: -------------------------------------------------------------------------------- 1 | set_project("RayTracing") 2 | 3 | add_rules("mode.debug", "mode.release") 4 | 5 | set_languages("cxx17") 6 | 7 | set_warnings("all") 8 | 9 | if is_os("windows") then 10 | add_defines("_CRT_SECURE_NO_WARNINGS") 11 | add_cxflags("/utf-8") 12 | end 13 | 14 | target("RayTracing") 15 | set_kind("binary") 16 | add_files("*.cpp") 17 | add_headerfiles("*.hpp") 18 | set_rundir("$(projectdir)") 19 | -------------------------------------------------------------------------------- /src/Assignment5/xmake.lua: -------------------------------------------------------------------------------- 1 | set_project("RayTracing") 2 | 3 | add_rules("mode.debug", "mode.release") 4 | 5 | set_languages("cxx17") 6 | 7 | set_warnings("all") 8 | 9 | if is_os("windows") then 10 | add_defines("_CRT_SECURE_NO_WARNINGS") 11 | add_cxflags("/utf-8") 12 | end 13 | 14 | target("RayTracing") 15 | set_kind("binary") 16 | add_files("*.cpp") 17 | add_headerfiles("*.hpp") 18 | 19 | set_rundir("$(projectdir)") 20 | -------------------------------------------------------------------------------- /src/Assignment8/src/spring.h: -------------------------------------------------------------------------------- 1 | #ifndef SPRING_H 2 | #define SPRING_H 3 | 4 | #include "CGL/CGL.h" 5 | #include "mass.h" 6 | 7 | using namespace std; 8 | 9 | namespace CGL { 10 | 11 | struct Spring { 12 | Spring(Mass *a, Mass *b, float k) 13 | : m1(a), m2(b), k(k), rest_length((a->position - b->position).norm()) {} 14 | 15 | float k; 16 | double rest_length; 17 | 18 | Mass *m1; 19 | Mass *m2; 20 | }; // struct Spring 21 | } 22 | #endif /* SPRING_H */ 23 | -------------------------------------------------------------------------------- /src/Assignment1/xmake.lua: -------------------------------------------------------------------------------- 1 | set_project("Rasterizer") 2 | 3 | add_rules("mode.debug", "mode.release") 4 | 5 | add_requires("opencv", "eigen") 6 | 7 | set_languages("cxx17") 8 | 9 | if is_os("windows") then 10 | add_defines("_CRT_SECURE_NO_WARNINGS") 11 | add_cxflags("/utf-8") 12 | end 13 | 14 | target("Rasterizer") 15 | set_kind("binary") 16 | add_packages("opencv", "eigen") 17 | add_files("*.cpp") 18 | add_headerfiles("*.hpp") 19 | set_rundir("$(projectdir)") 20 | -------------------------------------------------------------------------------- /src/Assignment2/xmake.lua: -------------------------------------------------------------------------------- 1 | set_project("Rasterizer") 2 | 3 | add_rules("mode.debug", "mode.release") 4 | 5 | add_requires("opencv", "eigen") 6 | 7 | set_languages("cxx17") 8 | 9 | if is_os("windows") then 10 | add_defines("_CRT_SECURE_NO_WARNINGS") 11 | add_cxflags("/utf-8") 12 | end 13 | 14 | target("Rasterizer") 15 | set_kind("binary") 16 | add_packages("opencv", "eigen") 17 | add_files("*.cpp") 18 | add_headerfiles("*.hpp") 19 | set_rundir("$(projectdir)") 20 | -------------------------------------------------------------------------------- /src/Assignment3/xmake.lua: -------------------------------------------------------------------------------- 1 | set_project("Rasterizer") 2 | 3 | add_rules("mode.debug", "mode.release") 4 | 5 | add_requires("opencv", "eigen") 6 | 7 | set_languages("cxx17") 8 | 9 | if is_os("windows") then 10 | add_defines("_CRT_SECURE_NO_WARNINGS") 11 | add_cxflags("/utf-8") 12 | end 13 | 14 | target("Rasterizer") 15 | set_kind("binary") 16 | add_packages("opencv", "eigen") 17 | add_files("*.cpp") 18 | add_headerfiles("*.hpp") 19 | set_rundir("$(projectdir)") 20 | -------------------------------------------------------------------------------- /models/cornellbox/shortbox.obj: -------------------------------------------------------------------------------- 1 | v 130.0 165.0 65.0 2 | v 82.0 165.0 225.0 3 | v 240.0 165.0 272.0 4 | v 290.0 165.0 114.0 5 | v 290.0 0.0 114.0 6 | v 290.0 165.0 114.0 7 | v 240.0 165.0 272.0 8 | v 240.0 0.0 272.0 9 | v 130.0 0.0 65.0 10 | v 130.0 165.0 65.0 11 | v 290.0 165.0 114.0 12 | v 290.0 0.0 114.0 13 | v 82.0 0.0 225.0 14 | v 82.0 165.0 225.0 15 | v 130.0 165.0 65.0 16 | v 130.0 0.0 65.0 17 | v 240.0 0.0 272.0 18 | v 240.0 165.0 272.0 19 | v 82.0 165.0 225.0 20 | v 82.0 0.0 225.0 21 | f 1 2 3 22 | f 1 3 4 23 | f 5 6 7 24 | f 5 7 8 25 | f 9 10 11 26 | f 9 11 12 27 | f 13 14 15 28 | f 13 15 16 29 | f 17 18 19 30 | f 17 19 20 -------------------------------------------------------------------------------- /models/cornellbox/tallbox.obj: -------------------------------------------------------------------------------- 1 | v 423.0 330.0 247.0 2 | v 265.0 330.0 296.0 3 | v 314.0 330.0 456.0 4 | v 472.0 330.0 406.0 5 | v 423.0 0.0 247.0 6 | v 423.0 330.0 247.0 7 | v 472.0 330.0 406.0 8 | v 472.0 0.0 406.0 9 | v 472.0 0.0 406.0 10 | v 472.0 330.0 406.0 11 | v 314.0 330.0 456.0 12 | v 314.0 0.0 456.0 13 | v 314.0 0.0 456.0 14 | v 314.0 330.0 456.0 15 | v 265.0 330.0 296.0 16 | v 265.0 0.0 296.0 17 | v 265.0 0.0 296.0 18 | v 265.0 330.0 296.0 19 | v 423.0 330.0 247.0 20 | v 423.0 0.0 247.0 21 | f 1 2 3 22 | f 1 3 4 23 | f 5 6 7 24 | f 5 7 8 25 | f 9 10 11 26 | f 9 11 12 27 | f 13 14 15 28 | f 13 15 16 29 | f 17 18 19 30 | f 17 19 20 -------------------------------------------------------------------------------- /src/Assignment8/CGL/src/console.h: -------------------------------------------------------------------------------- 1 | #ifndef CGL_CONSOLE_H 2 | #define CGL_CONSOLE_H 3 | 4 | #include 5 | 6 | #define ANSI_RESET "\x1b[0m" 7 | #define ANSI_R "\x1b[31m" 8 | #define ANSI_G "\x1b[32m" 9 | #define ANSI_B "\x1b[34m" 10 | #define ANSI_C "\x1b[36m" 11 | #define ANSI_M "\x1b[35m" 12 | #define ANSI_Y "\x1b[33m" 13 | 14 | #define out_msg(s) std::cout << ANSI_B << "[CGL] " << ANSI_RESET << s << std::endl << std::flush 15 | #define out_wrn(s) std::cout << ANSI_Y << "[CGL] " << ANSI_RESET << s << std::endl << std::flush 16 | #define out_err(s) std::cout << ANSI_R << "[CGL] " << ANSI_RESET << s << std::endl << std::flush 17 | 18 | #endif // CGL_CONSOLE_H 19 | -------------------------------------------------------------------------------- /models/Crate/Crate1.obj: -------------------------------------------------------------------------------- 1 | # Blender v2.67 (sub 1) OBJ File: 'Crate1.blend' 2 | # www.blender.org 3 | mtllib Crate1.mtl 4 | o Cube_Cube.001 5 | v -1.000000 -1.000000 1.000000 6 | v -1.000000 -1.000000 -1.000000 7 | v 1.000000 -1.000000 -1.000000 8 | v 1.000000 -1.000000 1.000000 9 | v -1.000000 1.000000 1.000000 10 | v -1.000000 1.000000 -1.000000 11 | v 1.000000 1.000000 -1.000000 12 | v 1.000000 1.000000 1.000000 13 | vt 0.000000 0.000000 14 | vt 1.000000 0.000000 15 | vt 1.000000 1.000000 16 | vt 0.000000 1.000000 17 | usemtl Material.001 18 | f 5/1 6/2 2/3 1/4 19 | f 6/1 7/2 3/3 2/4 20 | f 7/1 8/2 4/3 3/4 21 | f 8/1 5/2 1/3 4/4 22 | f 1/1 2/2 3/3 4/4 23 | f 8/1 7/2 6/3 5/4 24 | -------------------------------------------------------------------------------- /src/Assignment8/src/mass.h: -------------------------------------------------------------------------------- 1 | #ifndef MASS_H 2 | #define MASS_H 3 | 4 | #include "CGL/CGL.h" 5 | #include "CGL/vector2D.h" 6 | 7 | using namespace CGL; 8 | 9 | struct Mass { 10 | Mass(Vector2D position, float mass, bool pinned) 11 | : start_position(position), position(position), last_position(position), 12 | mass(mass), pinned(pinned) {} 13 | 14 | float mass; 15 | bool pinned; 16 | 17 | Vector2D start_position; 18 | Vector2D position; 19 | 20 | // explicit Verlet integration 21 | 22 | Vector2D last_position; 23 | 24 | // explicit Euler integration 25 | 26 | Vector2D velocity; 27 | Vector2D forces; 28 | }; 29 | 30 | #endif /* MASS_H */ 31 | -------------------------------------------------------------------------------- /src/Assignment8/src/rope.h: -------------------------------------------------------------------------------- 1 | #ifndef ROPE_H 2 | #define ROPE_H 3 | 4 | #include "CGL/CGL.h" 5 | #include "mass.h" 6 | #include "spring.h" 7 | 8 | using namespace std; 9 | 10 | namespace CGL { 11 | 12 | class Rope { 13 | public: 14 | Rope(vector &masses, vector &springs) 15 | : masses(masses), springs(springs) {} 16 | Rope(Vector2D start, Vector2D end, int num_nodes, float node_mass, float k, 17 | vector pinned_nodes); 18 | 19 | void simulateVerlet(float delta_t, Vector2D gravity); 20 | void simulateEuler(float delta_t, Vector2D gravity); 21 | 22 | vector masses; 23 | vector springs; 24 | }; // struct Rope 25 | } 26 | #endif /* ROPE_H */ 27 | -------------------------------------------------------------------------------- /src/Assignment6/Intersection.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by LEI XU on 5/16/19. 3 | // 4 | 5 | #ifndef RAYTRACING_INTERSECTION_H 6 | #define RAYTRACING_INTERSECTION_H 7 | #include "Vector.hpp" 8 | #include "Material.hpp" 9 | class Object; 10 | class Sphere; 11 | 12 | struct Intersection 13 | { 14 | Intersection(){ 15 | happened=false; 16 | coords=Vector3f(); 17 | normal=Vector3f(); 18 | distance= std::numeric_limits::max(); 19 | obj =nullptr; 20 | m=nullptr; 21 | } 22 | bool happened; 23 | Vector3f coords; 24 | Vector3f normal; 25 | double distance; 26 | Object* obj; 27 | Material* m; 28 | }; 29 | #endif //RAYTRACING_INTERSECTION_H 30 | -------------------------------------------------------------------------------- /src/Assignment7/Intersection.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by LEI XU on 5/16/19. 3 | // 4 | 5 | #ifndef RAYTRACING_INTERSECTION_H 6 | #define RAYTRACING_INTERSECTION_H 7 | #include "Vector.hpp" 8 | #include "Material.hpp" 9 | class Object; 10 | class Sphere; 11 | 12 | struct Intersection 13 | { 14 | Intersection(){ 15 | happened=false; 16 | coords=Vector3f(); 17 | normal=Vector3f(); 18 | distance= std::numeric_limits::max(); 19 | obj =nullptr; 20 | m=nullptr; 21 | } 22 | bool happened; 23 | Vector3f coords; 24 | Vector3f tcoords; 25 | Vector3f normal; 26 | Vector3f emit; 27 | double distance; 28 | Object* obj; 29 | Material* m; 30 | }; 31 | #endif //RAYTRACING_INTERSECTION_H 32 | -------------------------------------------------------------------------------- /src/Assignment8/xmake.lua: -------------------------------------------------------------------------------- 1 | set_project("Assignment8") 2 | 3 | add_rules("mode.debug", "mode.release") 4 | 5 | add_requires("glew", "glfw", "freetype") 6 | 7 | set_languages("cxx17") 8 | 9 | add_includedirs("CGL/include") 10 | 11 | if is_os("windows") then 12 | target("getopt") 13 | set_kind("static") 14 | add_files("getopt-for-windows/*.c") 15 | end 16 | 17 | target("CGL") 18 | set_kind("static") 19 | add_packages("glew", "glfw", "freetype") 20 | add_files("CGL/src/*.cpp") 21 | 22 | target("main") 23 | set_kind("binary") 24 | add_deps("CGL") 25 | if is_os("windows") then 26 | add_includedirs("getopt-for-windows") 27 | add_deps("getopt") 28 | end 29 | add_packages("glew", "glfw") 30 | add_files("src/*.cpp") -------------------------------------------------------------------------------- /src/Assignment6/AreaLight.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by Göksu Güvendiren on 2019-05-14. 3 | // 4 | 5 | #pragma once 6 | 7 | #include "Vector.hpp" 8 | #include "Light.hpp" 9 | #include "global.hpp" 10 | 11 | class AreaLight : public Light 12 | { 13 | public: 14 | AreaLight(const Vector3f &p, const Vector3f &i) : Light(p, i) 15 | { 16 | normal = Vector3f(0, -1, 0); 17 | u = Vector3f(1, 0, 0); 18 | v = Vector3f(0, 0, 1); 19 | length = 100; 20 | } 21 | 22 | Vector3f SamplePoint() const 23 | { 24 | auto random_u = get_random_float(); 25 | auto random_v = get_random_float(); 26 | return position + random_u * u + random_v * v; 27 | } 28 | 29 | float length; 30 | Vector3f normal; 31 | Vector3f u; 32 | Vector3f v; 33 | }; 34 | -------------------------------------------------------------------------------- /src/Assignment7/AreaLight.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by Göksu Güvendiren on 2019-05-14. 3 | // 4 | 5 | #pragma once 6 | 7 | #include "Vector.hpp" 8 | #include "Light.hpp" 9 | #include "global.hpp" 10 | 11 | class AreaLight : public Light 12 | { 13 | public: 14 | AreaLight(const Vector3f &p, const Vector3f &i) : Light(p, i) 15 | { 16 | normal = Vector3f(0, -1, 0); 17 | u = Vector3f(1, 0, 0); 18 | v = Vector3f(0, 0, 1); 19 | length = 100; 20 | } 21 | 22 | Vector3f SamplePoint() const 23 | { 24 | auto random_u = get_random_float(); 25 | auto random_v = get_random_float(); 26 | return position + random_u * u + random_v * v; 27 | } 28 | 29 | float length; 30 | Vector3f normal; 31 | Vector3f u; 32 | Vector3f v; 33 | }; 34 | -------------------------------------------------------------------------------- /src/Assignment3/Shader.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by LEI XU on 4/27/19. 3 | // 4 | 5 | #ifndef RASTERIZER_SHADER_H 6 | #define RASTERIZER_SHADER_H 7 | #include 8 | #include "Texture.hpp" 9 | 10 | 11 | struct fragment_shader_payload 12 | { 13 | fragment_shader_payload() 14 | { 15 | texture = nullptr; 16 | } 17 | 18 | fragment_shader_payload(const Eigen::Vector3f& col, const Eigen::Vector3f& nor,const Eigen::Vector2f& tc, Texture* tex) : 19 | color(col), normal(nor), tex_coords(tc), texture(tex) {} 20 | 21 | 22 | Eigen::Vector3f view_pos; 23 | Eigen::Vector3f color; 24 | Eigen::Vector3f normal; 25 | Eigen::Vector2f tex_coords; 26 | Texture* texture; 27 | }; 28 | 29 | struct vertex_shader_payload 30 | { 31 | Eigen::Vector3f position; 32 | }; 33 | 34 | #endif //RASTERIZER_SHADER_H 35 | -------------------------------------------------------------------------------- /src/Assignment6/Object.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by LEI XU on 5/13/19. 3 | // 4 | #pragma once 5 | #ifndef RAYTRACING_OBJECT_H 6 | #define RAYTRACING_OBJECT_H 7 | 8 | #include "Vector.hpp" 9 | #include "global.hpp" 10 | #include "Bounds3.hpp" 11 | #include "Ray.hpp" 12 | #include "Intersection.hpp" 13 | 14 | class Object 15 | { 16 | public: 17 | Object() {} 18 | virtual ~Object() {} 19 | virtual bool intersect(const Ray& ray) = 0; 20 | virtual bool intersect(const Ray& ray, float &, uint32_t &) const = 0; 21 | virtual Intersection getIntersection(Ray _ray) = 0; 22 | virtual void getSurfaceProperties(const Vector3f &, const Vector3f &, const uint32_t &, const Vector2f &, Vector3f &, Vector2f &) const = 0; 23 | virtual Vector3f evalDiffuseColor(const Vector2f &) const =0; 24 | virtual Bounds3 getBounds()=0; 25 | }; 26 | 27 | 28 | 29 | #endif //RAYTRACING_OBJECT_H 30 | -------------------------------------------------------------------------------- /src/Assignment3/Texture.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by LEI XU on 4/27/19. 3 | // 4 | 5 | #ifndef RASTERIZER_TEXTURE_H 6 | #define RASTERIZER_TEXTURE_H 7 | #include "global.hpp" 8 | #include 9 | #include 10 | class Texture{ 11 | private: 12 | cv::Mat image_data; 13 | 14 | public: 15 | Texture(const std::string& name) 16 | { 17 | image_data = cv::imread(name); 18 | cv::cvtColor(image_data, image_data, cv::COLOR_RGB2BGR); 19 | width = image_data.cols; 20 | height = image_data.rows; 21 | } 22 | 23 | int width, height; 24 | 25 | Eigen::Vector3f getColor(float u, float v) 26 | { 27 | auto u_img = u * width; 28 | auto v_img = (1 - v) * height; 29 | auto color = image_data.at(v_img, u_img); 30 | return Eigen::Vector3f(color[0], color[1], color[2]); 31 | } 32 | 33 | }; 34 | #endif //RASTERIZER_TEXTURE_H 35 | -------------------------------------------------------------------------------- /src/Assignment6/Ray.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by LEI XU on 5/16/19. 3 | // 4 | 5 | #ifndef RAYTRACING_RAY_H 6 | #define RAYTRACING_RAY_H 7 | #include "Vector.hpp" 8 | struct Ray{ 9 | //Destination = origin + t*direction 10 | Vector3f origin; 11 | Vector3f direction, direction_inv; 12 | double t;//transportation time, 13 | double t_min, t_max; 14 | 15 | Ray(const Vector3f& ori, const Vector3f& dir, const double _t = 0.0): origin(ori), direction(dir),t(_t) { 16 | direction_inv = Vector3f(1./direction.x, 1./direction.y, 1./direction.z); 17 | t_min = 0.0; 18 | t_max = std::numeric_limits::max(); 19 | 20 | } 21 | 22 | Vector3f operator()(double t) const{return origin+direction*t;} 23 | 24 | friend std::ostream &operator<<(std::ostream& os, const Ray& r){ 25 | os<<"[origin:="<::max(); 19 | 20 | } 21 | 22 | Vector3f operator()(double t) const{return origin+direction*t;} 23 | 24 | friend std::ostream &operator<<(std::ostream& os, const Ray& r){ 25 | os<<"[origin:="< 4 | #include 5 | #include "Vector.hpp" 6 | #include "Object.hpp" 7 | #include "Light.hpp" 8 | 9 | class Scene 10 | { 11 | public: 12 | // setting up options 13 | int width = 1280; 14 | int height = 960; 15 | double fov = 90; 16 | Vector3f backgroundColor = Vector3f(0.235294, 0.67451, 0.843137); 17 | int maxDepth = 5; 18 | float epsilon = 0.00001; 19 | 20 | Scene(int w, int h) : width(w), height(h) 21 | {} 22 | 23 | void Add(std::unique_ptr object) { objects.push_back(std::move(object)); } 24 | void Add(std::unique_ptr light) { lights.push_back(std::move(light)); } 25 | 26 | [[nodiscard]] const std::vector >& get_objects() const { return objects; } 27 | [[nodiscard]] const std::vector >& get_lights() const { return lights; } 28 | 29 | private: 30 | // creating the scene (adding objects and lights) 31 | std::vector > objects; 32 | std::vector > lights; 33 | }; -------------------------------------------------------------------------------- /src/Assignment2/Triangle.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by LEI XU on 4/11/19. 3 | // 4 | 5 | #ifndef RASTERIZER_TRIANGLE_H 6 | #define RASTERIZER_TRIANGLE_H 7 | 8 | #include 9 | 10 | 11 | using namespace Eigen; 12 | class Triangle{ 13 | 14 | public: 15 | Vector3f v[3]; /*the original coordinates of the triangle, v0, v1, v2 in counter clockwise order*/ 16 | /*Per vertex values*/ 17 | Vector3f color[3]; //color at each vertex; 18 | Vector2f tex_coords[3]; //texture u,v 19 | Vector3f normal[3]; //normal vector for each vertex 20 | 21 | //Texture *tex; 22 | Triangle(); 23 | 24 | void setVertex(int ind, Vector3f ver); /*set i-th vertex coordinates */ 25 | void setNormal(int ind, Vector3f n); /*set i-th vertex normal vector*/ 26 | void setColor(int ind, float r, float g, float b); /*set i-th vertex color*/ 27 | Vector3f getColor() const { return color[0]*255; } // Only one color per triangle. 28 | void setTexCoord(int ind, float s, float t); /*set i-th vertex texture coordinate*/ 29 | std::array toVector4() const; 30 | }; 31 | 32 | 33 | 34 | 35 | 36 | 37 | #endif //RASTERIZER_TRIANGLE_H 38 | -------------------------------------------------------------------------------- /src/Assignment5/Sphere.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "Object.hpp" 4 | #include "Vector.hpp" 5 | 6 | class Sphere : public Object 7 | { 8 | public: 9 | Sphere(const Vector3f& c, const float& r) 10 | : center(c) 11 | , radius(r) 12 | , radius2(r * r) 13 | {} 14 | 15 | bool intersect(const Vector3f& orig, const Vector3f& dir, float& tnear, uint32_t&, Vector2f&) const override 16 | { 17 | // analytic solution 18 | Vector3f L = orig - center; 19 | float a = dotProduct(dir, dir); 20 | float b = 2 * dotProduct(dir, L); 21 | float c = dotProduct(L, L) - radius2; 22 | float t0, t1; 23 | if (!solveQuadratic(a, b, c, t0, t1)) 24 | return false; 25 | if (t0 < 0) 26 | t0 = t1; 27 | if (t0 < 0) 28 | return false; 29 | tnear = t0; 30 | 31 | return true; 32 | } 33 | 34 | void getSurfaceProperties(const Vector3f& P, const Vector3f&, const uint32_t&, const Vector2f&, 35 | Vector3f& N, Vector2f&) const override 36 | { 37 | N = normalize(P - center); 38 | } 39 | 40 | Vector3f center; 41 | float radius, radius2; 42 | }; 43 | -------------------------------------------------------------------------------- /src/Assignment6/Material.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by LEI XU on 5/16/19. 3 | // 4 | 5 | #ifndef RAYTRACING_MATERIAL_H 6 | #define RAYTRACING_MATERIAL_H 7 | 8 | #include "Vector.hpp" 9 | 10 | enum MaterialType { DIFFUSE_AND_GLOSSY, REFLECTION_AND_REFRACTION, REFLECTION }; 11 | 12 | class Material{ 13 | public: 14 | MaterialType m_type; 15 | Vector3f m_color; 16 | Vector3f m_emission; 17 | float ior; 18 | float Kd, Ks; 19 | float specularExponent; 20 | //Texture tex; 21 | 22 | inline Material(MaterialType t=DIFFUSE_AND_GLOSSY, Vector3f c=Vector3f(1,1,1), Vector3f e=Vector3f(0,0,0)); 23 | inline MaterialType getType(); 24 | inline Vector3f getColor(); 25 | inline Vector3f getColorAt(double u, double v); 26 | inline Vector3f getEmission(); 27 | 28 | 29 | }; 30 | 31 | Material::Material(MaterialType t, Vector3f c, Vector3f e){ 32 | m_type = t; 33 | m_color = c; 34 | m_emission = e; 35 | } 36 | 37 | MaterialType Material::getType(){return m_type;} 38 | Vector3f Material::getColor(){return m_color;} 39 | Vector3f Material::getEmission() {return m_emission;} 40 | 41 | Vector3f Material::getColorAt(double u, double v) { 42 | return Vector3f(); 43 | } 44 | #endif //RAYTRACING_MATERIAL_H 45 | -------------------------------------------------------------------------------- /src/Assignment1/Triangle.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by LEI XU on 4/11/19. 3 | // 4 | 5 | #ifndef RASTERIZER_TRIANGLE_H 6 | #define RASTERIZER_TRIANGLE_H 7 | 8 | #include 9 | 10 | using namespace Eigen; 11 | class Triangle 12 | { 13 | public: 14 | Vector3f v[3]; /*the original coordinates of the triangle, v0, v1, v2 in 15 | counter clockwise order*/ 16 | /*Per vertex values*/ 17 | Vector3f color[3]; // color at each vertex; 18 | Vector2f tex_coords[3]; // texture u,v 19 | Vector3f normal[3]; // normal vector for each vertex 20 | 21 | // Texture *tex; 22 | Triangle(); 23 | 24 | Eigen::Vector3f a() const { return v[0]; } 25 | Eigen::Vector3f b() const { return v[1]; } 26 | Eigen::Vector3f c() const { return v[2]; } 27 | 28 | void setVertex(int ind, Vector3f ver); /*set i-th vertex coordinates */ 29 | void setNormal(int ind, Vector3f n); /*set i-th vertex normal vector*/ 30 | void setColor(int ind, float r, float g, float b); /*set i-th vertex color*/ 31 | void setTexCoord(int ind, float s, 32 | float t); /*set i-th vertex texture coordinate*/ 33 | std::array toVector4() const; 34 | }; 35 | 36 | #endif // RASTERIZER_TRIANGLE_H 37 | -------------------------------------------------------------------------------- /models/cube/cube.obj: -------------------------------------------------------------------------------- 1 | # Blender3D v249 OBJ File: untitled.blend 2 | # www.blender3d.org 3 | mtllib cube.mtl 4 | v 1.000000 -1.000000 -1.000000 5 | v 1.000000 -1.000000 1.000000 6 | v -1.000000 -1.000000 1.000000 7 | v -1.000000 -1.000000 -1.000000 8 | v 1.000000 1.000000 -1.000000 9 | v 0.999999 1.000000 1.000001 10 | v -1.000000 1.000000 1.000000 11 | v -1.000000 1.000000 -1.000000 12 | vt 0.748573 0.750412 13 | vt 0.749279 0.501284 14 | vt 0.999110 0.501077 15 | vt 0.999455 0.750380 16 | vt 0.250471 0.500702 17 | vt 0.249682 0.749677 18 | vt 0.001085 0.750380 19 | vt 0.001517 0.499994 20 | vt 0.499422 0.500239 21 | vt 0.500149 0.750166 22 | vt 0.748355 0.998230 23 | vt 0.500193 0.998728 24 | vt 0.498993 0.250415 25 | vt 0.748953 0.250920 26 | vn 0.000000 0.000000 -1.000000 27 | vn -1.000000 -0.000000 -0.000000 28 | vn -0.000000 -0.000000 1.000000 29 | vn -0.000001 0.000000 1.000000 30 | vn 1.000000 -0.000000 0.000000 31 | vn 1.000000 0.000000 0.000001 32 | vn 0.000000 1.000000 -0.000000 33 | vn -0.000000 -1.000000 0.000000 34 | s off 35 | f 5/1/1 1/2/1 4/3/1 36 | f 5/1/1 4/3/1 8/4/1 37 | f 3/5/2 7/6/2 8/7/2 38 | f 3/5/2 8/7/2 4/8/2 39 | f 2/9/3 6/10/3 3/5/3 40 | f 6/10/4 7/6/4 3/5/4 41 | f 1/2/5 5/1/5 2/9/5 42 | f 5/1/6 6/10/6 2/9/6 43 | f 5/1/7 8/11/7 6/10/7 44 | f 8/11/7 7/12/7 6/10/7 45 | f 1/2/8 2/9/8 3/13/8 46 | f 1/2/8 3/13/8 4/14/8 -------------------------------------------------------------------------------- /src/Assignment3/Triangle.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by LEI XU on 4/11/19. 3 | // 4 | 5 | #ifndef RASTERIZER_TRIANGLE_H 6 | #define RASTERIZER_TRIANGLE_H 7 | 8 | #include 9 | #include "Texture.hpp" 10 | 11 | using namespace Eigen; 12 | class Triangle{ 13 | 14 | public: 15 | Vector4f v[3]; /*the original coordinates of the triangle, v0, v1, v2 in counter clockwise order*/ 16 | /*Per vertex values*/ 17 | Vector3f color[3]; //color at each vertex; 18 | Vector2f tex_coords[3]; //texture u,v 19 | Vector3f normal[3]; //normal vector for each vertex 20 | 21 | Texture *tex= nullptr; 22 | Triangle(); 23 | 24 | Eigen::Vector4f a() const { return v[0]; } 25 | Eigen::Vector4f b() const { return v[1]; } 26 | Eigen::Vector4f c() const { return v[2]; } 27 | 28 | void setVertex(int ind, Vector4f ver); /*set i-th vertex coordinates */ 29 | void setNormal(int ind, Vector3f n); /*set i-th vertex normal vector*/ 30 | void setColor(int ind, float r, float g, float b); /*set i-th vertex color*/ 31 | 32 | void setNormals(const std::array& normals); 33 | void setColors(const std::array& colors); 34 | void setTexCoord(int ind,Vector2f uv ); /*set i-th vertex texture coordinate*/ 35 | std::array toVector4() const; 36 | }; 37 | 38 | 39 | 40 | 41 | 42 | 43 | #endif //RASTERIZER_TRIANGLE_H 44 | -------------------------------------------------------------------------------- /src/Assignment6/main.cpp: -------------------------------------------------------------------------------- 1 | #include "Renderer.hpp" 2 | #include "Scene.hpp" 3 | #include "Triangle.hpp" 4 | #include "Vector.hpp" 5 | #include "global.hpp" 6 | #include 7 | 8 | // In the main function of the program, we create the scene (create objects and 9 | // lights) as well as set the options for the render (image width and height, 10 | // maximum recursion depth, field-of-view, etc.). We then call the render 11 | // function(). 12 | int main(int argc, char** argv) 13 | { 14 | Scene scene(1280, 960); 15 | 16 | MeshTriangle bunny("../../models/bunny/bunny.obj"); 17 | 18 | scene.Add(&bunny); 19 | scene.Add(std::make_unique(Vector3f(-20, 70, 20), 1)); 20 | scene.Add(std::make_unique(Vector3f(20, 70, 20), 1)); 21 | scene.buildBVH(); 22 | 23 | Renderer r; 24 | 25 | auto start = std::chrono::system_clock::now(); 26 | r.Render(scene); 27 | auto stop = std::chrono::system_clock::now(); 28 | 29 | std::cout << "Render complete: \n"; 30 | std::cout << "Time taken: " << std::chrono::duration_cast(stop - start).count() << " hours\n"; 31 | std::cout << " : " << std::chrono::duration_cast(stop - start).count() << " minutes\n"; 32 | std::cout << " : " << std::chrono::duration_cast(stop - start).count() << " seconds\n"; 33 | 34 | return 0; 35 | } -------------------------------------------------------------------------------- /src/Assignment2/Triangle.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by LEI XU on 4/11/19. 3 | // 4 | 5 | #include "Triangle.hpp" 6 | #include 7 | #include 8 | 9 | 10 | Triangle::Triangle() { 11 | v[0] << 0,0,0; 12 | v[1] << 0,0,0; 13 | v[2] << 0,0,0; 14 | 15 | color[0] << 0.0, 0.0, 0.0; 16 | color[1] << 0.0, 0.0, 0.0; 17 | color[2] << 0.0, 0.0, 0.0; 18 | 19 | tex_coords[0] << 0.0, 0.0; 20 | tex_coords[1] << 0.0, 0.0; 21 | tex_coords[2] << 0.0, 0.0; 22 | } 23 | 24 | void Triangle::setVertex(int ind, Vector3f ver){ 25 | v[ind] = ver; 26 | } 27 | void Triangle::setNormal(int ind, Vector3f n){ 28 | normal[ind] = n; 29 | } 30 | void Triangle::setColor(int ind, float r, float g, float b) { 31 | if((r<0.0) || (r>255.) || 32 | (g<0.0) || (g>255.) || 33 | (b<0.0) || (b>255.)) { 34 | fprintf(stderr, "ERROR! Invalid color values"); 35 | fflush(stderr); 36 | exit(-1); 37 | } 38 | 39 | color[ind] = Vector3f((float)r/255.,(float)g/255.,(float)b/255.); 40 | return; 41 | } 42 | void Triangle::setTexCoord(int ind, float s, float t) { 43 | tex_coords[ind] = Vector2f(s,t); 44 | } 45 | 46 | std::array Triangle::toVector4() const 47 | { 48 | std::array res; 49 | std::transform(std::begin(v), std::end(v), res.begin(), [](auto& vec) { return Eigen::Vector4f(vec.x(), vec.y(), vec.z(), 1.f); }); 50 | return res; 51 | } 52 | -------------------------------------------------------------------------------- /src/Assignment1/Triangle.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by LEI XU on 4/11/19. 3 | // 4 | 5 | #include "Triangle.hpp" 6 | #include 7 | #include 8 | #include 9 | 10 | Triangle::Triangle() 11 | { 12 | v[0] << 0, 0, 0; 13 | v[1] << 0, 0, 0; 14 | v[2] << 0, 0, 0; 15 | 16 | color[0] << 0.0, 0.0, 0.0; 17 | color[1] << 0.0, 0.0, 0.0; 18 | color[2] << 0.0, 0.0, 0.0; 19 | 20 | tex_coords[0] << 0.0, 0.0; 21 | tex_coords[1] << 0.0, 0.0; 22 | tex_coords[2] << 0.0, 0.0; 23 | } 24 | 25 | void Triangle::setVertex(int ind, Eigen::Vector3f ver) { v[ind] = ver; } 26 | 27 | void Triangle::setNormal(int ind, Vector3f n) { normal[ind] = n; } 28 | 29 | void Triangle::setColor(int ind, float r, float g, float b) 30 | { 31 | if ((r < 0.0) || (r > 255.) || (g < 0.0) || (g > 255.) || (b < 0.0) || 32 | (b > 255.)) { 33 | throw std::runtime_error("Invalid color values"); 34 | } 35 | 36 | color[ind] = Vector3f((float)r / 255., (float)g / 255., (float)b / 255.); 37 | return; 38 | } 39 | void Triangle::setTexCoord(int ind, float s, float t) 40 | { 41 | tex_coords[ind] = Vector2f(s, t); 42 | } 43 | 44 | std::array Triangle::toVector4() const 45 | { 46 | std::array res; 47 | std::transform(std::begin(v), std::end(v), res.begin(), [](auto& vec) { 48 | return Vector4f(vec.x(), vec.y(), vec.z(), 1.f); 49 | }); 50 | return res; 51 | } 52 | -------------------------------------------------------------------------------- /src/Assignment5/main.cpp: -------------------------------------------------------------------------------- 1 | #include "Scene.hpp" 2 | #include "Sphere.hpp" 3 | #include "Triangle.hpp" 4 | #include "Light.hpp" 5 | #include "Renderer.hpp" 6 | 7 | // In the main function of the program, we create the scene (create objects and lights) 8 | // as well as set the options for the render (image width and height, maximum recursion 9 | // depth, field-of-view, etc.). We then call the render function(). 10 | int main() 11 | { 12 | Scene scene(1280, 960); 13 | 14 | auto sph1 = std::make_unique(Vector3f(-1, 0, -12), 2); 15 | sph1->materialType = DIFFUSE_AND_GLOSSY; 16 | sph1->diffuseColor = Vector3f(0.6, 0.7, 0.8); 17 | 18 | auto sph2 = std::make_unique(Vector3f(0.5, -0.5, -8), 1.5); 19 | sph2->ior = 1.5; 20 | sph2->materialType = REFLECTION_AND_REFRACTION; 21 | 22 | scene.Add(std::move(sph1)); 23 | scene.Add(std::move(sph2)); 24 | 25 | Vector3f verts[4] = {{-5,-3,-6}, {5,-3,-6}, {5,-3,-16}, {-5,-3,-16}}; 26 | uint32_t vertIndex[6] = {0, 1, 3, 1, 2, 3}; 27 | Vector2f st[4] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}}; 28 | auto mesh = std::make_unique(verts, vertIndex, 2, st); 29 | mesh->materialType = DIFFUSE_AND_GLOSSY; 30 | 31 | scene.Add(std::move(mesh)); 32 | scene.Add(std::make_unique(Vector3f(-20, 70, 20), 0.5)); 33 | scene.Add(std::make_unique(Vector3f(30, 50, -12), 0.5)); 34 | 35 | Renderer r; 36 | r.Render(scene); 37 | 38 | return 0; 39 | } -------------------------------------------------------------------------------- /src/Assignment8/src/application.h: -------------------------------------------------------------------------------- 1 | #ifndef CGL_APPLICATION_H 2 | #define CGL_APPLICATION_H 3 | 4 | // STL 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | // libCGL 12 | #include "CGL/CGL.h" 13 | #include "CGL/osdtext.h" 14 | #include "CGL/renderer.h" 15 | 16 | #include "rope.h" 17 | 18 | using namespace std; 19 | 20 | namespace CGL { 21 | 22 | struct AppConfig { 23 | AppConfig() { 24 | // Rope config variables 25 | mass = 1; 26 | ks = 100; 27 | 28 | // Environment variables 29 | gravity = Vector2D(0, -1); 30 | steps_per_frame = 64; 31 | } 32 | 33 | float mass; 34 | float ks; 35 | 36 | float steps_per_frame; 37 | Vector2D gravity; 38 | }; 39 | 40 | class Application : public Renderer { 41 | public: 42 | Application(AppConfig config); 43 | ~Application(); 44 | 45 | void init(); 46 | void render(); 47 | void resize(size_t w, size_t h); 48 | 49 | std::string name(); 50 | std::string info(); 51 | 52 | void keyboard_event(int key, int event, unsigned char mods); 53 | // void cursor_event(float x, float y); 54 | // void scroll_event(float offset_x, float offset_y); 55 | // void mouse_event(int key, int event, unsigned char mods); 56 | 57 | private: 58 | AppConfig config; 59 | 60 | Rope *ropeEuler; 61 | Rope *ropeVerlet; 62 | 63 | size_t screen_width; 64 | size_t screen_height; 65 | 66 | }; // class Application 67 | 68 | } // namespace CGL 69 | 70 | #endif // CGL_APPLICATION_H 71 | -------------------------------------------------------------------------------- /src/Assignment6/global.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #include 4 | #include 5 | 6 | #undef M_PI 7 | #define M_PI 3.141592653589793f 8 | 9 | extern const float EPSILON; 10 | const float kInfinity = std::numeric_limits::max(); 11 | 12 | inline float clamp(const float &lo, const float &hi, const float &v) 13 | { return std::max(lo, std::min(hi, v)); } 14 | 15 | inline bool solveQuadratic(const float &a, const float &b, const float &c, float &x0, float &x1) 16 | { 17 | float discr = b * b - 4 * a * c; 18 | if (discr < 0) return false; 19 | else if (discr == 0) x0 = x1 = - 0.5 * b / a; 20 | else { 21 | float q = (b > 0) ? 22 | -0.5 * (b + sqrt(discr)) : 23 | -0.5 * (b - sqrt(discr)); 24 | x0 = q / a; 25 | x1 = c / q; 26 | } 27 | if (x0 > x1) std::swap(x0, x1); 28 | return true; 29 | } 30 | 31 | inline float get_random_float() 32 | { 33 | std::random_device dev; 34 | std::mt19937 rng(dev()); 35 | std::uniform_real_distribution dist(0.f, 1.f); // distribution in range [1, 6] 36 | 37 | return dist(rng); 38 | } 39 | 40 | inline void UpdateProgress(float progress) 41 | { 42 | int barWidth = 70; 43 | 44 | std::cout << "["; 45 | int pos = barWidth * progress; 46 | for (int i = 0; i < barWidth; ++i) { 47 | if (i < pos) std::cout << "="; 48 | else if (i == pos) std::cout << ">"; 49 | else std::cout << " "; 50 | } 51 | std::cout << "] " << int(progress * 100.0) << " %\r"; 52 | std::cout.flush(); 53 | }; 54 | -------------------------------------------------------------------------------- /src/Assignment7/global.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #include 4 | #include 5 | 6 | #undef M_PI 7 | #define M_PI 3.141592653589793f 8 | 9 | extern const float EPSILON; 10 | const float kInfinity = std::numeric_limits::max(); 11 | 12 | inline float clamp(const float &lo, const float &hi, const float &v) 13 | { return std::max(lo, std::min(hi, v)); } 14 | 15 | inline bool solveQuadratic(const float &a, const float &b, const float &c, float &x0, float &x1) 16 | { 17 | float discr = b * b - 4 * a * c; 18 | if (discr < 0) return false; 19 | else if (discr == 0) x0 = x1 = - 0.5 * b / a; 20 | else { 21 | float q = (b > 0) ? 22 | -0.5 * (b + sqrt(discr)) : 23 | -0.5 * (b - sqrt(discr)); 24 | x0 = q / a; 25 | x1 = c / q; 26 | } 27 | if (x0 > x1) std::swap(x0, x1); 28 | return true; 29 | } 30 | 31 | inline float get_random_float() 32 | { 33 | std::random_device dev; 34 | std::mt19937 rng(dev()); 35 | std::uniform_real_distribution dist(0.f, 1.f); // distribution in range [1, 6] 36 | 37 | return dist(rng); 38 | } 39 | 40 | inline void UpdateProgress(float progress) 41 | { 42 | int barWidth = 70; 43 | 44 | std::cout << "["; 45 | int pos = barWidth * progress; 46 | for (int i = 0; i < barWidth; ++i) { 47 | if (i < pos) std::cout << "="; 48 | else if (i == pos) std::cout << ">"; 49 | else std::cout << " "; 50 | } 51 | std::cout << "] " << int(progress * 100.0) << " %\r"; 52 | std::cout.flush(); 53 | }; 54 | -------------------------------------------------------------------------------- /src/Assignment0/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | int main(){ 7 | 8 | // Basic Example of cpp 9 | std::cout << "Example of cpp \n"; 10 | float a = 1.0, b = 2.0; 11 | std::cout << a << std::endl; 12 | std::cout << a/b << std::endl; 13 | std::cout << std::sqrt(b) << std::endl; 14 | std::cout << std::acos(-1) << std::endl; 15 | std::cout << std::sin(30.0/180.0*acos(-1)) << std::endl; 16 | 17 | // Example of vector 18 | std::cout << "Example of vector \n"; 19 | // vector definition 20 | Eigen::Vector3f v(1.0f,2.0f,3.0f); 21 | Eigen::Vector3f w(1.0f,0.0f,0.0f); 22 | // vector output 23 | std::cout << "Example of output \n"; 24 | std::cout << v << std::endl; 25 | // vector add 26 | std::cout << "Example of add \n"; 27 | std::cout << v + w << std::endl; 28 | // vector scalar multiply 29 | std::cout << "Example of scalar multiply \n"; 30 | std::cout << v * 3.0f << std::endl; 31 | std::cout << 2.0f * v << std::endl; 32 | 33 | // Example of matrix 34 | std::cout << "Example of matrix \n"; 35 | // matrix definition 36 | Eigen::Matrix3f i,j; 37 | i << 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0; 38 | j << 2.0, 3.0, 1.0, 4.0, 6.0, 5.0, 9.0, 7.0, 8.0; 39 | // matrix output 40 | std::cout << "Example of output \n"; 41 | std::cout << i << std::endl; 42 | // matrix add i + j 43 | // matrix scalar multiply i * 2.0 44 | // matrix multiply i * j 45 | // matrix multiply vector i * v 46 | 47 | return 0; 48 | } -------------------------------------------------------------------------------- /src/Assignment8/src/main.cpp: -------------------------------------------------------------------------------- 1 | #include "CGL/CGL.h" 2 | #include "CGL/viewer.h" 3 | 4 | #include "application.h" 5 | typedef uint32_t gid_t; 6 | 7 | #include 8 | 9 | #ifdef _WIN32 10 | // from https://github.com/Chunde/getopt-for-windows 11 | #define __GNU_LIBRARY__ 12 | #include "getopt.h" 13 | #else 14 | #include 15 | #endif 16 | 17 | using namespace std; 18 | using namespace CGL; 19 | 20 | void usage(const char *binaryName) { 21 | printf("Usage: %s [options] \n", binaryName); 22 | printf("Program Options:\n"); 23 | printf(" -m Mass per node\n"); 24 | printf(" -g Gravity vector (x, y)\n"); 25 | printf(" -s Number of steps per simulation frame\n"); 26 | printf("\n"); 27 | } 28 | 29 | int main(int argc, char **argv) { 30 | AppConfig config; 31 | int opt; 32 | 33 | while ((opt = getopt(argc, argv, "s:l:t:m:e:h:f:r:c:a:p:")) != -1) { 34 | switch (opt) { 35 | case 'm': 36 | config.mass = atof(optarg); 37 | break; 38 | case 'g': 39 | config.gravity = Vector2D(atof(argv[optind - 1]), atof(argv[optind])); 40 | optind++; 41 | break; 42 | case 's': 43 | config.steps_per_frame = atoi(optarg); 44 | break; 45 | default: 46 | usage(argv[0]); 47 | return 1; 48 | } 49 | } 50 | 51 | // create application 52 | Application *app = new Application(config); 53 | 54 | // create viewer 55 | Viewer viewer = Viewer(); 56 | 57 | // set renderer 58 | viewer.set_renderer(app); 59 | 60 | // init viewer 61 | viewer.init(); 62 | 63 | // start viewer 64 | viewer.start(); 65 | 66 | return 0; 67 | } 68 | -------------------------------------------------------------------------------- /src/Assignment5/global.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | #include 6 | 7 | #define M_PI 3.14159265358979323846 8 | 9 | constexpr float kInfinity = std::numeric_limits::max(); 10 | 11 | inline float clamp(const float& lo, const float& hi, const float& v) 12 | { 13 | return std::max(lo, std::min(hi, v)); 14 | } 15 | 16 | inline bool solveQuadratic(const float& a, const float& b, const float& c, float& x0, float& x1) 17 | { 18 | float discr = b * b - 4 * a * c; 19 | if (discr < 0) 20 | return false; 21 | else if (discr == 0) 22 | x0 = x1 = -0.5 * b / a; 23 | else 24 | { 25 | float q = (b > 0) ? -0.5 * (b + sqrt(discr)) : -0.5 * (b - sqrt(discr)); 26 | x0 = q / a; 27 | x1 = c / q; 28 | } 29 | if (x0 > x1) 30 | std::swap(x0, x1); 31 | return true; 32 | } 33 | 34 | enum MaterialType 35 | { 36 | DIFFUSE_AND_GLOSSY, 37 | REFLECTION_AND_REFRACTION, 38 | REFLECTION 39 | }; 40 | 41 | inline float get_random_float() 42 | { 43 | std::random_device dev; 44 | std::mt19937 rng(dev()); 45 | std::uniform_real_distribution dist(0.f, 1.f); // distribution in range [1, 6] 46 | 47 | return dist(rng); 48 | } 49 | 50 | inline void UpdateProgress(float progress) 51 | { 52 | int barWidth = 70; 53 | 54 | std::cout << "["; 55 | int pos = barWidth * progress; 56 | for (int i = 0; i < barWidth; ++i) 57 | { 58 | if (i < pos) 59 | std::cout << "="; 60 | else if (i == pos) 61 | std::cout << ">"; 62 | else 63 | std::cout << " "; 64 | } 65 | std::cout << "] " << int(progress * 100.0) << " %\r"; 66 | std::cout.flush(); 67 | } 68 | -------------------------------------------------------------------------------- /src/Assignment6/BVH.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by LEI XU on 5/16/19. 3 | // 4 | 5 | #ifndef RAYTRACING_BVH_H 6 | #define RAYTRACING_BVH_H 7 | 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include "Object.hpp" 13 | #include "Ray.hpp" 14 | #include "Bounds3.hpp" 15 | #include "Intersection.hpp" 16 | #include "Vector.hpp" 17 | 18 | struct BVHBuildNode; 19 | // BVHAccel Forward Declarations 20 | struct BVHPrimitiveInfo; 21 | 22 | // BVHAccel Declarations 23 | inline int leafNodes, totalLeafNodes, totalPrimitives, interiorNodes; 24 | class BVHAccel { 25 | 26 | public: 27 | // BVHAccel Public Types 28 | enum class SplitMethod { NAIVE, SAH }; 29 | 30 | // BVHAccel Public Methods 31 | BVHAccel(std::vector p, int maxPrimsInNode = 1, SplitMethod splitMethod = SplitMethod::NAIVE); 32 | Bounds3 WorldBound() const; 33 | ~BVHAccel(); 34 | 35 | Intersection Intersect(const Ray &ray) const; 36 | Intersection getIntersection(BVHBuildNode* node, const Ray& ray)const; 37 | bool IntersectP(const Ray &ray) const; 38 | BVHBuildNode* root; 39 | 40 | // BVHAccel Private Methods 41 | BVHBuildNode* recursiveBuild(std::vectorobjects); 42 | 43 | // BVHAccel Private Data 44 | const int maxPrimsInNode; 45 | const SplitMethod splitMethod; 46 | std::vector primitives; 47 | }; 48 | 49 | struct BVHBuildNode { 50 | Bounds3 bounds; 51 | BVHBuildNode *left; 52 | BVHBuildNode *right; 53 | Object* object; 54 | 55 | public: 56 | int splitAxis=0, firstPrimOffset=0, nPrimitives=0; 57 | // BVHBuildNode Public Methods 58 | BVHBuildNode(){ 59 | bounds = Bounds3(); 60 | left = nullptr;right = nullptr; 61 | object = nullptr; 62 | } 63 | }; 64 | 65 | 66 | 67 | 68 | #endif //RAYTRACING_BVH_H 69 | -------------------------------------------------------------------------------- /src/Assignment7/Scene.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by Göksu Güvendiren on 2019-05-14. 3 | // 4 | 5 | #include "Scene.hpp" 6 | 7 | 8 | void Scene::buildBVH() { 9 | printf(" - Generating BVH...\n\n"); 10 | this->bvh = new BVHAccel(objects, 1, BVHAccel::SplitMethod::NAIVE); 11 | } 12 | 13 | Intersection Scene::intersect(const Ray &ray) const 14 | { 15 | return this->bvh->Intersect(ray); 16 | } 17 | 18 | void Scene::sampleLight(Intersection &pos, float &pdf) const 19 | { 20 | float emit_area_sum = 0; 21 | for (uint32_t k = 0; k < objects.size(); ++k) { 22 | if (objects[k]->hasEmit()){ 23 | emit_area_sum += objects[k]->getArea(); 24 | } 25 | } 26 | float p = get_random_float() * emit_area_sum; 27 | emit_area_sum = 0; 28 | for (uint32_t k = 0; k < objects.size(); ++k) { 29 | if (objects[k]->hasEmit()){ 30 | emit_area_sum += objects[k]->getArea(); 31 | if (p <= emit_area_sum){ 32 | objects[k]->Sample(pos, pdf); 33 | break; 34 | } 35 | } 36 | } 37 | } 38 | 39 | bool Scene::trace( 40 | const Ray &ray, 41 | const std::vector &objects, 42 | float &tNear, uint32_t &index, Object **hitObject) 43 | { 44 | *hitObject = nullptr; 45 | for (uint32_t k = 0; k < objects.size(); ++k) { 46 | float tNearK = kInfinity; 47 | uint32_t indexK; 48 | Vector2f uvK; 49 | if (objects[k]->intersect(ray, tNearK, indexK) && tNearK < tNear) { 50 | *hitObject = objects[k]; 51 | tNear = tNearK; 52 | index = indexK; 53 | } 54 | } 55 | 56 | 57 | return (*hitObject != nullptr); 58 | } 59 | 60 | // Implementation of Path Tracing 61 | Vector3f Scene::castRay(const Ray &ray, int depth) const 62 | { 63 | // TO DO Implement Path Tracing Algorithm here 64 | return {}; 65 | } -------------------------------------------------------------------------------- /src/Assignment3/Triangle.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by LEI XU on 4/11/19. 3 | // 4 | 5 | #include "Triangle.hpp" 6 | #include 7 | #include 8 | 9 | Triangle::Triangle() { 10 | v[0] << 0,0,0,1; 11 | v[1] << 0,0,0,1; 12 | v[2] << 0,0,0,1; 13 | 14 | color[0] << 0.0, 0.0, 0.0; 15 | color[1] << 0.0, 0.0, 0.0; 16 | color[2] << 0.0, 0.0, 0.0; 17 | 18 | tex_coords[0] << 0.0, 0.0; 19 | tex_coords[1] << 0.0, 0.0; 20 | tex_coords[2] << 0.0, 0.0; 21 | } 22 | 23 | void Triangle::setVertex(int ind, Vector4f ver){ 24 | v[ind] = ver; 25 | } 26 | void Triangle::setNormal(int ind, Vector3f n){ 27 | normal[ind] = n; 28 | } 29 | void Triangle::setColor(int ind, float r, float g, float b) { 30 | if((r<0.0) || (r>255.) || 31 | (g<0.0) || (g>255.) || 32 | (b<0.0) || (b>255.)) { 33 | fprintf(stderr, "ERROR! Invalid color values"); 34 | fflush(stderr); 35 | exit(-1); 36 | } 37 | 38 | color[ind] = Vector3f((float)r/255.,(float)g/255.,(float)b/255.); 39 | return; 40 | } 41 | void Triangle::setTexCoord(int ind, Vector2f uv) { 42 | tex_coords[ind] = uv; 43 | } 44 | 45 | std::array Triangle::toVector4() const 46 | { 47 | std::array res; 48 | std::transform(std::begin(v), std::end(v), res.begin(), [](auto& vec) { return Vector4f(vec.x(), vec.y(), vec.z(), 1.f); }); 49 | return res; 50 | } 51 | 52 | void Triangle::setNormals(const std::array& normals) 53 | { 54 | normal[0] = normals[0]; 55 | normal[1] = normals[1]; 56 | normal[2] = normals[2]; 57 | } 58 | 59 | void Triangle::setColors(const std::array& colors) 60 | { 61 | auto first_color = colors[0]; 62 | setColor(0, colors[0][0], colors[0][1], colors[0][2]); 63 | setColor(1, colors[1][0], colors[1][1], colors[1][2]); 64 | setColor(2, colors[2][0], colors[2][1], colors[2][2]); 65 | } -------------------------------------------------------------------------------- /src/Assignment8/src/rope.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include "CGL/vector2D.h" 5 | 6 | #include "mass.h" 7 | #include "rope.h" 8 | #include "spring.h" 9 | 10 | namespace CGL { 11 | 12 | Rope::Rope(Vector2D start, Vector2D end, int num_nodes, float node_mass, float k, vector pinned_nodes) 13 | { 14 | // TODO (Part 1): Create a rope starting at `start`, ending at `end`, and containing `num_nodes` nodes. 15 | 16 | // Comment-in this part when you implement the constructor 17 | // for (auto &i : pinned_nodes) { 18 | // masses[i]->pinned = true; 19 | // } 20 | } 21 | 22 | void Rope::simulateEuler(float delta_t, Vector2D gravity) 23 | { 24 | for (auto &s : springs) 25 | { 26 | // TODO (Part 2): Use Hooke's law to calculate the force on a node 27 | } 28 | 29 | for (auto &m : masses) 30 | { 31 | if (!m->pinned) 32 | { 33 | // TODO (Part 2): Add the force due to gravity, then compute the new velocity and position 34 | 35 | // TODO (Part 2): Add global damping 36 | } 37 | 38 | // Reset all forces on each mass 39 | m->forces = Vector2D(0, 0); 40 | } 41 | } 42 | 43 | void Rope::simulateVerlet(float delta_t, Vector2D gravity) 44 | { 45 | for (auto &s : springs) 46 | { 47 | // TODO (Part 3): Simulate one timestep of the rope using explicit Verlet (solving constraints) 48 | } 49 | 50 | for (auto &m : masses) 51 | { 52 | if (!m->pinned) 53 | { 54 | Vector2D temp_position = m->position; 55 | // TODO (Part 3.1): Set the new position of the rope mass 56 | 57 | // TODO (Part 4): Add global Verlet damping 58 | } 59 | } 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /src/Assignment8/CGL/src/color.cpp: -------------------------------------------------------------------------------- 1 | #include "color.h" 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | using namespace std; 9 | 10 | namespace CGL { 11 | 12 | // Constants 13 | const Color Color::White = Color(1,1,1,1); 14 | const Color Color::Black = Color(0,0,0,1); 15 | 16 | Color::Color( const unsigned char* arr ) { 17 | float inv = 1.0 / 255.0; 18 | r = arr[0] * inv; 19 | g = arr[1] * inv; 20 | b = arr[2] * inv; 21 | a = 1.0; 22 | } 23 | 24 | Color Color::fromHex( const char* s ) { 25 | // If the color is "none", return any color 26 | // with alpha zero (completely transparent). 27 | if( !strcmp(s, "none") ) { 28 | return Color(0,0,0,0); 29 | } 30 | 31 | // Ignore leading hashmark. 32 | if( s[0] == '#' ) { 33 | s++; 34 | } 35 | 36 | // Set stream formatting to hexadecimal. 37 | stringstream ss; 38 | ss << hex; 39 | 40 | // Convert to integer. 41 | unsigned int rgb; 42 | ss << s; 43 | ss >> rgb; 44 | 45 | // Extract 8-byte chunks and normalize. 46 | Color c; 47 | c.r = (float)( ( rgb & 0xFF0000 ) >> 16 ) / 255.0; 48 | c.g = (float)( ( rgb & 0x00FF00 ) >> 8 ) / 255.0; 49 | c.b = (float)( ( rgb & 0x0000FF ) >> 0 ) / 255.0; 50 | c.a = 1.0; // set alpha to 1 (opaque) by default 51 | 52 | return c; 53 | } 54 | 55 | string Color::toHex( void ) const { 56 | int R = (unsigned char) max( 0., min( 255.0, 255.0 * r )); 57 | int G = (unsigned char) max( 0., min( 255.0, 255.0 * g )); 58 | int B = (unsigned char) max( 0., min( 255.0, 255.0 * b )); 59 | 60 | stringstream ss; 61 | ss << hex; 62 | 63 | ss << R << G << B; 64 | return ss.str(); 65 | } 66 | 67 | std::ostream& operator<<( std::ostream& os, const Color& c ) { 68 | os << "( r=" << c.r; 69 | os << ", g=" << c.g; 70 | os << ", b=" << c.b; 71 | os << ", a=" << c.a; 72 | os << " )" << endl; 73 | return os; 74 | } 75 | 76 | } // namespace CGL 77 | -------------------------------------------------------------------------------- /src/Assignment7/BVH.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by LEI XU on 5/16/19. 3 | // 4 | 5 | #ifndef RAYTRACING_BVH_H 6 | #define RAYTRACING_BVH_H 7 | 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include "Object.hpp" 13 | #include "Ray.hpp" 14 | #include "Bounds3.hpp" 15 | #include "Intersection.hpp" 16 | #include "Vector.hpp" 17 | 18 | struct BVHBuildNode; 19 | // BVHAccel Forward Declarations 20 | struct BVHPrimitiveInfo; 21 | 22 | // BVHAccel Declarations 23 | inline int leafNodes, totalLeafNodes, totalPrimitives, interiorNodes; 24 | class BVHAccel { 25 | 26 | public: 27 | // BVHAccel Public Types 28 | enum class SplitMethod { NAIVE, SAH }; 29 | 30 | // BVHAccel Public Methods 31 | BVHAccel(std::vector p, int maxPrimsInNode = 1, SplitMethod splitMethod = SplitMethod::NAIVE); 32 | Bounds3 WorldBound() const; 33 | ~BVHAccel(); 34 | 35 | Intersection Intersect(const Ray &ray) const; 36 | Intersection getIntersection(BVHBuildNode* node, const Ray& ray)const; 37 | bool IntersectP(const Ray &ray) const; 38 | BVHBuildNode* root; 39 | 40 | // BVHAccel Private Methods 41 | BVHBuildNode* recursiveBuild(std::vectorobjects); 42 | 43 | // BVHAccel Private Data 44 | const int maxPrimsInNode; 45 | const SplitMethod splitMethod; 46 | std::vector primitives; 47 | 48 | void getSample(BVHBuildNode* node, float p, Intersection &pos, float &pdf); 49 | void Sample(Intersection &pos, float &pdf); 50 | }; 51 | 52 | struct BVHBuildNode { 53 | Bounds3 bounds; 54 | BVHBuildNode *left; 55 | BVHBuildNode *right; 56 | Object* object; 57 | float area; 58 | 59 | public: 60 | int splitAxis=0, firstPrimOffset=0, nPrimitives=0; 61 | // BVHBuildNode Public Methods 62 | BVHBuildNode(){ 63 | bounds = Bounds3(); 64 | left = nullptr;right = nullptr; 65 | object = nullptr; 66 | } 67 | }; 68 | 69 | 70 | 71 | 72 | #endif //RAYTRACING_BVH_H 73 | -------------------------------------------------------------------------------- /src/Assignment8/CGL/include/CGL/misc.h: -------------------------------------------------------------------------------- 1 | #ifndef CGL_MISC_H 2 | #define CGL_MISC_H 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | namespace CGL { 9 | 10 | #define PI (3.14159265358979323) 11 | #define EPS_D (0.00000000001) 12 | #define EPS_F (0.00001f) 13 | #define INF_D (std::numeric_limits::infinity()) 14 | #define INF_F (std::numeric_limits::infinity()) 15 | 16 | // MOUSE INPUTS // 17 | #define MOUSE_LEFT 0 18 | #define MOUSE_RIGHT 1 19 | #define MOUSE_MIDDLE 2 20 | 21 | // KEYBOARD INPUTS // 22 | #define KEYBOARD_ENTER 257 23 | #define KEYBOARD_TAB 258 24 | #define KEYBOARD_BACKSPACE 259 25 | #define KEYBOARD_INSERT 260 26 | #define KEYBOARD_DELETE 261 27 | #define KEYBOARD_RIGHT 262 28 | #define KEYBOARD_LEFT 263 29 | #define KEYBOARD_DOWN 264 30 | #define KEYBOARD_UP 265 31 | #define KEYBOARD_PAGE_UP 266 32 | #define KEYBOARD_PAGE_DOWN 267 33 | #define KEYBOARD_PRINT_SCREEN 283 34 | 35 | // EVENT TYPES // 36 | #define EVENT_RELEASE 0 37 | #define EVENT_PRESS 1 38 | #define EVENT_REPEAT 2 39 | 40 | // MODIFIERS // 41 | #define MOD_SHIFT 0x0001 42 | #define MOD_CTRL 0x0002 43 | #define MOD_ALT 0x0004 44 | #define MOD_SUPER 0x0008 45 | 46 | /* 47 | Takes any kind of number and converts from degrees to radians. 48 | */ 49 | template 50 | inline T radians(T deg) { 51 | return deg * (PI / 180); 52 | } 53 | 54 | /* 55 | Takes any kind of number and converts from radians to degrees. 56 | */ 57 | template 58 | inline T degrees(T rad) { 59 | return rad * (180 / PI); 60 | } 61 | 62 | /* 63 | Takes any kind of number, as well as a lower and upper bound, and clamps the 64 | number to be within the bound. 65 | NOTE: x, lo, and hi must all be the same type or compilation will fail. A 66 | common mistake is to pass an int for x and size_ts for lo and hi. 67 | */ 68 | template 69 | inline T clamp(T x, T lo, T hi) { 70 | return std::min(std::max(x, lo), hi); 71 | } 72 | 73 | } // namespace CGL 74 | 75 | #endif // CGL_MISCMATH_H 76 | -------------------------------------------------------------------------------- /src/Assignment6/Renderer.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by goksu on 2/25/20. 3 | // 4 | 5 | #include 6 | #include "Scene.hpp" 7 | #include "Renderer.hpp" 8 | 9 | 10 | inline float deg2rad(const float& deg) { return deg * M_PI / 180.0; } 11 | 12 | const float EPSILON = 0.00001; 13 | 14 | // The main render function. This where we iterate over all pixels in the image, 15 | // generate primary rays and cast these rays into the scene. The content of the 16 | // framebuffer is saved to a file. 17 | void Renderer::Render(const Scene& scene) 18 | { 19 | std::vector framebuffer(scene.width * scene.height); 20 | 21 | float scale = tan(deg2rad(scene.fov * 0.5)); 22 | float imageAspectRatio = scene.width / (float)scene.height; 23 | Vector3f eye_pos(-1, 5, 10); 24 | int m = 0; 25 | for (uint32_t j = 0; j < scene.height; ++j) { 26 | for (uint32_t i = 0; i < scene.width; ++i) { 27 | // generate primary ray direction 28 | float x = (2 * (i + 0.5) / (float)scene.width - 1) * 29 | imageAspectRatio * scale; 30 | float y = (1 - 2 * (j + 0.5) / (float)scene.height) * scale; 31 | // TODO: Find the x and y positions of the current pixel to get the 32 | // direction 33 | // vector that passes through it. 34 | // Also, don't forget to multiply both of them with the variable 35 | // *scale*, and x (horizontal) variable with the *imageAspectRatio* 36 | 37 | // Don't forget to normalize this direction! 38 | 39 | } 40 | UpdateProgress(j / (float)scene.height); 41 | } 42 | UpdateProgress(1.f); 43 | 44 | // save framebuffer to file 45 | FILE* fp = fopen("binary.ppm", "wb"); 46 | (void)fprintf(fp, "P6\n%d %d\n255\n", scene.width, scene.height); 47 | for (auto i = 0; i < scene.height * scene.width; ++i) { 48 | static unsigned char color[3]; 49 | color[0] = (unsigned char)(255 * clamp(0, 1, framebuffer[i].x)); 50 | color[1] = (unsigned char)(255 * clamp(0, 1, framebuffer[i].y)); 51 | color[2] = (unsigned char)(255 * clamp(0, 1, framebuffer[i].z)); 52 | fwrite(color, 1, 3, fp); 53 | } 54 | fclose(fp); 55 | } 56 | -------------------------------------------------------------------------------- /src/Assignment7/Renderer.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by goksu on 2/25/20. 3 | // 4 | 5 | #include 6 | #include "Scene.hpp" 7 | #include "Renderer.hpp" 8 | 9 | 10 | inline float deg2rad(const float& deg) { return deg * M_PI / 180.0; } 11 | 12 | const float EPSILON = 0.00001; 13 | 14 | // The main render function. This where we iterate over all pixels in the image, 15 | // generate primary rays and cast these rays into the scene. The content of the 16 | // framebuffer is saved to a file. 17 | void Renderer::Render(const Scene& scene) 18 | { 19 | std::vector framebuffer(scene.width * scene.height); 20 | 21 | float scale = tan(deg2rad(scene.fov * 0.5)); 22 | float imageAspectRatio = scene.width / (float)scene.height; 23 | Vector3f eye_pos(278, 273, -800); 24 | int m = 0; 25 | 26 | // change the spp value to change sample ammount 27 | int spp = 16; 28 | std::cout << "SPP: " << spp << "\n"; 29 | for (uint32_t j = 0; j < scene.height; ++j) { 30 | for (uint32_t i = 0; i < scene.width; ++i) { 31 | // generate primary ray direction 32 | float x = (2 * (i + 0.5) / (float)scene.width - 1) * 33 | imageAspectRatio * scale; 34 | float y = (1 - 2 * (j + 0.5) / (float)scene.height) * scale; 35 | 36 | Vector3f dir = normalize(Vector3f(-x, y, 1)); 37 | for (int k = 0; k < spp; k++){ 38 | framebuffer[m] += scene.castRay(Ray(eye_pos, dir), 0) / spp; 39 | } 40 | m++; 41 | } 42 | UpdateProgress(j / (float)scene.height); 43 | } 44 | UpdateProgress(1.f); 45 | 46 | // save framebuffer to file 47 | FILE* fp = fopen("binary.ppm", "wb"); 48 | (void)fprintf(fp, "P6\n%d %d\n255\n", scene.width, scene.height); 49 | for (auto i = 0; i < scene.height * scene.width; ++i) { 50 | static unsigned char color[3]; 51 | color[0] = (unsigned char)(255 * std::pow(clamp(0, 1, framebuffer[i].x), 0.6f)); 52 | color[1] = (unsigned char)(255 * std::pow(clamp(0, 1, framebuffer[i].y), 0.6f)); 53 | color[2] = (unsigned char)(255 * std::pow(clamp(0, 1, framebuffer[i].z), 0.6f)); 54 | fwrite(color, 1, 3, fp); 55 | } 56 | fclose(fp); 57 | } 58 | -------------------------------------------------------------------------------- /src/Assignment1/rasterizer.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by goksu on 4/6/19. 3 | // 4 | 5 | #pragma once 6 | 7 | #include "Triangle.hpp" 8 | #include 9 | #include 10 | using namespace Eigen; 11 | 12 | namespace rst { 13 | enum class Buffers 14 | { 15 | Color = 1, 16 | Depth = 2 17 | }; 18 | 19 | inline Buffers operator|(Buffers a, Buffers b) 20 | { 21 | return Buffers((int)a | (int)b); 22 | } 23 | 24 | inline Buffers operator&(Buffers a, Buffers b) 25 | { 26 | return Buffers((int)a & (int)b); 27 | } 28 | 29 | enum class Primitive 30 | { 31 | Line, 32 | Triangle 33 | }; 34 | 35 | /* 36 | * For the curious : The draw function takes two buffer id's as its arguments. 37 | * These two structs make sure that if you mix up with their orders, the 38 | * compiler won't compile it. Aka : Type safety 39 | * */ 40 | struct pos_buf_id 41 | { 42 | int pos_id = 0; 43 | }; 44 | 45 | struct ind_buf_id 46 | { 47 | int ind_id = 0; 48 | }; 49 | 50 | class rasterizer 51 | { 52 | public: 53 | rasterizer(int w, int h); 54 | pos_buf_id load_positions(const std::vector& positions); 55 | ind_buf_id load_indices(const std::vector& indices); 56 | 57 | void set_model(const Eigen::Matrix4f& m); 58 | void set_view(const Eigen::Matrix4f& v); 59 | void set_projection(const Eigen::Matrix4f& p); 60 | 61 | void set_pixel(const Eigen::Vector3f& point, const Eigen::Vector3f& color); 62 | 63 | void clear(Buffers buff); 64 | 65 | void draw(pos_buf_id pos_buffer, ind_buf_id ind_buffer, Primitive type); 66 | 67 | std::vector& frame_buffer() { return frame_buf; } 68 | 69 | private: 70 | void draw_line(Eigen::Vector3f begin, Eigen::Vector3f end); 71 | void rasterize_wireframe(const Triangle& t); 72 | 73 | private: 74 | Eigen::Matrix4f model; 75 | Eigen::Matrix4f view; 76 | Eigen::Matrix4f projection; 77 | 78 | std::map> pos_buf; 79 | std::map> ind_buf; 80 | 81 | std::vector frame_buf; 82 | std::vector depth_buf; 83 | int get_index(int x, int y); 84 | 85 | int width, height; 86 | 87 | int next_id = 0; 88 | int get_next_id() { return next_id++; } 89 | }; 90 | } // namespace rst 91 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # GAMES101作业 xmake 版本 2 | 3 | 理论上跨平台,只测试过 Windows,Linux/MAC 未测试 4 | 5 | 如果第三方库没有预编译版本,Xmake 会手动拉取依赖源码并行编译 6 | 7 | # 快速开始 8 | 9 | 1. 安装 C++ 编译工具链,如 MSVC(visual studio) 10 | 2. 安装 C++ 的 构建系统 + 包管理: [Xmake](https://xmake.io/#/zh-cn/guide/installation) 11 | 3. 下载所有作业源码 12 | 13 | ```bash 14 | git clone https://github.com/star-hengxing/GAMES101-xmake.git 15 | ``` 16 | 17 | 4. 进入作业目录测试,比如 18 | 19 | ```bash 20 | cd src/Assignment1 21 | xmake -y 22 | xmake run 23 | ``` 24 | 25 | 如果下载包失败请看[FQA](#fqa) 26 | 27 | --- 28 | 29 | 假如你是 mac 用户,可能需要以下步骤 30 | 31 | 安装包管理[Homebrew](https://brew.sh/),然后安装: 32 | 33 | ```bash 34 | brew install gcc xmake eigen opencv 35 | ``` 36 | 37 | 安装成功后,回到上面第三步 38 | 39 | # 注意事项 40 | 41 | * 所有作业文档在 doc 目录 42 | * 所有模型在 models 目录 43 | * 每个作业**运行环境**都在自己作业目录下,有需要可以在 xmake.lua 里修改`set_rundir`的路径 44 | 45 | # IDE 支持 46 | 47 | ## vscode 48 | 49 | 安装插件`clangd`和`codelldb`. 50 | 51 | 在作业目录创建以下文件`.vscode/settings.json`,然后写入 52 | ```json 53 | { 54 | "clangd.arguments": [ 55 | "--compile-commands-dir=.vscode", 56 | ], 57 | 58 | "xmake.debugConfigType": "codelldb", 59 | } 60 | ``` 61 | 62 | ## visual studio 63 | 64 | ```bash 65 | xmake project -k vsxmake -m "debug,release" 66 | ``` 67 | 68 | 更多请查看 xmake 文档 -> https://xmake.io/#/zh-cn/plugin/more_plugins 69 | 70 | # 学习资料 71 | 72 | - [GAMES101 作业问题整理](https://zhuanlan.zhihu.com/p/375391720) 73 | - [《GAMES101》作业框架问题详解](https://zhuanlan.zhihu.com/p/509902950) 74 | - [一篇搞定!GAMES101现代计算机图形学入门(全)](https://zhuanlan.zhihu.com/p/394932478) 75 | - [计算机图形学系列笔记](https://www.zhihu.com/column/c_1249465121615204352) 76 | - [【个人笔记】速通计算机图形学基础](https://zhuanlan.zhihu.com/p/539347633) 77 | - [图形学 - 关于透视矫正插值那些事](https://zhuanlan.zhihu.com/p/403259571) 78 | - [图形学基础拾遗 - 重新认识剪不断理还乱的矩阵变换](https://zhuanlan.zhihu.com/p/223033896) 79 | 80 | # FQA 81 | 82 | Q: xmake 下载包失败怎么办 83 | A: 查看文档[使用远程包 - xmake](https://xmake.io/#/zh-cn/package/remote_package?id=%e8%bf%9c%e7%a8%8b%e5%8c%85%e4%b8%8b%e8%bd%bd%e4%bc%98%e5%8c%96) 84 | 85 | Q: opencv 没有下载预编译版本怎么办 86 | A: 可能需要更新你的 msvc 版本 87 | 88 | Q: 编译失败怎么办 89 | A: 执行`xmake -vD` ,查看详细输出信息 90 | 91 | Q: 怎么学习 Xmake 92 | A: [Xmake 新手教程](https://zhuanlan.zhihu.com/p/640701847) 93 | 94 | # 鸣谢 95 | 96 | 1. [https://github.com/slicol/Games101-Homework-Win](https://github.com/slicol/Games101-Homework-Win) 97 | 2. [在M1平台搭建GAMES101开发环境(2022.2)](https://zhuanlan.zhihu.com/p/472114465) 98 | -------------------------------------------------------------------------------- /src/Assignment7/main.cpp: -------------------------------------------------------------------------------- 1 | #include "Renderer.hpp" 2 | #include "Scene.hpp" 3 | #include "Triangle.hpp" 4 | #include "Sphere.hpp" 5 | #include "Vector.hpp" 6 | #include "global.hpp" 7 | #include 8 | 9 | // In the main function of the program, we create the scene (create objects and 10 | // lights) as well as set the options for the render (image width and height, 11 | // maximum recursion depth, field-of-view, etc.). We then call the render 12 | // function(). 13 | int main(int argc, char** argv) 14 | { 15 | 16 | // Change the definition here to change resolution 17 | Scene scene(784, 784); 18 | 19 | Material* red = new Material(DIFFUSE, Vector3f(0.0f)); 20 | red->Kd = Vector3f(0.63f, 0.065f, 0.05f); 21 | Material* green = new Material(DIFFUSE, Vector3f(0.0f)); 22 | green->Kd = Vector3f(0.14f, 0.45f, 0.091f); 23 | Material* white = new Material(DIFFUSE, Vector3f(0.0f)); 24 | white->Kd = Vector3f(0.725f, 0.71f, 0.68f); 25 | Material* light = new Material(DIFFUSE, (8.0f * Vector3f(0.747f+0.058f, 0.747f+0.258f, 0.747f) + 15.6f * Vector3f(0.740f+0.287f,0.740f+0.160f,0.740f) + 18.4f *Vector3f(0.737f+0.642f,0.737f+0.159f,0.737f))); 26 | light->Kd = Vector3f(0.65f); 27 | 28 | MeshTriangle floor("../../models/cornellbox/floor.obj", white); 29 | MeshTriangle shortbox("../../models/cornellbox/shortbox.obj", white); 30 | MeshTriangle tallbox("../../models/cornellbox/tallbox.obj", white); 31 | MeshTriangle left("../../models/cornellbox/left.obj", red); 32 | MeshTriangle right("../../models/cornellbox/right.obj", green); 33 | MeshTriangle light_("../../models/cornellbox/light.obj", light); 34 | 35 | scene.Add(&floor); 36 | scene.Add(&shortbox); 37 | scene.Add(&tallbox); 38 | scene.Add(&left); 39 | scene.Add(&right); 40 | scene.Add(&light_); 41 | 42 | scene.buildBVH(); 43 | 44 | Renderer r; 45 | 46 | auto start = std::chrono::system_clock::now(); 47 | r.Render(scene); 48 | auto stop = std::chrono::system_clock::now(); 49 | 50 | std::cout << "Render complete: \n"; 51 | std::cout << "Time taken: " << std::chrono::duration_cast(stop - start).count() << " hours\n"; 52 | std::cout << " : " << std::chrono::duration_cast(stop - start).count() << " minutes\n"; 53 | std::cout << " : " << std::chrono::duration_cast(stop - start).count() << " seconds\n"; 54 | 55 | return 0; 56 | } -------------------------------------------------------------------------------- /src/Assignment8/CGL/src/complex.h: -------------------------------------------------------------------------------- 1 | #ifndef CGL_COMPLEX_H 2 | #define CGL_COMPLEX_H 3 | 4 | #include "vector2D.h" 5 | 6 | namespace CGL { 7 | 8 | /** 9 | * Represents complex numbers as 2D vectors of their real and 10 | * imaginary components. 11 | */ 12 | class Complex : public Vector2D { 13 | public: 14 | 15 | /** 16 | * Constructor. 17 | * Initializes to 0. 18 | */ 19 | Complex( ) : Vector2D( 0.0, 0.0 ) { } 20 | 21 | /** 22 | * Constructor. 23 | * Initializes to a+bi. 24 | */ 25 | Complex( double a, double b ) : Vector2D( a, b ) { } 26 | 27 | /** 28 | * Constructor. 29 | * Initializes to a+bi from vector v = (a,b) 30 | */ 31 | Complex( const Vector2D& v ) : Vector2D( v ) { } 32 | 33 | /** 34 | * Compute the complex conjugate. 35 | */ 36 | inline Complex conj( ) const { 37 | return Complex( x, -y ); 38 | } 39 | 40 | /** 41 | * Compute the inverse. 42 | */ 43 | inline Complex inv( ) const { 44 | double r = 1.0/norm2(); 45 | return Complex( r*x, -r*y ); 46 | } 47 | 48 | /** 49 | * Return argument. 50 | */ 51 | inline double arg( ) const { 52 | return atan2( y, x ); 53 | } 54 | 55 | /** 56 | * Complex exponentiation. 57 | */ 58 | inline Complex exponential( ) const { 59 | return exp( x ) * Complex( cos( y ), sin( y )); 60 | } 61 | 62 | // Complex multiply by z 63 | inline void operator*=( const Complex& z ) { 64 | double a = x; 65 | double b = y; 66 | double c = z.x; 67 | double d = z.y; 68 | x = a*c-b*d; 69 | y = a*d+b*c; 70 | } 71 | 72 | // complex divide by z 73 | void operator/=( const Complex& z ) { 74 | *this *= z.inv(); 75 | } 76 | 77 | }; // class Complex 78 | 79 | // returns the real part of z 80 | double Re( const Complex& z ); 81 | 82 | // returns the imaginary part of z 83 | double Im( const Complex& z ); 84 | 85 | // binary Complex multiplication 86 | inline Complex operator*( const Complex& z1, const Complex& z2 ) { 87 | Complex z = z1; 88 | z *= z2; 89 | return z; 90 | } 91 | 92 | // complex division 93 | inline Complex operator/( const Complex& z1, const Complex& z2 ) { 94 | Complex z = z1; 95 | z /= z2; 96 | return z; 97 | } 98 | 99 | // prints components 100 | std::ostream& operator<<( std::ostream& os, const Complex& z ); 101 | 102 | } // namespace CGL 103 | 104 | #endif // CGL_COMPLEX_H 105 | -------------------------------------------------------------------------------- /src/Assignment4/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | std::vector control_points; 6 | 7 | void mouse_handler(int event, int x, int y, int flags, void *userdata) 8 | { 9 | if (event == cv::EVENT_LBUTTONDOWN && control_points.size() < 4) 10 | { 11 | std::cout << "Left button of the mouse is clicked - position (" << x << ", " 12 | << y << ")" << '\n'; 13 | control_points.emplace_back(x, y); 14 | } 15 | } 16 | 17 | void naive_bezier(const std::vector &points, cv::Mat &window) 18 | { 19 | auto &p_0 = points[0]; 20 | auto &p_1 = points[1]; 21 | auto &p_2 = points[2]; 22 | auto &p_3 = points[3]; 23 | 24 | for (double t = 0.0; t <= 1.0; t += 0.001) 25 | { 26 | auto point = std::pow(1 - t, 3) * p_0 + 3 * t * std::pow(1 - t, 2) * p_1 + 27 | 3 * std::pow(t, 2) * (1 - t) * p_2 + std::pow(t, 3) * p_3; 28 | 29 | window.at(point.y, point.x)[2] = 255; 30 | } 31 | } 32 | 33 | cv::Point2f recursive_bezier(const std::vector &control_points, float t) 34 | { 35 | // TODO: Implement de Casteljau's algorithm 36 | return cv::Point2f(); 37 | 38 | } 39 | 40 | void bezier(const std::vector &control_points, cv::Mat &window) 41 | { 42 | // TODO: Iterate through all t = 0 to t = 1 with small steps, and call de Casteljau's 43 | // recursive Bezier algorithm. 44 | 45 | } 46 | 47 | int main() 48 | { 49 | cv::Mat window = cv::Mat(700, 700, CV_8UC3, cv::Scalar(0)); 50 | cv::cvtColor(window, window, cv::COLOR_BGR2RGB); 51 | cv::namedWindow("Bezier Curve", cv::WINDOW_AUTOSIZE); 52 | 53 | cv::setMouseCallback("Bezier Curve", mouse_handler, nullptr); 54 | 55 | int key = -1; 56 | while (key != 27) 57 | { 58 | for (auto &point : control_points) 59 | { 60 | cv::circle(window, point, 3, {255, 255, 255}, 3); 61 | } 62 | 63 | if (control_points.size() == 4) 64 | { 65 | naive_bezier(control_points, window); 66 | // bezier(control_points, window); 67 | 68 | cv::imshow("Bezier Curve", window); 69 | cv::imwrite("my_bezier_curve.png", window); 70 | key = cv::waitKey(0); 71 | 72 | return 0; 73 | } 74 | 75 | cv::imshow("Bezier Curve", window); 76 | key = cv::waitKey(20); 77 | } 78 | 79 | return 0; 80 | } 81 | -------------------------------------------------------------------------------- /src/Assignment8/CGL/include/CGL/complex.h: -------------------------------------------------------------------------------- 1 | #ifndef CGL_COMPLEX_H 2 | #define CGL_COMPLEX_H 3 | 4 | #include "vector2D.h" 5 | 6 | namespace CGL { 7 | 8 | /** 9 | * Represents complex numbers as 2D vectors of their real and 10 | * imaginary components. 11 | */ 12 | class Complex : public Vector2D { 13 | public: 14 | 15 | /** 16 | * Constructor. 17 | * Initializes to 0. 18 | */ 19 | Complex( ) : Vector2D( 0.0, 0.0 ) { } 20 | 21 | /** 22 | * Constructor. 23 | * Initializes to a+bi. 24 | */ 25 | Complex( double a, double b ) : Vector2D( a, b ) { } 26 | 27 | /** 28 | * Constructor. 29 | * Initializes to a+bi from vector v = (a,b) 30 | */ 31 | Complex( const Vector2D& v ) : Vector2D( v ) { } 32 | 33 | /** 34 | * Compute the complex conjugate. 35 | */ 36 | inline Complex conj( ) const { 37 | return Complex( x, -y ); 38 | } 39 | 40 | /** 41 | * Compute the inverse. 42 | */ 43 | inline Complex inv( ) const { 44 | double r = 1.0/norm2(); 45 | return Complex( r*x, -r*y ); 46 | } 47 | 48 | /** 49 | * Return argument. 50 | */ 51 | inline double arg( ) const { 52 | return atan2( y, x ); 53 | } 54 | 55 | /** 56 | * Complex exponentiation. 57 | */ 58 | inline Complex exponential( ) const { 59 | return exp( x ) * Complex( cos( y ), sin( y )); 60 | } 61 | 62 | // Complex multiply by z 63 | inline void operator*=( const Complex& z ) { 64 | double a = x; 65 | double b = y; 66 | double c = z.x; 67 | double d = z.y; 68 | x = a*c-b*d; 69 | y = a*d+b*c; 70 | } 71 | 72 | // complex divide by z 73 | void operator/=( const Complex& z ) { 74 | *this *= z.inv(); 75 | } 76 | 77 | }; // class Complex 78 | 79 | // returns the real part of z 80 | double Re( const Complex& z ); 81 | 82 | // returns the imaginary part of z 83 | double Im( const Complex& z ); 84 | 85 | // binary Complex multiplication 86 | inline Complex operator*( const Complex& z1, const Complex& z2 ) { 87 | Complex z = z1; 88 | z *= z2; 89 | return z; 90 | } 91 | 92 | // complex division 93 | inline Complex operator/( const Complex& z1, const Complex& z2 ) { 94 | Complex z = z1; 95 | z /= z2; 96 | return z; 97 | } 98 | 99 | // prints components 100 | std::ostream& operator<<( std::ostream& os, const Complex& z ); 101 | 102 | } // namespace CGL 103 | 104 | #endif // CGL_COMPLEX_H 105 | -------------------------------------------------------------------------------- /src/Assignment6/Sphere.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by LEI XU on 5/13/19. 3 | // 4 | 5 | #ifndef RAYTRACING_SPHERE_H 6 | #define RAYTRACING_SPHERE_H 7 | 8 | #include "Object.hpp" 9 | #include "Vector.hpp" 10 | #include "Bounds3.hpp" 11 | #include "Material.hpp" 12 | 13 | class Sphere : public Object{ 14 | public: 15 | Vector3f center; 16 | float radius, radius2; 17 | Material *m; 18 | Sphere(const Vector3f &c, const float &r) : center(c), radius(r), radius2(r * r), m(new Material()) {} 19 | bool intersect(const Ray& ray) { 20 | // analytic solution 21 | Vector3f L = ray.origin - center; 22 | float a = dotProduct(ray.direction, ray.direction); 23 | float b = 2 * dotProduct(ray.direction, L); 24 | float c = dotProduct(L, L) - radius2; 25 | float t0, t1; 26 | if (!solveQuadratic(a, b, c, t0, t1)) return false; 27 | if (t0 < 0) t0 = t1; 28 | if (t0 < 0) return false; 29 | return true; 30 | } 31 | bool intersect(const Ray& ray, float &tnear, uint32_t &index) const 32 | { 33 | // analytic solution 34 | Vector3f L = ray.origin - center; 35 | float a = dotProduct(ray.direction, ray.direction); 36 | float b = 2 * dotProduct(ray.direction, L); 37 | float c = dotProduct(L, L) - radius2; 38 | float t0, t1; 39 | if (! 40 | solveQuadratic(a, b, c, t0, t1)) return false; 41 | if (t0 < 0) t0 = t1; 42 | if (t0 < 0) return false; 43 | tnear = t0; 44 | 45 | return true; 46 | } 47 | Intersection getIntersection(Ray ray){ 48 | Intersection result; 49 | result.happened = false; 50 | Vector3f L = ray.origin - center; 51 | float a = dotProduct(ray.direction, ray.direction); 52 | float b = 2 * dotProduct(ray.direction, L); 53 | float c = dotProduct(L, L) - radius2; 54 | float t0, t1; 55 | if (!solveQuadratic(a, b, c, t0, t1)) return result; 56 | if (t0 < 0) t0 = t1; 57 | if (t0 < 0) return result; 58 | result.happened=true; 59 | 60 | result.coords = Vector3f(ray.origin + ray.direction * t0); 61 | result.normal = normalize(Vector3f(result.coords - center)); 62 | result.m = this->m; 63 | result.obj = this; 64 | result.distance = t0; 65 | return result; 66 | 67 | } 68 | void getSurfaceProperties(const Vector3f &P, const Vector3f &I, const uint32_t &index, const Vector2f &uv, Vector3f &N, Vector2f &st) const 69 | { N = normalize(P - center); } 70 | 71 | Vector3f evalDiffuseColor(const Vector2f &st)const { 72 | return m->getColor(); 73 | } 74 | Bounds3 getBounds(){ 75 | return Bounds3(Vector3f(center.x-radius, center.y-radius, center.z-radius), 76 | Vector3f(center.x+radius, center.y+radius, center.z+radius)); 77 | } 78 | }; 79 | 80 | 81 | 82 | 83 | #endif //RAYTRACING_SPHERE_H 84 | -------------------------------------------------------------------------------- /src/Assignment8/src/application.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "application.h" 4 | #include "rope.h" 5 | 6 | namespace CGL { 7 | 8 | Application::Application(AppConfig config) { this->config = config; } 9 | 10 | Application::~Application() {} 11 | 12 | void Application::init() { 13 | // Enable anti-aliasing and circular points. 14 | glEnable(GL_LINE_SMOOTH); 15 | glEnable(GL_POLYGON_SMOOTH); 16 | glEnable(GL_POINT_SMOOTH); 17 | glHint(GL_LINE_SMOOTH_HINT, GL_NICEST); 18 | glHint(GL_POLYGON_SMOOTH_HINT, GL_NICEST); 19 | glHint(GL_POINT_SMOOTH_HINT, GL_NICEST); 20 | 21 | glPointSize(8); 22 | glLineWidth(4); 23 | 24 | glColor3f(1.0, 1.0, 1.0); 25 | // Create two ropes 26 | ropeEuler = new Rope(Vector2D(0, 200), Vector2D(-400, 200), 3, config.mass, 27 | config.ks, {0}); 28 | ropeVerlet = new Rope(Vector2D(0, 200), Vector2D(-400, 200), 3, config.mass, 29 | config.ks, {0}); 30 | } 31 | 32 | void Application::render() { 33 | //Simulation loops 34 | for (int i = 0; i < config.steps_per_frame; i++) { 35 | ropeEuler->simulateEuler(1 / config.steps_per_frame, config.gravity); 36 | ropeVerlet->simulateVerlet(1 / config.steps_per_frame, config.gravity); 37 | } 38 | // Rendering ropes 39 | Rope *rope; 40 | 41 | for (int i = 0; i < 2; i++) { 42 | if (i == 0) { 43 | glColor3f(0.0, 0.0, 1.0); 44 | rope = ropeEuler; 45 | } else { 46 | glColor3f(0.0, 1.0, 0.0); 47 | rope = ropeVerlet; 48 | } 49 | 50 | glBegin(GL_POINTS); 51 | 52 | for (auto &m : rope->masses) { 53 | Vector2D p = m->position; 54 | glVertex2d(p.x, p.y); 55 | } 56 | 57 | glEnd(); 58 | 59 | glBegin(GL_LINES); 60 | 61 | for (auto &s : rope->springs) { 62 | Vector2D p1 = s->m1->position; 63 | Vector2D p2 = s->m2->position; 64 | glVertex2d(p1.x, p1.y); 65 | glVertex2d(p2.x, p2.y); 66 | } 67 | 68 | glEnd(); 69 | 70 | glFlush(); 71 | } 72 | } 73 | 74 | void Application::resize(size_t w, size_t h) { 75 | screen_width = w; 76 | screen_height = h; 77 | 78 | float half_width = (float)screen_width / 2; 79 | float half_height = (float)screen_height / 2; 80 | 81 | glMatrixMode(GL_PROJECTION); 82 | glLoadIdentity(); 83 | glOrtho(-half_width, half_width, -half_height, half_height, 1, 0); 84 | } 85 | 86 | void Application::keyboard_event(int key, int event, unsigned char mods) { 87 | switch (key) { 88 | case '-': 89 | if (config.steps_per_frame > 1) { 90 | config.steps_per_frame /= 2; 91 | } 92 | break; 93 | case '=': 94 | config.steps_per_frame *= 2; 95 | break; 96 | } 97 | } 98 | 99 | string Application::name() { return "Rope Simulator"; } 100 | 101 | string Application::info() { 102 | ostringstream steps; 103 | steps << "Steps per frame: " << config.steps_per_frame; 104 | 105 | return steps.str(); 106 | } 107 | } 108 | -------------------------------------------------------------------------------- /src/Assignment2/rasterizer.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by goksu on 4/6/19. 3 | // 4 | 5 | #pragma once 6 | 7 | #include 8 | #include 9 | #include "global.hpp" 10 | #include "Triangle.hpp" 11 | using namespace Eigen; 12 | 13 | namespace rst 14 | { 15 | enum class Buffers 16 | { 17 | Color = 1, 18 | Depth = 2 19 | }; 20 | 21 | inline Buffers operator|(Buffers a, Buffers b) 22 | { 23 | return Buffers((int)a | (int)b); 24 | } 25 | 26 | inline Buffers operator&(Buffers a, Buffers b) 27 | { 28 | return Buffers((int)a & (int)b); 29 | } 30 | 31 | enum class Primitive 32 | { 33 | Line, 34 | Triangle 35 | }; 36 | 37 | /* 38 | * For the curious : The draw function takes two buffer id's as its arguments. These two structs 39 | * make sure that if you mix up with their orders, the compiler won't compile it. 40 | * Aka : Type safety 41 | * */ 42 | struct pos_buf_id 43 | { 44 | int pos_id = 0; 45 | }; 46 | 47 | struct ind_buf_id 48 | { 49 | int ind_id = 0; 50 | }; 51 | 52 | struct col_buf_id 53 | { 54 | int col_id = 0; 55 | }; 56 | 57 | class rasterizer 58 | { 59 | public: 60 | rasterizer(int w, int h); 61 | pos_buf_id load_positions(const std::vector& positions); 62 | ind_buf_id load_indices(const std::vector& indices); 63 | col_buf_id load_colors(const std::vector& colors); 64 | 65 | void set_model(const Eigen::Matrix4f& m); 66 | void set_view(const Eigen::Matrix4f& v); 67 | void set_projection(const Eigen::Matrix4f& p); 68 | 69 | void set_pixel(const Eigen::Vector3f& point, const Eigen::Vector3f& color); 70 | 71 | void clear(Buffers buff); 72 | 73 | void draw(pos_buf_id pos_buffer, ind_buf_id ind_buffer, col_buf_id col_buffer, Primitive type); 74 | 75 | std::vector& frame_buffer() { return frame_buf; } 76 | 77 | private: 78 | void draw_line(Eigen::Vector3f begin, Eigen::Vector3f end); 79 | 80 | void rasterize_triangle(const Triangle& t); 81 | 82 | // VERTEX SHADER -> MVP -> Clipping -> /.W -> VIEWPORT -> DRAWLINE/DRAWTRI -> FRAGSHADER 83 | 84 | private: 85 | Eigen::Matrix4f model; 86 | Eigen::Matrix4f view; 87 | Eigen::Matrix4f projection; 88 | 89 | std::map> pos_buf; 90 | std::map> ind_buf; 91 | std::map> col_buf; 92 | 93 | std::vector frame_buf; 94 | 95 | std::vector depth_buf; 96 | int get_index(int x, int y); 97 | 98 | int width, height; 99 | 100 | int next_id = 0; 101 | int get_next_id() { return next_id++; } 102 | }; 103 | } 104 | -------------------------------------------------------------------------------- /src/Assignment5/Vector.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | 6 | class Vector3f 7 | { 8 | public: 9 | Vector3f() 10 | : x(0) 11 | , y(0) 12 | , z(0) 13 | {} 14 | Vector3f(float xx) 15 | : x(xx) 16 | , y(xx) 17 | , z(xx) 18 | {} 19 | Vector3f(float xx, float yy, float zz) 20 | : x(xx) 21 | , y(yy) 22 | , z(zz) 23 | {} 24 | Vector3f operator*(const float& r) const 25 | { 26 | return Vector3f(x * r, y * r, z * r); 27 | } 28 | Vector3f operator/(const float& r) const 29 | { 30 | return Vector3f(x / r, y / r, z / r); 31 | } 32 | 33 | Vector3f operator*(const Vector3f& v) const 34 | { 35 | return Vector3f(x * v.x, y * v.y, z * v.z); 36 | } 37 | Vector3f operator-(const Vector3f& v) const 38 | { 39 | return Vector3f(x - v.x, y - v.y, z - v.z); 40 | } 41 | Vector3f operator+(const Vector3f& v) const 42 | { 43 | return Vector3f(x + v.x, y + v.y, z + v.z); 44 | } 45 | Vector3f operator-() const 46 | { 47 | return Vector3f(-x, -y, -z); 48 | } 49 | Vector3f& operator+=(const Vector3f& v) 50 | { 51 | x += v.x, y += v.y, z += v.z; 52 | return *this; 53 | } 54 | friend Vector3f operator*(const float& r, const Vector3f& v) 55 | { 56 | return Vector3f(v.x * r, v.y * r, v.z * r); 57 | } 58 | friend std::ostream& operator<<(std::ostream& os, const Vector3f& v) 59 | { 60 | return os << v.x << ", " << v.y << ", " << v.z; 61 | } 62 | float x, y, z; 63 | }; 64 | 65 | class Vector2f 66 | { 67 | public: 68 | Vector2f() 69 | : x(0) 70 | , y(0) 71 | {} 72 | Vector2f(float xx) 73 | : x(xx) 74 | , y(xx) 75 | {} 76 | Vector2f(float xx, float yy) 77 | : x(xx) 78 | , y(yy) 79 | {} 80 | Vector2f operator*(const float& r) const 81 | { 82 | return Vector2f(x * r, y * r); 83 | } 84 | Vector2f operator+(const Vector2f& v) const 85 | { 86 | return Vector2f(x + v.x, y + v.y); 87 | } 88 | float x, y; 89 | }; 90 | 91 | inline Vector3f lerp(const Vector3f& a, const Vector3f& b, const float& t) 92 | { 93 | return a * (1 - t) + b * t; 94 | } 95 | 96 | inline Vector3f normalize(const Vector3f& v) 97 | { 98 | float mag2 = v.x * v.x + v.y * v.y + v.z * v.z; 99 | if (mag2 > 0) 100 | { 101 | float invMag = 1 / sqrtf(mag2); 102 | return Vector3f(v.x * invMag, v.y * invMag, v.z * invMag); 103 | } 104 | 105 | return v; 106 | } 107 | 108 | inline float dotProduct(const Vector3f& a, const Vector3f& b) 109 | { 110 | return a.x * b.x + a.y * b.y + a.z * b.z; 111 | } 112 | 113 | inline Vector3f crossProduct(const Vector3f& a, const Vector3f& b) 114 | { 115 | return Vector3f(a.y * b.z - a.z * b.y, a.z * b.x - a.x * b.z, a.x * b.y - a.y * b.x); 116 | } -------------------------------------------------------------------------------- /src/Assignment8/CGL/src/matrix3x3.h: -------------------------------------------------------------------------------- 1 | #ifndef CGL_MATRIX3X3_H 2 | #define CGL_MATRIX3X3_H 3 | 4 | #include 5 | 6 | #include "vector3D.h" 7 | 8 | namespace CGL { 9 | 10 | /** 11 | * Defines a 3x3 matrix. 12 | * 3x3 matrices are extremely useful in computer graphics. 13 | */ 14 | class Matrix3x3 { 15 | 16 | public: 17 | 18 | // The default constructor. 19 | Matrix3x3(void) { } 20 | 21 | // Constructor for row major form data. 22 | // Transposes to the internal column major form. 23 | // REQUIRES: data should be of size 9 for a 3 by 3 matrix.. 24 | Matrix3x3(double * data) 25 | { 26 | for( int i = 0; i < 3; i++ ) 27 | for( int j = 0; j < 3; j++ ) 28 | { 29 | // Transpostion happens within the () query. 30 | (*this)(i,j) = data[i*3 + j]; 31 | } 32 | 33 | } 34 | 35 | 36 | 37 | /** 38 | * Sets all elements to val. 39 | */ 40 | void zero(double val = 0.0 ); 41 | 42 | /** 43 | * Returns the determinant of A. 44 | */ 45 | double det( void ) const; 46 | 47 | /** 48 | * Returns the Frobenius norm of A. 49 | */ 50 | double norm( void ) const; 51 | 52 | /** 53 | * Returns the 3x3 identity matrix. 54 | */ 55 | static Matrix3x3 identity( void ); 56 | 57 | /** 58 | * Returns a matrix representing the (left) cross product with u. 59 | */ 60 | static Matrix3x3 crossProduct( const Vector3D& u ); 61 | 62 | /** 63 | * Returns the ith column. 64 | */ 65 | Vector3D& column( int i ); 66 | const Vector3D& column( int i ) const; 67 | 68 | /** 69 | * Returns the transpose of A. 70 | */ 71 | Matrix3x3 T( void ) const; 72 | 73 | /** 74 | * Returns the inverse of A. 75 | */ 76 | Matrix3x3 inv( void ) const; 77 | 78 | // accesses element (i,j) of A using 0-based indexing 79 | double& operator()( int i, int j ); 80 | const double& operator()( int i, int j ) const; 81 | 82 | // accesses the ith column of A 83 | Vector3D& operator[]( int i ); 84 | const Vector3D& operator[]( int i ) const; 85 | 86 | // increments by B 87 | void operator+=( const Matrix3x3& B ); 88 | 89 | // returns -A 90 | Matrix3x3 operator-( void ) const; 91 | 92 | // returns A-B 93 | Matrix3x3 operator-( const Matrix3x3& B ) const; 94 | 95 | // returns c*A 96 | Matrix3x3 operator*( double c ) const; 97 | 98 | // returns A*B 99 | Matrix3x3 operator*( const Matrix3x3& B ) const; 100 | 101 | // returns A*x 102 | Vector3D operator*( const Vector3D& x ) const; 103 | 104 | // divides each element by x 105 | void operator/=( double x ); 106 | 107 | protected: 108 | 109 | // column vectors 110 | Vector3D entries[3]; 111 | 112 | }; // class Matrix3x3 113 | 114 | // returns the outer product of u and v 115 | Matrix3x3 outer( const Vector3D& u, const Vector3D& v ); 116 | 117 | // returns c*A 118 | Matrix3x3 operator*( double c, const Matrix3x3& A ); 119 | 120 | // prints entries 121 | std::ostream& operator<<( std::ostream& os, const Matrix3x3& A ); 122 | 123 | } // namespace CGL 124 | 125 | #endif // CGL_MATRIX3X3_H 126 | -------------------------------------------------------------------------------- /src/Assignment8/CGL/include/CGL/matrix3x3.h: -------------------------------------------------------------------------------- 1 | #ifndef CGL_MATRIX3X3_H 2 | #define CGL_MATRIX3X3_H 3 | 4 | #include 5 | 6 | #include "vector3D.h" 7 | 8 | namespace CGL { 9 | 10 | /** 11 | * Defines a 3x3 matrix. 12 | * 3x3 matrices are extremely useful in computer graphics. 13 | */ 14 | class Matrix3x3 { 15 | 16 | public: 17 | 18 | // The default constructor. 19 | Matrix3x3(void) { } 20 | 21 | // Constructor for row major form data. 22 | // Transposes to the internal column major form. 23 | // REQUIRES: data should be of size 9 for a 3 by 3 matrix.. 24 | Matrix3x3(double * data) 25 | { 26 | for( int i = 0; i < 3; i++ ) 27 | for( int j = 0; j < 3; j++ ) 28 | { 29 | // Transpostion happens within the () query. 30 | (*this)(i,j) = data[i*3 + j]; 31 | } 32 | 33 | } 34 | 35 | 36 | 37 | /** 38 | * Sets all elements to val. 39 | */ 40 | void zero(double val = 0.0 ); 41 | 42 | /** 43 | * Returns the determinant of A. 44 | */ 45 | double det( void ) const; 46 | 47 | /** 48 | * Returns the Frobenius norm of A. 49 | */ 50 | double norm( void ) const; 51 | 52 | /** 53 | * Returns the 3x3 identity matrix. 54 | */ 55 | static Matrix3x3 identity( void ); 56 | 57 | /** 58 | * Returns a matrix representing the (left) cross product with u. 59 | */ 60 | static Matrix3x3 crossProduct( const Vector3D& u ); 61 | 62 | /** 63 | * Returns the ith column. 64 | */ 65 | Vector3D& column( int i ); 66 | const Vector3D& column( int i ) const; 67 | 68 | /** 69 | * Returns the transpose of A. 70 | */ 71 | Matrix3x3 T( void ) const; 72 | 73 | /** 74 | * Returns the inverse of A. 75 | */ 76 | Matrix3x3 inv( void ) const; 77 | 78 | // accesses element (i,j) of A using 0-based indexing 79 | double& operator()( int i, int j ); 80 | const double& operator()( int i, int j ) const; 81 | 82 | // accesses the ith column of A 83 | Vector3D& operator[]( int i ); 84 | const Vector3D& operator[]( int i ) const; 85 | 86 | // increments by B 87 | void operator+=( const Matrix3x3& B ); 88 | 89 | // returns -A 90 | Matrix3x3 operator-( void ) const; 91 | 92 | // returns A-B 93 | Matrix3x3 operator-( const Matrix3x3& B ) const; 94 | 95 | // returns c*A 96 | Matrix3x3 operator*( double c ) const; 97 | 98 | // returns A*B 99 | Matrix3x3 operator*( const Matrix3x3& B ) const; 100 | 101 | // returns A*x 102 | Vector3D operator*( const Vector3D& x ) const; 103 | 104 | // divides each element by x 105 | void operator/=( double x ); 106 | 107 | protected: 108 | 109 | // column vectors 110 | Vector3D entries[3]; 111 | 112 | }; // class Matrix3x3 113 | 114 | // returns the outer product of u and v 115 | Matrix3x3 outer( const Vector3D& u, const Vector3D& v ); 116 | 117 | // returns c*A 118 | Matrix3x3 operator*( double c, const Matrix3x3& A ); 119 | 120 | // prints entries 121 | std::ostream& operator<<( std::ostream& os, const Matrix3x3& A ); 122 | 123 | } // namespace CGL 124 | 125 | #endif // CGL_MATRIX3X3_H 126 | -------------------------------------------------------------------------------- /src/Assignment8/CGL/src/vector2D.h: -------------------------------------------------------------------------------- 1 | #ifndef CGL_VECTOR2D_H 2 | #define CGL_VECTOR2D_H 3 | 4 | #include 5 | #include 6 | 7 | namespace CGL { 8 | 9 | /** 10 | * Defines 2D vectors. 11 | */ 12 | class Vector2D { 13 | public: 14 | 15 | // components 16 | double x, y; 17 | 18 | /** 19 | * Constructor. 20 | * Initializes to vector (0,0). 21 | */ 22 | Vector2D() : x( 0.0 ), y( 0.0 ) { } 23 | 24 | /** 25 | * Constructor. 26 | * Initializes to vector (a,b). 27 | */ 28 | Vector2D( double x, double y ) : x( x ), y( y ) { } 29 | 30 | /** 31 | * Constructor. 32 | * Copy constructor. Creates a copy of the given vector. 33 | */ 34 | Vector2D( const Vector2D& v ) : x( v.x ), y( v.y ) { } 35 | 36 | // additive inverse 37 | inline Vector2D operator-( void ) const { 38 | return Vector2D( -x, -y ); 39 | } 40 | 41 | // addition 42 | inline Vector2D operator+( const Vector2D& v ) const { 43 | Vector2D u = *this; 44 | u += v; 45 | return u; 46 | } 47 | 48 | // subtraction 49 | inline Vector2D operator-( const Vector2D& v ) const { 50 | Vector2D u = *this; 51 | u -= v; 52 | return u; 53 | } 54 | 55 | // right scalar multiplication 56 | inline Vector2D operator*( double r ) const { 57 | Vector2D vr = *this; 58 | vr *= r; 59 | return vr; 60 | } 61 | 62 | // scalar division 63 | inline Vector2D operator/( double r ) const { 64 | Vector2D vr = *this; 65 | vr /= r; 66 | return vr; 67 | } 68 | 69 | // add v 70 | inline void operator+=( const Vector2D& v ) { 71 | x += v.x; 72 | y += v.y; 73 | } 74 | 75 | // subtract v 76 | inline void operator-=( const Vector2D& v ) { 77 | x -= v.x; 78 | y -= v.y; 79 | } 80 | 81 | // scalar multiply by r 82 | inline void operator*=( double r ) { 83 | x *= r; 84 | y *= r; 85 | } 86 | 87 | // scalar divide by r 88 | inline void operator/=( double r ) { 89 | x /= r; 90 | y /= r; 91 | } 92 | 93 | /** 94 | * Returns norm. 95 | */ 96 | inline double norm( void ) const { 97 | return sqrt( x*x + y*y ); 98 | } 99 | 100 | /** 101 | * Returns norm squared. 102 | */ 103 | inline double norm2( void ) const { 104 | return x*x + y*y; 105 | } 106 | 107 | /** 108 | * Returns unit vector parallel to this one. 109 | */ 110 | inline Vector2D unit( void ) const { 111 | return *this / this->norm(); 112 | } 113 | 114 | 115 | }; // clasd Vector2D 116 | 117 | // left scalar multiplication 118 | inline Vector2D operator*( double r, const Vector2D& v ) { 119 | return v*r; 120 | } 121 | 122 | // inner product 123 | inline double dot( const Vector2D& v1, const Vector2D& v2 ) { 124 | return v1.x*v2.x + v1.y*v2.y; 125 | } 126 | 127 | // cross product 128 | inline double cross( const Vector2D& v1, const Vector2D& v2 ) { 129 | return v1.x*v2.y - v1.y*v2.x; 130 | } 131 | 132 | // prints components 133 | std::ostream& operator<<( std::ostream& os, const Vector2D& v ); 134 | 135 | } // namespace CGL 136 | 137 | #endif // CGL_VECTOR2D_H 138 | -------------------------------------------------------------------------------- /src/Assignment8/CGL/include/CGL/vector2D.h: -------------------------------------------------------------------------------- 1 | #ifndef CGL_VECTOR2D_H 2 | #define CGL_VECTOR2D_H 3 | 4 | #include 5 | #include 6 | 7 | namespace CGL { 8 | 9 | /** 10 | * Defines 2D vectors. 11 | */ 12 | class Vector2D { 13 | public: 14 | 15 | // components 16 | double x, y; 17 | 18 | /** 19 | * Constructor. 20 | * Initializes to vector (0,0). 21 | */ 22 | Vector2D() : x( 0.0 ), y( 0.0 ) { } 23 | 24 | /** 25 | * Constructor. 26 | * Initializes to vector (a,b). 27 | */ 28 | Vector2D( double x, double y ) : x( x ), y( y ) { } 29 | 30 | /** 31 | * Constructor. 32 | * Copy constructor. Creates a copy of the given vector. 33 | */ 34 | Vector2D( const Vector2D& v ) : x( v.x ), y( v.y ) { } 35 | 36 | // additive inverse 37 | inline Vector2D operator-( void ) const { 38 | return Vector2D( -x, -y ); 39 | } 40 | 41 | // addition 42 | inline Vector2D operator+( const Vector2D& v ) const { 43 | Vector2D u = *this; 44 | u += v; 45 | return u; 46 | } 47 | 48 | // subtraction 49 | inline Vector2D operator-( const Vector2D& v ) const { 50 | Vector2D u = *this; 51 | u -= v; 52 | return u; 53 | } 54 | 55 | // right scalar multiplication 56 | inline Vector2D operator*( double r ) const { 57 | Vector2D vr = *this; 58 | vr *= r; 59 | return vr; 60 | } 61 | 62 | // scalar division 63 | inline Vector2D operator/( double r ) const { 64 | Vector2D vr = *this; 65 | vr /= r; 66 | return vr; 67 | } 68 | 69 | // add v 70 | inline void operator+=( const Vector2D& v ) { 71 | x += v.x; 72 | y += v.y; 73 | } 74 | 75 | // subtract v 76 | inline void operator-=( const Vector2D& v ) { 77 | x -= v.x; 78 | y -= v.y; 79 | } 80 | 81 | // scalar multiply by r 82 | inline void operator*=( double r ) { 83 | x *= r; 84 | y *= r; 85 | } 86 | 87 | // scalar divide by r 88 | inline void operator/=( double r ) { 89 | x /= r; 90 | y /= r; 91 | } 92 | 93 | /** 94 | * Returns norm. 95 | */ 96 | inline double norm( void ) const { 97 | return sqrt( x*x + y*y ); 98 | } 99 | 100 | /** 101 | * Returns norm squared. 102 | */ 103 | inline double norm2( void ) const { 104 | return x*x + y*y; 105 | } 106 | 107 | /** 108 | * Returns unit vector parallel to this one. 109 | */ 110 | inline Vector2D unit( void ) const { 111 | return *this / this->norm(); 112 | } 113 | 114 | 115 | }; // clasd Vector2D 116 | 117 | // left scalar multiplication 118 | inline Vector2D operator*( double r, const Vector2D& v ) { 119 | return v*r; 120 | } 121 | 122 | // inner product 123 | inline double dot( const Vector2D& v1, const Vector2D& v2 ) { 124 | return v1.x*v2.x + v1.y*v2.y; 125 | } 126 | 127 | // cross product 128 | inline double cross( const Vector2D& v1, const Vector2D& v2 ) { 129 | return v1.x*v2.y - v1.y*v2.x; 130 | } 131 | 132 | // prints components 133 | std::ostream& operator<<( std::ostream& os, const Vector2D& v ); 134 | 135 | } // namespace CGL 136 | 137 | #endif // CGL_VECTOR2D_H 138 | -------------------------------------------------------------------------------- /src/Assignment6/Vector.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by LEI XU on 5/13/19. 3 | // 4 | #pragma once 5 | #ifndef RAYTRACING_VECTOR_H 6 | #define RAYTRACING_VECTOR_H 7 | 8 | #include 9 | #include 10 | #include 11 | 12 | class Vector3f { 13 | public: 14 | float x, y, z; 15 | Vector3f() : x(0), y(0), z(0) {} 16 | Vector3f(float xx) : x(xx), y(xx), z(xx) {} 17 | Vector3f(float xx, float yy, float zz) : x(xx), y(yy), z(zz) {} 18 | Vector3f operator * (const float &r) const { return Vector3f(x * r, y * r, z * r); } 19 | Vector3f operator / (const float &r) const { return Vector3f(x / r, y / r, z / r); } 20 | 21 | Vector3f operator * (const Vector3f &v) const { return Vector3f(x * v.x, y * v.y, z * v.z); } 22 | Vector3f operator - (const Vector3f &v) const { return Vector3f(x - v.x, y - v.y, z - v.z); } 23 | Vector3f operator + (const Vector3f &v) const { return Vector3f(x + v.x, y + v.y, z + v.z); } 24 | Vector3f operator - () const { return Vector3f(-x, -y, -z); } 25 | Vector3f& operator += (const Vector3f &v) { x += v.x, y += v.y, z += v.z; return *this; } 26 | friend Vector3f operator * (const float &r, const Vector3f &v) 27 | { return Vector3f(v.x * r, v.y * r, v.z * r); } 28 | friend std::ostream & operator << (std::ostream &os, const Vector3f &v) 29 | { return os << v.x << ", " << v.y << ", " << v.z; } 30 | double operator[](int index) const; 31 | double& operator[](int index); 32 | 33 | 34 | static Vector3f Min(const Vector3f &p1, const Vector3f &p2) { 35 | return Vector3f(std::min(p1.x, p2.x), std::min(p1.y, p2.y), 36 | std::min(p1.z, p2.z)); 37 | } 38 | 39 | static Vector3f Max(const Vector3f &p1, const Vector3f &p2) { 40 | return Vector3f(std::max(p1.x, p2.x), std::max(p1.y, p2.y), 41 | std::max(p1.z, p2.z)); 42 | } 43 | }; 44 | inline double Vector3f::operator[](int index) const { 45 | return (&x)[index]; 46 | } 47 | 48 | 49 | class Vector2f 50 | { 51 | public: 52 | Vector2f() : x(0), y(0) {} 53 | Vector2f(float xx) : x(xx), y(xx) {} 54 | Vector2f(float xx, float yy) : x(xx), y(yy) {} 55 | Vector2f operator * (const float &r) const { return Vector2f(x * r, y * r); } 56 | Vector2f operator + (const Vector2f &v) const { return Vector2f(x + v.x, y + v.y); } 57 | float x, y; 58 | }; 59 | 60 | inline Vector3f lerp(const Vector3f &a, const Vector3f& b, const float &t) 61 | { return a * (1 - t) + b * t; } 62 | 63 | inline Vector3f normalize(const Vector3f &v) 64 | { 65 | float mag2 = v.x * v.x + v.y * v.y + v.z * v.z; 66 | if (mag2 > 0) { 67 | float invMag = 1 / sqrtf(mag2); 68 | return Vector3f(v.x * invMag, v.y * invMag, v.z * invMag); 69 | } 70 | 71 | return v; 72 | } 73 | 74 | inline float dotProduct(const Vector3f &a, const Vector3f &b) 75 | { return a.x * b.x + a.y * b.y + a.z * b.z; } 76 | 77 | inline Vector3f crossProduct(const Vector3f &a, const Vector3f &b) 78 | { 79 | return Vector3f( 80 | a.y * b.z - a.z * b.y, 81 | a.z * b.x - a.x * b.z, 82 | a.x * b.y - a.y * b.x 83 | ); 84 | } 85 | 86 | 87 | 88 | #endif //RAYTRACING_VECTOR_H 89 | -------------------------------------------------------------------------------- /src/Assignment8/CGL/src/viewer.h: -------------------------------------------------------------------------------- 1 | #ifndef CGL_VIEWER_H 2 | #define CGL_VIEWER_H 3 | 4 | #include "renderer.h" 5 | #include "osdtext.h" 6 | 7 | #include 8 | 9 | #include "GLFW/glfw3.h" 10 | 11 | namespace CGL { 12 | 13 | /** 14 | * Provides OpenGL context, window display, and event handling routines. 15 | * A user application may draw to the window's context by providing 16 | * a user renderer. The viewer manages other display components such as the 17 | * zoom views, text OSD, etc. It also takes care of window event handling and 18 | * event passing, through which the renderer may interact with user inputs. 19 | */ 20 | class Viewer { 21 | public: 22 | 23 | /** 24 | * Constructor. 25 | * Creates a new viewer with the default title (CGL). 26 | */ 27 | Viewer( void ); 28 | 29 | /** 30 | * Constructor. 31 | * Creates a new viweer with the given title. 32 | */ 33 | Viewer( const char* title ); 34 | 35 | /** 36 | * Destructor. 37 | * Destroys the viewer instance and frees memory. 38 | * Note that this does not change the user space renderer. 39 | */ 40 | ~Viewer( void ); 41 | 42 | /** 43 | * Initialize the viewer. 44 | * This will open up a window and install all the event handlers 45 | * and make the viewer ready for drawing. 46 | */ 47 | void init( void ); 48 | 49 | /** 50 | * Start the drawing loop of the viewer. 51 | * Once called this will block until the viewer is close. 52 | */ 53 | void start( void ); 54 | 55 | /** 56 | * Set a user space renderer. 57 | * The viewer will use the given user space renderer in drawing. 58 | * \param renderer The user space renderer to use in the viewer. 59 | */ 60 | void set_renderer( Renderer *renderer ); 61 | 62 | private: 63 | 64 | /** 65 | * Main update loop. 66 | */ 67 | static void update( void ); 68 | 69 | /** 70 | * Draw information view. 71 | */ 72 | static void drawInfo( void ); 73 | 74 | // window event callbacks 75 | static void err_callback( int error, const char* description ); 76 | static void key_callback( GLFWwindow* window, int key, int scancode, int action, int mods ); 77 | static void resize_callback( GLFWwindow* window, int width, int height ); 78 | static void cursor_callback( GLFWwindow* window, double xpos, double ypos ); 79 | static void scroll_callback( GLFWwindow* window, double xoffset, double yoffset); 80 | static void mouse_button_callback( GLFWwindow* window, int button, int action, int mods ); 81 | 82 | // HDPI display 83 | static bool HDPI; 84 | 85 | // framerate related timeers 86 | static int framecount; 87 | static std::chrono::time_point sys_last; 88 | static std::chrono::time_point sys_curr; 89 | 90 | // info toggle 91 | static bool showInfo; 92 | 93 | // window properties 94 | static GLFWwindow* window; 95 | static size_t buffer_w; 96 | static size_t buffer_h; 97 | 98 | // user space renderer 99 | static Renderer* renderer; 100 | 101 | // on-screen display 102 | static OSDText* osd_text; 103 | static int line_id_renderer; 104 | static int line_id_framerate; 105 | 106 | 107 | }; // class Viewer 108 | 109 | } // namespace CGL 110 | 111 | #endif // CGL_VIEWER_H 112 | -------------------------------------------------------------------------------- /src/Assignment8/CGL/include/CGL/viewer.h: -------------------------------------------------------------------------------- 1 | #ifndef CGL_VIEWER_H 2 | #define CGL_VIEWER_H 3 | 4 | #include "renderer.h" 5 | #include "osdtext.h" 6 | 7 | #include 8 | 9 | #include "GLFW/glfw3.h" 10 | 11 | namespace CGL { 12 | 13 | /** 14 | * Provides OpenGL context, window display, and event handling routines. 15 | * A user application may draw to the window's context by providing 16 | * a user renderer. The viewer manages other display components such as the 17 | * zoom views, text OSD, etc. It also takes care of window event handling and 18 | * event passing, through which the renderer may interact with user inputs. 19 | */ 20 | class Viewer { 21 | public: 22 | 23 | /** 24 | * Constructor. 25 | * Creates a new viewer with the default title (CGL). 26 | */ 27 | Viewer( void ); 28 | 29 | /** 30 | * Constructor. 31 | * Creates a new viweer with the given title. 32 | */ 33 | Viewer( const char* title ); 34 | 35 | /** 36 | * Destructor. 37 | * Destroys the viewer instance and frees memory. 38 | * Note that this does not change the user space renderer. 39 | */ 40 | ~Viewer( void ); 41 | 42 | /** 43 | * Initialize the viewer. 44 | * This will open up a window and install all the event handlers 45 | * and make the viewer ready for drawing. 46 | */ 47 | void init( void ); 48 | 49 | /** 50 | * Start the drawing loop of the viewer. 51 | * Once called this will block until the viewer is close. 52 | */ 53 | void start( void ); 54 | 55 | /** 56 | * Set a user space renderer. 57 | * The viewer will use the given user space renderer in drawing. 58 | * \param renderer The user space renderer to use in the viewer. 59 | */ 60 | void set_renderer( Renderer *renderer ); 61 | 62 | private: 63 | 64 | /** 65 | * Main update loop. 66 | */ 67 | static void update( void ); 68 | 69 | /** 70 | * Draw information view. 71 | */ 72 | static void drawInfo( void ); 73 | 74 | // window event callbacks 75 | static void err_callback( int error, const char* description ); 76 | static void key_callback( GLFWwindow* window, int key, int scancode, int action, int mods ); 77 | static void resize_callback( GLFWwindow* window, int width, int height ); 78 | static void cursor_callback( GLFWwindow* window, double xpos, double ypos ); 79 | static void scroll_callback( GLFWwindow* window, double xoffset, double yoffset); 80 | static void mouse_button_callback( GLFWwindow* window, int button, int action, int mods ); 81 | 82 | // HDPI display 83 | static bool HDPI; 84 | 85 | // framerate related timeers 86 | static int framecount; 87 | static std::chrono::time_point sys_last; 88 | static std::chrono::time_point sys_curr; 89 | 90 | // info toggle 91 | static bool showInfo; 92 | 93 | // window properties 94 | static GLFWwindow* window; 95 | static size_t buffer_w; 96 | static size_t buffer_h; 97 | 98 | // user space renderer 99 | static Renderer* renderer; 100 | 101 | // on-screen display 102 | static OSDText* osd_text; 103 | static int line_id_renderer; 104 | static int line_id_framerate; 105 | 106 | 107 | }; // class Viewer 108 | 109 | } // namespace CGL 110 | 111 | #endif // CGL_VIEWER_H 112 | -------------------------------------------------------------------------------- /src/Assignment7/Vector.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by LEI XU on 5/13/19. 3 | // 4 | #pragma once 5 | #ifndef RAYTRACING_VECTOR_H 6 | #define RAYTRACING_VECTOR_H 7 | 8 | #include 9 | #include 10 | #include 11 | 12 | class Vector3f { 13 | public: 14 | float x, y, z; 15 | Vector3f() : x(0), y(0), z(0) {} 16 | Vector3f(float xx) : x(xx), y(xx), z(xx) {} 17 | Vector3f(float xx, float yy, float zz) : x(xx), y(yy), z(zz) {} 18 | Vector3f operator * (const float &r) const { return Vector3f(x * r, y * r, z * r); } 19 | Vector3f operator / (const float &r) const { return Vector3f(x / r, y / r, z / r); } 20 | 21 | float norm() {return std::sqrt(x * x + y * y + z * z);} 22 | Vector3f normalized() { 23 | float n = std::sqrt(x * x + y * y + z * z); 24 | return Vector3f(x / n, y / n, z / n); 25 | } 26 | 27 | Vector3f operator * (const Vector3f &v) const { return Vector3f(x * v.x, y * v.y, z * v.z); } 28 | Vector3f operator - (const Vector3f &v) const { return Vector3f(x - v.x, y - v.y, z - v.z); } 29 | Vector3f operator + (const Vector3f &v) const { return Vector3f(x + v.x, y + v.y, z + v.z); } 30 | Vector3f operator - () const { return Vector3f(-x, -y, -z); } 31 | Vector3f& operator += (const Vector3f &v) { x += v.x, y += v.y, z += v.z; return *this; } 32 | friend Vector3f operator * (const float &r, const Vector3f &v) 33 | { return Vector3f(v.x * r, v.y * r, v.z * r); } 34 | friend std::ostream & operator << (std::ostream &os, const Vector3f &v) 35 | { return os << v.x << ", " << v.y << ", " << v.z; } 36 | double operator[](int index) const; 37 | double& operator[](int index); 38 | 39 | 40 | static Vector3f Min(const Vector3f &p1, const Vector3f &p2) { 41 | return Vector3f(std::min(p1.x, p2.x), std::min(p1.y, p2.y), 42 | std::min(p1.z, p2.z)); 43 | } 44 | 45 | static Vector3f Max(const Vector3f &p1, const Vector3f &p2) { 46 | return Vector3f(std::max(p1.x, p2.x), std::max(p1.y, p2.y), 47 | std::max(p1.z, p2.z)); 48 | } 49 | }; 50 | inline double Vector3f::operator[](int index) const { 51 | return (&x)[index]; 52 | } 53 | 54 | 55 | class Vector2f 56 | { 57 | public: 58 | Vector2f() : x(0), y(0) {} 59 | Vector2f(float xx) : x(xx), y(xx) {} 60 | Vector2f(float xx, float yy) : x(xx), y(yy) {} 61 | Vector2f operator * (const float &r) const { return Vector2f(x * r, y * r); } 62 | Vector2f operator + (const Vector2f &v) const { return Vector2f(x + v.x, y + v.y); } 63 | float x, y; 64 | }; 65 | 66 | inline Vector3f lerp(const Vector3f &a, const Vector3f& b, const float &t) 67 | { return a * (1 - t) + b * t; } 68 | 69 | inline Vector3f normalize(const Vector3f &v) 70 | { 71 | float mag2 = v.x * v.x + v.y * v.y + v.z * v.z; 72 | if (mag2 > 0) { 73 | float invMag = 1 / sqrtf(mag2); 74 | return Vector3f(v.x * invMag, v.y * invMag, v.z * invMag); 75 | } 76 | 77 | return v; 78 | } 79 | 80 | inline float dotProduct(const Vector3f &a, const Vector3f &b) 81 | { return a.x * b.x + a.y * b.y + a.z * b.z; } 82 | 83 | inline Vector3f crossProduct(const Vector3f &a, const Vector3f &b) 84 | { 85 | return Vector3f( 86 | a.y * b.z - a.z * b.y, 87 | a.z * b.x - a.x * b.z, 88 | a.x * b.y - a.y * b.x 89 | ); 90 | } 91 | 92 | 93 | 94 | #endif //RAYTRACING_VECTOR_H 95 | -------------------------------------------------------------------------------- /src/Assignment8/CGL/src/matrix4x4.h: -------------------------------------------------------------------------------- 1 | #ifndef CGL_MATRIX4X4_H 2 | #define CGL_MATRIX4X4_H 3 | 4 | #include 5 | 6 | #include "vector4D.h" 7 | 8 | namespace CGL { 9 | 10 | /** 11 | * Defines a 4x4 matrix. 12 | * 4x4 matrices are also extremely useful in computer graphics. 13 | * Written by Bryce Summers on 9/10/2015. 14 | * Adapted from the Matrix3x3 class. 15 | * 16 | * EXTEND_ME : It might be nice to add some combined operations 17 | * such as multiplying then adding, 18 | * etc to increase arithmetic intensity. 19 | * I have taken the liberty of removing cross product functionality form 4D Matrices and Vectors. 20 | */ 21 | class Matrix4x4 { 22 | 23 | public: 24 | 25 | 26 | // The default constructor. 27 | Matrix4x4(void) { } 28 | 29 | // Constructor for row major form data. 30 | // Transposes to the internal column major form. 31 | // REQUIRES: data should be of size 16. 32 | Matrix4x4(double * data) 33 | { 34 | for( int i = 0; i < 4; i++ ) 35 | for( int j = 0; j < 4; j++ ) 36 | { 37 | // Transpostion happens within the () query. 38 | (*this)(i,j) = data[i*4 + j]; 39 | } 40 | 41 | } 42 | 43 | 44 | /** 45 | * Sets all elements to val. 46 | */ 47 | void zero(double val = 0.0); 48 | 49 | /** 50 | * Returns the determinant of A. 51 | */ 52 | double det( void ) const; 53 | 54 | /** 55 | * Returns the Frobenius norm of A. 56 | */ 57 | double norm( void ) const; 58 | 59 | /** 60 | * Returns a fresh 4x4 identity matrix. 61 | */ 62 | static Matrix4x4 identity( void ); 63 | 64 | // No Cross products for 4 by 4 matrix. 65 | 66 | /** 67 | * Returns the ith column. 68 | */ 69 | Vector4D& column( int i ); 70 | const Vector4D& column( int i ) const; 71 | 72 | /** 73 | * Returns the transpose of A. 74 | */ 75 | Matrix4x4 T( void ) const; 76 | 77 | /** 78 | * Returns the inverse of A. 79 | */ 80 | Matrix4x4 inv( void ) const; 81 | 82 | // accesses element (i,j) of A using 0-based indexing 83 | // where (i, j) is (row, column). 84 | double& operator()( int i, int j ); 85 | const double& operator()( int i, int j ) const; 86 | 87 | // accesses the ith column of A 88 | Vector4D& operator[]( int i ); 89 | const Vector4D& operator[]( int i ) const; 90 | 91 | // increments by B 92 | void operator+=( const Matrix4x4& B ); 93 | 94 | // returns -A 95 | Matrix4x4 operator-( void ) const; 96 | 97 | // returns A-B 98 | Matrix4x4 operator+( const Matrix4x4& B ) const; 99 | 100 | // returns A-B 101 | Matrix4x4 operator-( const Matrix4x4& B ) const; 102 | 103 | // returns c*A 104 | Matrix4x4 operator*( double c ) const; 105 | 106 | // returns A*B 107 | Matrix4x4 operator*( const Matrix4x4& B ) const; 108 | 109 | // returns A*x 110 | Vector4D operator*( const Vector4D& x ) const; 111 | 112 | // divides each element by x 113 | void operator/=( double x ); 114 | 115 | protected: 116 | 117 | // 4 by 4 matrices are represented by an array of 4 column vectors. 118 | Vector4D entries[4]; 119 | 120 | }; // class Matrix3x3 121 | 122 | // returns the outer product of u and v. 123 | Matrix4x4 outer( const Vector4D& u, const Vector4D& v ); 124 | 125 | // returns c*A 126 | Matrix4x4 operator*( double c, const Matrix4x4& A ); 127 | 128 | // prints entries 129 | std::ostream& operator<<( std::ostream& os, const Matrix4x4& A ); 130 | 131 | } // namespace CGL 132 | 133 | #endif // CGL_MATRIX4X4_H 134 | -------------------------------------------------------------------------------- /src/Assignment8/CGL/include/CGL/matrix4x4.h: -------------------------------------------------------------------------------- 1 | #ifndef CGL_MATRIX4X4_H 2 | #define CGL_MATRIX4X4_H 3 | 4 | #include 5 | 6 | #include "vector4D.h" 7 | 8 | namespace CGL { 9 | 10 | /** 11 | * Defines a 4x4 matrix. 12 | * 4x4 matrices are also extremely useful in computer graphics. 13 | * Written by Bryce Summers on 9/10/2015. 14 | * Adapted from the Matrix3x3 class. 15 | * 16 | * EXTEND_ME : It might be nice to add some combined operations 17 | * such as multiplying then adding, 18 | * etc to increase arithmetic intensity. 19 | * I have taken the liberty of removing cross product functionality form 4D Matrices and Vectors. 20 | */ 21 | class Matrix4x4 { 22 | 23 | public: 24 | 25 | 26 | // The default constructor. 27 | Matrix4x4(void) { } 28 | 29 | // Constructor for row major form data. 30 | // Transposes to the internal column major form. 31 | // REQUIRES: data should be of size 16. 32 | Matrix4x4(double * data) 33 | { 34 | for( int i = 0; i < 4; i++ ) 35 | for( int j = 0; j < 4; j++ ) 36 | { 37 | // Transpostion happens within the () query. 38 | (*this)(i,j) = data[i*4 + j]; 39 | } 40 | 41 | } 42 | 43 | 44 | /** 45 | * Sets all elements to val. 46 | */ 47 | void zero(double val = 0.0); 48 | 49 | /** 50 | * Returns the determinant of A. 51 | */ 52 | double det( void ) const; 53 | 54 | /** 55 | * Returns the Frobenius norm of A. 56 | */ 57 | double norm( void ) const; 58 | 59 | /** 60 | * Returns a fresh 4x4 identity matrix. 61 | */ 62 | static Matrix4x4 identity( void ); 63 | 64 | // No Cross products for 4 by 4 matrix. 65 | 66 | /** 67 | * Returns the ith column. 68 | */ 69 | Vector4D& column( int i ); 70 | const Vector4D& column( int i ) const; 71 | 72 | /** 73 | * Returns the transpose of A. 74 | */ 75 | Matrix4x4 T( void ) const; 76 | 77 | /** 78 | * Returns the inverse of A. 79 | */ 80 | Matrix4x4 inv( void ) const; 81 | 82 | // accesses element (i,j) of A using 0-based indexing 83 | // where (i, j) is (row, column). 84 | double& operator()( int i, int j ); 85 | const double& operator()( int i, int j ) const; 86 | 87 | // accesses the ith column of A 88 | Vector4D& operator[]( int i ); 89 | const Vector4D& operator[]( int i ) const; 90 | 91 | // increments by B 92 | void operator+=( const Matrix4x4& B ); 93 | 94 | // returns -A 95 | Matrix4x4 operator-( void ) const; 96 | 97 | // returns A-B 98 | Matrix4x4 operator+( const Matrix4x4& B ) const; 99 | 100 | // returns A-B 101 | Matrix4x4 operator-( const Matrix4x4& B ) const; 102 | 103 | // returns c*A 104 | Matrix4x4 operator*( double c ) const; 105 | 106 | // returns A*B 107 | Matrix4x4 operator*( const Matrix4x4& B ) const; 108 | 109 | // returns A*x 110 | Vector4D operator*( const Vector4D& x ) const; 111 | 112 | // divides each element by x 113 | void operator/=( double x ); 114 | 115 | protected: 116 | 117 | // 4 by 4 matrices are represented by an array of 4 column vectors. 118 | Vector4D entries[4]; 119 | 120 | }; // class Matrix3x3 121 | 122 | // returns the outer product of u and v. 123 | Matrix4x4 outer( const Vector4D& u, const Vector4D& v ); 124 | 125 | // returns c*A 126 | Matrix4x4 operator*( double c, const Matrix4x4& A ); 127 | 128 | // prints entries 129 | std::ostream& operator<<( std::ostream& os, const Matrix4x4& A ); 130 | 131 | } // namespace CGL 132 | 133 | #endif // CGL_MATRIX4X4_H 134 | -------------------------------------------------------------------------------- /src/Assignment1/main.cpp: -------------------------------------------------------------------------------- 1 | #include "Triangle.hpp" 2 | #include "rasterizer.hpp" 3 | #include 4 | #include 5 | #include 6 | 7 | constexpr double MY_PI = 3.1415926; 8 | 9 | Eigen::Matrix4f get_view_matrix(Eigen::Vector3f eye_pos) 10 | { 11 | Eigen::Matrix4f view = Eigen::Matrix4f::Identity(); 12 | 13 | Eigen::Matrix4f translate; 14 | translate << 1, 0, 0, -eye_pos[0], 0, 1, 0, -eye_pos[1], 0, 0, 1, 15 | -eye_pos[2], 0, 0, 0, 1; 16 | 17 | view = translate * view; 18 | 19 | return view; 20 | } 21 | 22 | Eigen::Matrix4f get_model_matrix(float rotation_angle) 23 | { 24 | Eigen::Matrix4f model = Eigen::Matrix4f::Identity(); 25 | 26 | // TODO: Implement this function 27 | // Create the model matrix for rotating the triangle around the Z axis. 28 | // Then return it. 29 | 30 | return model; 31 | } 32 | 33 | Eigen::Matrix4f get_projection_matrix(float eye_fov, float aspect_ratio, 34 | float zNear, float zFar) 35 | { 36 | // Students will implement this function 37 | 38 | Eigen::Matrix4f projection = Eigen::Matrix4f::Identity(); 39 | 40 | // TODO: Implement this function 41 | // Create the projection matrix for the given parameters. 42 | // Then return it. 43 | 44 | return projection; 45 | } 46 | 47 | int main(int argc, const char** argv) 48 | { 49 | float angle = 0; 50 | bool command_line = false; 51 | std::string filename = "output.png"; 52 | 53 | if (argc >= 3) { 54 | command_line = true; 55 | angle = std::stof(argv[2]); // -r by default 56 | if (argc == 4) { 57 | filename = std::string(argv[3]); 58 | } 59 | else 60 | return 0; 61 | } 62 | 63 | rst::rasterizer r(700, 700); 64 | 65 | Eigen::Vector3f eye_pos = {0, 0, 5}; 66 | 67 | std::vector pos{{2, 0, -2}, {0, 2, -2}, {-2, 0, -2}}; 68 | 69 | std::vector ind{{0, 1, 2}}; 70 | 71 | auto pos_id = r.load_positions(pos); 72 | auto ind_id = r.load_indices(ind); 73 | 74 | int key = 0; 75 | int frame_count = 0; 76 | 77 | if (command_line) { 78 | r.clear(rst::Buffers::Color | rst::Buffers::Depth); 79 | 80 | r.set_model(get_model_matrix(angle)); 81 | r.set_view(get_view_matrix(eye_pos)); 82 | r.set_projection(get_projection_matrix(45, 1, 0.1, 50)); 83 | 84 | r.draw(pos_id, ind_id, rst::Primitive::Triangle); 85 | cv::Mat image(700, 700, CV_32FC3, r.frame_buffer().data()); 86 | image.convertTo(image, CV_8UC3, 1.0f); 87 | 88 | cv::imwrite(filename, image); 89 | 90 | return 0; 91 | } 92 | 93 | while (key != 27) { 94 | r.clear(rst::Buffers::Color | rst::Buffers::Depth); 95 | 96 | r.set_model(get_model_matrix(angle)); 97 | r.set_view(get_view_matrix(eye_pos)); 98 | r.set_projection(get_projection_matrix(45, 1, 0.1, 50)); 99 | 100 | r.draw(pos_id, ind_id, rst::Primitive::Triangle); 101 | 102 | cv::Mat image(700, 700, CV_32FC3, r.frame_buffer().data()); 103 | image.convertTo(image, CV_8UC3, 1.0f); 104 | cv::imshow("image", image); 105 | key = cv::waitKey(10); 106 | 107 | std::cout << "frame count: " << frame_count++ << '\n'; 108 | 109 | if (key == 'a') { 110 | angle += 10; 111 | } 112 | else if (key == 'd') { 113 | angle -= 10; 114 | } 115 | } 116 | 117 | return 0; 118 | } 119 | -------------------------------------------------------------------------------- /src/Assignment8/CGL/src/color.h: -------------------------------------------------------------------------------- 1 | #ifndef CGL_COLOR_H 2 | #define CGL_COLOR_H 3 | 4 | #include 5 | 6 | namespace CGL { 7 | 8 | /** 9 | * Encodes a color via additive red, green, and blue chanel values. 10 | * Each color chanel value is in the range [0,1]. The alpha value 11 | * defines the transparency of the color and is also in [0,1]. 12 | */ 13 | class Color { 14 | public: 15 | 16 | // Components. 17 | float r; /**< value of red chanel */ 18 | float g; /**< value of green chanel */ 19 | float b; /**< value of blue chanel */ 20 | float a; /**< value of alpha chanel */ 21 | 22 | // constants 23 | static const Color White; 24 | static const Color Black; 25 | 26 | /** 27 | * Constructor. 28 | * Initialize from component values. 29 | * By default, alpha is initialized to 1. 30 | * \param r Value of the red chanel. 31 | * \param g Value of the green chanel. 32 | * \param b Value of the blue chanel. 33 | * \param a Value of the alpha chanel. 34 | */ 35 | Color( float r = 0, float g = 0, float b = 0, float a = 1.0 ) 36 | : r( r ), g( g ), b( b ), a( a ) { } 37 | 38 | /** 39 | * Constructor. 40 | * Initialize from array of 8-bit component values (RGB only). 41 | * \param arr Array containing component values. 42 | */ 43 | Color( const unsigned char* arr ); 44 | 45 | // Addition. Alpha is ignored. 46 | inline Color operator+( const Color& rhs ) const { 47 | return Color( r + rhs.r, g + rhs.g, b + rhs.b, a + rhs.a); 48 | } 49 | 50 | inline Color& operator+=( const Color& rhs ) { 51 | r += rhs.r; g += rhs.g; b += rhs.b; a += rhs.a; 52 | return *this; 53 | } 54 | 55 | // Vector multiplication. Alpha is ignored. 56 | inline Color operator*( const Color& rhs ) const { 57 | return Color( r * rhs.r, g * rhs.g, b * rhs.b, a * rhs.a); 58 | } 59 | 60 | inline Color& operator*=( const Color& rhs ) { 61 | r *= rhs.r; g *= rhs.g; b *= rhs.b; a *= rhs.a; 62 | return *this; 63 | } 64 | 65 | // Scalar multiplication. 66 | inline Color operator*( float s ) const { 67 | return Color( r * s, g * s, b * s, a * s ); 68 | } 69 | 70 | inline Color& operator*=( float s ) { 71 | r *= s; g *= s; b *= s; a *= s; 72 | return *this; 73 | } 74 | 75 | // comparison 76 | inline bool operator==( const Color& rhs ) const { 77 | return r == rhs.r && g == rhs.g && b == rhs.b && a == rhs.a; 78 | } 79 | 80 | inline bool operator!=( const Color& rhs ) const { 81 | return !operator==( rhs ); 82 | } 83 | 84 | /** 85 | * Construct a Color object from a hexadecimal (8-bit per 86 | * component) ASCII string. 87 | * Since hexademical strings are typically not used to encode 88 | * alpha values, the alpha is set to 1 (opaque) by default. 89 | * This method also accepts the string "none", in which case 90 | * it returns a color value with alpha zero (transparent). 91 | * \return Color constructed from the input hex encoding. 92 | */ 93 | static Color fromHex( const char* s ); 94 | 95 | /** 96 | * Returns a hexadecimal string #rrggbb encoding this color. 97 | * \return the hexadecimal encoding of the color. 98 | */ 99 | std::string toHex( ) const; 100 | 101 | 102 | }; // class Color 103 | 104 | 105 | // Commutable scalar multiplication. 106 | inline Color operator*( float s, const Color& c ) { 107 | return c * s; 108 | } 109 | 110 | // Prints components. 111 | std::ostream& operator<<( std::ostream& os, const Color& c ); 112 | 113 | } // namespace CGL 114 | 115 | #endif 116 | -------------------------------------------------------------------------------- /src/Assignment8/CGL/include/CGL/color.h: -------------------------------------------------------------------------------- 1 | #ifndef CGL_COLOR_H 2 | #define CGL_COLOR_H 3 | 4 | #include 5 | 6 | namespace CGL { 7 | 8 | /** 9 | * Encodes a color via additive red, green, and blue chanel values. 10 | * Each color chanel value is in the range [0,1]. The alpha value 11 | * defines the transparency of the color and is also in [0,1]. 12 | */ 13 | class Color { 14 | public: 15 | 16 | // Components. 17 | float r; /**< value of red chanel */ 18 | float g; /**< value of green chanel */ 19 | float b; /**< value of blue chanel */ 20 | float a; /**< value of alpha chanel */ 21 | 22 | // constants 23 | static const Color White; 24 | static const Color Black; 25 | 26 | /** 27 | * Constructor. 28 | * Initialize from component values. 29 | * By default, alpha is initialized to 1. 30 | * \param r Value of the red chanel. 31 | * \param g Value of the green chanel. 32 | * \param b Value of the blue chanel. 33 | * \param a Value of the alpha chanel. 34 | */ 35 | Color( float r = 0, float g = 0, float b = 0, float a = 1.0 ) 36 | : r( r ), g( g ), b( b ), a( a ) { } 37 | 38 | /** 39 | * Constructor. 40 | * Initialize from array of 8-bit component values (RGB only). 41 | * \param arr Array containing component values. 42 | */ 43 | Color( const unsigned char* arr ); 44 | 45 | // Addition. Alpha is ignored. 46 | inline Color operator+( const Color& rhs ) const { 47 | return Color( r + rhs.r, g + rhs.g, b + rhs.b, a + rhs.a); 48 | } 49 | 50 | inline Color& operator+=( const Color& rhs ) { 51 | r += rhs.r; g += rhs.g; b += rhs.b; a += rhs.a; 52 | return *this; 53 | } 54 | 55 | // Vector multiplication. Alpha is ignored. 56 | inline Color operator*( const Color& rhs ) const { 57 | return Color( r * rhs.r, g * rhs.g, b * rhs.b, a * rhs.a); 58 | } 59 | 60 | inline Color& operator*=( const Color& rhs ) { 61 | r *= rhs.r; g *= rhs.g; b *= rhs.b; a *= rhs.a; 62 | return *this; 63 | } 64 | 65 | // Scalar multiplication. 66 | inline Color operator*( float s ) const { 67 | return Color( r * s, g * s, b * s, a * s ); 68 | } 69 | 70 | inline Color& operator*=( float s ) { 71 | r *= s; g *= s; b *= s; a *= s; 72 | return *this; 73 | } 74 | 75 | // comparison 76 | inline bool operator==( const Color& rhs ) const { 77 | return r == rhs.r && g == rhs.g && b == rhs.b && a == rhs.a; 78 | } 79 | 80 | inline bool operator!=( const Color& rhs ) const { 81 | return !operator==( rhs ); 82 | } 83 | 84 | /** 85 | * Construct a Color object from a hexadecimal (8-bit per 86 | * component) ASCII string. 87 | * Since hexademical strings are typically not used to encode 88 | * alpha values, the alpha is set to 1 (opaque) by default. 89 | * This method also accepts the string "none", in which case 90 | * it returns a color value with alpha zero (transparent). 91 | * \return Color constructed from the input hex encoding. 92 | */ 93 | static Color fromHex( const char* s ); 94 | 95 | /** 96 | * Returns a hexadecimal string #rrggbb encoding this color. 97 | * \return the hexadecimal encoding of the color. 98 | */ 99 | std::string toHex( ) const; 100 | 101 | 102 | }; // class Color 103 | 104 | 105 | // Commutable scalar multiplication. 106 | inline Color operator*( float s, const Color& c ) { 107 | return c * s; 108 | } 109 | 110 | // Prints components. 111 | std::ostream& operator<<( std::ostream& os, const Color& c ); 112 | 113 | } // namespace CGL 114 | 115 | #endif 116 | -------------------------------------------------------------------------------- /src/Assignment7/Sphere.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by LEI XU on 5/13/19. 3 | // 4 | 5 | #ifndef RAYTRACING_SPHERE_H 6 | #define RAYTRACING_SPHERE_H 7 | 8 | #include "Object.hpp" 9 | #include "Vector.hpp" 10 | #include "Bounds3.hpp" 11 | #include "Material.hpp" 12 | 13 | class Sphere : public Object{ 14 | public: 15 | Vector3f center; 16 | float radius, radius2; 17 | Material *m; 18 | float area; 19 | Sphere(const Vector3f &c, const float &r, Material* mt = new Material()) : center(c), radius(r), radius2(r * r), m(mt), area(4 * M_PI *r *r) {} 20 | bool intersect(const Ray& ray) { 21 | // analytic solution 22 | Vector3f L = ray.origin - center; 23 | float a = dotProduct(ray.direction, ray.direction); 24 | float b = 2 * dotProduct(ray.direction, L); 25 | float c = dotProduct(L, L) - radius2; 26 | float t0, t1; 27 | float area = 4 * M_PI * radius2; 28 | if (!solveQuadratic(a, b, c, t0, t1)) return false; 29 | if (t0 < 0) t0 = t1; 30 | if (t0 < 0) return false; 31 | return true; 32 | } 33 | bool intersect(const Ray& ray, float &tnear, uint32_t &index) const 34 | { 35 | // analytic solution 36 | Vector3f L = ray.origin - center; 37 | float a = dotProduct(ray.direction, ray.direction); 38 | float b = 2 * dotProduct(ray.direction, L); 39 | float c = dotProduct(L, L) - radius2; 40 | float t0, t1; 41 | if (! 42 | solveQuadratic(a, b, c, t0, t1)) return false; 43 | if (t0 < 0) t0 = t1; 44 | if (t0 < 0) return false; 45 | tnear = t0; 46 | 47 | return true; 48 | } 49 | Intersection getIntersection(Ray ray){ 50 | Intersection result; 51 | result.happened = false; 52 | Vector3f L = ray.origin - center; 53 | float a = dotProduct(ray.direction, ray.direction); 54 | float b = 2 * dotProduct(ray.direction, L); 55 | float c = dotProduct(L, L) - radius2; 56 | float t0, t1; 57 | if (!solveQuadratic(a, b, c, t0, t1)) return result; 58 | if (t0 < 0) t0 = t1; 59 | if (t0 < 0) return result; 60 | result.happened=true; 61 | 62 | result.coords = Vector3f(ray.origin + ray.direction * t0); 63 | result.normal = normalize(Vector3f(result.coords - center)); 64 | result.m = this->m; 65 | result.obj = this; 66 | result.distance = t0; 67 | return result; 68 | 69 | } 70 | void getSurfaceProperties(const Vector3f &P, const Vector3f &I, const uint32_t &index, const Vector2f &uv, Vector3f &N, Vector2f &st) const 71 | { N = normalize(P - center); } 72 | 73 | Vector3f evalDiffuseColor(const Vector2f &st)const { 74 | //return m->getColor(); 75 | } 76 | Bounds3 getBounds(){ 77 | return Bounds3(Vector3f(center.x-radius, center.y-radius, center.z-radius), 78 | Vector3f(center.x+radius, center.y+radius, center.z+radius)); 79 | } 80 | void Sample(Intersection &pos, float &pdf){ 81 | float theta = 2.0 * M_PI * get_random_float(), phi = M_PI * get_random_float(); 82 | Vector3f dir(std::cos(phi), std::sin(phi)*std::cos(theta), std::sin(phi)*std::sin(theta)); 83 | pos.coords = center + radius * dir; 84 | pos.normal = dir; 85 | pos.emit = m->getEmission(); 86 | pdf = 1.0f / area; 87 | } 88 | float getArea(){ 89 | return area; 90 | } 91 | bool hasEmit(){ 92 | return m->hasEmission(); 93 | } 94 | }; 95 | 96 | 97 | 98 | 99 | #endif //RAYTRACING_SPHERE_H 100 | -------------------------------------------------------------------------------- /src/Assignment5/Triangle.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "Object.hpp" 4 | 5 | #include 6 | 7 | bool rayTriangleIntersect(const Vector3f& v0, const Vector3f& v1, const Vector3f& v2, const Vector3f& orig, 8 | const Vector3f& dir, float& tnear, float& u, float& v) 9 | { 10 | // TODO: Implement this function that tests whether the triangle 11 | // that's specified bt v0, v1 and v2 intersects with the ray (whose 12 | // origin is *orig* and direction is *dir*) 13 | // Also don't forget to update tnear, u and v. 14 | return false; 15 | } 16 | 17 | class MeshTriangle : public Object 18 | { 19 | public: 20 | MeshTriangle(const Vector3f* verts, const uint32_t* vertsIndex, const uint32_t& numTris, const Vector2f* st) 21 | { 22 | uint32_t maxIndex = 0; 23 | for (uint32_t i = 0; i < numTris * 3; ++i) 24 | if (vertsIndex[i] > maxIndex) 25 | maxIndex = vertsIndex[i]; 26 | maxIndex += 1; 27 | vertices = std::unique_ptr(new Vector3f[maxIndex]); 28 | memcpy(vertices.get(), verts, sizeof(Vector3f) * maxIndex); 29 | vertexIndex = std::unique_ptr(new uint32_t[numTris * 3]); 30 | memcpy(vertexIndex.get(), vertsIndex, sizeof(uint32_t) * numTris * 3); 31 | numTriangles = numTris; 32 | stCoordinates = std::unique_ptr(new Vector2f[maxIndex]); 33 | memcpy(stCoordinates.get(), st, sizeof(Vector2f) * maxIndex); 34 | } 35 | 36 | bool intersect(const Vector3f& orig, const Vector3f& dir, float& tnear, uint32_t& index, 37 | Vector2f& uv) const override 38 | { 39 | bool intersect = false; 40 | for (uint32_t k = 0; k < numTriangles; ++k) 41 | { 42 | const Vector3f& v0 = vertices[vertexIndex[k * 3]]; 43 | const Vector3f& v1 = vertices[vertexIndex[k * 3 + 1]]; 44 | const Vector3f& v2 = vertices[vertexIndex[k * 3 + 2]]; 45 | float t, u, v; 46 | if (rayTriangleIntersect(v0, v1, v2, orig, dir, t, u, v) && t < tnear) 47 | { 48 | tnear = t; 49 | uv.x = u; 50 | uv.y = v; 51 | index = k; 52 | intersect |= true; 53 | } 54 | } 55 | 56 | return intersect; 57 | } 58 | 59 | void getSurfaceProperties(const Vector3f&, const Vector3f&, const uint32_t& index, const Vector2f& uv, Vector3f& N, 60 | Vector2f& st) const override 61 | { 62 | const Vector3f& v0 = vertices[vertexIndex[index * 3]]; 63 | const Vector3f& v1 = vertices[vertexIndex[index * 3 + 1]]; 64 | const Vector3f& v2 = vertices[vertexIndex[index * 3 + 2]]; 65 | Vector3f e0 = normalize(v1 - v0); 66 | Vector3f e1 = normalize(v2 - v1); 67 | N = normalize(crossProduct(e0, e1)); 68 | const Vector2f& st0 = stCoordinates[vertexIndex[index * 3]]; 69 | const Vector2f& st1 = stCoordinates[vertexIndex[index * 3 + 1]]; 70 | const Vector2f& st2 = stCoordinates[vertexIndex[index * 3 + 2]]; 71 | st = st0 * (1 - uv.x - uv.y) + st1 * uv.x + st2 * uv.y; 72 | } 73 | 74 | Vector3f evalDiffuseColor(const Vector2f& st) const override 75 | { 76 | float scale = 5; 77 | float pattern = (fmodf(st.x * scale, 1) > 0.5) ^ (fmodf(st.y * scale, 1) > 0.5); 78 | return lerp(Vector3f(0.815, 0.235, 0.031), Vector3f(0.937, 0.937, 0.231), pattern); 79 | } 80 | 81 | std::unique_ptr vertices; 82 | uint32_t numTriangles; 83 | std::unique_ptr vertexIndex; 84 | std::unique_ptr stCoordinates; 85 | }; 86 | -------------------------------------------------------------------------------- /src/Assignment3/rasterizer.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by goksu on 4/6/19. 3 | // 4 | 5 | #pragma once 6 | 7 | #include 8 | #include 9 | #include 10 | #include "global.hpp" 11 | #include "Shader.hpp" 12 | #include "Triangle.hpp" 13 | 14 | using namespace Eigen; 15 | 16 | namespace rst 17 | { 18 | enum class Buffers 19 | { 20 | Color = 1, 21 | Depth = 2 22 | }; 23 | 24 | inline Buffers operator|(Buffers a, Buffers b) 25 | { 26 | return Buffers((int)a | (int)b); 27 | } 28 | 29 | inline Buffers operator&(Buffers a, Buffers b) 30 | { 31 | return Buffers((int)a & (int)b); 32 | } 33 | 34 | enum class Primitive 35 | { 36 | Line, 37 | Triangle 38 | }; 39 | 40 | /* 41 | * For the curious : The draw function takes two buffer id's as its arguments. These two structs 42 | * make sure that if you mix up with their orders, the compiler won't compile it. 43 | * Aka : Type safety 44 | * */ 45 | struct pos_buf_id 46 | { 47 | int pos_id = 0; 48 | }; 49 | 50 | struct ind_buf_id 51 | { 52 | int ind_id = 0; 53 | }; 54 | 55 | struct col_buf_id 56 | { 57 | int col_id = 0; 58 | }; 59 | 60 | class rasterizer 61 | { 62 | public: 63 | rasterizer(int w, int h); 64 | pos_buf_id load_positions(const std::vector& positions); 65 | ind_buf_id load_indices(const std::vector& indices); 66 | col_buf_id load_colors(const std::vector& colors); 67 | col_buf_id load_normals(const std::vector& normals); 68 | 69 | void set_model(const Eigen::Matrix4f& m); 70 | void set_view(const Eigen::Matrix4f& v); 71 | void set_projection(const Eigen::Matrix4f& p); 72 | 73 | void set_texture(Texture tex) { texture = tex; } 74 | 75 | void set_vertex_shader(std::function vert_shader); 76 | void set_fragment_shader(std::function frag_shader); 77 | 78 | void set_pixel(const Vector2i &point, const Eigen::Vector3f &color); 79 | 80 | void clear(Buffers buff); 81 | 82 | void draw(pos_buf_id pos_buffer, ind_buf_id ind_buffer, col_buf_id col_buffer, Primitive type); 83 | void draw(std::vector &TriangleList); 84 | 85 | std::vector& frame_buffer() { return frame_buf; } 86 | 87 | private: 88 | void draw_line(Eigen::Vector3f begin, Eigen::Vector3f end); 89 | 90 | void rasterize_triangle(const Triangle& t, const std::array& world_pos); 91 | 92 | // VERTEX SHADER -> MVP -> Clipping -> /.W -> VIEWPORT -> DRAWLINE/DRAWTRI -> FRAGSHADER 93 | 94 | private: 95 | Eigen::Matrix4f model; 96 | Eigen::Matrix4f view; 97 | Eigen::Matrix4f projection; 98 | 99 | int normal_id = -1; 100 | 101 | std::map> pos_buf; 102 | std::map> ind_buf; 103 | std::map> col_buf; 104 | std::map> nor_buf; 105 | 106 | std::optional texture; 107 | 108 | std::function fragment_shader; 109 | std::function vertex_shader; 110 | 111 | std::vector frame_buf; 112 | std::vector depth_buf; 113 | int get_index(int x, int y); 114 | 115 | int width, height; 116 | 117 | int next_id = 0; 118 | int get_next_id() { return next_id++; } 119 | }; 120 | } 121 | -------------------------------------------------------------------------------- /src/Assignment6/BVH.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "BVH.hpp" 4 | 5 | BVHAccel::BVHAccel(std::vector p, int maxPrimsInNode, 6 | SplitMethod splitMethod) 7 | : maxPrimsInNode(std::min(255, maxPrimsInNode)), splitMethod(splitMethod), 8 | primitives(std::move(p)) 9 | { 10 | time_t start, stop; 11 | time(&start); 12 | if (primitives.empty()) 13 | return; 14 | 15 | root = recursiveBuild(primitives); 16 | 17 | time(&stop); 18 | double diff = difftime(stop, start); 19 | int hrs = (int)diff / 3600; 20 | int mins = ((int)diff / 60) - (hrs * 60); 21 | int secs = (int)diff - (hrs * 3600) - (mins * 60); 22 | 23 | printf( 24 | "\rBVH Generation complete: \nTime Taken: %i hrs, %i mins, %i secs\n\n", 25 | hrs, mins, secs); 26 | } 27 | 28 | BVHBuildNode* BVHAccel::recursiveBuild(std::vector objects) 29 | { 30 | BVHBuildNode* node = new BVHBuildNode(); 31 | 32 | // Compute bounds of all primitives in BVH node 33 | Bounds3 bounds; 34 | for (int i = 0; i < objects.size(); ++i) 35 | bounds = Union(bounds, objects[i]->getBounds()); 36 | if (objects.size() == 1) { 37 | // Create leaf _BVHBuildNode_ 38 | node->bounds = objects[0]->getBounds(); 39 | node->object = objects[0]; 40 | node->left = nullptr; 41 | node->right = nullptr; 42 | return node; 43 | } 44 | else if (objects.size() == 2) { 45 | node->left = recursiveBuild(std::vector{objects[0]}); 46 | node->right = recursiveBuild(std::vector{objects[1]}); 47 | 48 | node->bounds = Union(node->left->bounds, node->right->bounds); 49 | return node; 50 | } 51 | else { 52 | Bounds3 centroidBounds; 53 | for (int i = 0; i < objects.size(); ++i) 54 | centroidBounds = 55 | Union(centroidBounds, objects[i]->getBounds().Centroid()); 56 | int dim = centroidBounds.maxExtent(); 57 | switch (dim) { 58 | case 0: 59 | std::sort(objects.begin(), objects.end(), [](auto f1, auto f2) { 60 | return f1->getBounds().Centroid().x < 61 | f2->getBounds().Centroid().x; 62 | }); 63 | break; 64 | case 1: 65 | std::sort(objects.begin(), objects.end(), [](auto f1, auto f2) { 66 | return f1->getBounds().Centroid().y < 67 | f2->getBounds().Centroid().y; 68 | }); 69 | break; 70 | case 2: 71 | std::sort(objects.begin(), objects.end(), [](auto f1, auto f2) { 72 | return f1->getBounds().Centroid().z < 73 | f2->getBounds().Centroid().z; 74 | }); 75 | break; 76 | } 77 | 78 | auto beginning = objects.begin(); 79 | auto middling = objects.begin() + (objects.size() / 2); 80 | auto ending = objects.end(); 81 | 82 | auto leftshapes = std::vector(beginning, middling); 83 | auto rightshapes = std::vector(middling, ending); 84 | 85 | assert(objects.size() == (leftshapes.size() + rightshapes.size())); 86 | 87 | node->left = recursiveBuild(leftshapes); 88 | node->right = recursiveBuild(rightshapes); 89 | 90 | node->bounds = Union(node->left->bounds, node->right->bounds); 91 | } 92 | 93 | return node; 94 | } 95 | 96 | Intersection BVHAccel::Intersect(const Ray& ray) const 97 | { 98 | Intersection isect; 99 | if (!root) 100 | return isect; 101 | isect = BVHAccel::getIntersection(root, ray); 102 | return isect; 103 | } 104 | 105 | Intersection BVHAccel::getIntersection(BVHBuildNode* node, const Ray& ray) const 106 | { 107 | // TODO Traverse the BVH to find intersection 108 | return {}; 109 | } -------------------------------------------------------------------------------- /src/Assignment2/main.cpp: -------------------------------------------------------------------------------- 1 | // clang-format off 2 | #include 3 | #include 4 | #include "rasterizer.hpp" 5 | #include "global.hpp" 6 | #include "Triangle.hpp" 7 | 8 | constexpr double MY_PI = 3.1415926; 9 | 10 | Eigen::Matrix4f get_view_matrix(Eigen::Vector3f eye_pos) 11 | { 12 | Eigen::Matrix4f view = Eigen::Matrix4f::Identity(); 13 | 14 | Eigen::Matrix4f translate; 15 | translate << 1,0,0,-eye_pos[0], 16 | 0,1,0,-eye_pos[1], 17 | 0,0,1,-eye_pos[2], 18 | 0,0,0,1; 19 | 20 | view = translate*view; 21 | 22 | return view; 23 | } 24 | 25 | Eigen::Matrix4f get_model_matrix(float rotation_angle) 26 | { 27 | Eigen::Matrix4f model = Eigen::Matrix4f::Identity(); 28 | return model; 29 | } 30 | 31 | Eigen::Matrix4f get_projection_matrix(float eye_fov, float aspect_ratio, float zNear, float zFar) 32 | { 33 | // TODO: Copy-paste your implementation from the previous assignment. 34 | Eigen::Matrix4f projection; 35 | 36 | return projection; 37 | } 38 | 39 | int main(int argc, const char** argv) 40 | { 41 | float angle = 0; 42 | bool command_line = false; 43 | std::string filename = "output.png"; 44 | 45 | if (argc == 2) 46 | { 47 | command_line = true; 48 | filename = std::string(argv[1]); 49 | } 50 | 51 | rst::rasterizer r(700, 700); 52 | 53 | Eigen::Vector3f eye_pos = {0,0,5}; 54 | 55 | 56 | std::vector pos 57 | { 58 | {2, 0, -2}, 59 | {0, 2, -2}, 60 | {-2, 0, -2}, 61 | {3.5, -1, -5}, 62 | {2.5, 1.5, -5}, 63 | {-1, 0.5, -5} 64 | }; 65 | 66 | std::vector ind 67 | { 68 | {0, 1, 2}, 69 | {3, 4, 5} 70 | }; 71 | 72 | std::vector cols 73 | { 74 | {217.0, 238.0, 185.0}, 75 | {217.0, 238.0, 185.0}, 76 | {217.0, 238.0, 185.0}, 77 | {185.0, 217.0, 238.0}, 78 | {185.0, 217.0, 238.0}, 79 | {185.0, 217.0, 238.0} 80 | }; 81 | 82 | auto pos_id = r.load_positions(pos); 83 | auto ind_id = r.load_indices(ind); 84 | auto col_id = r.load_colors(cols); 85 | 86 | int key = 0; 87 | int frame_count = 0; 88 | 89 | if (command_line) 90 | { 91 | r.clear(rst::Buffers::Color | rst::Buffers::Depth); 92 | 93 | r.set_model(get_model_matrix(angle)); 94 | r.set_view(get_view_matrix(eye_pos)); 95 | r.set_projection(get_projection_matrix(45, 1, 0.1, 50)); 96 | 97 | r.draw(pos_id, ind_id, col_id, rst::Primitive::Triangle); 98 | cv::Mat image(700, 700, CV_32FC3, r.frame_buffer().data()); 99 | image.convertTo(image, CV_8UC3, 1.0f); 100 | cv::cvtColor(image, image, cv::COLOR_RGB2BGR); 101 | 102 | cv::imwrite(filename, image); 103 | 104 | return 0; 105 | } 106 | 107 | while(key != 27) 108 | { 109 | r.clear(rst::Buffers::Color | rst::Buffers::Depth); 110 | 111 | r.set_model(get_model_matrix(angle)); 112 | r.set_view(get_view_matrix(eye_pos)); 113 | r.set_projection(get_projection_matrix(45, 1, 0.1, 50)); 114 | 115 | r.draw(pos_id, ind_id, col_id, rst::Primitive::Triangle); 116 | 117 | cv::Mat image(700, 700, CV_32FC3, r.frame_buffer().data()); 118 | image.convertTo(image, CV_8UC3, 1.0f); 119 | cv::cvtColor(image, image, cv::COLOR_RGB2BGR); 120 | cv::imshow("image", image); 121 | key = cv::waitKey(10); 122 | 123 | std::cout << "frame count: " << frame_count++ << '\n'; 124 | } 125 | 126 | return 0; 127 | } 128 | // clang-format on -------------------------------------------------------------------------------- /src/Assignment7/Bounds3.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by LEI XU on 5/16/19. 3 | // 4 | 5 | #ifndef RAYTRACING_BOUNDS3_H 6 | #define RAYTRACING_BOUNDS3_H 7 | #include "Ray.hpp" 8 | #include "Vector.hpp" 9 | #include 10 | #include 11 | 12 | class Bounds3 13 | { 14 | public: 15 | Vector3f pMin, pMax; // two points to specify the bounding box 16 | Bounds3() 17 | { 18 | double minNum = std::numeric_limits::lowest(); 19 | double maxNum = std::numeric_limits::max(); 20 | pMax = Vector3f(minNum, minNum, minNum); 21 | pMin = Vector3f(maxNum, maxNum, maxNum); 22 | } 23 | Bounds3(const Vector3f p) : pMin(p), pMax(p) {} 24 | Bounds3(const Vector3f p1, const Vector3f p2) 25 | { 26 | pMin = Vector3f(fmin(p1.x, p2.x), fmin(p1.y, p2.y), fmin(p1.z, p2.z)); 27 | pMax = Vector3f(fmax(p1.x, p2.x), fmax(p1.y, p2.y), fmax(p1.z, p2.z)); 28 | } 29 | 30 | Vector3f Diagonal() const { return pMax - pMin; } 31 | int maxExtent() const 32 | { 33 | Vector3f d = Diagonal(); 34 | if (d.x > d.y && d.x > d.z) 35 | return 0; 36 | else if (d.y > d.z) 37 | return 1; 38 | else 39 | return 2; 40 | } 41 | 42 | double SurfaceArea() const 43 | { 44 | Vector3f d = Diagonal(); 45 | return 2 * (d.x * d.y + d.x * d.z + d.y * d.z); 46 | } 47 | 48 | Vector3f Centroid() { return 0.5 * pMin + 0.5 * pMax; } 49 | Bounds3 Intersect(const Bounds3& b) 50 | { 51 | return Bounds3(Vector3f(fmax(pMin.x, b.pMin.x), fmax(pMin.y, b.pMin.y), 52 | fmax(pMin.z, b.pMin.z)), 53 | Vector3f(fmin(pMax.x, b.pMax.x), fmin(pMax.y, b.pMax.y), 54 | fmin(pMax.z, b.pMax.z))); 55 | } 56 | 57 | Vector3f Offset(const Vector3f& p) const 58 | { 59 | Vector3f o = p - pMin; 60 | if (pMax.x > pMin.x) 61 | o.x /= pMax.x - pMin.x; 62 | if (pMax.y > pMin.y) 63 | o.y /= pMax.y - pMin.y; 64 | if (pMax.z > pMin.z) 65 | o.z /= pMax.z - pMin.z; 66 | return o; 67 | } 68 | 69 | bool Overlaps(const Bounds3& b1, const Bounds3& b2) 70 | { 71 | bool x = (b1.pMax.x >= b2.pMin.x) && (b1.pMin.x <= b2.pMax.x); 72 | bool y = (b1.pMax.y >= b2.pMin.y) && (b1.pMin.y <= b2.pMax.y); 73 | bool z = (b1.pMax.z >= b2.pMin.z) && (b1.pMin.z <= b2.pMax.z); 74 | return (x && y && z); 75 | } 76 | 77 | bool Inside(const Vector3f& p, const Bounds3& b) 78 | { 79 | return (p.x >= b.pMin.x && p.x <= b.pMax.x && p.y >= b.pMin.y && 80 | p.y <= b.pMax.y && p.z >= b.pMin.z && p.z <= b.pMax.z); 81 | } 82 | inline const Vector3f& operator[](int i) const 83 | { 84 | return (i == 0) ? pMin : pMax; 85 | } 86 | 87 | inline bool IntersectP(const Ray& ray, const Vector3f& invDir, 88 | const std::array& dirisNeg) const; 89 | }; 90 | 91 | 92 | 93 | inline bool Bounds3::IntersectP(const Ray& ray, const Vector3f& invDir, 94 | const std::array& dirIsNeg) const 95 | { 96 | // invDir: ray direction(x,y,z), invDir=(1.0/x,1.0/y,1.0/z), use this because Multiply is faster that Division 97 | // dirIsNeg: ray direction(x,y,z), dirIsNeg=[int(x>0),int(y>0),int(z>0)], use this to simplify your logic 98 | // TODO test if ray bound intersects 99 | 100 | } 101 | 102 | inline Bounds3 Union(const Bounds3& b1, const Bounds3& b2) 103 | { 104 | Bounds3 ret; 105 | ret.pMin = Vector3f::Min(b1.pMin, b2.pMin); 106 | ret.pMax = Vector3f::Max(b1.pMax, b2.pMax); 107 | return ret; 108 | } 109 | 110 | inline Bounds3 Union(const Bounds3& b, const Vector3f& p) 111 | { 112 | Bounds3 ret; 113 | ret.pMin = Vector3f::Min(b.pMin, p); 114 | ret.pMax = Vector3f::Max(b.pMax, p); 115 | return ret; 116 | } 117 | 118 | #endif // RAYTRACING_BOUNDS3_H 119 | -------------------------------------------------------------------------------- /src/Assignment6/Bounds3.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by LEI XU on 5/16/19. 3 | // 4 | 5 | #ifndef RAYTRACING_BOUNDS3_H 6 | #define RAYTRACING_BOUNDS3_H 7 | #include "Ray.hpp" 8 | #include "Vector.hpp" 9 | #include 10 | #include 11 | 12 | class Bounds3 13 | { 14 | public: 15 | Vector3f pMin, pMax; // two points to specify the bounding box 16 | Bounds3() 17 | { 18 | double minNum = std::numeric_limits::lowest(); 19 | double maxNum = std::numeric_limits::max(); 20 | pMax = Vector3f(minNum, minNum, minNum); 21 | pMin = Vector3f(maxNum, maxNum, maxNum); 22 | } 23 | Bounds3(const Vector3f p) : pMin(p), pMax(p) {} 24 | Bounds3(const Vector3f p1, const Vector3f p2) 25 | { 26 | pMin = Vector3f(fmin(p1.x, p2.x), fmin(p1.y, p2.y), fmin(p1.z, p2.z)); 27 | pMax = Vector3f(fmax(p1.x, p2.x), fmax(p1.y, p2.y), fmax(p1.z, p2.z)); 28 | } 29 | 30 | Vector3f Diagonal() const { return pMax - pMin; } 31 | int maxExtent() const 32 | { 33 | Vector3f d = Diagonal(); 34 | if (d.x > d.y && d.x > d.z) 35 | return 0; 36 | else if (d.y > d.z) 37 | return 1; 38 | else 39 | return 2; 40 | } 41 | 42 | double SurfaceArea() const 43 | { 44 | Vector3f d = Diagonal(); 45 | return 2 * (d.x * d.y + d.x * d.z + d.y * d.z); 46 | } 47 | 48 | Vector3f Centroid() { return 0.5 * pMin + 0.5 * pMax; } 49 | Bounds3 Intersect(const Bounds3& b) 50 | { 51 | return Bounds3(Vector3f(fmax(pMin.x, b.pMin.x), fmax(pMin.y, b.pMin.y), 52 | fmax(pMin.z, b.pMin.z)), 53 | Vector3f(fmin(pMax.x, b.pMax.x), fmin(pMax.y, b.pMax.y), 54 | fmin(pMax.z, b.pMax.z))); 55 | } 56 | 57 | Vector3f Offset(const Vector3f& p) const 58 | { 59 | Vector3f o = p - pMin; 60 | if (pMax.x > pMin.x) 61 | o.x /= pMax.x - pMin.x; 62 | if (pMax.y > pMin.y) 63 | o.y /= pMax.y - pMin.y; 64 | if (pMax.z > pMin.z) 65 | o.z /= pMax.z - pMin.z; 66 | return o; 67 | } 68 | 69 | bool Overlaps(const Bounds3& b1, const Bounds3& b2) 70 | { 71 | bool x = (b1.pMax.x >= b2.pMin.x) && (b1.pMin.x <= b2.pMax.x); 72 | bool y = (b1.pMax.y >= b2.pMin.y) && (b1.pMin.y <= b2.pMax.y); 73 | bool z = (b1.pMax.z >= b2.pMin.z) && (b1.pMin.z <= b2.pMax.z); 74 | return (x && y && z); 75 | } 76 | 77 | bool Inside(const Vector3f& p, const Bounds3& b) 78 | { 79 | return (p.x >= b.pMin.x && p.x <= b.pMax.x && p.y >= b.pMin.y && 80 | p.y <= b.pMax.y && p.z >= b.pMin.z && p.z <= b.pMax.z); 81 | } 82 | inline const Vector3f& operator[](int i) const 83 | { 84 | return (i == 0) ? pMin : pMax; 85 | } 86 | 87 | inline bool IntersectP(const Ray& ray, const Vector3f& invDir, 88 | const std::array& dirisNeg) const; 89 | }; 90 | 91 | 92 | 93 | inline bool Bounds3::IntersectP(const Ray& ray, const Vector3f& invDir, 94 | const std::array& dirIsNeg) const 95 | { 96 | // invDir: ray direction(x,y,z), invDir=(1.0/x,1.0/y,1.0/z), use this because Multiply is faster that Division 97 | // dirIsNeg: ray direction(x,y,z), dirIsNeg=[int(x>0),int(y>0),int(z>0)], use this to simplify your logic 98 | // TODO test if ray bound intersects 99 | 100 | } 101 | 102 | inline Bounds3 Union(const Bounds3& b1, const Bounds3& b2) 103 | { 104 | Bounds3 ret; 105 | ret.pMin = Vector3f::Min(b1.pMin, b2.pMin); 106 | ret.pMax = Vector3f::Max(b1.pMax, b2.pMax); 107 | return ret; 108 | } 109 | 110 | inline Bounds3 Union(const Bounds3& b, const Vector3f& p) 111 | { 112 | Bounds3 ret; 113 | ret.pMin = Vector3f::Min(b.pMin, p); 114 | ret.pMax = Vector3f::Max(b.pMax, p); 115 | return ret; 116 | } 117 | 118 | #endif // RAYTRACING_BOUNDS3_H 119 | -------------------------------------------------------------------------------- /src/Assignment8/CGL/include/CGL/vector3D.h: -------------------------------------------------------------------------------- 1 | #ifndef CGL_VECTOR3D_H 2 | #define CGL_VECTOR3D_H 3 | 4 | #include 5 | #include 6 | 7 | namespace CGL { 8 | 9 | /** 10 | * Defines 3D vectors. 11 | */ 12 | class Vector3D { 13 | public: 14 | 15 | // components 16 | double x, y, z; 17 | 18 | /** 19 | * Constructor. 20 | * Initializes tp vector (0,0,0). 21 | */ 22 | Vector3D() : x( 0.0 ), y( 0.0 ), z( 0.0 ) { } 23 | 24 | /** 25 | * Constructor. 26 | * Initializes to vector (x,y,z). 27 | */ 28 | Vector3D( double x, double y, double z) : x( x ), y( y ), z( z ) { } 29 | 30 | /** 31 | * Constructor. 32 | * Initializes to vector (c,c,c) 33 | */ 34 | Vector3D( double c ) : x( c ), y( c ), z( c ) { } 35 | 36 | /** 37 | * Constructor. 38 | * Initializes from existing vector 39 | */ 40 | Vector3D( const Vector3D& v ) : x( v.x ), y( v.y ), z( v.z ) { } 41 | 42 | // returns reference to the specified component (0-based indexing: x, y, z) 43 | inline double& operator[] ( const int& index ) { 44 | return ( &x )[ index ]; 45 | } 46 | 47 | // returns const reference to the specified component (0-based indexing: x, y, z) 48 | inline const double& operator[] ( const int& index ) const { 49 | return ( &x )[ index ]; 50 | } 51 | 52 | // negation 53 | inline Vector3D operator-( void ) const { 54 | return Vector3D( -x, -y, -z ); 55 | } 56 | 57 | // addition 58 | inline Vector3D operator+( const Vector3D& v ) const { 59 | return Vector3D( x + v.x, y + v.y, z + v.z ); 60 | } 61 | 62 | // subtraction 63 | inline Vector3D operator-( const Vector3D& v ) const { 64 | return Vector3D( x - v.x, y - v.y, z - v.z ); 65 | } 66 | 67 | // right scalar multiplication 68 | inline Vector3D operator*( const double& c ) const { 69 | return Vector3D( x * c, y * c, z * c ); 70 | } 71 | 72 | // scalar division 73 | inline Vector3D operator/( const double& c ) const { 74 | const double rc = 1.0/c; 75 | return Vector3D( rc * x, rc * y, rc * z ); 76 | } 77 | 78 | // addition / assignment 79 | inline void operator+=( const Vector3D& v ) { 80 | x += v.x; y += v.y; z += v.z; 81 | } 82 | 83 | // subtraction / assignment 84 | inline void operator-=( const Vector3D& v ) { 85 | x -= v.x; y -= v.y; z -= v.z; 86 | } 87 | 88 | // scalar multiplication / assignment 89 | inline void operator*=( const double& c ) { 90 | x *= c; y *= c; z *= c; 91 | } 92 | 93 | // scalar division / assignment 94 | inline void operator/=( const double& c ) { 95 | (*this) *= ( 1./c ); 96 | } 97 | 98 | /** 99 | * Returns Euclidean length. 100 | */ 101 | inline double norm( void ) const { 102 | return sqrt( x*x + y*y + z*z ); 103 | } 104 | 105 | /** 106 | * Returns Euclidean length squared. 107 | */ 108 | inline double norm2( void ) const { 109 | return x*x + y*y + z*z; 110 | } 111 | 112 | /** 113 | * Returns unit vector. 114 | */ 115 | inline Vector3D unit( void ) const { 116 | double rNorm = 1. / sqrt( x*x + y*y + z*z ); 117 | return Vector3D( rNorm*x, rNorm*y, rNorm*z ); 118 | } 119 | 120 | /** 121 | * Divides by Euclidean length. 122 | */ 123 | inline void normalize( void ) { 124 | (*this) /= norm(); 125 | } 126 | 127 | }; // class Vector3D 128 | 129 | // left scalar multiplication 130 | inline Vector3D operator* ( const double& c, const Vector3D& v ) { 131 | return Vector3D( c * v.x, c * v.y, c * v.z ); 132 | } 133 | 134 | // dot product (a.k.a. inner or scalar product) 135 | inline double dot( const Vector3D& u, const Vector3D& v ) { 136 | return u.x*v.x + u.y*v.y + u.z*v.z ; 137 | } 138 | 139 | // cross product 140 | inline Vector3D cross( const Vector3D& u, const Vector3D& v ) { 141 | return Vector3D( u.y*v.z - u.z*v.y, 142 | u.z*v.x - u.x*v.z, 143 | u.x*v.y - u.y*v.x ); 144 | } 145 | 146 | // prints components 147 | std::ostream& operator<<( std::ostream& os, const Vector3D& v ); 148 | 149 | } // namespace CGL 150 | 151 | #endif // CGL_VECTOR3D_H 152 | -------------------------------------------------------------------------------- /src/Assignment8/CGL/src/vector3D.h: -------------------------------------------------------------------------------- 1 | #ifndef CGL_VECTOR3D_H 2 | #define CGL_VECTOR3D_H 3 | 4 | #include 5 | #include 6 | 7 | namespace CGL { 8 | 9 | /** 10 | * Defines 3D vectors. 11 | */ 12 | class Vector3D { 13 | public: 14 | 15 | // components 16 | double x, y, z; 17 | 18 | /** 19 | * Constructor. 20 | * Initializes tp vector (0,0,0). 21 | */ 22 | Vector3D() : x( 0.0 ), y( 0.0 ), z( 0.0 ) { } 23 | 24 | /** 25 | * Constructor. 26 | * Initializes to vector (x,y,z). 27 | */ 28 | Vector3D( double x, double y, double z) : x( x ), y( y ), z( z ) { } 29 | 30 | /** 31 | * Constructor. 32 | * Initializes to vector (c,c,c) 33 | */ 34 | Vector3D( double c ) : x( c ), y( c ), z( c ) { } 35 | 36 | /** 37 | * Constructor. 38 | * Initializes from existing vector 39 | */ 40 | Vector3D( const Vector3D& v ) : x( v.x ), y( v.y ), z( v.z ) { } 41 | 42 | // returns reference to the specified component (0-based indexing: x, y, z) 43 | inline double& operator[] ( const int& index ) { 44 | return ( &x )[ index ]; 45 | } 46 | 47 | // returns const reference to the specified component (0-based indexing: x, y, z) 48 | inline const double& operator[] ( const int& index ) const { 49 | return ( &x )[ index ]; 50 | } 51 | 52 | // negation 53 | inline Vector3D operator-( void ) const { 54 | return Vector3D( -x, -y, -z ); 55 | } 56 | 57 | // addition 58 | inline Vector3D operator+( const Vector3D& v ) const { 59 | return Vector3D( x + v.x, y + v.y, z + v.z ); 60 | } 61 | 62 | // subtraction 63 | inline Vector3D operator-( const Vector3D& v ) const { 64 | return Vector3D( x - v.x, y - v.y, z - v.z ); 65 | } 66 | 67 | // right scalar multiplication 68 | inline Vector3D operator*( const double& c ) const { 69 | return Vector3D( x * c, y * c, z * c ); 70 | } 71 | 72 | // scalar division 73 | inline Vector3D operator/( const double& c ) const { 74 | const double rc = 1.0/c; 75 | return Vector3D( rc * x, rc * y, rc * z ); 76 | } 77 | 78 | // addition / assignment 79 | inline void operator+=( const Vector3D& v ) { 80 | x += v.x; y += v.y; z += v.z; 81 | } 82 | 83 | // subtraction / assignment 84 | inline void operator-=( const Vector3D& v ) { 85 | x -= v.x; y -= v.y; z -= v.z; 86 | } 87 | 88 | // scalar multiplication / assignment 89 | inline void operator*=( const double& c ) { 90 | x *= c; y *= c; z *= c; 91 | } 92 | 93 | // scalar division / assignment 94 | inline void operator/=( const double& c ) { 95 | (*this) *= ( 1./c ); 96 | } 97 | 98 | /** 99 | * Returns Euclidean length. 100 | */ 101 | inline double norm( void ) const { 102 | return sqrt( x*x + y*y + z*z ); 103 | } 104 | 105 | /** 106 | * Returns Euclidean length squared. 107 | */ 108 | inline double norm2( void ) const { 109 | return x*x + y*y + z*z; 110 | } 111 | 112 | /** 113 | * Returns unit vector. 114 | */ 115 | inline Vector3D unit( void ) const { 116 | double rNorm = 1. / sqrt( x*x + y*y + z*z ); 117 | return Vector3D( rNorm*x, rNorm*y, rNorm*z ); 118 | } 119 | 120 | /** 121 | * Divides by Euclidean length. 122 | */ 123 | inline void normalize( void ) { 124 | (*this) /= norm(); 125 | } 126 | 127 | }; // class Vector3D 128 | 129 | // left scalar multiplication 130 | inline Vector3D operator* ( const double& c, const Vector3D& v ) { 131 | return Vector3D( c * v.x, c * v.y, c * v.z ); 132 | } 133 | 134 | // dot product (a.k.a. inner or scalar product) 135 | inline double dot( const Vector3D& u, const Vector3D& v ) { 136 | return u.x*v.x + u.y*v.y + u.z*v.z ; 137 | } 138 | 139 | // cross product 140 | inline Vector3D cross( const Vector3D& u, const Vector3D& v ) { 141 | return Vector3D( u.y*v.z - u.z*v.y, 142 | u.z*v.x - u.x*v.z, 143 | u.x*v.y - u.y*v.x ); 144 | } 145 | 146 | // prints components 147 | std::ostream& operator<<( std::ostream& os, const Vector3D& v ); 148 | 149 | } // namespace CGL 150 | 151 | #endif // CGL_VECTOR3D_H 152 | -------------------------------------------------------------------------------- /src/Assignment6/Scene.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by Göksu Güvendiren on 2019-05-14. 3 | // 4 | 5 | #pragma once 6 | 7 | #include 8 | #include "Vector.hpp" 9 | #include "Object.hpp" 10 | #include "Light.hpp" 11 | #include "AreaLight.hpp" 12 | #include "BVH.hpp" 13 | #include "Ray.hpp" 14 | 15 | 16 | class Scene 17 | { 18 | public: 19 | // setting up options 20 | int width = 1280; 21 | int height = 960; 22 | double fov = 90; 23 | Vector3f backgroundColor = Vector3f(0.235294, 0.67451, 0.843137); 24 | int maxDepth = 5; 25 | 26 | Scene(int w, int h) : width(w), height(h) 27 | {} 28 | 29 | void Add(Object *object) { objects.push_back(object); } 30 | void Add(std::unique_ptr light) { lights.push_back(std::move(light)); } 31 | 32 | const std::vector& get_objects() const { return objects; } 33 | const std::vector >& get_lights() const { return lights; } 34 | Intersection intersect(const Ray& ray) const; 35 | BVHAccel *bvh; 36 | void buildBVH(); 37 | Vector3f castRay(const Ray &ray, int depth) const; 38 | bool trace(const Ray &ray, const std::vector &objects, float &tNear, uint32_t &index, Object **hitObject); 39 | std::tuple HandleAreaLight(const AreaLight &light, const Vector3f &hitPoint, const Vector3f &N, 40 | const Vector3f &shadowPointOrig, 41 | const std::vector &objects, uint32_t &index, 42 | const Vector3f &dir, float specularExponent); 43 | 44 | // creating the scene (adding objects and lights) 45 | std::vector objects; 46 | std::vector > lights; 47 | 48 | // Compute reflection direction 49 | Vector3f reflect(const Vector3f &I, const Vector3f &N) const 50 | { 51 | return I - 2 * dotProduct(I, N) * N; 52 | } 53 | 54 | 55 | 56 | // Compute refraction direction using Snell's law 57 | // 58 | // We need to handle with care the two possible situations: 59 | // 60 | // - When the ray is inside the object 61 | // 62 | // - When the ray is outside. 63 | // 64 | // If the ray is outside, you need to make cosi positive cosi = -N.I 65 | // 66 | // If the ray is inside, you need to invert the refractive indices and negate the normal N 67 | Vector3f refract(const Vector3f &I, const Vector3f &N, const float &ior) const 68 | { 69 | float cosi = clamp(-1, 1, dotProduct(I, N)); 70 | float etai = 1, etat = ior; 71 | Vector3f n = N; 72 | if (cosi < 0) { cosi = -cosi; } else { std::swap(etai, etat); n= -N; } 73 | float eta = etai / etat; 74 | float k = 1 - eta * eta * (1 - cosi * cosi); 75 | return k < 0 ? 0 : eta * I + (eta * cosi - sqrtf(k)) * n; 76 | } 77 | 78 | 79 | 80 | // Compute Fresnel equation 81 | // 82 | // \param I is the incident view direction 83 | // 84 | // \param N is the normal at the intersection point 85 | // 86 | // \param ior is the material refractive index 87 | // 88 | // \param[out] kr is the amount of light reflected 89 | void fresnel(const Vector3f &I, const Vector3f &N, const float &ior, float &kr) const 90 | { 91 | float cosi = clamp(-1, 1, dotProduct(I, N)); 92 | float etai = 1, etat = ior; 93 | if (cosi > 0) { std::swap(etai, etat); } 94 | // Compute sini using Snell's law 95 | float sint = etai / etat * sqrtf(std::max(0.f, 1 - cosi * cosi)); 96 | // Total internal reflection 97 | if (sint >= 1) { 98 | kr = 1; 99 | } 100 | else { 101 | float cost = sqrtf(std::max(0.f, 1 - sint * sint)); 102 | cosi = fabsf(cosi); 103 | float Rs = ((etat * cosi) - (etai * cost)) / ((etat * cosi) + (etai * cost)); 104 | float Rp = ((etai * cosi) - (etat * cost)) / ((etai * cosi) + (etat * cost)); 105 | kr = (Rs * Rs + Rp * Rp) / 2; 106 | } 107 | // As a consequence of the conservation of energy, transmittance is given by: 108 | // kt = 1 - kr; 109 | } 110 | }; -------------------------------------------------------------------------------- /src/Assignment7/Scene.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by Göksu Güvendiren on 2019-05-14. 3 | // 4 | 5 | #pragma once 6 | 7 | #include 8 | #include "Vector.hpp" 9 | #include "Object.hpp" 10 | #include "Light.hpp" 11 | #include "AreaLight.hpp" 12 | #include "BVH.hpp" 13 | #include "Ray.hpp" 14 | 15 | 16 | class Scene 17 | { 18 | public: 19 | // setting up options 20 | int width = 1280; 21 | int height = 960; 22 | double fov = 40; 23 | Vector3f backgroundColor = Vector3f(0.235294, 0.67451, 0.843137); 24 | int maxDepth = 1; 25 | float RussianRoulette = 0.8; 26 | 27 | Scene(int w, int h) : width(w), height(h) 28 | {} 29 | 30 | void Add(Object *object) { objects.push_back(object); } 31 | void Add(std::unique_ptr light) { lights.push_back(std::move(light)); } 32 | 33 | const std::vector& get_objects() const { return objects; } 34 | const std::vector >& get_lights() const { return lights; } 35 | Intersection intersect(const Ray& ray) const; 36 | BVHAccel *bvh; 37 | void buildBVH(); 38 | Vector3f castRay(const Ray &ray, int depth) const; 39 | void sampleLight(Intersection &pos, float &pdf) const; 40 | bool trace(const Ray &ray, const std::vector &objects, float &tNear, uint32_t &index, Object **hitObject); 41 | std::tuple HandleAreaLight(const AreaLight &light, const Vector3f &hitPoint, const Vector3f &N, 42 | const Vector3f &shadowPointOrig, 43 | const std::vector &objects, uint32_t &index, 44 | const Vector3f &dir, float specularExponent); 45 | 46 | // creating the scene (adding objects and lights) 47 | std::vector objects; 48 | std::vector > lights; 49 | 50 | // Compute reflection direction 51 | Vector3f reflect(const Vector3f &I, const Vector3f &N) const 52 | { 53 | return I - 2 * dotProduct(I, N) * N; 54 | } 55 | 56 | 57 | 58 | // Compute refraction direction using Snell's law 59 | // 60 | // We need to handle with care the two possible situations: 61 | // 62 | // - When the ray is inside the object 63 | // 64 | // - When the ray is outside. 65 | // 66 | // If the ray is outside, you need to make cosi positive cosi = -N.I 67 | // 68 | // If the ray is inside, you need to invert the refractive indices and negate the normal N 69 | Vector3f refract(const Vector3f &I, const Vector3f &N, const float &ior) const 70 | { 71 | float cosi = clamp(-1, 1, dotProduct(I, N)); 72 | float etai = 1, etat = ior; 73 | Vector3f n = N; 74 | if (cosi < 0) { cosi = -cosi; } else { std::swap(etai, etat); n= -N; } 75 | float eta = etai / etat; 76 | float k = 1 - eta * eta * (1 - cosi * cosi); 77 | return k < 0 ? 0 : eta * I + (eta * cosi - sqrtf(k)) * n; 78 | } 79 | 80 | 81 | 82 | // Compute Fresnel equation 83 | // 84 | // \param I is the incident view direction 85 | // 86 | // \param N is the normal at the intersection point 87 | // 88 | // \param ior is the material refractive index 89 | // 90 | // \param[out] kr is the amount of light reflected 91 | void fresnel(const Vector3f &I, const Vector3f &N, const float &ior, float &kr) const 92 | { 93 | float cosi = clamp(-1, 1, dotProduct(I, N)); 94 | float etai = 1, etat = ior; 95 | if (cosi > 0) { std::swap(etai, etat); } 96 | // Compute sini using Snell's law 97 | float sint = etai / etat * sqrtf(std::max(0.f, 1 - cosi * cosi)); 98 | // Total internal reflection 99 | if (sint >= 1) { 100 | kr = 1; 101 | } 102 | else { 103 | float cost = sqrtf(std::max(0.f, 1 - sint * sint)); 104 | cosi = fabsf(cosi); 105 | float Rs = ((etat * cosi) - (etai * cost)) / ((etat * cosi) + (etai * cost)); 106 | float Rp = ((etai * cosi) - (etat * cost)) / ((etai * cosi) + (etat * cost)); 107 | kr = (Rs * Rs + Rp * Rp) / 2; 108 | } 109 | // As a consequence of the conservation of energy, transmittance is given by: 110 | // kt = 1 - kr; 111 | } 112 | }; -------------------------------------------------------------------------------- /src/Assignment8/CGL/src/base64.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | base64.cpp and base64.h 3 | 4 | Copyright (C) 2004-2008 René Nyffenegger 5 | 6 | This source code is provided 'as-is', without any express or implied 7 | warranty. In no event will the author be held liable for any damages 8 | arising from the use of this software. 9 | 10 | Permission is granted to anyone to use this software for any purpose, 11 | including commercial applications, and to alter it and redistribute it 12 | freely, subject to the following restrictions: 13 | 14 | 1. The origin of this source code must not be misrepresented; you must not 15 | claim that you wrote the original source code. If you use this source code 16 | in a product, an acknowledgment in the product documentation would be 17 | appreciated but is not required. 18 | 19 | 2. Altered source versions must be plainly marked as such, and must not be 20 | misrepresented as being the original source code. 21 | 22 | 3. This notice may not be removed or altered from any source distribution. 23 | 24 | René Nyffenegger rene.nyffenegger@adp-gmbh.ch 25 | 26 | */ 27 | 28 | #include "base64.h" 29 | #include 30 | 31 | static const std::string base64_chars = 32 | "ABCDEFGHIJKLMNOPQRSTUVWXYZ" 33 | "abcdefghijklmnopqrstuvwxyz" 34 | "0123456789+/"; 35 | 36 | 37 | static inline bool is_base64(unsigned char c) { 38 | return (isalnum(c) || (c == '+') || (c == '/')); 39 | } 40 | 41 | std::string base64_encode(unsigned char const* bytes_to_encode, unsigned int in_len) { 42 | std::string ret; 43 | int i = 0; 44 | int j = 0; 45 | unsigned char char_array_3[3]; 46 | unsigned char char_array_4[4]; 47 | 48 | while (in_len--) { 49 | char_array_3[i++] = *(bytes_to_encode++); 50 | if (i == 3) { 51 | char_array_4[0] = (char_array_3[0] & 0xfc) >> 2; 52 | char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4); 53 | char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6); 54 | char_array_4[3] = char_array_3[2] & 0x3f; 55 | 56 | for(i = 0; (i <4) ; i++) 57 | ret += base64_chars[char_array_4[i]]; 58 | i = 0; 59 | } 60 | } 61 | 62 | if (i) 63 | { 64 | for(j = i; j < 3; j++) 65 | char_array_3[j] = '\0'; 66 | 67 | char_array_4[0] = (char_array_3[0] & 0xfc) >> 2; 68 | char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4); 69 | char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6); 70 | char_array_4[3] = char_array_3[2] & 0x3f; 71 | 72 | for (j = 0; (j < i + 1); j++) 73 | ret += base64_chars[char_array_4[j]]; 74 | 75 | while((i++ < 3)) 76 | ret += '='; 77 | 78 | } 79 | 80 | return ret; 81 | 82 | } 83 | 84 | std::string base64_decode(std::string const& encoded_string) { 85 | int in_len = encoded_string.size(); 86 | int i = 0; 87 | int j = 0; 88 | int in_ = 0; 89 | unsigned char char_array_4[4], char_array_3[3]; 90 | std::string ret; 91 | 92 | while (in_len-- && ( encoded_string[in_] != '=') && is_base64(encoded_string[in_])) { 93 | char_array_4[i++] = encoded_string[in_]; in_++; 94 | if (i ==4) { 95 | for (i = 0; i <4; i++) 96 | char_array_4[i] = base64_chars.find(char_array_4[i]); 97 | 98 | char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4); 99 | char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2); 100 | char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3]; 101 | 102 | for (i = 0; (i < 3); i++) 103 | ret += char_array_3[i]; 104 | i = 0; 105 | } 106 | } 107 | 108 | if (i) { 109 | for (j = i; j <4; j++) 110 | char_array_4[j] = 0; 111 | 112 | for (j = 0; j <4; j++) 113 | char_array_4[j] = base64_chars.find(char_array_4[j]); 114 | 115 | char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4); 116 | char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2); 117 | char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3]; 118 | 119 | for (j = 0; (j < i - 1); j++) ret += char_array_3[j]; 120 | } 121 | 122 | return ret; 123 | } 124 | -------------------------------------------------------------------------------- /src/Assignment7/BVH.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "BVH.hpp" 4 | 5 | BVHAccel::BVHAccel(std::vector p, int maxPrimsInNode, 6 | SplitMethod splitMethod) 7 | : maxPrimsInNode(std::min(255, maxPrimsInNode)), splitMethod(splitMethod), 8 | primitives(std::move(p)) 9 | { 10 | time_t start, stop; 11 | time(&start); 12 | if (primitives.empty()) 13 | return; 14 | 15 | root = recursiveBuild(primitives); 16 | 17 | time(&stop); 18 | double diff = difftime(stop, start); 19 | int hrs = (int)diff / 3600; 20 | int mins = ((int)diff / 60) - (hrs * 60); 21 | int secs = (int)diff - (hrs * 3600) - (mins * 60); 22 | 23 | printf( 24 | "\rBVH Generation complete: \nTime Taken: %i hrs, %i mins, %i secs\n\n", 25 | hrs, mins, secs); 26 | } 27 | 28 | BVHBuildNode* BVHAccel::recursiveBuild(std::vector objects) 29 | { 30 | BVHBuildNode* node = new BVHBuildNode(); 31 | 32 | // Compute bounds of all primitives in BVH node 33 | Bounds3 bounds; 34 | for (int i = 0; i < objects.size(); ++i) 35 | bounds = Union(bounds, objects[i]->getBounds()); 36 | if (objects.size() == 1) { 37 | // Create leaf _BVHBuildNode_ 38 | node->bounds = objects[0]->getBounds(); 39 | node->object = objects[0]; 40 | node->left = nullptr; 41 | node->right = nullptr; 42 | node->area = objects[0]->getArea(); 43 | return node; 44 | } 45 | else if (objects.size() == 2) { 46 | node->left = recursiveBuild(std::vector{objects[0]}); 47 | node->right = recursiveBuild(std::vector{objects[1]}); 48 | 49 | node->bounds = Union(node->left->bounds, node->right->bounds); 50 | node->area = node->left->area + node->right->area; 51 | return node; 52 | } 53 | else { 54 | Bounds3 centroidBounds; 55 | for (int i = 0; i < objects.size(); ++i) 56 | centroidBounds = 57 | Union(centroidBounds, objects[i]->getBounds().Centroid()); 58 | int dim = centroidBounds.maxExtent(); 59 | switch (dim) { 60 | case 0: 61 | std::sort(objects.begin(), objects.end(), [](auto f1, auto f2) { 62 | return f1->getBounds().Centroid().x < 63 | f2->getBounds().Centroid().x; 64 | }); 65 | break; 66 | case 1: 67 | std::sort(objects.begin(), objects.end(), [](auto f1, auto f2) { 68 | return f1->getBounds().Centroid().y < 69 | f2->getBounds().Centroid().y; 70 | }); 71 | break; 72 | case 2: 73 | std::sort(objects.begin(), objects.end(), [](auto f1, auto f2) { 74 | return f1->getBounds().Centroid().z < 75 | f2->getBounds().Centroid().z; 76 | }); 77 | break; 78 | } 79 | 80 | auto beginning = objects.begin(); 81 | auto middling = objects.begin() + (objects.size() / 2); 82 | auto ending = objects.end(); 83 | 84 | auto leftshapes = std::vector(beginning, middling); 85 | auto rightshapes = std::vector(middling, ending); 86 | 87 | assert(objects.size() == (leftshapes.size() + rightshapes.size())); 88 | 89 | node->left = recursiveBuild(leftshapes); 90 | node->right = recursiveBuild(rightshapes); 91 | 92 | node->bounds = Union(node->left->bounds, node->right->bounds); 93 | node->area = node->left->area + node->right->area; 94 | } 95 | 96 | return node; 97 | } 98 | 99 | Intersection BVHAccel::Intersect(const Ray& ray) const 100 | { 101 | Intersection isect; 102 | if (!root) 103 | return isect; 104 | isect = BVHAccel::getIntersection(root, ray); 105 | return isect; 106 | } 107 | 108 | Intersection BVHAccel::getIntersection(BVHBuildNode* node, const Ray& ray) const 109 | { 110 | // TODO Traverse the BVH to find intersection 111 | return {}; 112 | } 113 | 114 | 115 | void BVHAccel::getSample(BVHBuildNode* node, float p, Intersection &pos, float &pdf){ 116 | if(node->left == nullptr || node->right == nullptr){ 117 | node->object->Sample(pos, pdf); 118 | pdf *= node->area; 119 | return; 120 | } 121 | if(p < node->left->area) getSample(node->left, p, pos, pdf); 122 | else getSample(node->right, p - node->left->area, pos, pdf); 123 | } 124 | 125 | void BVHAccel::Sample(Intersection &pos, float &pdf){ 126 | float p = std::sqrt(get_random_float()) * root->area; 127 | getSample(root, p, pos, pdf); 128 | pdf /= root->area; 129 | } -------------------------------------------------------------------------------- /src/Assignment8/CGL/src/vector4D.h: -------------------------------------------------------------------------------- 1 | #ifndef CGL_VECTOR4D_H 2 | #define CGL_VECTOR4D_H 3 | 4 | #include 5 | #include 6 | #include "vector3D.h" 7 | 8 | namespace CGL { 9 | 10 | /** 11 | * Defines 4D standard vectors. 12 | */ 13 | class Vector4D { 14 | public: 15 | 16 | // components 17 | double x, y, z, w; 18 | 19 | /** 20 | * Constructor. 21 | * Initializes tp vector (0,0,0, 0). 22 | */ 23 | Vector4D() : x( 0.0 ), y( 0.0 ), z( 0.0 ), w( 0.0 ) { } 24 | 25 | /** 26 | * Constructor. 27 | * Initializes to vector (x,y,z,w). 28 | */ 29 | Vector4D( double x, double y, double z, double w) : x( x ), y( y ), z( z ), w( w ) { } 30 | 31 | /** 32 | * Constructor. 33 | * Initializes to vector (x,y,z,0). 34 | */ 35 | Vector4D( double x, double y, double z) : x( x ), y( y ), z( z ), w( 0.0 ) { } 36 | 37 | 38 | /** 39 | * Constructor. 40 | * Initializes to vector (c,c,c,c) 41 | */ 42 | Vector4D( double c ) : x( c ), y( c ), z( c ), w ( c ) { } 43 | 44 | /** 45 | * Constructor. 46 | * Initializes from existing vector4D. 47 | */ 48 | Vector4D( const Vector4D& v ) : x( v.x ), y( v.y ), z( v.z ), w( v.w ) { } 49 | 50 | /** 51 | * Constructor. 52 | * Initializes from existing vector3D. 53 | */ 54 | Vector4D( const Vector3D& v ) : x( v.x ), y( v.y ), z( v.z ), w( 0.0 ) { } 55 | 56 | // returns reference to the specified component (0-based indexing: x, y, z) 57 | inline double& operator[] ( const int& index ) { 58 | return ( &x )[ index ]; 59 | } 60 | 61 | // returns const reference to the specified component (0-based indexing: x, y, z) 62 | inline const double& operator[] ( const int& index ) const { 63 | return ( &x )[ index ]; 64 | } 65 | 66 | // negation 67 | inline Vector4D operator-( void ) const { 68 | return Vector4D( -x, -y, -z, -w); 69 | } 70 | 71 | // addition 72 | inline Vector4D operator+( const Vector4D& v ) const { 73 | return Vector4D( x + v.x, y + v.y, z + v.z, w + v.w); 74 | } 75 | 76 | // subtraction 77 | inline Vector4D operator-( const Vector4D& v ) const { 78 | return Vector4D( x - v.x, y - v.y, z - v.z, w - v.w ); 79 | } 80 | 81 | // right scalar multiplication 82 | inline Vector4D operator*( const double& c ) const { 83 | return Vector4D( x * c, y * c, z * c, w * c ); 84 | } 85 | 86 | // scalar division 87 | inline Vector4D operator/( const double& c ) const { 88 | const double rc = 1.0/c; 89 | return Vector4D( rc * x, rc * y, rc * z, rc * w ); 90 | } 91 | 92 | // addition / assignment 93 | inline void operator+=( const Vector4D& v ) { 94 | x += v.x; y += v.y; z += v.z; z += v.w; 95 | } 96 | 97 | // subtraction / assignment 98 | inline void operator-=( const Vector4D& v ) { 99 | x -= v.x; y -= v.y; z -= v.z; w -= v.w; 100 | } 101 | 102 | // scalar multiplication / assignment 103 | inline void operator*=( const double& c ) { 104 | x *= c; y *= c; z *= c; z *= c; 105 | } 106 | 107 | // scalar division / assignment 108 | inline void operator/=( const double& c ) { 109 | (*this) *= ( 1./c ); 110 | } 111 | 112 | /** 113 | * Returns Euclidean distance metric extended to 4 dimensions. 114 | */ 115 | inline double norm( void ) const { 116 | return sqrt( x*x + y*y + z*z + w*w ); 117 | } 118 | 119 | /** 120 | * Returns Euclidean length squared. 121 | */ 122 | inline double norm2( void ) const { 123 | return x*x + y*y + z*z + w*w; 124 | } 125 | 126 | /** 127 | * Returns unit vector. (returns the normalized copy of this vector.) 128 | */ 129 | inline Vector4D unit( void ) const { 130 | double rNorm = 1. / sqrt( x*x + y*y + z*z + w*w); 131 | return Vector4D( rNorm*x, rNorm*y, rNorm*z ); 132 | } 133 | 134 | /** 135 | * Divides by Euclidean length. 136 | * This vector will be of unit length i.e. "normalized" afterwards. 137 | */ 138 | inline void normalize( void ) { 139 | (*this) /= norm(); 140 | } 141 | 142 | /** 143 | * Converts this vector to a 3D vector ignoring the w component. 144 | */ 145 | Vector3D to3D(); 146 | 147 | }; // class Vector4D 148 | 149 | // left scalar multiplication 150 | inline Vector4D operator* ( const double& c, const Vector4D& v ) { 151 | return Vector4D( c * v.x, c * v.y, c * v.z, c*v.w ); 152 | } 153 | 154 | // dot product (a.k.a. inner or scalar product) 155 | inline double dot( const Vector4D& u, const Vector4D& v ) { 156 | return u.x*v.x + u.y*v.y + u.z*v.z + u.w*v.w; 157 | } 158 | 159 | // prints components 160 | std::ostream& operator<<( std::ostream& os, const Vector3D& v ); 161 | 162 | } // namespace CGL 163 | 164 | #endif // CGL_VECTOR3D_H 165 | -------------------------------------------------------------------------------- /src/Assignment8/CGL/include/CGL/vector4D.h: -------------------------------------------------------------------------------- 1 | #ifndef CGL_VECTOR4D_H 2 | #define CGL_VECTOR4D_H 3 | 4 | #include 5 | #include 6 | #include "vector3D.h" 7 | 8 | namespace CGL { 9 | 10 | /** 11 | * Defines 4D standard vectors. 12 | */ 13 | class Vector4D { 14 | public: 15 | 16 | // components 17 | double x, y, z, w; 18 | 19 | /** 20 | * Constructor. 21 | * Initializes tp vector (0,0,0, 0). 22 | */ 23 | Vector4D() : x( 0.0 ), y( 0.0 ), z( 0.0 ), w( 0.0 ) { } 24 | 25 | /** 26 | * Constructor. 27 | * Initializes to vector (x,y,z,w). 28 | */ 29 | Vector4D( double x, double y, double z, double w) : x( x ), y( y ), z( z ), w( w ) { } 30 | 31 | /** 32 | * Constructor. 33 | * Initializes to vector (x,y,z,0). 34 | */ 35 | Vector4D( double x, double y, double z) : x( x ), y( y ), z( z ), w( 0.0 ) { } 36 | 37 | 38 | /** 39 | * Constructor. 40 | * Initializes to vector (c,c,c,c) 41 | */ 42 | Vector4D( double c ) : x( c ), y( c ), z( c ), w ( c ) { } 43 | 44 | /** 45 | * Constructor. 46 | * Initializes from existing vector4D. 47 | */ 48 | Vector4D( const Vector4D& v ) : x( v.x ), y( v.y ), z( v.z ), w( v.w ) { } 49 | 50 | /** 51 | * Constructor. 52 | * Initializes from existing vector3D. 53 | */ 54 | Vector4D( const Vector3D& v ) : x( v.x ), y( v.y ), z( v.z ), w( 0.0 ) { } 55 | 56 | // returns reference to the specified component (0-based indexing: x, y, z) 57 | inline double& operator[] ( const int& index ) { 58 | return ( &x )[ index ]; 59 | } 60 | 61 | // returns const reference to the specified component (0-based indexing: x, y, z) 62 | inline const double& operator[] ( const int& index ) const { 63 | return ( &x )[ index ]; 64 | } 65 | 66 | // negation 67 | inline Vector4D operator-( void ) const { 68 | return Vector4D( -x, -y, -z, -w); 69 | } 70 | 71 | // addition 72 | inline Vector4D operator+( const Vector4D& v ) const { 73 | return Vector4D( x + v.x, y + v.y, z + v.z, w + v.w); 74 | } 75 | 76 | // subtraction 77 | inline Vector4D operator-( const Vector4D& v ) const { 78 | return Vector4D( x - v.x, y - v.y, z - v.z, w - v.w ); 79 | } 80 | 81 | // right scalar multiplication 82 | inline Vector4D operator*( const double& c ) const { 83 | return Vector4D( x * c, y * c, z * c, w * c ); 84 | } 85 | 86 | // scalar division 87 | inline Vector4D operator/( const double& c ) const { 88 | const double rc = 1.0/c; 89 | return Vector4D( rc * x, rc * y, rc * z, rc * w ); 90 | } 91 | 92 | // addition / assignment 93 | inline void operator+=( const Vector4D& v ) { 94 | x += v.x; y += v.y; z += v.z; z += v.w; 95 | } 96 | 97 | // subtraction / assignment 98 | inline void operator-=( const Vector4D& v ) { 99 | x -= v.x; y -= v.y; z -= v.z; w -= v.w; 100 | } 101 | 102 | // scalar multiplication / assignment 103 | inline void operator*=( const double& c ) { 104 | x *= c; y *= c; z *= c; z *= c; 105 | } 106 | 107 | // scalar division / assignment 108 | inline void operator/=( const double& c ) { 109 | (*this) *= ( 1./c ); 110 | } 111 | 112 | /** 113 | * Returns Euclidean distance metric extended to 4 dimensions. 114 | */ 115 | inline double norm( void ) const { 116 | return sqrt( x*x + y*y + z*z + w*w ); 117 | } 118 | 119 | /** 120 | * Returns Euclidean length squared. 121 | */ 122 | inline double norm2( void ) const { 123 | return x*x + y*y + z*z + w*w; 124 | } 125 | 126 | /** 127 | * Returns unit vector. (returns the normalized copy of this vector.) 128 | */ 129 | inline Vector4D unit( void ) const { 130 | double rNorm = 1. / sqrt( x*x + y*y + z*z + w*w); 131 | return Vector4D( rNorm*x, rNorm*y, rNorm*z ); 132 | } 133 | 134 | /** 135 | * Divides by Euclidean length. 136 | * This vector will be of unit length i.e. "normalized" afterwards. 137 | */ 138 | inline void normalize( void ) { 139 | (*this) /= norm(); 140 | } 141 | 142 | /** 143 | * Converts this vector to a 3D vector ignoring the w component. 144 | */ 145 | Vector3D to3D(); 146 | 147 | }; // class Vector4D 148 | 149 | // left scalar multiplication 150 | inline Vector4D operator* ( const double& c, const Vector4D& v ) { 151 | return Vector4D( c * v.x, c * v.y, c * v.z, c*v.w ); 152 | } 153 | 154 | // dot product (a.k.a. inner or scalar product) 155 | inline double dot( const Vector4D& u, const Vector4D& v ) { 156 | return u.x*v.x + u.y*v.y + u.z*v.z + u.w*v.w;; 157 | } 158 | 159 | // prints components 160 | std::ostream& operator<<( std::ostream& os, const Vector3D& v ); 161 | 162 | } // namespace CGL 163 | 164 | #endif // CGL_VECTOR3D_H 165 | -------------------------------------------------------------------------------- /models/spot/spot_texture.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 21 | 23 | 25 | 27 | 29 | 31 | 33 | 35 | 37 | 39 | 42 | 44 | 46 | 48 | 51 | 54 | 56 | 58 | 60 | 61 | -------------------------------------------------------------------------------- /src/Assignment8/CGL/include/CGL/osdtext.h: -------------------------------------------------------------------------------- 1 | #ifndef CGL_TEXTOSD_H 2 | #define CGL_TEXTOSD_H 3 | 4 | #include 5 | #include 6 | 7 | #include "GL/glew.h" 8 | 9 | #include "color.h" 10 | 11 | // forward declare freetype stuff 12 | struct FT_LibraryRec_; 13 | typedef struct FT_LibraryRec_* FT_Library; 14 | struct FT_FaceRec_; 15 | typedef struct FT_FaceRec_* FT_Face; 16 | 17 | namespace CGL { 18 | 19 | // base64 encoded embeded font 20 | extern "C" char osdfont_base64[]; 21 | 22 | struct OSDLine { 23 | 24 | // UID of the line 25 | int id; 26 | 27 | // screen space anchor position 28 | float x, y; 29 | 30 | // line content 31 | std::string text; 32 | 33 | // font size 34 | size_t size; 35 | 36 | // font color 37 | Color color; 38 | 39 | }; 40 | 41 | /** 42 | * Provides an interface for text on-screen display. 43 | * Note that this requires GL_BLEND enabled to work. Do note this is a very 44 | * basic implementation and the cost of all operations increases linearly with 45 | * respect to the number of lines and the length of the lines. 46 | */ 47 | class OSDText { 48 | public: 49 | 50 | /** 51 | * Constructor. 52 | * Creates an empty text OSD. 53 | */ 54 | OSDText(); 55 | 56 | /** 57 | * Destructor. 58 | * Destroys the textOSD instance and free resources. 59 | */ 60 | ~OSDText(); 61 | 62 | /** 63 | * Initializes resources required for rendering text. 64 | * This will load a freetype font and compile shaders, etc. 65 | * \param if text is renderer on HDPI displays 66 | * \return 0 if successful, -1 on error. 67 | */ 68 | int init(bool use_hdpi); 69 | 70 | /** 71 | * Draw the text OSD. 72 | * Render all the lines. 73 | */ 74 | void render(); 75 | 76 | /** 77 | * Resize internal scales when context size has changed. 78 | * This should be called on a window resize, etc. 79 | */ 80 | void resize(size_t w, size_t h); 81 | 82 | /** 83 | * Add a line of text to the OSD. 84 | * This uses GL's screen space standard and maps the horizontal space 85 | * to [-1, 1] from left to right and the vertical space to [-1, 1] from 86 | * bottom to top. 87 | * \param x Horizontal coordinate of the anchor. 88 | * \param y Vertical coordinate of the anchor. 89 | * \param text The text to add. 90 | * \param size The font size of the text. 91 | * \param color The color of the text. 92 | * \return the line index if successfully added. A valid line index 93 | * is non-negative. If the line was not successfully added, 94 | * -1 is returned. 95 | */ 96 | int add_line(float x, float y, std::string text = "", 97 | size_t size = 16, Color color = Color::White); 98 | 99 | /** 100 | * Deletes a line. 101 | * If the given id is not valid, the call has no effect. 102 | * \param line_id Index of the line to be removed. 103 | */ 104 | void del_line(int line_id); 105 | 106 | /** 107 | * Set the anchor position of a given line. Lines are anchored at 108 | * the bottom left corner 109 | * If the given id is not valid, the call has no effect. 110 | * \param line_id Index of the line to set the text. 111 | * \param x The new x coordinate to set for the line. 112 | * \param y The new y coordinate to set for the line. 113 | */ 114 | void set_anchor(int line_id, float x, float y); 115 | 116 | /** 117 | * Set the text of a given line. 118 | * If the given id is not valid, the call has no effect. 119 | * \param line_id Index of the line to set the text. 120 | * \param text The new text to set for the line. 121 | */ 122 | void set_text(int line_id, std::string text); 123 | 124 | /** 125 | * Set the font size of a given line. 126 | * If the given id is not valid, the call has no effect. 127 | * \param line_id Index of the line to set the text. 128 | * \param size The new size to set for the line. 129 | */ 130 | void set_size(int line_id, size_t size); 131 | 132 | /** 133 | * Set the font color of a given line. 134 | * If the given id is not valid, the call has no effect. 135 | * \param line_id Index of the line to set the text. 136 | * \param color The new color to set for the line. 137 | */ 138 | void set_color(int line_id, Color color); 139 | 140 | private: 141 | 142 | // draw a single line 143 | void draw_line(OSDLine line); 144 | 145 | // HDPI displays 146 | bool use_hdpi; 147 | 148 | // internal scale factors 149 | float sx, sy; 150 | 151 | // line id counter 152 | int next_id; 153 | 154 | // freetype 155 | char* font; size_t font_size; 156 | FT_Library* ft; FT_Face* face; 157 | 158 | // lines to draw 159 | std::vector lines; 160 | 161 | // GL stuff 162 | GLuint vbo; 163 | GLuint program; 164 | GLint attribute_coord; 165 | GLint uniform_tex; 166 | GLint uniform_color; 167 | 168 | // GL helpers 169 | GLuint compile_shaders(); 170 | GLint get_attribu(GLuint program, const char *name); 171 | GLint get_uniform(GLuint program, const char *name); 172 | 173 | }; // class textOSD 174 | 175 | } // namespace CGL 176 | 177 | #endif // CGL_TEXTOSD_H 178 | --------------------------------------------------------------------------------