-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
494405f
commit 076488e
Showing
32 changed files
with
6,629 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
cmake-build-debug | ||
|
||
.idea | ||
|
||
raytracer |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
cmake_minimum_required(VERSION 3.21) | ||
project(477_hw2) | ||
|
||
set(CMAKE_CXX_STANDARD 17) | ||
set(CMAKE_CXX_FLAGS_RELEASE "-O3") | ||
|
||
add_executable(rasterizer Camera.cpp Camera.h Color.cpp Color.h Vector/Helpers.cpp Vector/Helpers.h Main.cpp Matrix4.cpp Matrix4.h | ||
Mesh.cpp Mesh.h Transformation/Rotation.cpp Transformation/Rotation.h Transformation/Scaling.cpp Transformation/Scaling.h Scene.cpp Scene.h tinyxml2.cpp tinyxml2.h | ||
Transformation/Translation.cpp Transformation/Translation.h Triangle.cpp Triangle.h Vector/Vec3.cpp Vector/Vec3.h Vector/Vec4.cpp Vector/Vec4.h Transformation/Base.cpp Transformation/Base.h) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
#include "Camera.h" | ||
#include <string> | ||
#include <iostream> | ||
#include <iomanip> | ||
|
||
using namespace std; | ||
|
||
Camera::Camera() {} | ||
|
||
Camera::Camera(int cameraId, | ||
int projectionType, | ||
Vec3 pos, Vec3 gaze, | ||
Vec3 u, Vec3 v, Vec3 w, | ||
double left, double right, double bottom, double top, | ||
double near, double far, | ||
int horRes, int verRes, | ||
string outputFileName) { | ||
|
||
this->cameraId = cameraId; | ||
this->projectionType = projectionType; | ||
this->pos = pos; | ||
this->gaze = gaze; | ||
this->u = u; | ||
this->v = v; | ||
this->w = w; | ||
this->left = left; | ||
this->right = right; | ||
this->bottom = bottom; | ||
this->top = top; | ||
this->near = near; | ||
this->far = far; | ||
this->horRes = horRes; | ||
this->verRes = verRes; | ||
this->outputFileName = outputFileName; | ||
} | ||
|
||
Camera::Camera(const Camera &other) { | ||
this->cameraId = other.cameraId; | ||
this->projectionType = other.projectionType; | ||
this->pos = other.pos; | ||
this->gaze = other.gaze; | ||
this->u = other.u; | ||
this->v = other.v; | ||
this->w = other.w; | ||
this->left = other.left; | ||
this->right = other.right; | ||
this->bottom = other.bottom; | ||
this->top = other.top; | ||
this->near = other.near; | ||
this->far = other.far; | ||
this->horRes = other.horRes; | ||
this->verRes = other.verRes; | ||
this->outputFileName = other.outputFileName; | ||
} | ||
|
||
void Camera::computeTransforms() { | ||
this->computeCameraTransform(); | ||
this->computeProjectionTransform(); | ||
this->computeViewportTransform(); | ||
|
||
this->perCamTransform = multiplyMatrixWithMatrix(this->projectionTransform, this->cameraTransform); | ||
} | ||
|
||
void Camera::computeCameraTransform() { | ||
double translate[4][4] = {{1, 0, 0, -(this->pos.x)}, | ||
{0, 1, 0, -(this->pos.y)}, | ||
{0, 0, 1, -(this->pos.z)}, | ||
{0, 0, 0, 1}}; | ||
double rotate[4][4] = {{this->u.x, this->u.y, this->u.z, 0}, | ||
{this->v.x, this->v.y, this->v.z, 0}, | ||
{this->w.x, this->w.y, this->w.z, 0}, | ||
{0, 0, 0, 1}}; | ||
this->cameraTransform = multiplyMatrixWithMatrix(rotate, translate); | ||
} | ||
|
||
void Camera::computeProjectionTransform() { | ||
switch (this->projectionType) { | ||
case 0: { | ||
double orthographic[4][4] = {{2/(this->right-this->left),0,0,-((this->right+this->left)/(this->right-this->left))}, | ||
{0, 2/(this->top-this->bottom),0,-((this->top+this->bottom)/(this->top-this->bottom))}, | ||
{0,0, -(2/(this->far-this->near)),-((this->far+this->near)/(this->far-this->near))}, | ||
{0,0,0,1}}; | ||
this->projectionTransform = Matrix4(orthographic); | ||
break; | ||
} | ||
case 1: { | ||
double perspective[4][4] = {{(2*this->near)/(this->right-this->left),0,(this->right+this->left)/(this->right-this->left),0}, | ||
{0,(2*this->near)/(this->top-this->bottom),(this->top+this->bottom)/(this->top-this->bottom),0}, | ||
{0,0,-((this->far+this->near)/(this->far-this->near)),-((2*this->far*this->near)/(this->far-this->near))}, | ||
{0,0,-1,0}}; | ||
this->projectionTransform = Matrix4(perspective); | ||
break; | ||
} | ||
} | ||
} | ||
|
||
void Camera::computeViewportTransform() { | ||
double viewport[4][4] = {{this->horRes / 2.0, 0, 0, (this->horRes - 1) / 2.0}, | ||
{0, this->verRes / 2.0, 0, (this->verRes - 1) / 2.0}, | ||
{0, 0, 0.5, 0.5}, | ||
{0, 0, 0, 1}}; | ||
this->viewportTransform = Matrix4(viewport); | ||
} | ||
|
||
ostream &operator<<(ostream &os, const Camera &c) { | ||
const char *camType = c.projectionType ? "perspective" : "orthographic"; | ||
|
||
os << fixed << setprecision(6) << "Camera " << c.cameraId << " (" << camType << ") => pos: " << c.pos << " gaze: " | ||
<< c.gaze << endl | ||
<< "\tu: " << c.u << " v: " << c.v << " w: " << c.w << endl | ||
<< fixed << setprecision(3) << "\tleft: " << c.left << " right: " << c.right << " bottom: " << c.bottom | ||
<< " top: " << c.top << endl | ||
<< "\tnear: " << c.near << " far: " << c.far << " resolutions: " << c.horRes << "x" << c.verRes << " fileName: " | ||
<< c.outputFileName; | ||
|
||
return os; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
#ifndef __CAMERA_H__ | ||
#define __CAMERA_H__ | ||
|
||
#include "Vector/Vec3.h" | ||
#include "Vector/Helpers.h" | ||
#include "Matrix4.h" | ||
#include <string> | ||
|
||
using namespace std; | ||
|
||
class Camera | ||
{ | ||
|
||
public: | ||
int cameraId; | ||
int projectionType; // 1 for perspective, 0 for orthographic | ||
Vec3 pos; | ||
Vec3 gaze; | ||
Vec3 u; | ||
Vec3 v; | ||
Vec3 w; | ||
double left, right, bottom, top; | ||
double near; | ||
double far; | ||
int horRes; | ||
int verRes; | ||
string outputFileName; | ||
|
||
Matrix4 cameraTransform; | ||
Matrix4 projectionTransform; | ||
Matrix4 perCamTransform; | ||
Matrix4 viewportTransform; | ||
|
||
Camera(); | ||
|
||
Camera(int cameraId, | ||
int projectionType, | ||
Vec3 pos, Vec3 gaze, | ||
Vec3 u, Vec3 v, Vec3 w, | ||
double left, double right, double bottom, double top, | ||
double near, double far, | ||
int horRes, int verRes, | ||
string outputFileName); | ||
|
||
Camera(const Camera &other); | ||
|
||
void computeTransforms(); | ||
void computeCameraTransform(); | ||
void computeProjectionTransform(); | ||
void computeViewportTransform(); | ||
|
||
friend std::ostream &operator<<(std::ostream &os, const Camera &c); | ||
}; | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
#include "Color.h" | ||
#include <iostream> | ||
#include <iomanip> | ||
|
||
using namespace std; | ||
|
||
Color::Color() {} | ||
|
||
Color::Color(double r, double g, double b) | ||
{ | ||
this->r = r; | ||
this->g = g; | ||
this->b = b; | ||
} | ||
|
||
Color::Color(const Color &other) | ||
{ | ||
this->r = other.r; | ||
this->g = other.g; | ||
this->b = other.b; | ||
} | ||
|
||
ostream& operator<<(ostream& os, const Color& c) | ||
{ | ||
os << fixed << setprecision(0) << "rgb(" << c.r << ", " << c.g << ", " << c.b << ")"; | ||
return os; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#ifndef __COLOR_H__ | ||
#define __COLOR_H__ | ||
|
||
#include <iostream> | ||
|
||
class Color | ||
{ | ||
public: | ||
double r, g, b; | ||
|
||
Color(); | ||
Color(double r, double g, double b); | ||
Color(const Color &other); | ||
friend std::ostream& operator<<(std::ostream& os, const Color& c); | ||
}; | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
#include <iostream> | ||
#include <string> | ||
#include <vector> | ||
#include "Scene.h" | ||
#include "Matrix4.h" | ||
#include "Vector/Helpers.h" | ||
|
||
using namespace std; | ||
|
||
Scene *scene; | ||
|
||
int main(int argc, char *argv[]) | ||
{ | ||
if (argc != 2) | ||
{ | ||
cout << "Please run the rasterizer as:" << endl | ||
<< "\t./rasterizer <input_file_name>" << endl; | ||
return 1; | ||
} | ||
else | ||
{ | ||
const char *xmlPath = argv[1]; | ||
|
||
scene = new Scene(xmlPath); | ||
|
||
for (int i = 0; i < scene->cameras.size(); i++) | ||
{ | ||
// initialize image with basic values | ||
scene->initializeImage(scene->cameras[i]); | ||
|
||
// do forward rendering pipeline operations | ||
scene->forwardRenderingPipeline(scene->cameras[i]); | ||
|
||
// generate PPM file | ||
scene->writeImageToPPMFile(scene->cameras[i]); | ||
|
||
// Converts PPM image in given path to PNG file, by calling ImageMagick's 'convert' command. | ||
// Notice that os_type is not given as 1 (Ubuntu) or 2 (Windows), below call doesn't do conversion. | ||
// Change os_type to 1 or 2, after being sure that you have ImageMagick installed. | ||
scene->convertPPMToPNG(scene->cameras[i]->outputFileName, 99); | ||
} | ||
|
||
return 0; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
all: | ||
g++ *.cpp Transformation/*.cpp Vector/*.cpp -o raytracer -std=c++11 -O3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
#include "Matrix4.h" | ||
#include <iostream> | ||
#include <iomanip> | ||
|
||
using namespace std; | ||
|
||
Matrix4::Matrix4() | ||
{ | ||
for (int i = 0; i < 4; i++) | ||
{ | ||
for (int j = 0; j < 4; j++) | ||
{ | ||
this->val[i][j] = 0; | ||
} | ||
} | ||
} | ||
|
||
Matrix4::Matrix4(double val[4][4]) | ||
{ | ||
for (int i = 0; i < 4; i++) | ||
{ | ||
for (int j = 0; j < 4; j++) | ||
{ | ||
this->val[i][j] = val[i][j]; | ||
} | ||
} | ||
} | ||
|
||
Matrix4::Matrix4(const Matrix4 &other) | ||
{ | ||
for (int i = 0; i < 4; i++) | ||
{ | ||
for (int j = 0; j < 4; j++) | ||
{ | ||
this->val[i][j] = other.val[i][j]; | ||
} | ||
} | ||
} | ||
|
||
ostream &operator<<(ostream &os, const Matrix4 &m) | ||
{ | ||
|
||
os << fixed << setprecision(6) << "|" << m.val[0][0] << "|" << m.val[0][1] << "|" << m.val[0][2] << "|" << m.val[0][3] << "|" | ||
<< endl | ||
<< "|" << m.val[1][0] << "|" << m.val[1][1] << "|" << m.val[1][2] << "|" << m.val[1][3] << "|" | ||
<< endl | ||
<< "|" << m.val[2][0] << "|" << m.val[2][1] << "|" << m.val[2][2] << "|" << m.val[2][3] << "|" | ||
<< endl | ||
<< "|" << m.val[3][0] << "|" << m.val[3][1] << "|" << m.val[3][2] << "|" << m.val[3][3] << "|"; | ||
|
||
return os; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
#ifndef __MATRIX4_H__ | ||
#define __MATRIX4_H__ | ||
|
||
#include <iostream> | ||
|
||
using namespace std; | ||
|
||
class Matrix4 | ||
{ | ||
public: | ||
double val[4][4]; | ||
|
||
Matrix4(); | ||
Matrix4(double val[4][4]); | ||
Matrix4(const Matrix4 &other); | ||
friend ostream &operator<<(ostream &os, const Matrix4 &m); | ||
}; | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
#include <vector> | ||
#include "Triangle.h" | ||
#include "Mesh.h" | ||
#include <iostream> | ||
#include <iomanip> | ||
|
||
using namespace std; | ||
|
||
Mesh::Mesh() {} | ||
|
||
// TODO bunu burdan al | ||
Mesh::Mesh(int meshId, int type, int numberOfTransformations, | ||
vector<int> transformationIds, | ||
vector<char> transformationTypes, | ||
int numberOfTriangles, | ||
vector<Triangle> triangles) | ||
{ | ||
this->meshId = meshId; | ||
this->type = type; | ||
this->numberOfTransformations = numberOfTransformations; | ||
this->numberOfTriangles = numberOfTriangles; | ||
|
||
this->transformationIds = transformationIds; | ||
this->transformationTypes = transformationTypes; | ||
this->triangles = triangles; | ||
} | ||
|
||
void Mesh::computeModelingTransform() { | ||
Matrix4 modelingMatrix = getIdentityMatrix(); | ||
for(auto transformation : this->transformations) { | ||
modelingMatrix = multiplyMatrixWithMatrix(transformation->matrix, modelingMatrix); | ||
} | ||
this->modelingTransform = modelingMatrix; | ||
} |
Oops, something went wrong.