Camera and rays
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
@@ -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 {};
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
#include "scene.hpp"
|
||||
|
||||
Scene::Scene(int width, int height) {
|
||||
w = width;
|
||||
h = height;
|
||||
}
|
||||
|
||||
@@ -8,6 +8,8 @@ class Primative;
|
||||
|
||||
class Scene {
|
||||
public:
|
||||
Scene(int width, int height);
|
||||
int w, h;
|
||||
Camera* camera;
|
||||
std::vector<Primative*> objects;
|
||||
};
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
#include "inferno.hpp"
|
||||
|
||||
#include <iostream>
|
||||
#include <SDL2/SDL.h>
|
||||
#include "pixel.hpp"
|
||||
#include "common.hpp"
|
||||
|
||||
#include "display/display.hpp"
|
||||
#include "core/renderer.hpp"
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user