mirror of
https://github.com/vonhyou/ray-tracer-comp371.git
synced 2025-06-08 07:22:01 +00:00
reformat
This commit is contained in:
parent
f5936b2ada
commit
05914ac768
3 changed files with 38 additions and 32 deletions
|
@ -11,11 +11,9 @@ using std::vector;
|
|||
|
||||
class Output {
|
||||
public:
|
||||
Output(const Vector3f &bgc, string path, int w, int h)
|
||||
: red(vector<float>(w * h + 1, bgc.x())),
|
||||
green(vector<float>(w * h + 1, bgc.y())),
|
||||
blue(vector<float>(w * h + 1, bgc.z())), path(path), width(w),
|
||||
height(h) {}
|
||||
Output(string path, int w, int h)
|
||||
: red(vector<float>(w * h + 1)), green(vector<float>(w * h + 1)),
|
||||
blue(vector<float>(w * h + 1)), path(path), width(w), height(h) {}
|
||||
|
||||
void write();
|
||||
|
||||
|
|
|
@ -28,16 +28,19 @@ Ray getRay(int x, int y, const Vector3f &camPos, const Vector3f &pxUpperLeft,
|
|||
return Ray(camPos, pxUpperLeft + x * du + y * dv - camPos);
|
||||
}
|
||||
|
||||
void RayTracer::calculateColor(const HitRecord &hit, int i) {
|
||||
void writeColor(int i, const Vector3f &color) {
|
||||
Output::current->r(i, color.x());
|
||||
Output::current->g(i, color.y());
|
||||
Output::current->b(i, color.z());
|
||||
}
|
||||
|
||||
Vector3f RayTracer::calculateColor(const HitRecord &hit, int i) const {
|
||||
Vector3f result(0, 0, 0);
|
||||
for (auto light : lights)
|
||||
result += light->isUse() ? light->illumination(hit, geometries)
|
||||
: Vector3f::Zero();
|
||||
|
||||
result = result.cwiseMax(0.0f).cwiseMin(1.0f);
|
||||
Output::current->r(i, result.x());
|
||||
Output::current->g(i, result.y());
|
||||
Output::current->b(i, result.z());
|
||||
return result.cwiseMax(0.0f).cwiseMin(1.0f);
|
||||
}
|
||||
|
||||
int getGridWidth(Eigen::VectorXi data) {
|
||||
|
@ -52,6 +55,8 @@ int getRayNumber(Eigen::VectorXi data) {
|
|||
return data.size() == 2 ? data.y() : (data.size() == 3 ? data.z() : 1);
|
||||
}
|
||||
|
||||
Vector3f trace() { return Vector3f::Zero(); }
|
||||
|
||||
void RayTracer::render() {
|
||||
int width = Scene::current->width();
|
||||
int height = Scene::current->height();
|
||||
|
@ -68,8 +73,7 @@ void RayTracer::render() {
|
|||
Vector3f vpUpperLeft = cameraPos + lookAt - u / 2.0 - v / 2.0;
|
||||
Vector3f pxUpperLeft = vpUpperLeft + (du + dv) / 2.0;
|
||||
|
||||
Output::current = new Output(Scene::current->backgroundColor(),
|
||||
Scene::current->name(), width, height);
|
||||
Output::current = new Output(Scene::current->name(), width, height);
|
||||
|
||||
Eigen::VectorXi data = Scene::current->raysPerPixel();
|
||||
int gridWidth = getGridWidth(data);
|
||||
|
@ -84,13 +88,17 @@ void RayTracer::render() {
|
|||
}
|
||||
|
||||
for (int y = 0; y < height; ++y) {
|
||||
// print progress bar
|
||||
utils::Progress::of((y + 1.0f) / height);
|
||||
|
||||
for (int x = 0; x < width; ++x)
|
||||
for (int x = 0; x < width; ++x) {
|
||||
Vector3f color = Scene::current->backgroundColor();
|
||||
|
||||
if (Scene::current->globalIllum()) {
|
||||
for (int j = 0; j < gridHeight; ++j)
|
||||
for (int i = 0; i < gridWidth; ++i) {
|
||||
if (Scene::current->globalIllum()) {
|
||||
// TODO: Path tracing for global illumination
|
||||
color = trace();
|
||||
}
|
||||
} else {
|
||||
Ray ray = getRay(x, y, cameraPos, pxUpperLeft, du, dv);
|
||||
priority_queue<HitRecord> records;
|
||||
|
@ -103,12 +111,12 @@ void RayTracer::render() {
|
|||
if (!records.empty()) {
|
||||
HitRecord hit = records.top();
|
||||
hit.calcNormal();
|
||||
calculateColor(hit, y * width + x);
|
||||
color = calculateColor(hit, y * width + x);
|
||||
}
|
||||
}
|
||||
writeColor(y * width + x, color);
|
||||
}
|
||||
}
|
||||
|
||||
std::cout << std::endl;
|
||||
}
|
||||
|
||||
|
|
|
@ -22,7 +22,7 @@ private:
|
|||
std::vector<Output *> outputs;
|
||||
|
||||
void parse();
|
||||
void calculateColor(const HitRecord &, int);
|
||||
Vector3f calculateColor(const HitRecord &, int) const;
|
||||
void render();
|
||||
};
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue