From 3bb08e48ca3b9906aea20df6c65fb26e56bbba91 Mon Sep 17 00:00:00 2001 From: Ben Kyd Date: Tue, 30 Jul 2019 01:23:45 +0100 Subject: [PATCH] Camera and rays --- src/definitions/camera.cpp | 18 +++++++++++++----- src/definitions/ray.cpp | 15 +++++++++++---- src/definitions/ray.hpp | 2 +- src/definitions/scene.cpp | 6 ++++++ src/definitions/scene.hpp | 2 ++ src/engine/progressiverenderer.cpp | 14 ++++++++------ src/inferno.cpp | 4 +--- test/main.cpp | 2 +- 8 files changed, 43 insertions(+), 20 deletions(-) diff --git a/src/definitions/camera.cpp b/src/definitions/camera.cpp index 8ec6ef9..8c36f39 100644 --- a/src/definitions/camera.cpp +++ b/src/definitions/camera.cpp @@ -7,15 +7,23 @@ Camera::Camera(glm::vec3 point, glm::vec3 dir, float w, float h, float f) { } void Camera::Update() { - // Vector3 x_c = c->position; + glm::vec3 up = {0.0f, -1.0f, 0.0f}; + glm::vec3 c0 = point + (direction * focalLen); + glm::vec3 uX = glm::normalize(glm::cross(direction, up)); + glm::vec3 uY = glm::cross((direction * -1.0f), uX); + planeCenter = c0; + planeDirX = uX; + planeDirY = uY; + + // Vector3 x_c = c->position; // Vector3 u_c = c->direction; // double z_p = c->focalLength; // Vector3 v_up = vec3_make(0.0, -1.0, 0.0); - // Vector3 c_0 = vec3_add(x_c, vec3_mult(u_c, z_p)); - // Vector3 u_x = vec3_unit(vec3_cross(u_c, v_up)); - // Vector3 u_y = vec3_cross(vec3_mult(u_c, -1.0), u_x); + // Vector3 c_0 = vec3_add(position, vec3_mult(direction, focalLength)); + // Vector3 u_x = vec3_unit(vec3_cross(direction, v_up)); + // Vector3 u_y = vec3_cross(vec3_mult(direction, -1.0), u_x); // c->planeCenter = c_0; // c->planeDirectionX = u_x; // c->planeDirectionY = u_y; - // // Vector3 u_z = vec3_mult(u_c, -1.0); // Normal to the view plane + // Vector3 u_z = vec3_mult(direction, -1.0); // Normal to the view plane } diff --git a/src/definitions/ray.cpp b/src/definitions/ray.cpp index 218afab..75f8675 100644 --- a/src/definitions/ray.cpp +++ b/src/definitions/ray.cpp @@ -3,15 +3,22 @@ #include "camera.hpp" #include "scene.hpp" -Ray GeneratePrimaryRay(int x, int y, Scene& scene, float xSubPix, float ySubPix) { - // double dy = 1.0; +Ray GeneratePrimaryRay(int x, int y, Scene* scene, float xSubPix, float ySubPix) { + Camera* cam = scene->camera; + float dy = 1.0f; + float dx = 1.0f; + float px = (-(float)cam->w / 2.0f) + dx * ((float)x + xSubPix); + float py = (-(float)cam->h / 2.0f) + dy * ((float)y + ySubPix); + glm::vec3 p = cam->planeCenter + (cam->planeDirX * px) + (cam->planeDirY * py); + glm::vec3 dir = glm::normalize(p - cam->point); + return {cam->point, dir}; + // double dy = 1.0; // double dx = 1.0; - // double py = (- c->height / 2.0) + dy * ((double)y + 0.5); // double px = (- c->width / 2.0) + dx * ((double)x + 0.5); + // double py = (- c->height / 2.0) + dy * ((double)y + 0.5); // Vector3 p = vec3_add3(c->planeCenter, // vec3_mult(c->planeDirectionX, px), // vec3_mult(c->planeDirectionY, py)); // Vector3 u_r = vec3_unit(vec3_sub(p, c->position)); // return ray_make(c->position, u_r); - return {}; } diff --git a/src/definitions/ray.hpp b/src/definitions/ray.hpp index 734dcea..f75007b 100644 --- a/src/definitions/ray.hpp +++ b/src/definitions/ray.hpp @@ -11,6 +11,6 @@ public: glm::vec3 direction; }; -Ray GeneratePrimaryRay(int x, int y, Scene& scene, float xSubPix = 0.5f, float ySubPix = 0.5f); +Ray GeneratePrimaryRay(int x, int y, Scene* scene, float xSubPix = 0.5f, float ySubPix = 0.5f); #endif diff --git a/src/definitions/scene.cpp b/src/definitions/scene.cpp index e69de29..604307b 100644 --- a/src/definitions/scene.cpp +++ b/src/definitions/scene.cpp @@ -0,0 +1,6 @@ +#include "scene.hpp" + +Scene::Scene(int width, int height) { + w = width; + h = height; +} diff --git a/src/definitions/scene.hpp b/src/definitions/scene.hpp index 0b60dfd..de91c50 100644 --- a/src/definitions/scene.hpp +++ b/src/definitions/scene.hpp @@ -8,6 +8,8 @@ class Primative; class Scene { public: + Scene(int width, int height); + int w, h; Camera* camera; std::vector objects; }; diff --git a/src/engine/progressiverenderer.cpp b/src/engine/progressiverenderer.cpp index 558a400..54cdd26 100644 --- a/src/engine/progressiverenderer.cpp +++ b/src/engine/progressiverenderer.cpp @@ -20,16 +20,18 @@ void ProgressiveRenderer::Render() { // RENDERING CAN ACTUALLY START while (m_interface->Active) { + + // Take input SDL_Event e; while (SDL_PollEvent(&e) == SDL_TRUE) if (e.type == SDL_QUIT) m_interface->Close(); - - for (int i = 0; i < 360000; i++) { - m_interface->SetPixelSafe(rand() % m_interface->XRes, - rand() % m_interface->YRes, - rgb888(rand() % 255, rand() % 255, rand() % 255)); - } + // Update the camera + m_scene->camera->Update(); + + + + // Swap framebuffers m_interface->Update(); } } diff --git a/src/inferno.cpp b/src/inferno.cpp index 2e3d3e4..24c9967 100644 --- a/src/inferno.cpp +++ b/src/inferno.cpp @@ -1,8 +1,6 @@ #include "inferno.hpp" -#include -#include -#include "pixel.hpp" +#include "common.hpp" #include "display/display.hpp" #include "core/renderer.hpp" diff --git a/test/main.cpp b/test/main.cpp index 146f088..3e081df 100644 --- a/test/main.cpp +++ b/test/main.cpp @@ -11,7 +11,7 @@ int main(int argc, char** argv) { std::cout << "Error initializing window: " << inferno.LastError() << std::endl; } - Scene* scene = new Scene(); + Scene* scene = new Scene(600, 600); scene->camera = new Camera({0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 1.0f}, 600, 600); inferno.SetScene(scene);