diff options
author | Fate Bussey <lafayette_bussey@brown.edu> | 2023-12-13 19:41:38 -0500 |
---|---|---|
committer | Fate Bussey <lafayette_bussey@brown.edu> | 2023-12-13 19:41:38 -0500 |
commit | 015884f1bfe701871b3227fc69598d5d83ab4f5b (patch) | |
tree | 30efd18d33a0e3dff4bb67b54f113c5f1527059e /src | |
parent | 5de0f3b6de57aa0cdc6b1aa7efc57836a99ee59a (diff) | |
parent | 6e27cd596611758bf82f58cff25ad6310bb5ad6e (diff) |
new cyl
Diffstat (limited to 'src')
-rw-r--r-- | src/camera/camera.cpp | 38 | ||||
-rw-r--r-- | src/camera/camera.h | 3 | ||||
-rw-r--r-- | src/intersect/intersect.cpp | 38 | ||||
-rw-r--r-- | src/mainwindow.cpp | 38 | ||||
-rw-r--r-- | src/physics/physics.cpp | 72 | ||||
-rw-r--r-- | src/physics/physics.h | 12 | ||||
-rw-r--r-- | src/raytracer/raytracer.cpp | 6 | ||||
-rw-r--r-- | src/raytracer/raytracescene.cpp | 2 | ||||
-rw-r--r-- | src/raytracer/raytracescene.h | 4 | ||||
-rw-r--r-- | src/vec4ops/rotations4d.cpp | 43 | ||||
-rw-r--r-- | src/vec4ops/vec4ops.h | 3 |
11 files changed, 158 insertions, 101 deletions
diff --git a/src/camera/camera.cpp b/src/camera/camera.cpp index 704be7e..69f2787 100644 --- a/src/camera/camera.cpp +++ b/src/camera/camera.cpp @@ -5,28 +5,34 @@ #include <glm/glm.hpp> #include <glm/gtc/matrix_transform.hpp> // Include this header for glm::rotate +void Camera::updateViewMatrix(SceneCameraData cameraData) { + m_viewMatrix = Vec4Ops::getViewMatrix4(cameraData.look, cameraData.up, cameraData.over); + m_viewMatrix = glm::rotate(m_viewMatrix, glm::radians(settings.xy), glm::vec3(0.f, 1.f, 0.f)); + m_viewMatrix = glm::rotate(m_viewMatrix, glm::radians(settings.yz), glm::vec3(1.f, 0.f, 0.f)); + m_viewMatrix = glm::rotate(m_viewMatrix, glm::radians(settings.xz), glm::vec3(0.f, 0.f, 1.f)); + + + // TODO: link sliders here. make them smaller changes since our objects are so small + glm::vec4 uiTranslation = glm::vec4(settings.xw, settings.yw, 0.f, settings.zw); + m_translationVector = -cameraData.pos + uiTranslation; + + m_inverseViewMatrix = glm::inverse(m_viewMatrix); + m_inverseTranslationVector = -m_translationVector; + m_controlPoints = { + {cameraData.pos[0], cameraData.pos[1], cameraData.pos[2]}, + {cameraData.pos[0], cameraData.pos[1] - 2.f, cameraData.pos[2] - 2.f}, + {cameraData.pos[0] + 2.f, cameraData.pos[1] + 2.f, cameraData.pos[2] -2.f}, + {cameraData.pos[0] + 2.f, cameraData.pos[1], cameraData.pos[2]} + }; +} + Camera::Camera(SceneCameraData cameraData) : m_pos(cameraData.pos), m_heightAngle(cameraData.heightAngle), m_focalLength(cameraData.focalLength), m_aperture(cameraData.aperture) { - m_viewMatrix = Vec4Ops::getViewMatrix4(cameraData.look, cameraData.up, cameraData.over); - // add settings.xy rotation - m_viewMatrix = glm::rotate(m_viewMatrix, glm::radians(settings.xy), glm::vec3(0.f, 1.f, 0.f)); - m_viewMatrix = glm::rotate(m_viewMatrix, glm::radians(settings.yz), glm::vec3(1.f, 0.f, 0.f)); - m_viewMatrix = glm::rotate(m_viewMatrix, glm::radians(settings.xz), glm::vec3(0.f, 0.f, 1.f)); - - m_translationVector = -cameraData.pos; - - m_inverseViewMatrix = glm::inverse(m_viewMatrix); - m_inverseTranslationVector = -m_translationVector; - m_controlPoints = { - {cameraData.pos[0], cameraData.pos[1], cameraData.pos[2]}, - {cameraData.pos[0], cameraData.pos[1] - 2.f, cameraData.pos[2] - 2.f}, - {cameraData.pos[0] + 2.f, cameraData.pos[1] + 2.f, cameraData.pos[2] -2.f}, - {cameraData.pos[0] + 2.f, cameraData.pos[1], cameraData.pos[2]} - }; + updateViewMatrix(cameraData); } glm::mat4 Camera::getViewMatrix() const { diff --git a/src/camera/camera.h b/src/camera/camera.h index 08750fb..37314c5 100644 --- a/src/camera/camera.h +++ b/src/camera/camera.h @@ -40,6 +40,8 @@ public: float cameraDepth = -1.f; std::vector<glm::vec3> m_controlPoints; + void updateViewMatrix(SceneCameraData cameraData); + private: glm::mat4 m_viewMatrix{}; glm::mat4 m_inverseViewMatrix{}; @@ -51,5 +53,6 @@ private: glm::vec4 m_translationVector{}; glm::vec4 m_inverseTranslationVector{}; + }; diff --git a/src/intersect/intersect.cpp b/src/intersect/intersect.cpp index d5ab015..05a4d64 100644 --- a/src/intersect/intersect.cpp +++ b/src/intersect/intersect.cpp @@ -59,7 +59,7 @@ glm::vec4 intersectCone( isHit = false; float t = FINF; // updated to 4d - // x^2 + z^2 - y^2 - w^2= 0, conic top + // x^2 + z^2 - y^2 - w^2= r^2, conic top float radius = 0.5f; float a = d.x*d.x + d.z*d.z - .25f*(d.y*d.y) - .25f*(d[3]*d[3]); float b = 2.f*(p.x*d.x + p.z*d.z) - .5f*(p.y*d.y) + .25f*d.y - .5f*(p[3]*d[3]) + .25f*d[3]; @@ -104,12 +104,12 @@ glm::vec4 intersectCone( t = std::min(t, twBase); } - // x^2 + y^2 - z^2 = 0, base w.r.t. y axis + // x^2 + w^2 - z^2 = 0, base w.r.t. y axis float tyBase = (- .5f - p.y) / d.y; auto pyBase = p + tyBase * d; if ( tyBase > 0 && - pyBase.x*pyBase.x + pyBase.z*pyBase.z <= pyBase[3]*pyBase[3] +.25f && + pyBase.x*pyBase.x + pyBase.z*pyBase.z <= pyBase[3]*pyBase[3] + .25f && pyBase[3] >= -.5f && pyBase[3] <= .5f ) { @@ -134,11 +134,11 @@ glm::vec4 intersectCylinder( isHit = false; float t = FINF; - // implicit: x^2 + z^2 = r^2, y + w between -.5, .5 rectuangular side + // implicit: x^2 + z^2 - w/2 = r^2, y + w between -.5, .5 float radius = 0.5f; float a = d.x*d.x + d.z*d.z; - float b = 2.f * (p.x*d.x + p.z*d.z); - float c = p.x*p.x + p.z*p.z - radius*radius; + float b = 2.f * (p.x*d.x + p.z*d.z) - .5f * d[3]; + float c = p.x*p.x + p.z*p.z - .5f * p[3] - radius*radius; float discriminant = b*b - 4*a*c; if (discriminant >= 0) @@ -149,9 +149,9 @@ glm::vec4 intersectCylinder( auto p1Top = p + t1 * d; if ( t1 > 0 && - p1Top.y + p1Top[3] >= -.5f && p1Top.y + p1Top[3] <= .5f && - p1Top.y >= -.5f && p1Top.y <= .5f && - p1Top[3] >= -.5f && p1Top[3] <= .5f) + p1Top.y + p1Top[3] >= -.5f && p1Top.y - p1Top[3] <= .5f && + p1Top[3] >= -.5f && p1Top[3] <= .5f + ) { t = std::min(t1, t); } @@ -159,8 +159,7 @@ glm::vec4 intersectCylinder( auto p2Top = p + t2 * d; if ( t2 > 0 && - p2Top.y + p2Top[3] >= -.5f && p2Top.y + p2Top[3] <= .5f && - p2Top.y >= -.5f && p2Top.y <= .5f && + p2Top.y + p2Top[3] >= -.5f && p2Top.y - p2Top[3] <= .5f && p2Top[3] >= -.5f && p2Top[3] <= .5f) { t = std::min(t2, t); @@ -168,28 +167,27 @@ glm::vec4 intersectCylinder( } - // implicit y + w = .5f, top base - float tTop = (.5f - p.y - p[3]) / (d[3] + d.y); + // implicit y - w = .5f, top base + + float tTop = (.5f - p.y + p[3]) / (d.y - d[3]); auto pTop = p + tTop * d; if ( tTop > 0 && - pTop.x*pTop.x + pTop.z*pTop.z <= radius*radius && - pTop.y >= -.5f && pTop.y <= .5f //&& -// pTop[3] >= -.5f && pTop[3] <= .5f + pTop.x*pTop.x + pTop.z*pTop.z <= radius*radius + pTop[3]/2.f && + pTop[3] >= -.5f && pTop[3] <= .5f ) { t = std::min(t, tTop); } - // implicit p_y + t*d_y = -.5f, Bottom base + // implicit y + w = -.5f, Bottom base float tBase = (-.5f - p.y - p[3]) / (d[3] + d.y); auto pBase = p + tBase * d; if ( tBase > 0 && - pBase.x*pBase.x + pBase.z*pBase.z <= radius*radius && - pBase.y >= -.5f && pBase.y <= .5f //&& -// pBase[3] >= -.5f && pBase[3] <= .5f + pBase.x*pBase.x + pBase.z*pBase.z <= radius*radius + pBase[3]/2.f && + pBase[3] >= -.5f && pBase[3] <= .5f ) { t = std::min(t, tBase); diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 334d6ae..ac65c1f 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -410,84 +410,84 @@ void MainWindow::onValChangexySlider(int newValue) { //wSlider->setValue(newValue); xyBox->setValue(newValue/100.f); settings.xy = xyBox->value(); - // rayTracer->settingsChanged(imageLabel); + rayTracer->settingsChanged(imageLabel); } void MainWindow::onValChangexyBox(double newValue) { xySlider->setValue(int(newValue*100.f)); //wBox->setValue(newValue); settings.xy = xyBox->value(); - // rayTracer->settingsChanged(imageLabel); + rayTracer->settingsChanged(imageLabel); } void MainWindow::onValChangexzSlider(int newValue) { //wSlider->setValue(newValue); xzBox->setValue(newValue/100.f); settings.xz = xzBox->value(); - // rayTracer->settingsChanged(imageLabel); + rayTracer->settingsChanged(imageLabel); } void MainWindow::onValChangexzBox(double newValue) { xzSlider->setValue(int(newValue*100.f)); //wBox->setValue(newValue); settings.xz = xzBox->value(); - // rayTracer->settingsChanged(imageLabel); + rayTracer->settingsChanged(imageLabel); } void MainWindow::onValChangexwSlider(int newValue) { //wSlider->setValue(newValue); xwBox->setValue(newValue/100.f); settings.xw = xwBox->value(); - // rayTracer->settingsChanged(imageLabel); + rayTracer->settingsChanged(imageLabel); } void MainWindow::onValChangexwBox(double newValue) { xwSlider->setValue(int(newValue*100.f)); //wBox->setValue(newValue); settings.xw = xwBox->value(); - // rayTracer->settingsChanged(imageLabel); + rayTracer->settingsChanged(imageLabel); } void MainWindow::onValChangeyzSlider(int newValue) { //wSlider->setValue(newValue); yzBox->setValue(newValue/100.f); settings.yz = yzBox->value(); - // rayTracer->settingsChanged(imageLabel); + rayTracer->settingsChanged(imageLabel); } void MainWindow::onValChangeyzBox(double newValue) { yzSlider->setValue(int(newValue*100.f)); //wBox->setValue(newValue); settings.yz = yzBox->value(); - // rayTracer->settingsChanged(imageLabel); + rayTracer->settingsChanged(imageLabel); } void MainWindow::onValChangeywSlider(int newValue) { //wSlider->setValue(newValue); ywBox->setValue(newValue/100.f); settings.yw = ywBox->value(); - // rayTracer->settingsChanged(imageLabel); + rayTracer->settingsChanged(imageLabel); } void MainWindow::onValChangeywBox(double newValue) { ywSlider->setValue(int(newValue*100.f)); //wBox->setValue(newValue); settings.yw = ywBox->value(); - // rayTracer->settingsChanged(imageLabel); + rayTracer->settingsChanged(imageLabel); } void MainWindow::onValChangezwSlider(int newValue) { //wSlider->setValue(newValue); zwBox->setValue(newValue/100.f); settings.zw = zwBox->value(); - // rayTracer->settingsChanged(imageLabel); + rayTracer->settingsChanged(imageLabel); } void MainWindow::onValChangezwBox(double newValue) { zwSlider->setValue(int(newValue*100.f)); //wBox->setValue(newValue); settings.zw = zwBox->value(); - // rayTracer->settingsChanged(imageLabel); + rayTracer->settingsChanged(imageLabel); } void MainWindow::onValChangeMaxTimeSlider(int newValue) { @@ -509,7 +509,6 @@ void MainWindow::onValChangeWSlider(int newValue) { wBox->setValue(newValue/100.f); settings.w = wBox->value(); rayTracer->settingsChanged(imageLabel); - // ray->wSliderChanged(imageLabel); } void MainWindow::onValChangeWBox(double newValue) { @@ -517,7 +516,6 @@ void MainWindow::onValChangeWBox(double newValue) { //wBox->setValue(newValue); settings.w = wBox->value(); rayTracer->settingsChanged(imageLabel); - // ray->wSliderChanged(imageLabel); } void MainWindow::onRotateNegative() { @@ -528,37 +526,37 @@ void MainWindow::onRotateNegative() { void MainWindow::updateXySlider(double value) { xySlider->setValue(int(value*100.f)); xyBox->setValue(value); - // rayTracer->settingsChanged(imageLabel); + rayTracer->settingsChanged(imageLabel); } void MainWindow::updateXzSlider(double value) { xzSlider->setValue(int(value*100.f)); xzBox->setValue(value); - // rayTracer->settingsChanged(imageLabel); + rayTracer->settingsChanged(imageLabel); } void MainWindow::updateXwSlider(double value) { xwSlider->setValue(int(value*100.f)); xwBox->setValue(value); - // rayTracer->settingsChanged(imageLabel); + rayTracer->settingsChanged(imageLabel); } void MainWindow::updateYzSlider(double value) { yzSlider->setValue(int(value*100.f)); yzBox->setValue(value); - // rayTracer->settingsChanged(imageLabel); + rayTracer->settingsChanged(imageLabel); } void MainWindow::updateYwSlider(double value) { ywSlider->setValue(int(value*100.f)); ywBox->setValue(value); - // rayTracer->settingsChanged(imageLabel); + rayTracer->settingsChanged(imageLabel); } void MainWindow::updateZwSlider(double value) { zwSlider->setValue(int(value*100.f)); zwBox->setValue(value); - // rayTracer->settingsChanged(imageLabel); + rayTracer->settingsChanged(imageLabel); } void MainWindow::updateRotationSlider(float value) { diff --git a/src/physics/physics.cpp b/src/physics/physics.cpp index 111c25a..31b2cc7 100644 --- a/src/physics/physics.cpp +++ b/src/physics/physics.cpp @@ -4,19 +4,19 @@ #include <iostream> #include "physics.h" -bool sphereCube(RenderShapeData &sphere, RenderShapeData &cube) +bool sphereCube(RenderShapeData *sphere, RenderShapeData *cube) { // get center of cube - glm::vec4 cubeCenter = cube.translation4d; + glm::vec4 cubeCenter = cube->translation4d; // get the width, height, depth, & yawl of cube's box - glm::vec4 cubeScales = glm::vec4(cube.ctm[0][0], cube.ctm[1][1], cube.ctm[2][2], cube.ctm[3][3]); + glm::vec4 cubeScales = glm::vec4(cube->ctm[0][0], cube->ctm[1][1], cube->ctm[2][2], cube->ctm[3][3]); // note: assumption that cube is axis aligned glm::vec4 maxes = cubeCenter + cubeScales / 2.f; glm::vec4 mins = cubeCenter - cubeScales / 2.f; // get the center of sphere - glm::vec4 sphereCenter = sphere.translation4d; + glm::vec4 sphereCenter = sphere->translation4d; // get radius of sphere // note: assumption that sphere is not scaled (TODO: make radius adaptive) float radius = .5f; @@ -35,26 +35,26 @@ bool sphereCube(RenderShapeData &sphere, RenderShapeData &cube) if (distSquared > 0) { std::cout << "distanceSquared: " << distSquared << std::endl; - // update velocity of the objects, based on math, assuming the objects are the same mass - sphere.velocity *= -1.f; - cube.velocity *= -1.f; + // update velocity of the objects + sphere->velocity *= -1.f; + cube->velocity *= -1.f; // move the objects in new dir so they don't collide again - sphere.translation4d += sphere.velocity; - cube.translation4d += cube.velocity; + sphere->translation4d += sphere->velocity * (1.1f); + cube->translation4d += cube->velocity * (1.1f); } return distSquared > 0; } -bool cubeCube(RenderShapeData &c1, RenderShapeData &c2) { +bool cubeCube(RenderShapeData *c1, RenderShapeData *c2) { // get the width, height, depth, & yawl of cubes boxes - glm::vec4 cube1Scales = glm::vec4(c1.ctm[0][0], c1.ctm[1][1], c1.ctm[2][2], c1.ctm[3][3]); - glm::vec4 cube2Scales = glm::vec4(c2.ctm[0][0], c2.ctm[1][1], c2.ctm[2][2], c2.ctm[3][3]); + glm::vec4 cube1Scales = glm::vec4(c1->ctm[0][0], c1->ctm[1][1], c1->ctm[2][2], c1->ctm[3][3]); + glm::vec4 cube2Scales = glm::vec4(c2->ctm[0][0], c2->ctm[1][1], c2->ctm[2][2], c2->ctm[3][3]); // get the center of cubes - glm::vec4 cube1Center = c1.translation4d; - glm::vec4 cube2Center = c2.translation4d; + glm::vec4 cube1Center = c1->translation4d; + glm::vec4 cube2Center = c2->translation4d; // note: assumption that cube is axis aligned glm::vec4 cube1Maxes = cube1Center + cube1Scales / 2.f; @@ -77,21 +77,21 @@ bool cubeCube(RenderShapeData &c1, RenderShapeData &c2) { std::cout << "intersect: " << intersect << std::endl; // update velocity of the objects, based on math, assuming the objects are the same mass - c1.velocity *= -1.f; - c1.velocity *= -1.f; + c1->velocity *= -1.f; + c2->velocity *= -1.f; // move the objects in new dir so they don't collide again - c1.translation4d += c2.velocity; - c1.translation4d += c2.velocity; + c1->translation4d += c1->velocity * (1.1f); + c2->translation4d += c2->velocity * (1.1f); } return intersect; } -bool sphereSphere(RenderShapeData &s1, RenderShapeData &s2) +bool sphereSphere(RenderShapeData *s1, RenderShapeData *s2) { - glm::vec4 currentCenter = s1.translation4d; - glm::vec4 shapeCenter = s2.translation4d; + glm::vec4 currentCenter = s1->translation4d; + glm::vec4 shapeCenter = s2->translation4d; // define a radius vector float radius = .5; float distance = glm::distance(currentCenter, shapeCenter); @@ -102,20 +102,24 @@ bool sphereSphere(RenderShapeData &s1, RenderShapeData &s2) if (distance <= radius + radius) { std::cout << "distance: " << distance << std::endl; - s1.velocity *= -1.f; - s2.velocity *= -1.f; + s1->velocity *= -1.f; + s2->velocity *= -1.f; + + // print the new velocity + std::cout << "s1 velocity: " << s1->velocity.x << ", " << s1->velocity.y << ", " << s1->velocity.z << ", " << s1->velocity.w << std::endl; + std::cout << "s2 velocity: " << s2->velocity.x << ", " << s2->velocity.y << ", " << s2->velocity.z << ", " << s2->velocity.w << std::endl; // move the objects in new dir so they don't collide again - s1.translation4d += s1.velocity; - s2.translation4d += s2.velocity; + s1->translation4d += s1->velocity * (1.1f); + s2->translation4d += s2->velocity * (1.1f); } return distance <= radius + radius; } -bool Physics::checkForSphereCollision(RenderShapeData ¤tShape, RenderShapeData &otherShape) +bool Physics::checkForSphereCollision(RenderShapeData *currentShape, RenderShapeData *otherShape) { - switch (otherShape.primitive.type) + switch (otherShape->primitive.type) { case PrimitiveType::PRIMITIVE_CUBE: return sphereCube(currentShape, otherShape); @@ -128,19 +132,19 @@ bool Physics::checkForSphereCollision(RenderShapeData ¤tShape, RenderShape return false; } -bool Physics::checkForConeCollision(RenderShapeData ¤tShape, RenderShapeData &shape) +bool Physics::checkForConeCollision(RenderShapeData *currentShape, RenderShapeData *otherShape) { return false; } -bool Physics::checkForCylinderCollision(RenderShapeData ¤tShape, RenderShapeData &shape) +bool Physics::checkForCylinderCollision(RenderShapeData *currentShape, RenderShapeData *otherShape) { return false; } -bool Physics::checkForCubeCollision(RenderShapeData ¤tShape, RenderShapeData &otherShape) +bool Physics::checkForCubeCollision(RenderShapeData *currentShape, RenderShapeData *otherShape) { - switch (otherShape.primitive.type) + switch (otherShape->primitive.type) { case PrimitiveType::PRIMITIVE_CUBE: return cubeCube(currentShape, otherShape); @@ -154,12 +158,12 @@ bool Physics::checkForCubeCollision(RenderShapeData ¤tShape, RenderShapeDa void Physics::handleCollisions(std::vector<RenderShapeData> &shapes) { for (int i = 0; i < shapes.size(); i++) { - auto shape = shapes[i]; + auto shape = &shapes[i]; for (int j = i + 1; j < shapes.size(); j++) { - auto otherShape = shapes[j]; - switch (shape.primitive.type) + auto otherShape = &shapes[j]; + switch (shape->primitive.type) { case PrimitiveType::PRIMITIVE_CONE: checkForConeCollision(shape, otherShape); diff --git a/src/physics/physics.h b/src/physics/physics.h index 6410d74..6aad980 100644 --- a/src/physics/physics.h +++ b/src/physics/physics.h @@ -10,17 +10,17 @@ class Physics { public: - static bool checkForSphereCollision(RenderShapeData ¤tShape, RenderShapeData &shape); + static void updateShapePositions(std::vector<RenderShapeData> &shapes); - static bool checkForConeCollision(RenderShapeData ¤tShape, RenderShapeData &shape); + static void handleCollisions(std::vector<RenderShapeData> &shapes); - static bool checkForCylinderCollision(RenderShapeData ¤tShape, RenderShapeData &shape); + static bool checkForSphereCollision(RenderShapeData *currentShape, RenderShapeData *otherShape); - static bool checkForCubeCollision(RenderShapeData ¤tShape, RenderShapeData &shape); + static bool checkForConeCollision(RenderShapeData *currentShape, RenderShapeData *otherShape); - static void updateShapePositions(std::vector<RenderShapeData> &shapes); + static bool checkForCylinderCollision(RenderShapeData *currentShape, RenderShapeData *otherShape); - static void handleCollisions(std::vector<RenderShapeData> &shapes); + static bool checkForCubeCollision(RenderShapeData *currentShape, RenderShapeData *otherShape); }; #endif //PROJECTS_RAY_PHYSICS_H diff --git a/src/raytracer/raytracer.cpp b/src/raytracer/raytracer.cpp index 4d48848..6202937 100644 --- a/src/raytracer/raytracer.cpp +++ b/src/raytracer/raytracer.cpp @@ -54,9 +54,10 @@ void RayTracer::render(RGBA *imageData, const RayTraceScene &scene) { settings.currentTime++; // settings.w++; - // update physics + // update physics for moving objects Physics::updateShapePositions(m_metaData.shapes); Physics::handleCollisions(m_metaData.shapes); + } else { // done rendering // assemble the video saveFFMPEGVideo(settings.bulkOutputFolderPath); @@ -173,7 +174,8 @@ void RayTracer::sceneChanged(QLabel* imageLabel) { m_imageData = reinterpret_cast<RGBA *>(image.bits()); RayTraceScene rtScene{ m_width, m_height, m_metaData, m_depth }; - + // update the camera position + rtScene.m_camera.updateViewMatrix(m_metaData.cameraData); this->render(m_imageData, rtScene); QImage flippedImage = image.mirrored(false, false); diff --git a/src/raytracer/raytracescene.cpp b/src/raytracer/raytracescene.cpp index 77541e3..a81d535 100644 --- a/src/raytracer/raytracescene.cpp +++ b/src/raytracer/raytracescene.cpp @@ -57,7 +57,7 @@ const std::vector<SceneLightData> RayTraceScene::getLights() const { return m_lights; } -const Camera& RayTraceScene::getCamera() const { +const Camera RayTraceScene::getCamera() const { // Optional TODO: implement the getter or make your own design return m_camera; } diff --git a/src/raytracer/raytracescene.h b/src/raytracer/raytracescene.h index 35e4fb4..b608918 100644 --- a/src/raytracer/raytracescene.h +++ b/src/raytracer/raytracescene.h @@ -28,18 +28,18 @@ public: const std::vector<SceneLightData> getLights() const; // The getter of the shared pointer to the camera instance of the scene - const Camera& getCamera() const; + const Camera getCamera() const; KdTree *m_kdTree; bvh *m_bvh; const int &depth() const; + Camera& m_camera; private: int m_width; int m_height; SceneGlobalData m_sceneGlobalData; - Camera& m_camera; std::vector<RenderShapeData>m_shapes; std::vector<SceneLightData>m_lights; int m_depth; diff --git a/src/vec4ops/rotations4d.cpp b/src/vec4ops/rotations4d.cpp index 37997de..1ff43a7 100644 --- a/src/vec4ops/rotations4d.cpp +++ b/src/vec4ops/rotations4d.cpp @@ -72,3 +72,46 @@ glm::mat4 Vec4Ops::getRotationMatrix4ZW( result[1][1] = 1; return result; } + +glm::mat4 Vec4Ops::getRotationMatrix4( + std::vector<float> anglesRadians) +{ + // make the identity + glm::mat4 result = glm::mat4(0.f); + result[0][0] = 1.f; + result[1][1] = 1.f; + result[2][2] = 1.f; + result[3][3] = 1.f; + + // apply the rotations, starting with the last angle, which corresponds to ZW + if (anglesRadians.size() != 6) { + throw std::runtime_error("invalid rotation angle array"); + } + for (int i = 5; i >= 0; i--) { + switch (i) { + case 0: + result *= getRotationMatrix4XY(anglesRadians[i]) * result; + break; + case 1: + result *= getRotationMatrix4ZX(anglesRadians[i]) * result; + break; + case 2: + result *= getRotationMatrix4YZ(anglesRadians[i]) * result; + break; + case 3: + result *= getRotationMatrix4XW(anglesRadians[i]) * result; + break; + case 4: + result *= getRotationMatrix4YW(anglesRadians[i]) * result; + break; + case 5: + result *= getRotationMatrix4ZW(anglesRadians[i]) * result; + break; + default: + throw std::runtime_error("invalid rotation matrix"); + } + } + + return result; +} + diff --git a/src/vec4ops/vec4ops.h b/src/vec4ops/vec4ops.h index 796722a..f4f62f1 100644 --- a/src/vec4ops/vec4ops.h +++ b/src/vec4ops/vec4ops.h @@ -6,6 +6,7 @@ #define PROJECTS_RAY_VEC4OPS_H #include <glm/glm.hpp> +#include <vector> class Vec4Ops { public: @@ -36,6 +37,8 @@ public: static glm::vec4 inverseTransformDir4(glm::vec4 dir4, glm::mat4 inverseTransformDirectionMatrix); static glm::mat4 getViewMatrix4(glm::vec4 upVector, glm::vec4 lookVector, glm::vec4 overVector); + + static glm::mat4 getRotationMatrix4(std::vector<float> anglesRadians); }; #endif //PROJECTS_RAY_VEC4OPS_H |