Skip to content

Commit

Permalink
Allow printing "work_done" messages to measure performance
Browse files Browse the repository at this point in the history
  • Loading branch information
wentasah committed Nov 2, 2020
1 parent 8ab8581 commit fbc8f9e
Showing 1 changed file with 21 additions and 3 deletions.
24 changes: 21 additions & 3 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <unistd.h>
#include "model.h"
#include "our_gl.h"
#include <err.h>

constexpr int width = 800; // output image size
constexpr int height = 800;
Expand Down Expand Up @@ -60,18 +61,29 @@ struct Shader : IShader {
};

void print_usage(char *cmd) {
std::cerr << "Usage: " << cmd << " [-f] obj/model.obj..." << std::endl;
std::cerr << "Usage: " << cmd << " [-f] [-w work_done_string [-e num]] obj/model.obj..." << std::endl;
}

int main(int argc, char** argv) {
int opt;
bool forever = false;
char *work_done_string = NULL;
unsigned work_done = 0;
int work_done_every = 1;

while ((opt = getopt(argc, argv, "f")) != -1) {
while ((opt = getopt(argc, argv, "e:fw:")) != -1) {
switch (opt) {
case 'e':
work_done_every = atoi(optarg);
if (work_done_every <= 0)
errx(1, "Usage: -e 'number > 0'");
break;
case 'f':
forever = true;
break;
case 'w':
work_done_string = optarg;
break;
default: /* '?' */
print_usage(argv[0]);
return 1;
Expand All @@ -83,6 +95,9 @@ int main(int argc, char** argv) {
return 1;
}

if (work_done_string == NULL && work_done_every != 1)
errx(1, "-e only makes sense with -w");

std::vector<double> zbuffer(width*height, -std::numeric_limits<double>::max()); // note that the z-buffer is initialized with minimal possible values
TGAImage framebuffer(width, height, TGAImage::RGB); // the output image
lookat(eye, center, up); // build the ModelView matrix
Expand All @@ -103,8 +118,11 @@ int main(int argc, char** argv) {
triangle(clip_vert, shader, framebuffer, zbuffer); // actual rasterization routine call
}
}
if (work_done_string && (work_done++ % work_done_every == 0)) {
printf("%s=%d\n", work_done_string, work_done - 1);
fflush(stdout);
}
} while (forever);
framebuffer.write_tga_file("framebuffer.tga"); // the vertical flip is moved inside the function
return 0;
}

0 comments on commit fbc8f9e

Please sign in to comment.