Skip to content

Commit

Permalink
tbwrap integration
Browse files Browse the repository at this point in the history
(Changes from David Hornof)
  • Loading branch information
wentasah committed Feb 19, 2021
1 parent 15cdee7 commit cd9a2b1
Showing 1 changed file with 41 additions and 31 deletions.
72 changes: 41 additions & 31 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "model.h"
#include "our_gl.h"
#include <err.h>
#include "../tbwrap.h"

constexpr int width = 800; // output image size
constexpr int height = 800;
Expand All @@ -16,6 +17,13 @@ const vec3 up(0,1,0); // camera up vector
extern mat<4,4> ModelView; // "OpenGL" state matrices
extern mat<4,4> Projection;

int opt;
bool forever = false;
char *work_done_string = NULL;
unsigned long long work_done = 0;
int work_done_every = 1;
std::vector<Model> models;

struct Shader : IShader {
const Model &model;
vec3 l; // light direction in normalized device coordinates
Expand Down Expand Up @@ -64,13 +72,33 @@ void print_usage(char *cmd) {
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 long long work_done = 0;
int work_done_every = 1;
void tinyrender_run() {
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
viewport(width/8, height/8, width*3/4, height*3/4); // build the Viewport matrix
projection(-1.f/(eye-center).norm()); // build the Projection matrix

do {
for (auto &model : models) { // iterate through all input objects
Shader shader(model);
for (int i=0; i<model.nfaces(); i++) { // for every triangle
vec4 clip_vert[3]; // triangle coordinates (clip coordinates), written by VS, read by FS
for (int j=0; j<3; j++)
clip_vert[j] = shader.vertex(i, j); // call the vertex shader for each triangle vertex
triangle(clip_vert, shader, framebuffer, zbuffer); // actual rasterization routine call
}
}
if (work_done_string && (work_done++ % work_done_every == 0)) {
printf("%s=%lld\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
}

void tinyrender_init(int argc, char** argv)
{
while ((opt = getopt(argc, argv, "e:fw:")) != -1) {
switch (opt) {
case 'e':
Expand All @@ -86,43 +114,25 @@ int main(int argc, char** argv) {
break;
default: /* '?' */
print_usage(argv[0]);
return 1;
return;
}
}

if (optind >= argc) {
print_usage(argv[0]);
return 1;
return;
}

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
viewport(width/8, height/8, width*3/4, height*3/4); // build the Viewport matrix
projection(-1.f/(eye-center).norm()); // build the Projection matrix

std::vector<Model> models;
for (int m=optind; m<argc; m++) // iterate through all input objects
models.emplace_back(argv[m]);
}

do {
for (auto &model : models) { // iterate through all input objects
Shader shader(model);
for (int i=0; i<model.nfaces(); i++) { // for every triangle
vec4 clip_vert[3]; // triangle coordinates (clip coordinates), written by VS, read by FS
for (int j=0; j<3; j++)
clip_vert[j] = shader.vertex(i, j); // call the vertex shader for each triangle vertex
triangle(clip_vert, shader, framebuffer, zbuffer); // actual rasterization routine call
}
}
if (work_done_string && (work_done++ % work_done_every == 0)) {
printf("%s=%lld\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
int main(int argc, char** argv)
{
tinyrender_init(argc, argv);
thermobench_wrap(tinyrender_run);
return 0;
}

0 comments on commit cd9a2b1

Please sign in to comment.