add ray constructor

This commit is contained in:
Shuo Feng 2024-02-19 14:18:22 -05:00
parent 29c5fe91d9
commit e4e2d26a16
Signed by: sfeng
GPG key ID: 1E83AE6CD1C037B1
2 changed files with 23 additions and 2 deletions

View file

@ -4,6 +4,13 @@ void Geometry::setTransform(const Matrix4f &transform) {
this->transform = transform;
}
bool Sphere::intersect(const Ray &r) const { return false; }
bool Sphere::intersect(const Ray &r) const {
Vector3f originCenter = r.getOrigin() - center;
int a = r.getDirection().dot(r.getDirection());
int b = 2 * originCenter.dot(r.getDirection());
int c = originCenter.dot(originCenter) - radius * radius;
return b * b - 4 * a * c >= 0;
}
bool Rectangle::intersect(const Ray &r) const { return false; }

View file

@ -2,6 +2,7 @@
#include "../external/simpleppm.h"
#include "Parser.h"
#include "Ray.h"
#include <Eigen/src/Core/Matrix.h>
void RayTracer::parse() {
for (auto i = json["output"].begin(); i != json["output"].end(); ++i)
@ -17,7 +18,20 @@ void RayTracer::parse() {
Ray getRay(int x, int y, const Vector3f &camPos, const Vector3f &lookat,
float fov, int width, int height) {
// TODO: compute ray
return Ray(Vector3f(), Vector3f());
float viewportHeight = 2.0f;
float viewportWidth = viewportHeight * width / height;
Vector3f viewportU = Vector3f(viewportWidth, 0, 0);
Vector3f viewportV = Vector3f(0, -viewportHeight, 0);
Vector3f deltaU = viewportU / width;
Vector3f deltaV = viewportV / height;
Vector3f viewportUpperLeft = camPos - lookat - viewportU / 2 - viewportV / 2;
Vector3f pixelUpperLeftPos = viewportUpperLeft + 0.5 * (deltaU + deltaV);
Vector3f pixelCenter = pixelUpperLeftPos + (x * deltaU) + (y * deltaV);
return Ray(camPos, pixelCenter - camPos);
}
void RayTracer::render(Scene *scene) {