add progress bar

This commit is contained in:
Shuo Feng 2024-03-18 20:36:03 -04:00
parent 4d4c257ef1
commit 0d450261a7
Signed by: sfeng
GPG key ID: 1E83AE6CD1C037B1
3 changed files with 36 additions and 1 deletions

View file

@ -1,4 +1,5 @@
#ifndef OPTIONAL_H_
#define OPTIONAL_H_
namespace utils {

29
src/Progress.h Normal file
View file

@ -0,0 +1,29 @@
#ifndef PROGRESS_H_
#define PROGRESS_H_
#include <iostream>
namespace utils {
#define BAR_WIDTH 70
class Progress {
public:
static void of(float p) {
std::cout << "[";
int pos = BAR_WIDTH * p;
for (int i = 0; i < BAR_WIDTH; ++i) {
if (i < pos)
std::cout << "=";
else if (i == pos)
std::cout << ">";
else
std::cout << " ";
}
std::cout << "] " << int(p * 100.0) << " %\r";
std::cout.flush();
}
};
} // namespace utils
#endif // !PROGRESS_H_

View file

@ -2,6 +2,7 @@
#include "HitRecord.h"
#include "Output.h"
#include "Parser.h"
#include "Progress.h"
#include "Ray.h"
#include <Eigen/Core>
@ -70,7 +71,8 @@ void RayTracer::render() {
rpp = raysPerPixel.z();
}
for (int y = 0; y < height; ++y)
for (int y = 0; y < height; ++y) {
utils::Progress::of((y + 1.0f) / height);
for (int x = 0; x < width; ++x)
for (int j = 0; j < gridHeight; ++j)
for (int i = 0; i < gridWidth; ++i) {
@ -90,6 +92,9 @@ void RayTracer::render() {
}
}
std::cout << std::endl;
}
void RayTracer::run() {
parse();