diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..8b1aab2b71fd0f02ff8bbd7c301118dcf4406d4f --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.idea +cmake-build-debug \ No newline at end of file diff --git a/Skeleton.cpp b/Skeleton.cpp index 6c51754341b727e6a58838be181fcac00f61a7fa..22b9db3ed4351cef9cb1fcf6f0a9e1ca5fab5354 100644 --- a/Skeleton.cpp +++ b/Skeleton.cpp @@ -58,12 +58,52 @@ struct Sphere : public Intersectable { } }; +struct Triangle : public Intersectable { + vec3 r1; + vec3 r2; + vec3 r3; + Material* material; + + Triangle(const vec3& _r1, const vec3& _r2, const vec3& _r3, Material* _material) { + r1 = _r1; + r2 = _r2; + r3 = _r3; + material = _material; + } + + Hit intersect(const Ray& ray) { + Hit hit; + auto n = cross(r2-r1, r3-r1); + n = normalize(n); + auto t = dot(r1 - ray.start,n)/dot(ray.dir, n); + if (t < 0) { + return hit; + } + + auto p = ray.start + t * ray.dir; + + if ( + dot(cross(r2-r1, p-r1), n) > 0 && + dot(cross(r3-r2, p-r2), n) > 0 && + dot(cross(r1-r3, p-r3), n) > 0 + ){ + hit.t = t; + hit.normal = n; + hit.material = material; + hit.position = p; + } + return hit; + } +}; + class Camera { vec3 eye, lookat, right, up; + float fov; public: - void set(vec3 _eye, vec3 _lookat, vec3 vup, float fov) { + void set(vec3 _eye, vec3 _lookat, vec3 vup, float _fov) { eye = _eye; lookat = _lookat; + fov = _fov; vec3 w = eye - lookat; float focus = length(w); right = normalize(cross(vup, w)) * focus * tanf(fov / 2); @@ -73,6 +113,11 @@ public: vec3 dir = lookat + right * (2.0f * (X + 0.5f) / windowWidth - 1) + up * (2.0f * (Y + 0.5f) / windowHeight - 1) - eye; return Ray(eye, dir); } + void Animate(float dt){ + vec3 d = eye - lookat; + eye = vec3(d.x * cos(dt) + d.z * sin(dt), d.y, -d.x * sin(dt) + d.z * cos(dt)) + lookat; + set(eye, lookat, up, fov); + } }; struct Light { @@ -105,8 +150,9 @@ public: vec3 kd(0.3f, 0.2f, 0.1f), ks(2, 2, 2); Material * material = new Material(kd, ks, 50); - for (int i = 0; i < 500; i++) - objects.push_back(new Sphere(vec3(rnd() - 0.5f, rnd() - 0.5f, rnd() - 0.5f), rnd() * 0.1f, material)); + + //objects.push_back(new Sphere(vec3(0, 0, 0), .25f, material)); + objects.push_back(new Triangle(vec3(0.1, 0, 0), vec3(0,0,.1), vec3(0, .1, .1), material)); } void render(std::vector<vec4>& image) { @@ -150,6 +196,10 @@ public: } return outRadiance; } + + void Animate(float dt) { + camera.Animate(dt); + } }; GPUProgram gpuProgram; // vertex and fragment shaders @@ -184,7 +234,7 @@ const char *fragmentSource = R"( )"; class FullScreenTexturedQuad { - unsigned int vao; // vertex array object id and texture id + unsigned int vao = 0; // vertex array object id and texture id Texture texture; public: FullScreenTexturedQuad(int windowWidth, int windowHeight, std::vector<vec4>& image)