Code clean up and refactoring.

This commit is contained in:
2016-12-26 15:41:29 -04:00
parent 876ecc14cc
commit 2d437f07bc
10 changed files with 183 additions and 85 deletions

View File

@@ -1,10 +1,8 @@
#include <iostream>
#include <vector>
#include <limits>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <omp.h>
#include <glm/glm.hpp>
@@ -12,11 +10,14 @@
#include "ray.hpp"
#include "figure.hpp"
#include "sphere.hpp"
#include "light.hpp"
#include "tracer.hpp"
using namespace std;
using namespace glm;
static const vec3 BCKG_COLOR = vec3(0.16f, 0.66f, 0.72f);
#define MAX_RECURSION 9
static const char * OUT_FILE = "output.ppm";
static char * input_file;
@@ -24,21 +25,17 @@ static int g_samples = 25;
static float g_fov = 90.0f;
static int g_w = 640;
static int g_h = 480;
static float g_aspect_ratio = static_cast<float>(g_w) / g_h;
static vec3 ** image;
float random01();
vec2 sample_pixel(int i, int j);
int main(int argc, char ** argv) {
FILE * out;
float t;
float _t;
Sphere * s;
Figure * _f;
Light * l;
Ray r;
vec2 sample;
vector<Figure *> figures;
vector<Light *> lights;
Tracer tracer;
if(argc < 2 || argc == 3 || argc > 6) {
cerr << "USAGE: " << argv[0] << " IN FILE [OUT FILE [HEIGHT WIDTH [SAMPLES [FIELD OF VIEW]]]]" << endl;
@@ -108,38 +105,34 @@ int main(int argc, char ** argv) {
s->set_color(1.0f, 1.0f, 0.0f);
figures.push_back(static_cast<Figure *>(s));
#pragma omp parallel for schedule(dynamic, 1) private(r, sample, _f, t, _t)
l = new Light();
lights.push_back(l);
tracer = Tracer(g_h, g_w, g_fov);
#pragma omp parallel for schedule(dynamic, 1) private(r, sample)
for (int i = 0; i < g_h; i++) {
for (int j = 0; j < g_w; j++) {
for (int k = 0; k < g_samples; k++) {
sample = sample_pixel(i, j);
sample = tracer.sample_pixel(i, j);
r = Ray(normalize(vec3(sample, -1.0f) - vec3(0.0f, 0.0f, 0.0f)), vec3(0.0f, 0.0f, 0.0f));
t = numeric_limits<float>::max();
_f = NULL;
for (size_t f = 0; f < figures.size(); f++) {
if (figures[f]->intersect(r, _t) && _t < t) {
t = _t;
_f = figures[f];
}
}
if (_f != NULL) {
image[i][j] += vec3(_f->color);
} else {
image[i][j] += vec3(BCKG_COLOR);
}
image[i][j] += tracer.trace_ray(r, figures, lights, MAX_RECURSION);
}
image[i][j] /= g_samples;
}
}
for (size_t f = 0; f < figures.size(); f++) {
delete static_cast<Sphere *>(figures[f]);
for (size_t i = 0; i < figures.size(); i++) {
delete static_cast<Sphere *>(figures[i]);
}
figures.clear();
for (size_t i = 0; i < figures.size(); i++) {
delete lights[i];
}
lights.clear();
fprintf(out, "P6 %d %d %d ", g_w, g_h, 255);
for (int i = 0; i < g_h; i++) {
for (int j = 0; j < g_w; j++) {
@@ -156,21 +149,3 @@ int main(int argc, char ** argv) {
return EXIT_SUCCESS;
}
inline float random01() {
return static_cast<float>(rand()) / static_cast<float>(RAND_MAX);
}
vec2 sample_pixel(int i, int j) {
float pxNDC;
float pyNDC;
float pxS;
float pyS;
pyNDC = (static_cast<float>(i) + random01()) / g_h;
pyS = (1.0f - (2.0f * pyNDC)) * tan(radians(g_fov) / 2);
pxNDC = (static_cast<float>(j) + random01()) / g_w;
pxS = (2.0f * pxNDC) - 1.0f;
pxS *= g_aspect_ratio * tan(radians(g_fov) / 2);
return vec2(pxS, pyS);
}