Skip to content

Commit

Permalink
Added camera util. Added vector3 cross product.
Browse files Browse the repository at this point in the history
  • Loading branch information
PhilipPragerUrbina committed Oct 28, 2022
1 parent f6fb7af commit 2a30a71
Show file tree
Hide file tree
Showing 8 changed files with 127 additions and 26 deletions.
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ project(TensorMath)
set(CMAKE_CXX_STANDARD 17)


add_executable(TensorMath main.cpp TensorMath/Matrix.hpp TensorMath/Vector.hpp TensorMath/FixedVector.hpp TensorMath/3D/BoundingVolume.hpp TensorMath/3D/Camera.hpp TensorMath/Tensor.hpp TensorMath/3D/VoxelVolume.hpp TensorMath/FixedMatrix.hpp)
add_library(TensorMath_lib main.cpp TensorMath/Matrix.hpp TensorMath/Vector.hpp TensorMath/FixedVector.hpp TensorMath/3D/BoundingVolume.hpp TensorMath/3D/Camera.hpp TensorMath/Tensor.hpp TensorMath/3D/VoxelVolume.hpp TensorMath/FixedMatrix.hpp)
add_executable(TensorMath main.cpp TensorMath/Matrix.hpp TensorMath/Vector.hpp TensorMath/FixedVector.hpp TensorMath/3DUtils/BoundingVolume.hpp TensorMath/3DUtils/Camera.hpp TensorMath/Tensor.hpp TensorMath/3DUtils/VoxelVolume.hpp TensorMath/FixedMatrix.hpp)
add_library(TensorMath_lib main.cpp TensorMath/Matrix.hpp TensorMath/Vector.hpp TensorMath/FixedVector.hpp TensorMath/3DUtils/BoundingVolume.hpp TensorMath/3DUtils/Camera.hpp TensorMath/Tensor.hpp TensorMath/3DUtils/VoxelVolume.hpp TensorMath/FixedMatrix.hpp)
add_subdirectory(Tests)
23 changes: 0 additions & 23 deletions TensorMath/3D/Camera.hpp

This file was deleted.

File renamed without changes.
108 changes: 108 additions & 0 deletions TensorMath/3DUtils/Camera.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
//
// Created by Philip on 10/17/2022.
//

#ifndef TENSORMATH_CAMERA_HPP
#define TENSORMATH_CAMERA_HPP
#include "../FixedVector.hpp"
#include "../FixedMatrix.hpp"
# define M_PI 3.14159265358979323846
namespace TensorMath {
//unified Class for projecting and un-projecting things from a camera
class Camera {
private:
//world space
Vector3 m_position; //camera position
Vector3 m_direction; //camera direction
double m_aspect_ratio; //relation width and height of camera
Vector3 m_up; //direction of up for the world
//camera settings
double m_field_of_view_radians; //field of view in radians
//projection things(going from world space to screen space like in rasterization)
FixedMatrix<4,4> m_projection_matrix; //transformation matrix for project point into screen space

//ray tracing things(going from screen space to world space like in ray tracing)
Vector3 m_lower_left_corner; //corner of screen to offset by
Vector3 m_horizontal; //rotated m_vertical direction of camera
Vector3 m_vertical; //rotated m_horizontal direction of camera

//update rasterization projection matrix
void generateMatrix(){
//todo rasterization

}

//update ray tracing direction variables
void generatePlane(){
//calculate height and width of the viewport in the world
//the height corresponds to the field of view
double view_height = 2.0* tan(m_field_of_view_radians/2.0);
//width corresponds to height based on aspect ratio
double view_width = m_aspect_ratio * view_height;
//rotate and scale to get horizontal and vertical vectors
Vector3 horizontal_direction= m_up.crossProduct(m_direction).normalized(); //rotate
m_horizontal =horizontal_direction * view_width; //scale
m_vertical = m_direction.crossProduct(horizontal_direction) * view_height; //get perpendicular vector
//get lowest corner
m_lower_left_corner = m_position - m_horizontal/2.0 - m_vertical/2.0 - m_direction;
}

//update the camera state to account for new settings
void update(){
generatePlane();
generateMatrix();
}

public:
//create a camera at a position looking in a direction, with a certain size in world units, and a fov in degrees. +Z is up by default
Camera(Vector3 position, Vector3 direction, double aspect_ratio, double degree_fov, Vector3 up = {0,0,1}){
//set values
m_position = position;
m_direction = direction.normalized();
m_aspect_ratio = aspect_ratio;
m_up = up;
//update
setFOVDegrees(degree_fov);
//no need to update, setFOV already has
}

//get where rays should originate in this camera
Vector3 getRayOrigin(){
return m_position;
}

//get the proper ray direction at a given position in screen space
Vector3 getRayDirection(Vector2 position){
return m_lower_left_corner + m_horizontal * position.x() + m_vertical * position.y() - m_position;
}

//convert pixel coordinates to screen space from pixel width and height
Vector2 getScreenPosition(int x, int y, int width, int height){
return Vector2 { (double)x / (width - 1), (double)y / (height -1 ) };
}

//change direction to look at coordinate and update
void setLookAt(Vector3 look_at){
m_direction = (m_position-look_at).normalized();
update();
}

//set the camera fov in degrees and update
void setFOVDegrees(double degrees){
m_field_of_view_radians = ( degrees * M_PI ) / 180.0 ; //degrees to radians
update();
}
//set the camera fov in radians and update
void setFOVRadians(double radians){
m_field_of_view_radians = radians;
update();
}


//https://www.scratchapixel.com/lessons/3d-basic-rendering/perspective-and-orthographic-projection-matrix/orthographic-projection-matrix


};
}

#endif //TENSORMATH_CAMERA_HPP
File renamed without changes.
10 changes: 10 additions & 0 deletions TensorMath/FixedVector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,12 @@ namespace TensorMath {
}
return sum;
} //Get the dot product of two vectors. Combine two vectors into single value.
FixedVector<3> crossProduct(const FixedVector<3> &other) const {
return FixedVector<3>{getValue(1) * other[2] - getValue(2) * other[1],
getValue(2) * other[0] - getValue(0) * other[2],
getValue(0) * other[1] - getValue(1) * other[0]};
} //Get the cross product of two vectors. Only for 3d vectors. (Right-hand rule)

double distance(const FixedVector<dimensions> &other) const {
double sum = 0; //sqrt((x2-x1)^2 + (y2-y1)^2...)
for (int i = 0; i < dimensions; ++i) {
Expand All @@ -225,6 +231,10 @@ namespace TensorMath {
FixedVector<dimensions> normalized() const {
return *this / length(); //(1/||v||) * v = unit v
} //get the normalized(unit) vector. The direction of the vector.
FixedVector<dimensions> inverse() const {
FixedVector<dimensions> one(1.0);
return one / *this;
} //get 1.0/vector. Useful for ray tracing.
FixedVector<dimensions> min(const FixedVector<dimensions> &other) const {
FixedVector<dimensions> out; //vector to return
for (int i = 0; i < dimensions; ++i) {
Expand Down
6 changes: 6 additions & 0 deletions TensorMath/Vector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,12 @@ namespace TensorMath {
}
return std::sqrt(sum);
} //get the distance between two vectors.

Vector inverse() const {
Vector one(m_dimensions);
one.setScalar(1.0);
return one / *this;
} //get 1.0/vector. Useful for ray tracing.
Vector normalized() const {
return *this / length(); //(1/||v||) * v = unit v
} //get the normalized(unit) vector. The direction of the vector.
Expand Down
2 changes: 1 addition & 1 deletion main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#include "TensorMath//Matrix.hpp"
#include "TensorMath/Vector.hpp"
#include "TensorMath/FixedVector.hpp"

#include "TensorMath/3DUtils/Camera.hpp"
int main() {
using namespace TensorMath;

Expand Down

0 comments on commit 2a30a71

Please sign in to comment.