1
Fork 0
raytracer/include/camera.h

31 lines
946 B
C
Raw Normal View History

2020-02-17 10:33:56 -05:00
#pragma once
#include "ray.h"
class Camera {
public:
Camera() : position(glm::vec3(0)), direction(glm::vec3(0)) {}
2022-08-16 07:41:12 -04:00
2020-02-17 10:33:56 -05:00
Camera(glm::vec3 position, glm::vec3 direction) : position(position), direction(direction) {}
2022-08-16 07:41:12 -04:00
2020-02-17 10:33:56 -05:00
void look_at(glm::vec3 eye, glm::vec3 target) {
position = eye;
direction = glm::normalize(target - eye);
}
2022-08-16 07:41:12 -04:00
2020-02-17 10:33:56 -05:00
Ray get_ray(const int32_t x, const int32_t y, const int32_t width, const int32_t height) const {
const glm::vec3 up = glm::vec3(0, 1, 0);
const glm::vec3 right = glm::normalize(glm::cross(direction, up));
2022-08-16 07:41:12 -04:00
2020-02-17 10:33:56 -05:00
const float h2 = height / 2.0f;
const float w2 = width / 2.0f;
2022-08-16 07:41:12 -04:00
const glm::vec3 ray_dir = position + (h2 / tan(glm::radians(fov) / 2)) * direction + (y - h2) * up +
static_cast<float>(x - w2) * right;
2020-02-17 10:33:56 -05:00
return Ray(position, ray_dir);
}
2022-08-16 07:41:12 -04:00
2020-02-17 10:33:56 -05:00
glm::vec3 position, direction;
float fov = 45.0f;
};